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