Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / string / strcat / strcat_test.c
blob8fa0155912576554518156dbe5749e8b15d21b78
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_strcat(void);
34 void
35 check_strcat(void)
37 /* try to trick the compiler */
38 char * (*f)(char *, const char *s) = strcat;
40 int a0, a1;
41 int t0, t1;
42 char buf0[64];
43 char buf1[64];
44 char *ret;
46 struct tab {
47 const char* val;
48 size_t len;
51 const struct tab tab[] = {
53 * patterns that check for all combinations of leading and
54 * trailing unaligned characters (on a 64 bit processor)
57 { "", 0 },
58 { "a", 1 },
59 { "ab", 2 },
60 { "abc", 3 },
61 { "abcd", 4 },
62 { "abcde", 5 },
63 { "abcdef", 6 },
64 { "abcdefg", 7 },
65 { "abcdefgh", 8 },
66 { "abcdefghi", 9 },
67 { "abcdefghij", 10 },
68 { "abcdefghijk", 11 },
69 { "abcdefghijkl", 12 },
70 { "abcdefghijklm", 13 },
71 { "abcdefghijklmn", 14 },
72 { "abcdefghijklmno", 15 },
73 { "abcdefghijklmnop", 16 },
74 { "abcdefghijklmnopq", 17 },
75 { "abcdefghijklmnopqr", 18 },
76 { "abcdefghijklmnopqrs", 19 },
77 { "abcdefghijklmnopqrst", 20 },
78 { "abcdefghijklmnopqrstu", 21 },
79 { "abcdefghijklmnopqrstuv", 22 },
80 { "abcdefghijklmnopqrstuvw", 23 },
83 * patterns that check for the cases where the expression:
85 * ((word - 0x7f7f..7f) & 0x8080..80)
87 * returns non-zero even though there are no zero bytes in the
88 * word.
91 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
92 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
93 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
94 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
95 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
96 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
97 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
98 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
99 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
102 for (a0 = 0; a0 < sizeof(long); ++a0) {
103 for (a1 = 0; a1 < sizeof(long); ++a1) {
104 for (t0 = 0; t0 < (sizeof(tab) / sizeof(tab[0])); ++t0) {
105 for (t1 = 0; t1 < (sizeof(tab) / sizeof(tab[0])); ++t1) {
107 memcpy(&buf0[a0], tab[t0].val, tab[t0].len + 1);
108 memcpy(&buf1[a1], tab[t1].val, tab[t1].len + 1);
110 ret = f(&buf0[a0], &buf1[a1]);
112 // verify strcat returns address of first parameter
113 assert(&buf0[a0] == ret);
115 // verify string was copied correctly
116 assert(memcmp(&buf0[a0] + tab[t0].len,
117 &buf1[a1],
118 tab[t1].len + 1) == 0);
126 main(void)
128 check_strcat();
129 return 0;