sheredom / utest.h

🧪 single header unit testing framework for C and C++

Home Page:https://www.duskborn.com/utest_h/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

STRN*-checks assume null-terminated arguments

peterlindgren opened this issue · comments

Hi,

Thanks for this excellent library, I'm using it for most of my tests now and it's been working great.

While browsing the header I noticed that the STRN*-checks all assume both arguments are null-terminated. I'd like to suggest a third argument, len, be added to let the user specify the desired string length to compare. My particular use case is that I'm writing tests for a lexer/parser where tokens point to the middle of an input string buffer.

So my suggestion is to change from this:

#define ASSERT_STRNEQ(x, y)                                                    \
  if (0 != UTEST_STRNCMP(x, y, strlen(x))) {                                   \
    UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__);                      \
    UTEST_PRINTF("  Expected : \"%s\"\n", x);                                  \
    UTEST_PRINTF("    Actual : \"%s\"\n", y);                                  \
    *utest_result = 1;                                                         \
    return;                                                                    \
  }

To this:

#define ASSERT_STRNEQ(x, y, len)                                              \
  if (0 != UTEST_STRNCMP(x, y, len)) {                                        \
    UTEST_PRINTF("%s:%u: Failure\n", __FILE__, __LINE__);                     \
    UTEST_PRINTF("  Expected : \"%.*s\"\n", len, x);                          \
    UTEST_PRINTF("    Actual : \"%.*s\"\n", len, y);                          \
    *utest_result = 1;                                                        \
    return;                                                                   \
  }

Best,
Peter

Happy to accept that PR if you made it! Good find 😄