tal/path: new module
[ccan] / ccan / tal / path / test / run-split.c
1 #include <ccan/tal/path/path.h>
2 #include <ccan/tal/path/path.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         char *ctx = tal_strdup(NULL, "ctx"), **split;
8
9         plan_tests(46);
10
11         split = path_split(ctx, "foo" PATH_SEP_STR "bar");
12         ok1(tal_parent(split) == ctx);
13         ok1(streq(split[0], "foo"));
14         ok1(streq(split[1], "bar"));
15         ok1(split[2] == NULL);
16         tal_free(split);
17
18         split = path_split(ctx, "foo" PATH_SEP_STR "bar" PATH_SEP_STR);
19         ok1(tal_parent(split) == ctx);
20         ok1(streq(split[0], "foo"));
21         ok1(streq(split[1], "bar"));
22         ok1(split[2] == NULL);
23         tal_free(split);
24
25         split = path_split(ctx, PATH_SEP_STR "foo"
26                            PATH_SEP_STR "bar" PATH_SEP_STR);
27         ok1(tal_parent(split) == ctx);
28         ok1(streq(split[0], "foo"));
29         ok1(streq(split[1], "bar"));
30         ok1(split[2] == NULL);
31         tal_free(split);
32
33         split = path_split(ctx, PATH_SEP_STR PATH_SEP_STR "foo"
34                            PATH_SEP_STR PATH_SEP_STR "bar"
35                            PATH_SEP_STR PATH_SEP_STR);
36         ok1(tal_parent(split) == ctx);
37         ok1(streq(split[0], "foo"));
38         ok1(streq(split[1], "bar"));
39         ok1(split[2] == NULL);
40         tal_free(split);
41
42         split = path_split(ctx, "foo");
43         ok1(tal_parent(split) == ctx);
44         ok1(streq(split[0], "foo"));
45         ok1(split[1] == NULL);
46         tal_free(split);
47
48         split = path_split(ctx, PATH_SEP_STR "foo");
49         ok1(tal_parent(split) == ctx);
50         ok1(streq(split[0], "foo"));
51         ok1(split[1] == NULL);
52         tal_free(split);
53
54         split = path_split(ctx, PATH_SEP_STR PATH_SEP_STR "foo");
55         ok1(tal_parent(split) == ctx);
56         ok1(streq(split[0], "foo"));
57         ok1(split[1] == NULL);
58         tal_free(split);
59
60         split = path_split(ctx, "foo" PATH_SEP_STR);
61         ok1(tal_parent(split) == ctx);
62         ok1(streq(split[0], "foo"));
63         ok1(split[1] == NULL);
64         tal_free(split);
65
66         split = path_split(ctx, "foo" PATH_SEP_STR PATH_SEP_STR);
67         ok1(tal_parent(split) == ctx);
68         ok1(streq(split[0], "foo"));
69         ok1(split[1] == NULL);
70         tal_free(split);
71
72         split = path_split(ctx, PATH_SEP_STR "foo" PATH_SEP_STR);
73         ok1(tal_parent(split) == ctx);
74         ok1(streq(split[0], "foo"));
75         ok1(split[1] == NULL);
76         tal_free(split);
77
78         split = path_split(ctx, "");
79         ok1(tal_parent(split) == ctx);
80         ok1(split[0] == NULL);
81         tal_free(split);
82
83         split = path_split(ctx, PATH_SEP_STR);
84         ok1(tal_parent(split) == ctx);
85         ok1(streq(split[0], PATH_SEP_STR));
86         ok1(split[1] == NULL);
87         tal_free(split);
88
89         /* Test take */
90         split = path_split(ctx, take(tal_strdup(ctx, PATH_SEP_STR)));
91         ok1(tal_parent(split) == ctx);
92         ok1(streq(split[0], PATH_SEP_STR));
93         ok1(split[1] == NULL);
94         tal_free(split);
95         ok1(tal_first(ctx) == NULL);
96
97         split = path_split(ctx, take(NULL));
98         ok1(!split);
99         ok1(tal_first(ctx) == NULL);
100
101         ok1(tal_first(NULL) == ctx && tal_next(NULL, ctx) == NULL);
102         tal_free(ctx);
103
104         return exit_status();
105 }