089bfd3e4fda6a72be3f157b3a1ca60617b71d0c
[ccan] / ccan / string / test / run.c
1 #include "string/string.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "string/string.c"
5 #include "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" };
11
12 static char *strdup_rev(const char *s)
13 {
14         char *ret = strdup(s);
15         unsigned int i;
16
17         for (i = 0; i < strlen(s); i++)
18                 ret[i] = s[strlen(s) - i - 1];
19         return ret;
20 }
21
22 int main(int argc, char *argv[])
23 {
24         unsigned int i, j, n;
25         char **split;
26         void *ctx;
27         char *strings[ARRAY_SIZE(substrings) * ARRAY_SIZE(substrings)];
28
29         n = 0;
30         for (i = 0; i < ARRAY_SIZE(substrings); i++) {
31                 for (j = 0; j < ARRAY_SIZE(substrings); j++) {
32                         strings[n] = malloc(strlen(substrings[i])
33                                             + strlen(substrings[j]) + 1);
34                         sprintf(strings[n++], "%s%s",
35                                 substrings[i], substrings[j]);
36                 }
37         }
38
39         plan_tests(n * n * 5 + 16);
40         for (i = 0; i < n; i++) {
41                 for (j = 0; j < n; j++) {
42                         unsigned int k, identical = 0;
43                         char *reva, *revb;
44
45                         /* Find first difference. */
46                         for (k = 0; strings[i][k]==strings[j][k]; k++) {
47                                 if (k == strlen(strings[i])) {
48                                         identical = 1;
49                                         break;
50                                 }
51                         }
52
53                         if (identical) 
54                                 ok1(streq(strings[i], strings[j]));
55                         else
56                                 ok1(!streq(strings[i], strings[j]));
57
58                         /* Postfix test should be equivalent to prefix
59                          * test on reversed string. */
60                         reva = strdup_rev(strings[i]);
61                         revb = strdup_rev(strings[j]);
62
63                         if (!strings[i][k]) {
64                                 ok1(strstarts(strings[j], strings[i]));
65                                 ok1(strends(revb, reva));
66                         } else {
67                                 ok1(!strstarts(strings[j], strings[i]));
68                                 ok1(!strends(revb, reva));
69                         }
70                         if (!strings[j][k]) {
71                                 ok1(strstarts(strings[i], strings[j]));
72                                 ok1(strends(reva, revb));
73                         } else {
74                                 ok1(!strstarts(strings[i], strings[j]));
75                                 ok1(!strends(reva, revb));
76                         }
77                 }
78         }
79
80         split = strsplit(NULL, "hello  world", " ", &n);
81         ok1(n == 3);
82         ok1(streq(split[0], "hello"));
83         ok1(streq(split[1], ""));
84         ok1(streq(split[2], "world"));
85         ok1(split[3] == NULL);
86         talloc_free(split);
87
88         split = strsplit(NULL, "hello  world", " ", NULL);
89         ok1(streq(split[0], "hello"));
90         ok1(streq(split[1], ""));
91         ok1(streq(split[2], "world"));
92         ok1(split[3] == NULL);
93         talloc_free(split);
94
95         split = strsplit(NULL, "hello  world", "o ", NULL);
96         ok1(streq(split[0], "hell"));
97         ok1(streq(split[1], ""));
98         ok1(streq(split[2], ""));
99         ok1(streq(split[3], "w"));
100         ok1(streq(split[4], "rld"));
101         ok1(split[5] == NULL);
102
103         ctx = split;
104         split = strsplit(ctx, "hello  world", "o ", NULL);
105         ok1(talloc_parent(split) == ctx);
106         talloc_free(ctx);
107         
108         return exit_status();
109 }