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 
strdup_rev(const char * s)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 
main(int argc,char * argv[])24 int main(int argc, char *argv[])
25 {
26 	unsigned int i, j, n;
27 	char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
28 
29 	(void)argc;
30 	(void)argv;
31 
32 	n = 0;
33 	for (i = 0; i < NUM_SUBSTRINGS; i++) {
34 		for (j = 0; j < NUM_SUBSTRINGS; j++) {
35 			strings[n] = malloc(strlen(substrings[i])
36 					    + strlen(substrings[j]) + 1);
37 			sprintf(strings[n++], "%s%s",
38 				substrings[i], substrings[j]);
39 		}
40 	}
41 
42 	plan_tests(n * n * 5 + 16);
43 	for (i = 0; i < n; i++) {
44 		for (j = 0; j < n; j++) {
45 			unsigned int k, identical = 0;
46 			char *reva, *revb;
47 
48 			/* Find first difference. */
49 			for (k = 0; strings[i][k]==strings[j][k]; k++) {
50 				if (k == strlen(strings[i])) {
51 					identical = 1;
52 					break;
53 				}
54 			}
55 
56 			if (identical)
57 				ok1(streq(strings[i], strings[j]));
58 			else
59 				ok1(!streq(strings[i], strings[j]));
60 
61 			/* Postfix test should be equivalent to prefix
62 			 * test on reversed string. */
63 			reva = strdup_rev(strings[i]);
64 			revb = strdup_rev(strings[j]);
65 
66 			if (!strings[i][k]) {
67 				ok1(strstarts(strings[j], strings[i]));
68 				ok1(strends(revb, reva));
69 			} else {
70 				ok1(!strstarts(strings[j], strings[i]));
71 				ok1(!strends(revb, reva));
72 			}
73 			if (!strings[j][k]) {
74 				ok1(strstarts(strings[i], strings[j]));
75 				ok1(strends(reva, revb));
76 			} else {
77 				ok1(!strstarts(strings[i], strings[j]));
78 				ok1(!strends(reva, revb));
79 			}
80 			free(reva);
81 			free(revb);
82 		}
83 	}
84 
85 	for (i = 0; i < n; i++)
86 		free(strings[i]);
87 
88 	ok1(streq(stringify(NUM_SUBSTRINGS),
89 		  "((sizeof(substrings) / sizeof(substrings[0])) - 1)"));
90 	ok1(streq(stringify(ARRAY_SIZE(substrings)),
91 		  "(sizeof(substrings) / sizeof(substrings[0]))"));
92 	ok1(streq(stringify(i == 0), "i == 0"));
93 
94 	ok1(strcount("aaaaaa", "b") == 0);
95 	ok1(strcount("aaaaaa", "a") == 6);
96 	ok1(strcount("aaaaaa", "aa") == 3);
97 	ok1(strcount("aaaaaa", "aaa") == 2);
98 	ok1(strcount("aaaaaa", "aaaa") == 1);
99 	ok1(strcount("aaaaaa", "aaaaa") == 1);
100 	ok1(strcount("aaaaaa", "aaaaaa") == 1);
101 	ok1(strcount("aaa aaa", "b") == 0);
102 	ok1(strcount("aaa aaa", "a") == 6);
103 	ok1(strcount("aaa aaa", "aa") == 2);
104 	ok1(strcount("aaa aaa", "aaa") == 2);
105 	ok1(strcount("aaa aaa", "aaaa") == 0);
106 	ok1(strcount("aaa aaa", "aaaaa") == 0);
107 
108 	return exit_status();
109 }
110