6 /* r = place to store result
7 * f = function call to test (or any expression)
9 * m = message to print on failure (with formats for r & x)
12 #define TEST(r, f, x, m) ( \
13 ((r) = (f)) == (x) || \
14 (t_error("%s failed (" m ")\n", #f, r, x), 0) )
16 #define TEST_S(s, x, m) ( \
18 (t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
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");
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");
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)");
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");
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");
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");
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");
68 strcpy(b
, "abc 123; xyz; foo");
69 TEST(s
, strtok(b
, " "), b
, "%p != %p");
70 TEST_S(s
, "abc", "strtok result");
72 TEST(s
, strtok(NULL
, ";"), b
+4, "%p != %p");
73 TEST_S(s
, " 123", "strtok result");
75 TEST(s
, strtok(NULL
, " ;"), b
+11, "%p != %p");
76 TEST_S(s
, "xyz", "strtok result");
78 TEST(s
, strtok(NULL
, " ;"), b
+16, "%p != %p");
79 TEST_S(s
, "foo", "strtok result");
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)");
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)");
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)");
95 TEST(i
, strlcpy(NULL
, "abc", 0), 3, "length %d != %d");
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");
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");
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");
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");