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 = 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);
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);
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);
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);
46 split = strsplit(ctx, "hello world", "o ", STR_EMPTY_OK);
47 ok1(tal_parent(split) == ctx);
50 str = strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
51 ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
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);
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);
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));
83 /* Previous free should get rid of str */
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));
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));
109 /* temp allocs are gone... */
110 ok1(!tal_first(ctx));
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);
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));
126 ok1(!tal_first(ctx));
128 /* strjoin take delim */
129 split = strsplit(ctx, "hello world", " ", STR_EMPTY_OK);
130 str = strjoin(ctx, split, take(tal_strdup(ctx, " there ")),
132 ok1(!strcmp(str, "hello there world"));
133 ok1(tal_parent(str) == ctx);
135 /* tmp alloc is gone, str is only remainder. */
136 ok1(tal_first(ctx) == str && !tal_next(ctx, str));
138 ok1(!tal_first(ctx));
140 /* strjoin take both. */
141 str = strjoin(ctx, take(strsplit(ctx, "hello world", " ",
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));
149 ok1(!tal_first(ctx));
152 return exit_status();