xref: /xnu-12377.41.6/tests/test_strings.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions #define _FORTIFY_SOURCE 0
2*bbb1b6f9SApple OSS Distributions #define __arch_memcmp_zero_ptr_aligned
3*bbb1b6f9SApple OSS Distributions 
4*bbb1b6f9SApple OSS Distributions /* must include first because otherwise header guard conflicts with SDK's
5*bbb1b6f9SApple OSS Distributions  * string.h (quite reasonably)
6*bbb1b6f9SApple OSS Distributions  */
7*bbb1b6f9SApple OSS Distributions #include "../osfmk/libsa/string.h"
8*bbb1b6f9SApple OSS Distributions 
9*bbb1b6f9SApple OSS Distributions char *strerror(int);
10*bbb1b6f9SApple OSS Distributions char *itoa(int, char *);
11*bbb1b6f9SApple OSS Distributions 
12*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
13*bbb1b6f9SApple OSS Distributions #include <darwintest_utils.h>
14*bbb1b6f9SApple OSS Distributions 
15*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wlanguage-extension-token"
16*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wformat-pedantic"
17*bbb1b6f9SApple OSS Distributions 
18*bbb1b6f9SApple OSS Distributions #define DEVELOPMENT 0
19*bbb1b6f9SApple OSS Distributions #define DEBUG 0
20*bbb1b6f9SApple OSS Distributions #define XNU_KERNEL_PRIVATE 1
21*bbb1b6f9SApple OSS Distributions 
22*bbb1b6f9SApple OSS Distributions __printflike(1, 2) __attribute__((noreturn))
23*bbb1b6f9SApple OSS Distributions static void
panic(const char * fmt,...)24*bbb1b6f9SApple OSS Distributions panic(const char *fmt, ...)
25*bbb1b6f9SApple OSS Distributions {
26*bbb1b6f9SApple OSS Distributions 	va_list ap;
27*bbb1b6f9SApple OSS Distributions 	va_start(ap, fmt);
28*bbb1b6f9SApple OSS Distributions 	vfprintf(stderr, fmt, ap);
29*bbb1b6f9SApple OSS Distributions 	abort();
30*bbb1b6f9SApple OSS Distributions }
31*bbb1b6f9SApple OSS Distributions 
32*bbb1b6f9SApple OSS Distributions #include "../libkern/libkern/section_keywords.h"
33*bbb1b6f9SApple OSS Distributions #include "../osfmk/machine/string.h"
34*bbb1b6f9SApple OSS Distributions #include "../osfmk/device/subrs.c"
35*bbb1b6f9SApple OSS Distributions 
36*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeclaration-after-statement"
37*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wgnu-designator"
38*bbb1b6f9SApple OSS Distributions 
39*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
40*bbb1b6f9SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true),
41*bbb1b6f9SApple OSS Distributions 	T_META_TAG_VM_PREFERRED);
42*bbb1b6f9SApple OSS Distributions 
43*bbb1b6f9SApple OSS Distributions T_DECL(strbufcmp, "strbufcmp") {
44*bbb1b6f9SApple OSS Distributions #define T_COMPARE(A, AS, B, BS, EQ) T_ASSERT_EQ(strbufcmp_impl((A), (AS), (B), (BS)), (EQ), "compare '%s'.%zu, '%s'.%zu", (A), (AS), (B), (BS))
45*bbb1b6f9SApple OSS Distributions 	// two identical strings
46*bbb1b6f9SApple OSS Distributions 	char a[] = "hello";
47*bbb1b6f9SApple OSS Distributions 	char b[] = "hello";
48*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, sizeof(b), 0);
49*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, sizeof(a), 0);
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions 	// the same string
52*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, sizeof(b), 0);
53*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, sizeof(a), 0);
54*bbb1b6f9SApple OSS Distributions 
55*bbb1b6f9SApple OSS Distributions 	// two different strings
56*bbb1b6f9SApple OSS Distributions 	char c[] = "world";
57*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), c, sizeof(c), a[0] - c[0]);
58*bbb1b6f9SApple OSS Distributions 	T_COMPARE(c, sizeof(c), a, sizeof(a), c[0] - a[0]);
59*bbb1b6f9SApple OSS Distributions 	char d[] = "hellp";
60*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), d, sizeof(d), 'o' - 'p');
61*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d), a, sizeof(a), 'p' - 'o');
62*bbb1b6f9SApple OSS Distributions 
63*bbb1b6f9SApple OSS Distributions 	// strings of different size
64*bbb1b6f9SApple OSS Distributions 	char e[] = "aaaa";
65*bbb1b6f9SApple OSS Distributions 	char f[] = "aaaab";
66*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e), f, sizeof(f), 0 - 'b');
67*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f), e, sizeof(e), 'b' - 0);
68*bbb1b6f9SApple OSS Distributions 
69*bbb1b6f9SApple OSS Distributions 	// strings that are not NUL-terminated
70*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, b, sizeof(b) - 1, 0);
71*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b) - 1, a, sizeof(a) - 1, 0);
72*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, d, sizeof(d) - 1, 'o' - 'p');
73*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d) - 1, a, sizeof(a) - 1, 'p' - 'o');
74*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e) - 1, f, sizeof(f) - 1, 0 - 'b');
75*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f) - 1, e, sizeof(e) - 1, 'b' - 0);
76*bbb1b6f9SApple OSS Distributions #undef T_COMPARE
77*bbb1b6f9SApple OSS Distributions }
78*bbb1b6f9SApple OSS Distributions 
79*bbb1b6f9SApple OSS Distributions T_DECL(strlcmp, "strlcmp") {
80*bbb1b6f9SApple OSS Distributions #define T_COMPARE(A, AS, B, EQ) T_ASSERT_EQ(strlcmp_impl((A), (B), (AS)), (EQ), "compare '%s'.%zu, '%s'", (A), (AS), (B))
81*bbb1b6f9SApple OSS Distributions 	// two identical strings
82*bbb1b6f9SApple OSS Distributions 	char a[] = "hello";
83*bbb1b6f9SApple OSS Distributions 	char b[] = "hello";
84*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, 0);
85*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, 0);
86*bbb1b6f9SApple OSS Distributions 
87*bbb1b6f9SApple OSS Distributions 	// the same string
88*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, 0);
89*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, 0);
90*bbb1b6f9SApple OSS Distributions 
91*bbb1b6f9SApple OSS Distributions 	// two different strings
92*bbb1b6f9SApple OSS Distributions 	char c[] = "world";
93*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), c, a[0] - c[0]);
94*bbb1b6f9SApple OSS Distributions 	T_COMPARE(c, sizeof(c), a, c[0] - a[0]);
95*bbb1b6f9SApple OSS Distributions 	char d[] = "hellp";
96*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), d, 'o' - 'p');
97*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d), a, 'p' - 'o');
98*bbb1b6f9SApple OSS Distributions 
99*bbb1b6f9SApple OSS Distributions 	// strings of different size
100*bbb1b6f9SApple OSS Distributions 	char e[] = "aaaa";
101*bbb1b6f9SApple OSS Distributions 	char f[] = "aaaab";
102*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e), f, 0 - 'b');
103*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f), e, 'b' - 0);
104*bbb1b6f9SApple OSS Distributions 
105*bbb1b6f9SApple OSS Distributions 	// strings that are not NUL-terminated
106*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, b, 0);
107*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b) - 1, a, 0);
108*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, d, 'o' - 'p');
109*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d) - 1, a, 'p' - 'o');
110*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e) - 1, f, 0 - 'b');
111*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f) - 1, e, 'b' - 0);
112*bbb1b6f9SApple OSS Distributions #undef T_COMPARE
113*bbb1b6f9SApple OSS Distributions }
114*bbb1b6f9SApple OSS Distributions 
115*bbb1b6f9SApple OSS Distributions T_DECL(strbufcasecmp, "strbufcasecmp") {
116*bbb1b6f9SApple OSS Distributions #define T_COMPARE(A, AS, B, BS, EQ) T_ASSERT_EQ(strbufcasecmp_impl((A), (AS), (B), (BS)), (EQ), "case-insensitive compare '%s'.%zu, '%s'.%zu", (A), (AS), (B), (BS))
117*bbb1b6f9SApple OSS Distributions 	// same tests as strcasecmp, then tests with individual characters
118*bbb1b6f9SApple OSS Distributions 	// two identical strings
119*bbb1b6f9SApple OSS Distributions 	char a[] = "hElLo";
120*bbb1b6f9SApple OSS Distributions 	char b[] = "HeLlO";
121*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, sizeof(b), 0);
122*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, sizeof(a), 0);
123*bbb1b6f9SApple OSS Distributions 
124*bbb1b6f9SApple OSS Distributions 	// the same string
125*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, sizeof(b), 0);
126*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, sizeof(a), 0);
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions 	// two different strings
129*bbb1b6f9SApple OSS Distributions 	char c[] = "world";
130*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), c, sizeof(c), a[0] - c[0]);
131*bbb1b6f9SApple OSS Distributions 	T_COMPARE(c, sizeof(c), a, sizeof(a), c[0] - a[0]);
132*bbb1b6f9SApple OSS Distributions 	char d[] = "hellp";
133*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), d, sizeof(d), 'o' - 'p');
134*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d), a, sizeof(a), 'p' - 'o');
135*bbb1b6f9SApple OSS Distributions 
136*bbb1b6f9SApple OSS Distributions 	// strings of different size
137*bbb1b6f9SApple OSS Distributions 	char e[] = "aAaA";
138*bbb1b6f9SApple OSS Distributions 	char f[] = "AaAaB";
139*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e), f, sizeof(f), 0 - 'b');
140*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f), e, sizeof(e), 'b' - 0);
141*bbb1b6f9SApple OSS Distributions 
142*bbb1b6f9SApple OSS Distributions 	// strings that are not NUL-terminated
143*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, b, sizeof(b) - 1, 0);
144*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b) - 1, a, sizeof(a) - 1, 0);
145*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, d, sizeof(d) - 1, 'o' - 'p');
146*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d) - 1, a, sizeof(a) - 1, 'p' - 'o');
147*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e) - 1, f, sizeof(f) - 1, 0 - 'b');
148*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f) - 1, e, sizeof(e) - 1, 'b' - 0);
149*bbb1b6f9SApple OSS Distributions #undef T_COMPARE
150*bbb1b6f9SApple OSS Distributions }
151*bbb1b6f9SApple OSS Distributions 
152*bbb1b6f9SApple OSS Distributions T_DECL(strlcasecmp, "strlcasecmp") {
153*bbb1b6f9SApple OSS Distributions #define T_COMPARE(A, AS, B, EQ) T_ASSERT_EQ(strlcasecmp_impl((A), (B), (AS)), (EQ), "case-insensitive compare '%s'.%zu, '%s'", (A), (AS), (B))
154*bbb1b6f9SApple OSS Distributions 	// same tests as strcasecmp, then tests with individual characters
155*bbb1b6f9SApple OSS Distributions 	// two identical strings
156*bbb1b6f9SApple OSS Distributions 	char a[] = "hElLo";
157*bbb1b6f9SApple OSS Distributions 	char b[] = "HeLlO";
158*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, 0);
159*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, 0);
160*bbb1b6f9SApple OSS Distributions 
161*bbb1b6f9SApple OSS Distributions 	// the same string
162*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), b, 0);
163*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b), a, 0);
164*bbb1b6f9SApple OSS Distributions 
165*bbb1b6f9SApple OSS Distributions 	// two different strings
166*bbb1b6f9SApple OSS Distributions 	char c[] = "world";
167*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), c, a[0] - c[0]);
168*bbb1b6f9SApple OSS Distributions 	T_COMPARE(c, sizeof(c), a, c[0] - a[0]);
169*bbb1b6f9SApple OSS Distributions 	char d[] = "hellp";
170*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a), d, 'o' - 'p');
171*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d), a, 'p' - 'o');
172*bbb1b6f9SApple OSS Distributions 
173*bbb1b6f9SApple OSS Distributions 	// strings of different size
174*bbb1b6f9SApple OSS Distributions 	char e[] = "aAaA";
175*bbb1b6f9SApple OSS Distributions 	char f[] = "AaAaB";
176*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e), f, 0 - 'b');
177*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f), e, 'b' - 0);
178*bbb1b6f9SApple OSS Distributions 
179*bbb1b6f9SApple OSS Distributions 	// strings that are not NUL-terminated
180*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, b, 0);
181*bbb1b6f9SApple OSS Distributions 	T_COMPARE(b, sizeof(b) - 1, a, 0);
182*bbb1b6f9SApple OSS Distributions 	T_COMPARE(a, sizeof(a) - 1, d, 'o' - 'p');
183*bbb1b6f9SApple OSS Distributions 	T_COMPARE(d, sizeof(d) - 1, a, 'p' - 'o');
184*bbb1b6f9SApple OSS Distributions 	T_COMPARE(e, sizeof(e) - 1, f, 0 - 'b');
185*bbb1b6f9SApple OSS Distributions 	T_COMPARE(f, sizeof(f) - 1, e, 'b' - 0);
186*bbb1b6f9SApple OSS Distributions #undef T_COMPARE
187*bbb1b6f9SApple OSS Distributions }
188*bbb1b6f9SApple OSS Distributions 
189*bbb1b6f9SApple OSS Distributions T_DECL(strbufcasecmp_all, "strbufcasecmp_all") {
190*bbb1b6f9SApple OSS Distributions #define T_CHAR_COMPARE(A, AS, B, BS, EQ) do { \
191*bbb1b6f9SApple OSS Distributions     int r = strbufcasecmp_impl((A), (AS), (B), (BS)); \
192*bbb1b6f9SApple OSS Distributions     if (r != (EQ)) T_FAIL("case-insensitive compare '0x%02hhx' to '0x%02hhx' was %i instead of %i", *(A), *(B), r, (EQ)); \
193*bbb1b6f9SApple OSS Distributions } while (0)
194*bbb1b6f9SApple OSS Distributions 	// test each character
195*bbb1b6f9SApple OSS Distributions 	char ga, gb, ha, hb;
196*bbb1b6f9SApple OSS Distributions 	char nul = 0;
197*bbb1b6f9SApple OSS Distributions 	for (int i = 0; i < 256; ++i) {
198*bbb1b6f9SApple OSS Distributions 		ga = (char)(i);
199*bbb1b6f9SApple OSS Distributions 		gb = (i >= 'A' && i <= 'Z') ? (char)(i - 'A' + 'a') : ga;
200*bbb1b6f9SApple OSS Distributions 		T_CHAR_COMPARE(&ga, 1, &nul, 0, gb);
201*bbb1b6f9SApple OSS Distributions 		T_CHAR_COMPARE(&nul, 0, &ga, 1, -gb);
202*bbb1b6f9SApple OSS Distributions 
203*bbb1b6f9SApple OSS Distributions 		for (int j = 0; j < 256; ++j) {
204*bbb1b6f9SApple OSS Distributions 			ha = (char)(j);
205*bbb1b6f9SApple OSS Distributions 			hb = (j >= 'A' && j <= 'Z') ? (char)(j - 'A' + 'a') : ha;
206*bbb1b6f9SApple OSS Distributions 			T_CHAR_COMPARE(&ga, 1, &ha, 1, gb - hb);
207*bbb1b6f9SApple OSS Distributions 			T_CHAR_COMPARE(&ha, 1, &ga, 1, hb - gb);
208*bbb1b6f9SApple OSS Distributions 		}
209*bbb1b6f9SApple OSS Distributions 	}
210*bbb1b6f9SApple OSS Distributions 	T_PASS("ASCII character case insensitivity");
211*bbb1b6f9SApple OSS Distributions }
212*bbb1b6f9SApple OSS Distributions 
213*bbb1b6f9SApple OSS Distributions T_DECL(strbufcpy, "strbufcpy") {
214*bbb1b6f9SApple OSS Distributions 	char dst[32];
215*bbb1b6f9SApple OSS Distributions 	// empty dest
216*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbufcpy_impl(NULL, 0, "hello", 5), NULL, "0-length destination");
217*bbb1b6f9SApple OSS Distributions 
218*bbb1b6f9SApple OSS Distributions #define T_CPY(A, AS, B, BS) T_ASSERT_EQ(strbufcpy_impl((A), (AS), (B), (BS)), (char *)(A), "copy '%.*s'.%zu to dst.%zu", (int)(BS), (B), (size_t)(BS), (AS))
219*bbb1b6f9SApple OSS Distributions 	// copy NUL terminated string that fits in dst
220*bbb1b6f9SApple OSS Distributions 	char hello[] = "hello";
221*bbb1b6f9SApple OSS Distributions 	memset(dst, 0, sizeof(dst));
222*bbb1b6f9SApple OSS Distributions 	T_CPY(dst, sizeof(dst), hello, sizeof(hello));
223*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(memcmp_impl(dst, (char[32]){"hello"}, sizeof(dst)), 0, "check result is 'hello'");
224*bbb1b6f9SApple OSS Distributions 
225*bbb1b6f9SApple OSS Distributions 	// copy NUL terminated string that does not fit in dst
226*bbb1b6f9SApple OSS Distributions 	char aaa[40] = {[0 ... 38] = 'a' };
227*bbb1b6f9SApple OSS Distributions 	memset(dst, 0, sizeof(dst));
228*bbb1b6f9SApple OSS Distributions 	T_CPY(dst, sizeof(dst), aaa, sizeof(aaa));
229*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(memcmp_impl(aaa, dst, 31), 0, "check result is 'aaaaaa...'");
230*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(dst[31], 0, "check result is NUL-terminated");
231*bbb1b6f9SApple OSS Distributions 
232*bbb1b6f9SApple OSS Distributions 	// copy non-terminated string
233*bbb1b6f9SApple OSS Distributions 	memset(dst, 0xff, sizeof(dst));
234*bbb1b6f9SApple OSS Distributions 	T_CPY(dst, sizeof(dst), "bbb", 3);
235*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strcmp_impl(dst, "bbb"), 0, "check result is 'bbb'");
236*bbb1b6f9SApple OSS Distributions 
237*bbb1b6f9SApple OSS Distributions 	// copy string over itself
238*bbb1b6f9SApple OSS Distributions 	char hw1[32] = "hello world";
239*bbb1b6f9SApple OSS Distributions 	T_CPY(hw1 + 6, sizeof(hw1) - 6, hw1, sizeof(hw1));
240*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strcmp_impl(hw1, "hello hello world"), 0, "check copy over self is 'hello hello world'");
241*bbb1b6f9SApple OSS Distributions 
242*bbb1b6f9SApple OSS Distributions 	char hw2[32] = "hello world";
243*bbb1b6f9SApple OSS Distributions 	T_CPY(hw2, sizeof(hw2), hw2 + 6, sizeof(hw2) - 6);
244*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strcmp_impl(hw2, "world"), 0, "check copy over self is 'world'");
245*bbb1b6f9SApple OSS Distributions #undef T_CPY
246*bbb1b6f9SApple OSS Distributions }
247*bbb1b6f9SApple OSS Distributions 
248*bbb1b6f9SApple OSS Distributions T_DECL(strbufcat, "strbufcat") {
249*bbb1b6f9SApple OSS Distributions 	char dst[32] = {0};
250*bbb1b6f9SApple OSS Distributions 
251*bbb1b6f9SApple OSS Distributions 	// empty dst
252*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbufcat_impl(NULL, 0, "hello", 5), NULL, "check concatenation to 0-length destination");
253*bbb1b6f9SApple OSS Distributions 
254*bbb1b6f9SApple OSS Distributions #define T_CAT_RESULT(RESULT) \
255*bbb1b6f9SApple OSS Distributions     T_ASSERT_EQ(strcmp_impl(dst, (RESULT)), 0, "check result of concatenation is '%s'", (RESULT)); \
256*bbb1b6f9SApple OSS Distributions 
257*bbb1b6f9SApple OSS Distributions #define T_CAT(TO_CAT, RESULT) do { \
258*bbb1b6f9SApple OSS Distributions     T_ASSERT_EQ(strbufcat_impl(dst, sizeof(dst), (TO_CAT), sizeof(TO_CAT)), (char *)dst, "check concatenation of '%s'", (TO_CAT)); \
259*bbb1b6f9SApple OSS Distributions     T_CAT_RESULT(RESULT); \
260*bbb1b6f9SApple OSS Distributions } while (0)
261*bbb1b6f9SApple OSS Distributions 
262*bbb1b6f9SApple OSS Distributions 	// append "hello "
263*bbb1b6f9SApple OSS Distributions 	T_CAT("hello ", "hello ");
264*bbb1b6f9SApple OSS Distributions 
265*bbb1b6f9SApple OSS Distributions 	// append "world!"
266*bbb1b6f9SApple OSS Distributions 	T_CAT("world!", "hello world!");
267*bbb1b6f9SApple OSS Distributions 
268*bbb1b6f9SApple OSS Distributions 	// append itself
269*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbufcat_impl(dst, sizeof(dst), dst, sizeof(dst)), (char *)dst, "check concatenating self");
270*bbb1b6f9SApple OSS Distributions 	T_CAT_RESULT("hello world!hello world!");
271*bbb1b6f9SApple OSS Distributions 
272*bbb1b6f9SApple OSS Distributions 	// append bunch of 'a's
273*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbufcat_impl(dst, sizeof(dst), "aaaaaaaaaa", 10), (char *)dst, "check concatenating 'aaaa...'");
274*bbb1b6f9SApple OSS Distributions 	T_CAT_RESULT("hello world!hello world!aaaaaaa");
275*bbb1b6f9SApple OSS Distributions 
276*bbb1b6f9SApple OSS Distributions #undef T_CAT
277*bbb1b6f9SApple OSS Distributions #undef T_CAT_RESULT
278*bbb1b6f9SApple OSS Distributions }
279*bbb1b6f9SApple OSS Distributions 
280*bbb1b6f9SApple OSS Distributions T_DECL(libsa_overloads, "libsa_overloads") {
281*bbb1b6f9SApple OSS Distributions 	char buf[32] = "hello, world";
282*bbb1b6f9SApple OSS Distributions 	char buf2[32] = "world, hello";
283*bbb1b6f9SApple OSS Distributions 
284*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbuflen(buf), (size_t)12, "strbuflen one argument");
285*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strbuflen(buf, sizeof(buf)), (size_t)12, "strbuflen two arguments");
286*bbb1b6f9SApple OSS Distributions 
287*bbb1b6f9SApple OSS Distributions 	T_ASSERT_LT(strbufcmp(buf, buf2), 0, "strbufcmp two arguments");
288*bbb1b6f9SApple OSS Distributions 	T_ASSERT_LT(strbufcmp(buf, sizeof(buf), buf2, sizeof(buf2)), 0, "strbufcmp four arguments");
289*bbb1b6f9SApple OSS Distributions 
290*bbb1b6f9SApple OSS Distributions 	T_ASSERT_LT(strbufcasecmp(buf, buf2), 0, "strbufcasecmp two arguments");
291*bbb1b6f9SApple OSS Distributions 	T_ASSERT_LT(strbufcasecmp(buf, sizeof(buf), buf2, sizeof(buf2)), 0, "strbufcasecmp four arguments");
292*bbb1b6f9SApple OSS Distributions 
293*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(strbufcpy(buf, buf2), NULL, "strbufcpy two arguments");
294*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(strbufcpy(buf, sizeof(buf), buf2, sizeof(buf2)), NULL, "strbufcpy four arguments");
295*bbb1b6f9SApple OSS Distributions 
296*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(strbufcat(buf, buf2), NULL, "strbufcat two arguments");
297*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(strbufcat(buf, sizeof(buf), buf2, sizeof(buf2)), NULL, "strbufcat four arguments");
298*bbb1b6f9SApple OSS Distributions }
299