tal/str: new module.
[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
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
8
9 static const char *substrings[]
10 = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
11
12 int main(int argc, char *argv[])
13 {
14         char **split, *str;
15         void *ctx;
16
17         plan_tests(24);
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);
23         tal_free(split);
24
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);
29         tal_free(split);
30
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);
35         tal_free(split);
36
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);
44
45         ctx = split;
46         split = strsplit(ctx, "hello  world", "o ", STR_EMPTY_OK);
47         ok1(tal_parent(split) == ctx);
48         tal_free(ctx);
49
50         str = strjoin(NULL, (char **)substrings, ", ", STR_TRAIL);
51         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
52         ctx = str;
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);
62         tal_free(ctx);
63
64         return exit_status();
65 }