1 #include <ccan/tal/str/str.h>
4 #include <ccan/tal/str/str.c>
5 #include <ccan/tap/tap.h>
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
9 static const char *substrings[]
10 = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
12 int main(int argc, char *argv[])
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);
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);
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);
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);
50 split = tal_strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);
51 ok1(tal_parent(split) == ctx);
54 str = tal_strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
55 ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
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);
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);
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));
87 /* Previous free should get rid of str */
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));
101 ok1(!tal_first(ctx));
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));
113 /* temp allocs are gone... */
114 ok1(!tal_first(ctx));
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);
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));
130 ok1(!tal_first(ctx));
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 ")),
136 ok1(!strcmp(str, "hello there world"));
137 ok1(tal_parent(str) == ctx);
139 /* tmp alloc is gone, str is only remainder. */
140 ok1(tal_first(ctx) == str && !tal_next(ctx, str));
142 ok1(!tal_first(ctx));
144 /* tal_strjoin take both. */
145 str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
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));
153 ok1(!tal_first(ctx));
156 return exit_status();