]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/test/run.c
grab_file: don't use str_talloc for tests.
[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 #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(16);
18         split = strsplit(NULL, "hello  world", " ");
19         ok1(talloc_array_length(split) == 4);
20         ok1(!strcmp(split[0], "hello"));
21         ok1(!strcmp(split[1], ""));
22         ok1(!strcmp(split[2], "world"));
23         ok1(split[3] == NULL);
24         talloc_free(split);
25
26         split = strsplit(NULL, "hello  world", "o ");
27         ok1(talloc_array_length(split) == 6);
28         ok1(!strcmp(split[0], "hell"));
29         ok1(!strcmp(split[1], ""));
30         ok1(!strcmp(split[2], ""));
31         ok1(!strcmp(split[3], "w"));
32         ok1(!strcmp(split[4], "rld"));
33         ok1(split[5] == NULL);
34
35         ctx = split;
36         split = strsplit(ctx, "hello  world", "o ");
37         ok1(talloc_parent(split) == ctx);
38         talloc_free(ctx);
39
40         str = strjoin(NULL, (char **)substrings, ", ");
41         ok1(!strcmp(str, "far, bar, baz, b, ba, z, ar, "));
42         ctx = str;
43         str = strjoin(ctx, (char **)substrings, "");
44         ok1(!strcmp(str, "farbarbazbbazar"));
45         ok1(talloc_parent(str) == ctx);
46         talloc_free(ctx);
47
48         return exit_status();
49 }