X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Ftal%2Fstr%2Ftest%2Frun-string.c;h=03d4eb51b9c061283a3a822977b909bacdacd0bf;hb=55d814230f7fb628bb5303cd53498209c7928040;hp=bb614ae2fa070e09af6da909cc61b456ec2693c2;hpb=4710a92838fc03ffa54ce2bc60cf0e214c97a199;p=ccan diff --git a/ccan/tal/str/test/run-string.c b/ccan/tal/str/test/run-string.c index bb614ae2..03d4eb51 100644 --- a/ccan/tal/str/test/run-string.c +++ b/ccan/tal/str/test/run-string.c @@ -1,12 +1,13 @@ #include #include #include +#include "helper.h" int main(void) { char *parent, *c; - plan_tests(27); + plan_tests(43); parent = tal(NULL, char); ok1(parent); @@ -14,61 +15,85 @@ int main(void) c = tal_strdup(parent, "hello"); ok1(strcmp(c, "hello") == 0); ok1(tal_parent(c) == parent); + ok1(tal_count(c) == strlen(c) + 1); tal_free(c); c = tal_strndup(parent, "hello", 3); ok1(strcmp(c, "hel") == 0); ok1(tal_parent(c) == parent); + ok1(tal_count(c) == strlen(c) + 1); tal_free(c); +#ifdef TAL_USE_TALLOC + c = tal_talloc_typechk_(parent, char *); +#else c = tal_typechk_(parent, char *); - c = tal_dup(parent, char, "hello", 6, 0); +#endif + c = tal_dup_arr(parent, char, "hello", 6, 0); ok1(strcmp(c, "hello") == 0); ok1(strcmp(tal_name(c), "char[]") == 0); + ok1(tal_count(c) == 6); ok1(tal_parent(c) == parent); tal_free(c); /* Now with an extra byte. */ - c = tal_dup(parent, char, "hello", 6, 1); + c = tal_dup_arr(parent, char, "hello", 6, 1); ok1(strcmp(c, "hello") == 0); ok1(strcmp(tal_name(c), "char[]") == 0); + ok1(tal_count(c) == 7); ok1(tal_parent(c) == parent); strcat(c, "x"); tal_free(c); c = tal_fmt(parent, "hello %s", "there"); ok1(strcmp(c, "hello there") == 0); + ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); tal_free(c); c = tal_strcat(parent, "hello ", "there"); ok1(strcmp(c, "hello there") == 0); + ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); /* Make sure take works correctly. */ c = tal_strcat(parent, take(c), " again"); ok1(strcmp(c, "hello there again") == 0); + ok1(tal_count(c) == strlen(c) + 1); ok1(tal_parent(c) == parent); - ok1(tal_first(parent) == c && !tal_next(parent, c)); + ok1(single_child(parent, c)); c = tal_strcat(parent, "And ", take(c)); + ok1(tal_count(c) == strlen(c) + 1); ok1(strcmp(c, "And hello there again") == 0); ok1(tal_parent(c) == parent); - ok1(tal_first(parent) == c && !tal_next(parent, c)); + ok1(single_child(parent, c)); /* NULL pass through works... */ c = tal_strcat(parent, take(NULL), take(c)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); c = tal_strcat(parent, take(tal_strdup(parent, "hi")), take(NULL)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); c = tal_strcat(parent, take(NULL), take(NULL)); ok1(!c); - ok1(!tal_first(parent)); + ok1(no_children(parent)); + + /* Appending formatted strings. */ + c = tal_strdup(parent, "hi"); + ok1(tal_count(c) == strlen(c) + 1); + ok1(tal_append_fmt(&c, "%s %s", "there", "world")); + ok1(strcmp(c, "hithere world") == 0); + ok1(tal_count(c) == strlen(c) + 1); + ok1(tal_parent(c) == parent); + + ok1(!tal_append_fmt(&c, take(NULL), "there", "world")); + ok1(strcmp(c, "hithere world") == 0); + ok1(tal_count(c) == strlen(c) + 1); tal_free(parent);