Our STREQ_NULLABLE and STRNEQ_NULLABLE macros are too complicated. This was a result of some broken version of gcc. However, that is long gone and therefore we can simplify the macros. Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> --- src/internal.h | 4 ++-- tests/virstringtest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/internal.h b/src/internal.h index d8cc5adc3..334659d32 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 1d660b798..db1731f96 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 = { \ -- 2.11.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list