str: fix warnings.
[ccan] / ccan / str / test / run.c
1 #include <ccan/str/str.h>
2 #include <ccan/str/str.c>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ccan/tap/tap.h>
6
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
8
9 static const char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar",
10                                     NULL };
11
12 #define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
13
14 static char *strdup_rev(const char *s)
15 {
16         char *ret = strdup(s);
17         unsigned int i;
18
19         for (i = 0; i < strlen(s); i++)
20                 ret[i] = s[strlen(s) - i - 1];
21         return ret;
22 }
23
24 int main(int argc, char *argv[])
25 {
26         unsigned int i, j, n;
27         char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
28         
29         n = 0;
30         for (i = 0; i < NUM_SUBSTRINGS; i++) {
31                 for (j = 0; j < NUM_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                         free(reva);
78                         free(revb);
79                 }
80         }
81
82         for (i = 0; i < n; i++)
83                 free(strings[i]);
84
85         ok1(streq(stringify(NUM_SUBSTRINGS),
86                   "((sizeof(substrings) / sizeof(substrings[0])) - 1)"));
87         ok1(streq(stringify(ARRAY_SIZE(substrings)),
88                   "(sizeof(substrings) / sizeof(substrings[0]))"));
89         ok1(streq(stringify(i == 0), "i == 0"));
90
91         ok1(strcount("aaaaaa", "b") == 0);
92         ok1(strcount("aaaaaa", "a") == 6);
93         ok1(strcount("aaaaaa", "aa") == 3);
94         ok1(strcount("aaaaaa", "aaa") == 2);
95         ok1(strcount("aaaaaa", "aaaa") == 1);
96         ok1(strcount("aaaaaa", "aaaaa") == 1);
97         ok1(strcount("aaaaaa", "aaaaaa") == 1);
98         ok1(strcount("aaa aaa", "b") == 0);
99         ok1(strcount("aaa aaa", "a") == 6);
100         ok1(strcount("aaa aaa", "aa") == 2);
101         ok1(strcount("aaa aaa", "aaa") == 2);
102         ok1(strcount("aaa aaa", "aaaa") == 0);
103         ok1(strcount("aaa aaa", "aaaaa") == 0);
104
105         return exit_status();
106 }