]> git.ozlabs.org Git - ccan/blobdiff - ccan/strmap/test/run.c
strmap: new module for ordered map of strings using a critbit tree.
[ccan] / ccan / strmap / test / run.c
diff --git a/ccan/strmap/test/run.c b/ccan/strmap/test/run.c
new file mode 100644 (file)
index 0000000..349ef4f
--- /dev/null
@@ -0,0 +1,69 @@
+#include <ccan/strmap/strmap.h>
+#include <ccan/strmap/strmap.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       struct strmap_charp {
+               STRMAP_MEMBERS(char *);
+       } map;
+       const char str[] = "hello";
+       const char val[] = "there";
+       const char none[] = "";
+       char *dup = strdup(str);
+       char *v;
+
+       /* This is how many tests you plan to run */
+       plan_tests(31);
+
+       strmap_init(&map);
+
+       ok1(!strmap_get(&map, str));
+       ok1(!strmap_get(&map, none));
+       ok1(!strmap_del(&map, str, NULL));
+       ok1(!strmap_del(&map, none, NULL));
+
+       ok1(strmap_add(&map, str, val));
+       ok1(strmap_get(&map, str) == val);
+       /* We compare the string, not the pointer. */
+       ok1(strmap_get(&map, dup) == val);
+       ok1(!strmap_get(&map, none));
+
+       /* Add a duplicate should fail. */
+       ok1(!strmap_add(&map, dup, val));
+       ok1(strmap_get(&map, dup) == val);
+
+       /* Delete should return original string. */
+       ok1(strmap_del(&map, dup, &v) == str);
+       ok1(v == val);
+       ok1(!strmap_get(&map, str));
+       ok1(!strmap_get(&map, none));
+
+       /* Try insert and delete of empty string. */
+       ok1(strmap_add(&map, none, none));
+       ok1(strmap_get(&map, none) == none);
+       ok1(!strmap_get(&map, str));
+
+       /* Delete should return original string. */
+       ok1(strmap_del(&map, "", &v) == none);
+       ok1(v == none);
+       ok1(!strmap_get(&map, str));
+       ok1(!strmap_get(&map, none));
+
+       /* Both at once... */
+       ok1(strmap_add(&map, none, none));
+       ok1(strmap_add(&map, str, val));
+       ok1(strmap_get(&map, str) == val);
+       ok1(strmap_get(&map, none) == none);
+       ok1(strmap_del(&map, "does not exist", NULL) == NULL);
+       ok1(strmap_del(&map, "", NULL) == none);
+       ok1(strmap_get(&map, str) == val);
+       ok1(strmap_del(&map, dup, &v) == str);
+       ok1(v == val);
+
+       ok1(strmap_empty(&map));
+       free(dup);
+
+       /* This exits depending on whether all tests passed */
+       return exit_status();
+}