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