Rename string to str, and split into three modules.
[ccan] / ccan / str / 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", 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 **split, *str;
28         void *ctx;
29         char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
30         
31         n = 0;
32         for (i = 0; i < NUM_SUBSTRINGS; i++) {
33                 for (j = 0; j < NUM_SUBSTRINGS; j++) {
34                         strings[n] = malloc(strlen(substrings[i])
35                                             + strlen(substrings[j]) + 1);
36                         sprintf(strings[n++], "%s%s",
37                                 substrings[i], substrings[j]);
38                 }
39         }
40
41         plan_tests(n * n * 5);
42         for (i = 0; i < n; i++) {
43                 for (j = 0; j < n; j++) {
44                         unsigned int k, identical = 0;
45                         char *reva, *revb;
46
47                         /* Find first difference. */
48                         for (k = 0; strings[i][k]==strings[j][k]; k++) {
49                                 if (k == strlen(strings[i])) {
50                                         identical = 1;
51                                         break;
52                                 }
53                         }
54
55                         if (identical) 
56                                 ok1(streq(strings[i], strings[j]));
57                         else
58                                 ok1(!streq(strings[i], strings[j]));
59
60                         /* Postfix test should be equivalent to prefix
61                          * test on reversed string. */
62                         reva = strdup_rev(strings[i]);
63                         revb = strdup_rev(strings[j]);
64
65                         if (!strings[i][k]) {
66                                 ok1(strstarts(strings[j], strings[i]));
67                                 ok1(strends(revb, reva));
68                         } else {
69                                 ok1(!strstarts(strings[j], strings[i]));
70                                 ok1(!strends(revb, reva));
71                         }
72                         if (!strings[j][k]) {
73                                 ok1(strstarts(strings[i], strings[j]));
74                                 ok1(strends(reva, revb));
75                         } else {
76                                 ok1(!strstarts(strings[i], strings[j]));
77                                 ok1(!strends(reva, revb));
78                         }
79                 }
80         }
81
82         return exit_status();
83 }