tal/path: new module
[ccan] / ccan / tal / path / test / run-basename.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 *path, *ctx = tal_strdup(NULL, "ctx");
8
9         plan_tests(26);
10
11         path = path_basename(ctx, "/usr/lib");
12         ok1(streq(path, "lib"));
13         ok1(tal_parent(path) == ctx);
14         path = path_basename(ctx, "/usr/");
15         ok1(streq(path, "usr"));
16         ok1(tal_parent(path) == ctx);
17         path = path_basename(ctx, "/usr//");
18         ok1(streq(path, "usr"));
19         ok1(tal_parent(path) == ctx);
20         path = path_basename(ctx, "usr");
21         ok1(streq(path, "usr"));
22         ok1(tal_parent(path) == ctx);
23         path = path_basename(ctx, "/");
24         ok1(streq(path, "/"));
25         ok1(tal_parent(path) == ctx);
26         path = path_basename(ctx, "//");
27         ok1(streq(path, "/"));
28         ok1(tal_parent(path) == ctx);
29         path = path_basename(ctx, ".");
30         ok1(streq(path, "."));
31         ok1(tal_parent(path) == ctx);
32         path = path_basename(ctx, "./");
33         ok1(streq(path, "."));
34         ok1(tal_parent(path) == ctx);
35         path = path_basename(ctx, "..");
36         ok1(streq(path, ".."));
37         ok1(tal_parent(path) == ctx);
38         path = path_basename(ctx, "../");
39         ok1(streq(path, ".."));
40         ok1(tal_parent(path) == ctx);
41         tal_free(ctx);
42
43         ctx = tal_strdup(NULL, "ctx");
44         ok1(!tal_first(ctx));
45
46         /* Test take */
47         path = path_basename(ctx, take(tal_strdup(ctx, "..")));
48         ok1(streq(path, ".."));
49         ok1(tal_parent(path) == ctx);
50         ok1(tal_first(ctx) == path && !tal_next(ctx, path));
51         tal_free(path);
52         ok1(path_basename(ctx, take(NULL)) == NULL);
53         ok1(!tal_first(ctx));
54
55         tal_free(ctx);
56
57         return exit_status();
58 }