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