]> git.ozlabs.org Git - ccan/commitdiff
mem: Correct testcases
authorDavid Gibson <david@gibson.dropbear.id.au>
Wed, 22 Oct 2014 11:56:52 +0000 (13:56 +0200)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 27 Oct 2014 05:43:15 +0000 (16:13 +1030)
Currently the 'mem' module testcases use test/run.c even though they don't
rely on access to the module internals.  They're also missing an include
of mem.c, which has the effect that on systems with a C library memmem()
implementaiton, only that is tested, not the (re-)implementation in the
mem module itself.  This corrects that by moving run.c to api.c/

Additionally, the memmem() testcases don't cover the case where the
"needle" appears multiple times in the "haystack".  This patch also adds
such a test.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/mem/test/api.c [new file with mode: 0644]
ccan/mem/test/run.c [deleted file]

diff --git a/ccan/mem/test/api.c b/ccan/mem/test/api.c
new file mode 100644 (file)
index 0000000..a7cfb9a
--- /dev/null
@@ -0,0 +1,25 @@
+#include <ccan/array_size/array_size.h>
+#include <ccan/mem/mem.h>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       char haystack1[] = "abcd\0efgh";
+       char haystack2[] = "ab\0ab\0ab\0ab";
+       char needle1[] = "ab";
+       char needle2[] = "d\0e";
+
+       /* This is how many tests you plan to run */
+       plan_tests(5);
+
+       ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
+       ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
+       ok1(memmem(haystack1, sizeof(haystack1), needle2, 3) == (haystack1 + 3));
+
+       ok1(memmem(haystack2, sizeof(haystack2), needle1, sizeof(needle1))
+           == haystack2);
+       ok1(memmem(haystack2, sizeof(haystack2), needle2, 3) == NULL);
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}
diff --git a/ccan/mem/test/run.c b/ccan/mem/test/run.c
deleted file mode 100644 (file)
index 3efd1d8..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-#include <ccan/array_size/array_size.h>
-#include <ccan/mem/mem.h>
-#include <ccan/tap/tap.h>
-
-int main(void)
-{
-       char haystack1[] = "abcd\0efgh";
-       char needle1[] = "ab";
-       char needle2[] = "d\0e";
-
-       /* This is how many tests you plan to run */
-       plan_tests(3);
-
-       ok1(memmem(haystack1, sizeof(haystack1), needle1, 2) == haystack1);
-       ok1(memmem(haystack1, sizeof(haystack1), needle1, 3) == NULL);
-       ok1(memmem(haystack1, sizeof(haystack1), needle2, 3) == (haystack1 + 3));
-
-       /* This exits depending on whether all tests passed */
-       return exit_status();
-}