]> git.ozlabs.org Git - ccan/blobdiff - ccan/tal/str/test/run-take.c
tal/str: move tal string functions here from tal.
[ccan] / ccan / tal / str / test / run-take.c
diff --git a/ccan/tal/str/test/run-take.c b/ccan/tal/str/test/run-take.c
new file mode 100644 (file)
index 0000000..0cbab9e
--- /dev/null
@@ -0,0 +1,47 @@
+#include <ccan/tal/str/str.h>
+#include <ccan/tal/str/str.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       char *parent, *c;
+
+       plan_tests(14);
+
+       parent = tal(NULL, char);
+       ok1(parent);
+
+       c = tal_strdup(parent, "hello");
+
+       c = tal_strdup(parent, take(c));
+       ok1(strcmp(c, "hello") == 0);
+       ok1(tal_parent(c) == parent);
+
+       c = tal_strndup(parent, take(c), 5);
+       ok1(strcmp(c, "hello") == 0);
+       ok1(tal_parent(c) == parent);
+
+       c = tal_strndup(parent, take(c), 3);
+       ok1(strcmp(c, "hel") == 0);
+       ok1(tal_parent(c) == parent);
+       tal_free(c);
+
+       c = tal_strdup(parent, "hello %s");
+       c = tal_asprintf(parent, take(c), "there");
+       ok1(strcmp(c, "hello there") == 0);
+       ok1(tal_parent(c) == parent);
+       /* No leftover allocations. */
+       tal_free(c);
+       ok1(tal_first(parent) == NULL);
+
+       tal_free(parent);
+       ok1(!taken_any());
+
+       /* NULL pass-through. */
+       c = NULL;
+       ok1(tal_strdup(NULL, take(c)) == NULL);
+       ok1(tal_strndup(NULL, take(c), 5) == NULL);
+       ok1(tal_asprintf(NULL, take(c), 0) == NULL);
+
+       return exit_status();
+}