]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run-string.c
tal/str: add tal_append_fmt() and tal_append_vfmt() helpers.
[ccan] / ccan / tal / str / test / run-string.c
1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         char *parent, *c;
8
9         plan_tests(32);
10
11         parent = tal(NULL, char);
12         ok1(parent);
13
14         c = tal_strdup(parent, "hello");
15         ok1(strcmp(c, "hello") == 0);
16         ok1(tal_parent(c) == parent);
17         tal_free(c);
18
19         c = tal_strndup(parent, "hello", 3);
20         ok1(strcmp(c, "hel") == 0);
21         ok1(tal_parent(c) == parent);
22         tal_free(c);
23
24         c = tal_typechk_(parent, char *);
25         c = tal_dup(parent, char, "hello", 6, 0);
26         ok1(strcmp(c, "hello") == 0);
27         ok1(strcmp(tal_name(c), "char[]") == 0);
28         ok1(tal_parent(c) == parent);
29         tal_free(c);
30
31         /* Now with an extra byte. */
32         c = tal_dup(parent, char, "hello", 6, 1);
33         ok1(strcmp(c, "hello") == 0);
34         ok1(strcmp(tal_name(c), "char[]") == 0);
35         ok1(tal_parent(c) == parent);
36         strcat(c, "x");
37         tal_free(c);
38
39         c = tal_fmt(parent, "hello %s", "there");
40         ok1(strcmp(c, "hello there") == 0);
41         ok1(tal_parent(c) == parent);
42         tal_free(c);
43
44         c = tal_strcat(parent, "hello ", "there");
45         ok1(strcmp(c, "hello there") == 0);
46         ok1(tal_parent(c) == parent);
47
48         /* Make sure take works correctly. */
49         c = tal_strcat(parent, take(c), " again");
50         ok1(strcmp(c, "hello there again") == 0);
51         ok1(tal_parent(c) == parent);
52         ok1(tal_first(parent) == c && !tal_next(parent, c));
53
54         c = tal_strcat(parent, "And ", take(c));
55         ok1(strcmp(c, "And hello there again") == 0);
56         ok1(tal_parent(c) == parent);
57         ok1(tal_first(parent) == c && !tal_next(parent, c));
58
59         /* NULL pass through works... */
60         c = tal_strcat(parent, take(NULL), take(c));
61         ok1(!c);
62         ok1(!tal_first(parent));
63
64         c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
65                        take(NULL));
66         ok1(!c);
67         ok1(!tal_first(parent));
68
69         c = tal_strcat(parent, take(NULL), take(NULL));
70         ok1(!c);
71         ok1(!tal_first(parent));
72
73         /* Appending formatted strings. */
74         c = tal_strdup(parent, "hi");
75         ok1(tal_append_fmt(&c, "%s %s", "there", "world"));
76         ok1(strcmp(c, "hithere world") == 0);
77         ok1(tal_parent(c) == parent);
78
79         ok1(!tal_append_fmt(&c, take(NULL), "there", "world"));
80         ok1(strcmp(c, "hithere world") == 0);
81
82         tal_free(parent);
83
84         return exit_status();
85 }