tal/str: use tal_ prefix.
[ccan] / ccan / tal / str / test / run.c
1 #include <ccan/tal/str/str.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <ccan/tal/str/str.c>
5 #include <ccan/tap/tap.h>
6
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
8
9 static const char *substrings[]
10 = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
11
12 int main(int argc, char *argv[])
13 {
14         char **split, *str;
15         void *ctx;
16
17         plan_tests(69);
18         split = tal_strsplit(NULL, "hello  world", " ", STR_EMPTY_OK);
19         ok1(!strcmp(split[0], "hello"));
20         ok1(!strcmp(split[1], ""));
21         ok1(!strcmp(split[2], "world"));
22         ok1(split[3] == NULL);
23         ok1(tal_count(split) == 4);
24         tal_free(split);
25
26         split = tal_strsplit(NULL, "hello  world", " ", STR_NO_EMPTY);
27         ok1(!strcmp(split[0], "hello"));
28         ok1(!strcmp(split[1], "world"));
29         ok1(split[2] == NULL);
30         ok1(tal_count(split) == 3);
31         tal_free(split);
32
33         split = tal_strsplit(NULL, "  hello  world", " ", STR_NO_EMPTY);
34         ok1(!strcmp(split[0], "hello"));
35         ok1(!strcmp(split[1], "world"));
36         ok1(split[2] == NULL);
37         ok1(tal_count(split) == 3);
38         tal_free(split);
39
40         split = tal_strsplit(NULL, "hello  world", "o ", STR_EMPTY_OK);
41         ok1(!strcmp(split[0], "hell"));
42         ok1(!strcmp(split[1], ""));
43         ok1(!strcmp(split[2], ""));
44         ok1(!strcmp(split[3], "w"));
45         ok1(!strcmp(split[4], "rld"));
46         ok1(split[5] == NULL);
47         ok1(tal_count(split) == 6);
48
49         ctx = split;
50         split = tal_strsplit(ctx, "hello  world", "o ", STR_EMPTY_OK);
51         ok1(tal_parent(split) == ctx);
52         tal_free(ctx);
53
54         str = tal_strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
55         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
56         ctx = str;
57         str = tal_strjoin(ctx, (char **)substrings, "", STR_TRAIL);
58         ok1(!strcmp(str, "farbarbazbbazar"));
59         ok1(tal_parent(str) == ctx);
60         str = tal_strjoin(ctx, (char **)substrings, ", ", STR_NO_TRAIL);
61         ok1(tal_parent(str) == ctx);
62         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar"));
63         str = tal_strjoin(ctx, (char **)substrings, "", STR_NO_TRAIL);
64         ok1(!strcmp(str, "farbarbazbbazar"));
65         ok1(tal_parent(str) == ctx);
66         tal_free(ctx);
67
68         ctx = tal_strdup(NULL, "context");
69         /* Pass through NULLs from take. */
70         ok1(tal_strsplit(NULL, take(NULL), " ", STR_EMPTY_OK) == NULL);
71         ok1(tal_strsplit(NULL, "foo", take(NULL), STR_EMPTY_OK) == NULL);
72
73         /* tal_strsplit take string.  It reallocs it to same size, but
74          * that sometimes causes a move, so we can't directly check
75          * that split[0] == str. */
76         str = tal_strdup(ctx, "hello world");
77         ok1(tal_check(ctx, NULL));
78         ok1(tal_check(str, NULL));
79         split = tal_strsplit(ctx, take(str), " ", STR_EMPTY_OK);
80         ok1(tal_parent(split) == ctx);
81         ok1(!strcmp(split[0], "hello"));
82         ok1(!strcmp(split[1], "world"));
83         ok1(split[2] == NULL);
84         ok1(tal_check(split, NULL));
85         ok1(tal_check(ctx, NULL));
86         tal_free(split);
87         /* Previous free should get rid of str */
88         ok1(!tal_first(ctx));
89
90         /* tal_strsplit take delims */
91         str = tal_strdup(ctx, " ");
92         split = tal_strsplit(ctx, "hello world", take(str), STR_EMPTY_OK);
93         ok1(tal_parent(split) == ctx);
94         ok1(!strcmp(split[0], "hello"));
95         ok1(!strcmp(split[1], "world"));
96         ok1(split[2] == NULL);
97         ok1(tal_check(split, NULL));
98         ok1(tal_check(ctx, NULL));
99         tal_free(split);
100         /* str is gone... */
101         ok1(!tal_first(ctx));
102
103         /* tal_strsplit takes both. */
104         split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")),
105                              take(tal_strdup(NULL, " ")), STR_EMPTY_OK);
106         ok1(tal_parent(split) == ctx);
107         ok1(!strcmp(split[0], "hello"));
108         ok1(!strcmp(split[1], "world"));
109         ok1(split[2] == NULL);
110         ok1(tal_check(split, NULL));
111         ok1(tal_check(ctx, NULL));
112         tal_free(split);
113         /* temp allocs are gone... */
114         ok1(!tal_first(ctx));
115
116         /* tal_strjoin passthrough taken NULLs OK. */
117         ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
118         ok1(tal_strjoin(ctx, take(NULL), "", STR_NO_TRAIL) == NULL);
119         ok1(tal_strjoin(ctx, split, take(NULL), STR_TRAIL) == NULL);
120         ok1(tal_strjoin(ctx, split, take(NULL), STR_NO_TRAIL) == NULL);
121
122         /* tal_strjoin take strings[] */
123         split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
124         str = tal_strjoin(ctx, take(split), " there ", STR_NO_TRAIL);
125         ok1(!strcmp(str, "hello there world"));
126         ok1(tal_parent(str) == ctx);
127         /* split is gone... */
128         ok1(tal_first(ctx) == str && !tal_next(ctx, str));
129         tal_free(str);
130         ok1(!tal_first(ctx));
131
132         /* tal_strjoin take delim */
133         split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
134         str = tal_strjoin(ctx, split, take(tal_strdup(ctx, " there ")),
135                           STR_NO_TRAIL);
136         ok1(!strcmp(str, "hello there world"));
137         ok1(tal_parent(str) == ctx);
138         tal_free(split);
139         /* tmp alloc is gone, str is only remainder. */
140         ok1(tal_first(ctx) == str && !tal_next(ctx, str));
141         tal_free(str);
142         ok1(!tal_first(ctx));
143
144         /* tal_strjoin take both. */
145         str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
146                                                  STR_EMPTY_OK)),
147                           take(tal_strdup(ctx, " there ")), STR_NO_TRAIL);
148         ok1(!strcmp(str, "hello there world"));
149         ok1(tal_parent(str) == ctx);
150         /* tmp allocs are gone, str is only remainder. */
151         ok1(tal_first(ctx) == str && !tal_next(ctx, str));
152         tal_free(str);
153         ok1(!tal_first(ctx));
154         tal_free(ctx);
155
156         return exit_status();
157 }