]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run.c
tal/str: use tal/talloc backend #ifdef TAL_USE_TALLOC.
[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(int argc, char *argv[])
14 {
15         char **split, *str;
16         void *ctx;
17
18         plan_tests(69);
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         ctx = str;
58         str = tal_strjoin(ctx, (char **)substrings, "", STR_TRAIL);
59         ok1(!strcmp(str, "farbarbazbbazar"));
60         ok1(tal_parent(str) == ctx);
61         str = tal_strjoin(ctx, (char **)substrings, ", ", STR_NO_TRAIL);
62         ok1(tal_parent(str) == ctx);
63         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar"));
64         str = tal_strjoin(ctx, (char **)substrings, "", STR_NO_TRAIL);
65         ok1(!strcmp(str, "farbarbazbbazar"));
66         ok1(tal_parent(str) == ctx);
67         tal_free(ctx);
68
69         ctx = tal_strdup(NULL, "context");
70         /* Pass through NULLs from take. */
71         ok1(tal_strsplit(NULL, take(NULL), " ", STR_EMPTY_OK) == NULL);
72         ok1(tal_strsplit(NULL, "foo", take(NULL), STR_EMPTY_OK) == NULL);
73
74         /* tal_strsplit take string.  It reallocs it to same size, but
75          * that sometimes causes a move, so we can't directly check
76          * that split[0] == str. */
77         str = tal_strdup(ctx, "hello world");
78         ok1(tal_check(ctx, NULL));
79         ok1(tal_check(str, NULL));
80         split = tal_strsplit(ctx, take(str), " ", STR_EMPTY_OK);
81         ok1(tal_parent(split) == ctx);
82         ok1(!strcmp(split[0], "hello"));
83         ok1(!strcmp(split[1], "world"));
84         ok1(split[2] == NULL);
85         ok1(tal_check(split, NULL));
86         ok1(tal_check(ctx, NULL));
87         tal_free(split);
88         /* Previous free should get rid of str */
89         ok1(no_children(ctx));
90
91         /* tal_strsplit take delims */
92         str = tal_strdup(ctx, " ");
93         split = tal_strsplit(ctx, "hello world", take(str), STR_EMPTY_OK);
94         ok1(tal_parent(split) == ctx);
95         ok1(!strcmp(split[0], "hello"));
96         ok1(!strcmp(split[1], "world"));
97         ok1(split[2] == NULL);
98         ok1(tal_check(split, NULL));
99         ok1(tal_check(ctx, NULL));
100         tal_free(split);
101         /* str is gone... */
102         ok1(no_children(ctx));
103
104         /* tal_strsplit takes both. */
105         split = tal_strsplit(ctx, take(tal_strdup(NULL, "hello world")),
106                              take(tal_strdup(NULL, " ")), STR_EMPTY_OK);
107         ok1(tal_parent(split) == ctx);
108         ok1(!strcmp(split[0], "hello"));
109         ok1(!strcmp(split[1], "world"));
110         ok1(split[2] == NULL);
111         ok1(tal_check(split, NULL));
112         ok1(tal_check(ctx, NULL));
113         tal_free(split);
114         /* temp allocs are gone... */
115         ok1(no_children(ctx));
116
117         /* tal_strjoin passthrough taken NULLs OK. */
118         ok1(tal_strjoin(ctx, take(NULL), "", STR_TRAIL) == NULL);
119         ok1(tal_strjoin(ctx, take(NULL), "", STR_NO_TRAIL) == NULL);
120         ok1(tal_strjoin(ctx, split, take(NULL), STR_TRAIL) == NULL);
121         ok1(tal_strjoin(ctx, split, take(NULL), STR_NO_TRAIL) == NULL);
122
123         /* tal_strjoin take strings[] */
124         split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
125         str = tal_strjoin(ctx, take(split), " there ", STR_NO_TRAIL);
126         ok1(!strcmp(str, "hello there world"));
127         ok1(tal_parent(str) == ctx);
128         /* split is gone... */
129         ok1(single_child(ctx, str));
130         tal_free(str);
131         ok1(no_children(ctx));
132
133         /* tal_strjoin take delim */
134         split = tal_strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
135         str = tal_strjoin(ctx, split, take(tal_strdup(ctx, " there ")),
136                           STR_NO_TRAIL);
137         ok1(!strcmp(str, "hello there world"));
138         ok1(tal_parent(str) == ctx);
139         tal_free(split);
140         /* tmp alloc is gone, str is only remainder. */
141         ok1(single_child(ctx, str));
142         tal_free(str);
143         ok1(no_children(ctx));
144
145         /* tal_strjoin take both. */
146         str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
147                                                  STR_EMPTY_OK)),
148                           take(tal_strdup(ctx, " there ")), STR_NO_TRAIL);
149         ok1(!strcmp(str, "hello there world"));
150         ok1(tal_parent(str) == ctx);
151         /* tmp allocs are gone, str is only remainder. */
152         ok1(single_child(ctx, str));
153         tal_free(str);
154         ok1(no_children(ctx));
155         tal_free(ctx);
156
157         return exit_status();
158 }