Add author and maintainer fields.
[ccan] / ccan / str / test / run.c
1 #include <ccan/str/str.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <ccan/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", NULL };
10
11 #define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
12
13 static char *strdup_rev(const char *s)
14 {
15         char *ret = strdup(s);
16         unsigned int i;
17
18         for (i = 0; i < strlen(s); i++)
19                 ret[i] = s[strlen(s) - i - 1];
20         return ret;
21 }
22
23 int main(int argc, char *argv[])
24 {
25         unsigned int i, j, n;
26         char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
27         
28         n = 0;
29         for (i = 0; i < NUM_SUBSTRINGS; i++) {
30                 for (j = 0; j < NUM_SUBSTRINGS; j++) {
31                         strings[n] = malloc(strlen(substrings[i])
32                                             + strlen(substrings[j]) + 1);
33                         sprintf(strings[n++], "%s%s",
34                                 substrings[i], substrings[j]);
35                 }
36         }
37
38         plan_tests(n * n * 5 + 3);
39         for (i = 0; i < n; i++) {
40                 for (j = 0; j < n; j++) {
41                         unsigned int k, identical = 0;
42                         char *reva, *revb;
43
44                         /* Find first difference. */
45                         for (k = 0; strings[i][k]==strings[j][k]; k++) {
46                                 if (k == strlen(strings[i])) {
47                                         identical = 1;
48                                         break;
49                                 }
50                         }
51
52                         if (identical) 
53                                 ok1(streq(strings[i], strings[j]));
54                         else
55                                 ok1(!streq(strings[i], strings[j]));
56
57                         /* Postfix test should be equivalent to prefix
58                          * test on reversed string. */
59                         reva = strdup_rev(strings[i]);
60                         revb = strdup_rev(strings[j]);
61
62                         if (!strings[i][k]) {
63                                 ok1(strstarts(strings[j], strings[i]));
64                                 ok1(strends(revb, reva));
65                         } else {
66                                 ok1(!strstarts(strings[j], strings[i]));
67                                 ok1(!strends(revb, reva));
68                         }
69                         if (!strings[j][k]) {
70                                 ok1(strstarts(strings[i], strings[j]));
71                                 ok1(strends(reva, revb));
72                         } else {
73                                 ok1(!strstarts(strings[i], strings[j]));
74                                 ok1(!strends(reva, revb));
75                         }
76                 }
77         }
78
79         ok1(streq(stringify(NUM_SUBSTRINGS),
80                   "((sizeof(substrings) / sizeof(substrings[0])) - 1)"));
81         ok1(streq(stringify(ARRAY_SIZE(substrings)),
82                   "(sizeof(substrings) / sizeof(substrings[0]))"));
83         ok1(streq(stringify(i == 0), "i == 0"));
84
85         return exit_status();
86 }