]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/test/run.c
antithread, foreach, grab_file, hashtable, str_talloc: fix _info depends.
[ccan] / ccan / str_talloc / test / run.c
1 #include <ccan/str_talloc/str_talloc.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <ccan/str_talloc/str_talloc.c>
5 #include <ccan/tap/tap.h>
6
7 /* FIXME: ccanize */
8 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
9
10 static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL };
11
12 int main(int argc, char *argv[])
13 {
14         unsigned int n;
15         char **split, *str;
16         void *ctx;
17
18         plan_tests(19);
19         split = strsplit(NULL, "hello  world", " ", &n);
20         ok1(n == 3);
21         ok1(!strcmp(split[0], "hello"));
22         ok1(!strcmp(split[1], ""));
23         ok1(!strcmp(split[2], "world"));
24         ok1(split[3] == NULL);
25         talloc_free(split);
26
27         split = strsplit(NULL, "hello  world", " ", NULL);
28         ok1(!strcmp(split[0], "hello"));
29         ok1(!strcmp(split[1], ""));
30         ok1(!strcmp(split[2], "world"));
31         ok1(split[3] == NULL);
32         talloc_free(split);
33
34         split = strsplit(NULL, "hello  world", "o ", NULL);
35         ok1(!strcmp(split[0], "hell"));
36         ok1(!strcmp(split[1], ""));
37         ok1(!strcmp(split[2], ""));
38         ok1(!strcmp(split[3], "w"));
39         ok1(!strcmp(split[4], "rld"));
40         ok1(split[5] == NULL);
41
42         ctx = split;
43         split = strsplit(ctx, "hello  world", "o ", NULL);
44         ok1(talloc_parent(split) == ctx);
45         talloc_free(ctx);
46
47         str = strjoin(NULL, substrings, ", ");
48         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
49         ctx = str;
50         str = strjoin(ctx, substrings, "");
51         ok1(!strcmp(str, "farbarbazbbazar"));
52         ok1(talloc_parent(str) == ctx);
53         talloc_free(ctx);
54
55         return exit_status();
56 }