new test system
[libc-test] / src / functional / string.c
1 #define _BSD_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include "test.h"
5
6 /* r = place to store result
7  * f = function call to test (or any expression)
8  * x = expected result
9  * m = message to print on failure (with formats for r & x)
10 **/
11
12 #define TEST(r, f, x, m) ( \
13         ((r) = (f)) == (x) || \
14         (t_error("%s failed (" m ")\n", #f, r, x), 0) )
15
16 #define TEST_S(s, x, m) ( \
17         !strcmp((s),(x)) || \
18         (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
19
20 int main(void)
21 {
22         char b[32];
23         char *s;
24         int i;
25
26         b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
27         TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
28         TEST_S(s, "abc", "strcpy gave incorrect string");
29         TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
30         TEST_S(s, "abc", "strcpy gave incorrect string");
31         TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
32         TEST_S(s, "abc", "strcpy gave incorrect string");
33         TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
34         TEST_S(s, "abc", "strcpy gave incorrect string");
35
36         TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
37         TEST_S(s, "bc", "strcpy gave incorrect string");
38         TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
39         TEST_S(s, "c", "strcpy gave incorrect string");
40         TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
41         TEST_S(s, "", "strcpy gave incorrect string");
42
43         TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
44         TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
45         TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
46         TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
47
48         b[3] = 'x'; b[4] = 0;
49         strncpy(b, "abc", 3);
50         TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
51         TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
52
53         TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
54         TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
55
56         strcpy(b, "abc");
57         TEST(s, strncat(b, "123456", 3), b, "%p != %p");
58         TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
59         TEST_S(s, "abc123", "strncat gave incorrect string");
60
61         strcpy(b, "aaababccdd0001122223");
62         TEST(s, strchr(b, 'b'), b+3, "%p != %p");
63         TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
64         TEST(i, strspn(b, "abcd"), 10, "%d != %d");
65         TEST(i, strcspn(b, "0123"), 10, "%d != %d");
66         TEST(s, strpbrk(b, "0123"), b+10, "%d != %d");
67
68         strcpy(b, "abc   123; xyz; foo");
69         TEST(s, strtok(b, " "), b, "%p != %p");
70         TEST_S(s, "abc", "strtok result");
71
72         TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
73         TEST_S(s, "  123", "strtok result");
74
75         TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
76         TEST_S(s, "xyz", "strtok result");
77
78         TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
79         TEST_S(s, "foo", "strtok result");
80
81 #ifdef HAVE_BSD_STRL
82         memset(b, 'x', sizeof b);
83         TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
84         TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
85         TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
86
87         memset(b, 'x', sizeof b);
88         TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
89         TEST(i, b[0], 'a', "strlcpy did not copy character %d");
90         TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
91
92         memset(b, 'x', sizeof b);
93         TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
94         TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
95
96         TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
97
98         memcpy(b, "abc\0\0\0x\0", 8);
99         TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
100         TEST_S(b, "abc123", "strlcat result");
101
102         memcpy(b, "abc\0\0\0x\0", 8);
103         TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
104         TEST_S(b, "abc12", "strlcat result");
105         TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
106
107         memcpy(b, "abc\0\0\0x\0", 8);
108         TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
109         TEST_S(b, "abc", "strlcat result");
110
111         memcpy(b, "abc\0\0\0x\0", 8);
112         TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
113         TEST_S(b, "abc", "strlcat result");
114 #endif
115         return t_status;
116 }