Move modules to ccan/ tools to tools/
[ccan] / ccan / string / test / run.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "string/string.h"
4 #include "tap/tap.h"
5
6 /* FIXME: ccanize */
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
8
9 static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar" };
10
11 static char *strdup_rev(const char *s)
12 {
13         char *ret = strdup(s);
14         unsigned int i;
15
16         for (i = 0; i < strlen(s); i++)
17                 ret[i] = s[strlen(s) - i - 1];
18         return ret;
19 }
20
21 int main(int argc, char *argv[])
22 {
23         unsigned int i, j, n;
24         char *strings[ARRAY_SIZE(substrings) * ARRAY_SIZE(substrings)];
25
26         n = 0;
27         for (i = 0; i < ARRAY_SIZE(substrings); i++) {
28                 for (j = 0; j < ARRAY_SIZE(substrings); j++) {
29                         strings[n] = malloc(strlen(substrings[i])
30                                             + strlen(substrings[j]) + 1);
31                         sprintf(strings[n++], "%s%s",
32                                 substrings[i], substrings[j]);
33                 }
34         }
35
36         plan_tests(n * n * 5);
37         for (i = 0; i < n; i++) {
38                 for (j = 0; j < n; j++) {
39                         unsigned int k, identical = 0;
40                         char *reva, *revb;
41
42                         /* Find first difference. */
43                         for (k = 0; strings[i][k]==strings[j][k]; k++) {
44                                 if (k == strlen(strings[i])) {
45                                         identical = 1;
46                                         break;
47                                 }
48                         }
49
50                         if (identical) 
51                                 ok1(streq(strings[i], strings[j]));
52                         else
53                                 ok1(!streq(strings[i], strings[j]));
54
55                         /* Postfix test should be equivalent to prefix
56                          * test on reversed string. */
57                         reva = strdup_rev(strings[i]);
58                         revb = strdup_rev(strings[j]);
59
60                         if (!strings[i][k]) {
61                                 ok1(strstarts(strings[j], strings[i]));
62                                 ok1(strends(revb, reva));
63                         } else {
64                                 ok1(!strstarts(strings[j], strings[i]));
65                                 ok1(!strends(revb, reva));
66                         }
67                         if (!strings[j][k]) {
68                                 ok1(strstarts(strings[i], strings[j]));
69                                 ok1(strends(reva, revb));
70                         } else {
71                                 ok1(!strstarts(strings[i], strings[j]));
72                                 ok1(!strends(reva, revb));
73                         }
74                 }
75         }
76         return exit_status();
77 }