Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / string / strcmp / strcmp_test.c
blob6c8da26dca05da3ce739496bd110263f70a5cfa6
1 /*
2 * Written by J.T. Conklin <jtc@acorntoolworks.com>
3 * Public domain.
4 */
6 /*
7 * str*() regression suite
9 * Trivial str*() implementations can be audited by hand. Optimized
10 * versions that unroll loops, use naturally-aligned memory acesses,
11 * and "magic" arithmetic sequences to detect zero-bytes, written in
12 * assembler are much harder to validate. This program attempts to
13 * catch the corner cases.
15 * BUGS:
16 * Misssing checks for strncpy, strncat, strncmp, etc.
18 * TODO:
19 * Use mmap/mprotect to ensure the functions don't access memory
20 * across page boundaries.
22 * Consider generating tables programmatically. It would reduce
23 * the size, but it's also one more thing to go wrong.
25 * Share tables between strlen, strcpy, and strcat?
26 * Share tables between strchr and strrchr?
29 #include <assert.h>
30 #include <string.h>
32 void check_strcmp(void);
34 void
35 check_strcmp(void)
37 /* try to trick the compiler */
38 int (*f)(const char *, const char *s) = strcmp;
40 int a0;
41 int a1;
42 int t;
43 char buf0[64];
44 char buf1[64];
45 int ret;
47 struct tab {
48 const char* val0;
49 const char* val1;
50 int ret;
53 const struct tab tab[] = {
54 { "", "", 0 },
56 { "a", "a", 0 },
57 { "a", "b", -1 },
58 { "b", "a", +1 },
59 { "", "a", -1 },
60 { "a", "", +1 },
62 { "aa", "aa", 0 },
63 { "aa", "ab", -1 },
64 { "ab", "aa", +1 },
65 { "a", "aa", -1 },
66 { "aa", "a", +1 },
68 { "aaa", "aaa", 0 },
69 { "aaa", "aab", -1 },
70 { "aab", "aaa", +1 },
71 { "aa", "aaa", -1 },
72 { "aaa", "aa", +1 },
74 { "aaaa", "aaaa", 0 },
75 { "aaaa", "aaab", -1 },
76 { "aaab", "aaaa", +1 },
77 { "aaa", "aaaa", -1 },
78 { "aaaa", "aaa", +1 },
80 { "aaaaa", "aaaaa", 0 },
81 { "aaaaa", "aaaab", -1 },
82 { "aaaab", "aaaaa", +1 },
83 { "aaaa", "aaaaa", -1 },
84 { "aaaaa", "aaaa", +1 },
86 { "aaaaaa", "aaaaaa", 0 },
87 { "aaaaaa", "aaaaab", -1 },
88 { "aaaaab", "aaaaaa", +1 },
89 { "aaaaa", "aaaaaa", -1 },
90 { "aaaaaa", "aaaaa", +1 },
93 for (a0 = 0; a0 < sizeof(long); ++a0) {
94 for (a1 = 0; a1 < sizeof(long); ++a1) {
95 for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
96 memcpy(&buf0[a0], tab[t].val0, strlen(tab[t].val0) + 1);
97 memcpy(&buf1[a1], tab[t].val1, strlen(tab[t].val1) + 1);
99 ret = f(&buf0[a0], &buf1[a1]);
101 assert ((ret == 0 && tab[t].ret == 0) ||
102 (ret < 0 && tab[t].ret < 0) ||
103 (ret > 0 && tab[t].ret > 0));
110 main(void)
112 check_strcmp();
113 return 0;