diff --git a/src/internal.h b/src/internal.h index d8cc5adc38f35613bb1b7bf6ecba7c74e553226c..334659d329d42528355fb12533d0604a5d5e4591 100644 --- a/src/internal.h +++ b/src/internal.h @@ -92,9 +92,9 @@ # define STRSKIP(a, b) (STRPREFIX(a, b) ? (a) + strlen(b) : NULL) # define STREQ_NULLABLE(a, b) \ - ((a) ? (b) && STREQ((a) ? (a) : "", (b) ? (b) : "") : !(b)) + ((a) ? (b) && STREQ((a), (b)) : !(b)) # define STRNEQ_NULLABLE(a, b) \ - ((a) ? !(b) || STRNEQ((a) ? (a) : "", (b) ? (b) : "") : !!(b)) + ((a) ? !(b) || STRNEQ((a), (b)) : !!(b)) # define NUL_TERMINATE(buf) do { (buf)[sizeof(buf)-1] = '\0'; } while (0) # define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array))) diff --git a/tests/virstringtest.c b/tests/virstringtest.c index 1d660b79848846eb063229f10bb945889f570f6f..db1731f96aacde1d54fd25559ef84a6391264d24 100644 --- a/tests/virstringtest.c +++ b/tests/virstringtest.c @@ -34,6 +34,53 @@ VIR_LOG_INIT("tests.stringtest"); +struct testStreqData { + const char *a; + const char *b; +}; + +static int testStreq(const void *args) +{ + const struct testStreqData *data = args; + int ret = -1; + bool equal = true; + bool streq_rv, strneq_rv; + size_t i; + + if ((size_t) data->a ^ (size_t) data->b) + equal = false; + if (data->a && data->b) { + for (i = 0; data->a[i] != '\0'; i++) { + if (data->b[i] == '\0' || + data->a[i] != data->b[i]) { + equal = false; + break; + } + } + } + + streq_rv = STREQ_NULLABLE(data->a, data->b); + strneq_rv = STRNEQ_NULLABLE(data->a, data->b); + + if (streq_rv != equal) { + virFilePrintf(stderr, + "STREQ not working correctly. Expected %d got %d", + (int) equal, (int) streq_rv); + goto cleanup; + } + + if (strneq_rv == equal) { + virFilePrintf(stderr, + "STRNEQ not working correctly. Expected %d got %d", + (int) equal, (int) strneq_rv); + goto cleanup; + } + + ret = 0; + cleanup: + return ret; +} + struct testSplitData { const char *string; const char *delim; @@ -651,6 +698,20 @@ mymain(void) { int ret = 0; +#define TEST_STREQ(aa, bb) \ + do { \ + struct testStreqData streqData = {.a = aa, .b = bb}; \ + if (virTestRun("Streq", testStreq, &streqData) < 0) \ + ret = -1; \ + } while (0) + + TEST_STREQ("hello", "world"); + TEST_STREQ(NULL, NULL); + TEST_STREQ(NULL, ""); + TEST_STREQ("", NULL); + TEST_STREQ("", ""); + TEST_STREQ("hello", "hello"); + #define TEST_SPLIT(str, del, max, toks) \ do { \ struct testSplitData splitData = { \