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