[libvirt] [PATCH 3/2] string: test VIR_STRDUP

Eric Blake eblake at redhat.com
Tue May 7 21:42:01 UTC 2013


The surest way to avoid regressions is to test documented behavior :)

* tests/virstringtest.c (testStrdup): New test case.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 tests/virstringtest.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/tests/virstringtest.c b/tests/virstringtest.c
index db1e8a9..159566b 100644
--- a/tests/virstringtest.c
+++ b/tests/virstringtest.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 Red Hat, Inc.
+ * Copyright (C) 2012-2013 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -107,6 +107,51 @@ cleanup:
     return ret;
 }

+static int
+testStrdup(const void *data ATTRIBUTE_UNUSED)
+{
+    char *array[] = { NULL, NULL };
+    size_t i = 0;
+    int ret = -1;
+    int value;
+
+    value = VIR_STRDUP(array[i++], "hello");
+    if (value != 1) {
+        fprintf(stderr, "unexpected strdup result %d, expected 1\n", value);
+        goto cleanup;
+    }
+    if (i != 1) {
+        fprintf(stderr, "unexpected side effects i=%zu, expected 1\n", i);
+        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);
+    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;
+    }
+    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;
+cleanup:
+    for (i = 0; i < ARRAY_CARDINALITY(array); i++)
+        VIR_FREE(array[i]);
+    return ret;
+}
+

 static int
 mymain(void)
@@ -153,6 +198,8 @@ mymain(void)
     const char *tokens7[] = { "The", "quick", "brown", "fox", "", NULL };
     TEST_SPLIT("The quick brown fox ", " ", 0, tokens7);

+    if (virtTestRun("strdup", 1, testStrdup, NULL) < 0)
+        ret = -1;

     return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
-- 
1.8.1.4




More information about the libvir-list mailing list