1 #include <ccan/str/str.h>
2 #include <ccan/str/str.c>
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",
12 #define NUM_SUBSTRINGS (ARRAY_SIZE(substrings) - 1)
14 static char *strdup_rev(const char *s
)
16 char *ret
= strdup(s
);
19 for (i
= 0; i
< strlen(s
); i
++)
20 ret
[i
] = s
[strlen(s
) - i
- 1];
24 int main(int argc
, char *argv
[])
27 char *strings
[NUM_SUBSTRINGS
* NUM_SUBSTRINGS
];
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;
45 /* Find first difference. */
46 for (k
= 0; strings
[i
][k
]==strings
[j
][k
]; k
++) {
47 if (k
== strlen(strings
[i
])) {
54 ok1(streq(strings
[i
], strings
[j
]));
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
]);
64 ok1(strstarts(strings
[j
], strings
[i
]));
65 ok1(strends(revb
, reva
));
67 ok1(!strstarts(strings
[j
], strings
[i
]));
68 ok1(!strends(revb
, reva
));
71 ok1(strstarts(strings
[i
], strings
[j
]));
72 ok1(strends(reva
, revb
));
74 ok1(!strstarts(strings
[i
], strings
[j
]));
75 ok1(!strends(reva
, revb
));
82 for (i
= 0; i
< n
; 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();