]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run-string.c
base64: fix for unsigned chars (e.g. ARM).
[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(43);
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         ok1(tal_count(c) == strlen(c) + 1);
19         tal_free(c);
20
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);
25         tal_free(c);
26
27 #ifdef TAL_USE_TALLOC
28         c = tal_talloc_typechk_(parent, char *);
29 #else
30         c = tal_typechk_(parent, char *);
31 #endif
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);
37         tal_free(c);
38
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);
45         strcat(c, "x");
46         tal_free(c);
47
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);
52         tal_free(c);
53
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);
58
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));
65
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));
71
72         /* NULL pass through works... */
73         c = tal_strcat(parent, take(NULL), take(c));
74         ok1(!c);
75         ok1(no_children(parent));
76
77         c = tal_strcat(parent, take(tal_strdup(parent, "hi")),
78                        take(NULL));
79         ok1(!c);
80         ok1(no_children(parent));
81
82         c = tal_strcat(parent, take(NULL), take(NULL));
83         ok1(!c);
84         ok1(no_children(parent));
85
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);
93
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);
97
98         tal_free(parent);
99
100         return exit_status();
101 }