1 #include <ccan/tal/str/str.h>
4 #include <ccan/tal/str/str.c>
5 #include <ccan/tap/tap.h>
8 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
10 static const char *substrings[]
11 = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
13 int main(int argc, char *argv[])
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);
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);
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);
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);
51 split = tal_strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);
52 ok1(tal_parent(split) == ctx);
55 str = tal_strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
56 ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
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);
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);
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));
88 /* Previous free should get rid of str */
89 ok1(no_children(ctx));
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));
102 ok1(no_children(ctx));
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));
114 /* temp allocs are gone... */
115 ok1(no_children(ctx));
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);
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));
131 ok1(no_children(ctx));
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 ")),
137 ok1(!strcmp(str, "hello there world"));
138 ok1(tal_parent(str) == ctx);
140 /* tmp alloc is gone, str is only remainder. */
141 ok1(single_child(ctx, str));
143 ok1(no_children(ctx));
145 /* tal_strjoin take both. */
146 str = tal_strjoin(ctx, take(tal_strsplit(ctx, "hello world", " ",
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));
154 ok1(no_children(ctx));
157 return exit_status();