s3: Fix some nonempty line endings
[Samba/gebeck_regimport.git] / lib / ccan / str / test / run.c
blobf253746e7b354765a23456fc719affb81351a0d6
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>
7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
9 static const char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar",
10 NULL };
12 #define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
14 static char *strdup_rev(const char *s)
16 char *ret = strdup(s);
17 unsigned int i;
19 for (i = 0; i < strlen(s); i++)
20 ret[i] = s[strlen(s) - i - 1];
21 return ret;
24 int main(int argc, char *argv[])
26 unsigned int i, j, n;
27 char *strings[NUM_SUBSTRINGS * NUM_SUBSTRINGS];
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]);
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;
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;
53 if (identical)
54 ok1(streq(strings[i], strings[j]));
55 else
56 ok1(!streq(strings[i], strings[j]));
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]);
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));
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));
77 free(reva);
78 free(revb);
82 for (i = 0; i < n; i++)
83 free(strings[i]);
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"));
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);
105 return exit_status();