1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
12 parent = tal(NULL, char);
15 c = tal_strdup(parent, "hello");
16 ok1(strcmp(c, "hello") == 0);
17 ok1(tal_parent(c) == parent);
18 ok1(tal_count(c) == strlen(c) + 1);
21 c = tal_strndup(parent, "hello", 3);
22 ok1(strcmp(c, "hel") == 0);
23 ok1(tal_parent(c) == parent);
24 ok1(tal_count(c) == strlen(c) + 1);
28 c = tal_talloc_typechk_(parent, char *);
30 c = tal_typechk_(parent, char *);
32 c = tal_dup_arr(parent, char, "hello", 6, 0);
33 ok1(strcmp(c, "hello") == 0);
34 ok1(strcmp(tal_name(c), "char[]") == 0);
35 ok1(tal_count(c) == 6);
36 ok1(tal_parent(c) == parent);
39 /* Now with an extra byte. */
40 c = tal_dup_arr(parent, char, "hello", 6, 1);
41 ok1(strcmp(c, "hello") == 0);
42 ok1(strcmp(tal_name(c), "char[]") == 0);
43 ok1(tal_count(c) == 7);
44 ok1(tal_parent(c) == parent);
48 c = tal_fmt(parent, "hello %s", "there");
49 ok1(strcmp(c, "hello there") == 0);
50 ok1(tal_count(c) == strlen(c) + 1);
51 ok1(tal_parent(c) == parent);
54 c = tal_strcat(parent, "hello ", "there");
55 ok1(strcmp(c, "hello there") == 0);
56 ok1(tal_count(c) == strlen(c) + 1);
57 ok1(tal_parent(c) == parent);
59 /* Make sure take works correctly. */
60 c = tal_strcat(parent, take(c), " again");
61 ok1(strcmp(c, "hello there again") == 0);
62 ok1(tal_count(c) == strlen(c) + 1);
63 ok1(tal_parent(c) == parent);
64 ok1(single_child(parent, c));
66 c = tal_strcat(parent, "And ", take(c));
67 ok1(tal_count(c) == strlen(c) + 1);
68 ok1(strcmp(c, "And hello there again") == 0);
69 ok1(tal_parent(c) == parent);
70 ok1(single_child(parent, c));
72 /* NULL pass through works... */
73 c = tal_strcat(parent, take(NULL), take(c));
75 ok1(no_children(parent));
77 c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
80 ok1(no_children(parent));
82 c = tal_strcat(parent, take(NULL), take(NULL));
84 ok1(no_children(parent));
86 /* Appending formatted strings. */
87 c = tal_strdup(parent, "hi");
88 ok1(tal_count(c) == strlen(c) + 1);
89 ok1(tal_append_fmt(&c, "%s %s", "there", "world"));
90 ok1(strcmp(c, "hithere world") == 0);
91 ok1(tal_count(c) == strlen(c) + 1);
92 ok1(tal_parent(c) == parent);
94 ok1(!tal_append_fmt(&c, take(NULL), "there", "world"));
95 ok1(strcmp(c, "hithere world") == 0);
96 ok1(tal_count(c) == strlen(c) + 1);
100 return exit_status();