strjoin()
[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", 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 + 19);
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         split = strsplit(NULL, "hello  world", " ", &n);
83         ok1(n == 3);
84         ok1(streq(split[0], "hello"));
85         ok1(streq(split[1], ""));
86         ok1(streq(split[2], "world"));
87         ok1(split[3] == NULL);
88         talloc_free(split);
89
90         split = strsplit(NULL, "hello  world", " ", NULL);
91         ok1(streq(split[0], "hello"));
92         ok1(streq(split[1], ""));
93         ok1(streq(split[2], "world"));
94         ok1(split[3] == NULL);
95         talloc_free(split);
96
97         split = strsplit(NULL, "hello  world", "o ", NULL);
98         ok1(streq(split[0], "hell"));
99         ok1(streq(split[1], ""));
100         ok1(streq(split[2], ""));
101         ok1(streq(split[3], "w"));
102         ok1(streq(split[4], "rld"));
103         ok1(split[5] == NULL);
104
105         ctx = split;
106         split = strsplit(ctx, "hello  world", "o ", NULL);
107         ok1(talloc_parent(split) == ctx);
108         talloc_free(ctx);
109
110         str = strjoin(NULL, substrings, ", ");
111         ok1(streq(str, "far, bar, baz, b, ba, z, ar, "));
112         ctx = str;
113         str = strjoin(ctx, substrings, "");
114         ok1(streq(str, "farbarbazbbazar"));
115         ok1(talloc_parent(str) == ctx);
116         talloc_free(ctx);
117
118
119         
120         return exit_status();
121 }