
On 05/14/2013 06:50 AM, Ján Tomko wrote:
On 05/07/2013 11:42 PM, Eric Blake wrote:
The surest way to avoid regressions is to test documented behavior :)
* tests/virstringtest.c (testStrdup): New test case.
Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/virstringtest.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-)
...
+ value = VIR_STRNDUP(array[i++], NULL, 5); + if (value != 0) { + fprintf(stderr, "unexpected strdup result %d, expected 0\n", value); + goto cleanup; + } + if (i != 2) { + fprintf(stderr, "unexpected side effects i=%zu, expected 2\n", i); + goto cleanup; + }
Should we test the side effects for the other two arguments as well?
Ooh, good idea.
+ if (STRNEQ_NULLABLE(array[0], "hello") || array[1]) { + fprintf(stderr, "incorrect array contents '%s' '%s'\n", + NULLSTR(array[0]), NULLSTR(array[1])); + goto cleanup; + } + + ret = 0;
ACK
I'm squashing this in (it still passes, and now exercises more side effects), then pushing. diff --git i/tests/virstringtest.c w/tests/virstringtest.c index 359cb9a..da06c0f 100644 --- i/tests/virstringtest.c +++ w/tests/virstringtest.c @@ -107,15 +107,41 @@ cleanup: return ret; } +static bool fail; + +static const char * +testStrdupLookup1(size_t i) +{ + switch (i) { + case 0: + return "hello"; + case 1: + return NULL; + default: + fail = true; + return "oops"; + } +} + +static size_t +testStrdupLookup2(size_t i) +{ + if (i) + fail = true; + return 5; +} + static int testStrdup(const void *data ATTRIBUTE_UNUSED) { char *array[] = { NULL, NULL }; size_t i = 0; + size_t j = 0; + size_t k = 0; int ret = -1; int value; - value = VIR_STRDUP(array[i++], "hello"); + value = VIR_STRDUP(array[i++], testStrdupLookup1(j++)); if (value != 1) { fprintf(stderr, "unexpected strdup result %d, expected 1\n", value); goto cleanup; @@ -124,13 +150,18 @@ testStrdup(const void *data ATTRIBUTE_UNUSED) fprintf(stderr, "unexpected side effects i=%zu, expected 1\n", i); goto cleanup; } + if (j != 1) { + fprintf(stderr, "unexpected side effects j=%zu, expected 1\n", j); + goto cleanup; + } if (STRNEQ_NULLABLE(array[0], "hello") || array[1]) { fprintf(stderr, "incorrect array contents '%s' '%s'\n", NULLSTR(array[0]), NULLSTR(array[1])); goto cleanup; } - value = VIR_STRNDUP(array[i++], NULL, 5); + value = VIR_STRNDUP(array[i++], testStrdupLookup1(j++), + testStrdupLookup2(k++)); if (value != 0) { fprintf(stderr, "unexpected strdup result %d, expected 0\n", value); goto cleanup; @@ -139,12 +170,25 @@ testStrdup(const void *data ATTRIBUTE_UNUSED) fprintf(stderr, "unexpected side effects i=%zu, expected 2\n", i); goto cleanup; } + if (j != 2) { + fprintf(stderr, "unexpected side effects j=%zu, expected 2\n", j); + goto cleanup; + } + if (k != 1) { + fprintf(stderr, "unexpected side effects k=%zu, expected 1\n", k); + goto cleanup; + } if (STRNEQ_NULLABLE(array[0], "hello") || array[1]) { fprintf(stderr, "incorrect array contents '%s' '%s'\n", NULLSTR(array[0]), NULLSTR(array[1])); goto cleanup; } + if (fail) { + fprintf(stderr, "side effects failed\n"); + goto cleanup; + } + ret = 0; cleanup: for (i = 0; i < ARRAY_CARDINALITY(array); i++) -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org