xref: /freebsd/contrib/netbsd-tests/lib/libc/string/t_strchr.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper /* $NetBSD: t_strchr.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*57718be8SEnji Cooper  * Public domain.
6*57718be8SEnji Cooper  */
7*57718be8SEnji Cooper 
8*57718be8SEnji Cooper #include <atf-c.h>
9*57718be8SEnji Cooper #include <string.h>
10*57718be8SEnji Cooper #include <unistd.h>
11*57718be8SEnji Cooper #include <stdio.h>
12*57718be8SEnji Cooper #include <stdlib.h>
13*57718be8SEnji Cooper #include <dlfcn.h>
14*57718be8SEnji Cooper 
15*57718be8SEnji Cooper static char	*slow_strchr(char *, int);
16*57718be8SEnji Cooper static void	 verify_strchr(char *, int, unsigned int, unsigned int);
17*57718be8SEnji Cooper 
18*57718be8SEnji Cooper char * (*volatile strchr_fn)(const char *, int);
19*57718be8SEnji Cooper 
20*57718be8SEnji Cooper static char *
21*57718be8SEnji Cooper slow_strchr(char *buf, int ch)
22*57718be8SEnji Cooper {
23*57718be8SEnji Cooper 	unsigned char c = 1;
24*57718be8SEnji Cooper 
25*57718be8SEnji Cooper 	ch &= 0xff;
26*57718be8SEnji Cooper 
27*57718be8SEnji Cooper 	for (; c != 0; buf++) {
28*57718be8SEnji Cooper 		c = *buf;
29*57718be8SEnji Cooper 		if (c == ch)
30*57718be8SEnji Cooper 			return buf;
31*57718be8SEnji Cooper 	}
32*57718be8SEnji Cooper 	return 0;
33*57718be8SEnji Cooper }
34*57718be8SEnji Cooper 
35*57718be8SEnji Cooper static void
36*57718be8SEnji Cooper verify_strchr(char *buf, int ch, unsigned int t, unsigned int a)
37*57718be8SEnji Cooper {
38*57718be8SEnji Cooper 	const char *off, *ok_off;
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper 	off = strchr_fn(buf, ch);
41*57718be8SEnji Cooper 	ok_off = slow_strchr(buf, ch);
42*57718be8SEnji Cooper 	if (off == ok_off)
43*57718be8SEnji Cooper 		return;
44*57718be8SEnji Cooper 
45*57718be8SEnji Cooper 	fprintf(stderr, "test_strchr(\"%s\", %#x) gave %zd not %zd (test %d, "
46*57718be8SEnji Cooper 	    "alignment %d)\n",
47*57718be8SEnji Cooper 	    buf, ch, off ? off - buf : -1, ok_off ? ok_off - buf : -1, t, a);
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper 	atf_tc_fail("Check stderr for details");
50*57718be8SEnji Cooper }
51*57718be8SEnji Cooper 
52*57718be8SEnji Cooper ATF_TC(strchr_basic);
53*57718be8SEnji Cooper ATF_TC_HEAD(strchr_basic, tc)
54*57718be8SEnji Cooper {
55*57718be8SEnji Cooper 
56*57718be8SEnji Cooper         atf_tc_set_md_var(tc, "descr", "Test strchr(3) results");
57*57718be8SEnji Cooper }
58*57718be8SEnji Cooper 
59*57718be8SEnji Cooper ATF_TC_BODY(strchr_basic, tc)
60*57718be8SEnji Cooper {
61*57718be8SEnji Cooper 	unsigned int t, a;
62*57718be8SEnji Cooper 	char *off;
63*57718be8SEnji Cooper 	char buf[32];
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper 	const char *tab[] = {
66*57718be8SEnji Cooper 		"",
67*57718be8SEnji Cooper 		"a",
68*57718be8SEnji Cooper 		"aa",
69*57718be8SEnji Cooper 		"abc",
70*57718be8SEnji Cooper 		"abcd",
71*57718be8SEnji Cooper 		"abcde",
72*57718be8SEnji Cooper 		"abcdef",
73*57718be8SEnji Cooper 		"abcdefg",
74*57718be8SEnji Cooper 		"abcdefgh",
75*57718be8SEnji Cooper 
76*57718be8SEnji Cooper 		"/",
77*57718be8SEnji Cooper 		"//",
78*57718be8SEnji Cooper 		"/a",
79*57718be8SEnji Cooper 		"/a/",
80*57718be8SEnji Cooper 		"/ab",
81*57718be8SEnji Cooper 		"/ab/",
82*57718be8SEnji Cooper 		"/abc",
83*57718be8SEnji Cooper 		"/abc/",
84*57718be8SEnji Cooper 		"/abcd",
85*57718be8SEnji Cooper 		"/abcd/",
86*57718be8SEnji Cooper 		"/abcde",
87*57718be8SEnji Cooper 		"/abcde/",
88*57718be8SEnji Cooper 		"/abcdef",
89*57718be8SEnji Cooper 		"/abcdef/",
90*57718be8SEnji Cooper 		"/abcdefg",
91*57718be8SEnji Cooper 		"/abcdefg/",
92*57718be8SEnji Cooper 		"/abcdefgh",
93*57718be8SEnji Cooper 		"/abcdefgh/",
94*57718be8SEnji Cooper 
95*57718be8SEnji Cooper 		"a/",
96*57718be8SEnji Cooper 		"a//",
97*57718be8SEnji Cooper 		"a/a",
98*57718be8SEnji Cooper 		"a/a/",
99*57718be8SEnji Cooper 		"a/ab",
100*57718be8SEnji Cooper 		"a/ab/",
101*57718be8SEnji Cooper 		"a/abc",
102*57718be8SEnji Cooper 		"a/abc/",
103*57718be8SEnji Cooper 		"a/abcd",
104*57718be8SEnji Cooper 		"a/abcd/",
105*57718be8SEnji Cooper 		"a/abcde",
106*57718be8SEnji Cooper 		"a/abcde/",
107*57718be8SEnji Cooper 		"a/abcdef",
108*57718be8SEnji Cooper 		"a/abcdef/",
109*57718be8SEnji Cooper 		"a/abcdefg",
110*57718be8SEnji Cooper 		"a/abcdefg/",
111*57718be8SEnji Cooper 		"a/abcdefgh",
112*57718be8SEnji Cooper 		"a/abcdefgh/",
113*57718be8SEnji Cooper 
114*57718be8SEnji Cooper 		"ab/",
115*57718be8SEnji Cooper 		"ab//",
116*57718be8SEnji Cooper 		"ab/a",
117*57718be8SEnji Cooper 		"ab/a/",
118*57718be8SEnji Cooper 		"ab/ab",
119*57718be8SEnji Cooper 		"ab/ab/",
120*57718be8SEnji Cooper 		"ab/abc",
121*57718be8SEnji Cooper 		"ab/abc/",
122*57718be8SEnji Cooper 		"ab/abcd",
123*57718be8SEnji Cooper 		"ab/abcd/",
124*57718be8SEnji Cooper 		"ab/abcde",
125*57718be8SEnji Cooper 		"ab/abcde/",
126*57718be8SEnji Cooper 		"ab/abcdef",
127*57718be8SEnji Cooper 		"ab/abcdef/",
128*57718be8SEnji Cooper 		"ab/abcdefg",
129*57718be8SEnji Cooper 		"ab/abcdefg/",
130*57718be8SEnji Cooper 		"ab/abcdefgh",
131*57718be8SEnji Cooper 		"ab/abcdefgh/",
132*57718be8SEnji Cooper 
133*57718be8SEnji Cooper 		"abc/",
134*57718be8SEnji Cooper 		"abc//",
135*57718be8SEnji Cooper 		"abc/a",
136*57718be8SEnji Cooper 		"abc/a/",
137*57718be8SEnji Cooper 		"abc/ab",
138*57718be8SEnji Cooper 		"abc/ab/",
139*57718be8SEnji Cooper 		"abc/abc",
140*57718be8SEnji Cooper 		"abc/abc/",
141*57718be8SEnji Cooper 		"abc/abcd",
142*57718be8SEnji Cooper 		"abc/abcd/",
143*57718be8SEnji Cooper 		"abc/abcde",
144*57718be8SEnji Cooper 		"abc/abcde/",
145*57718be8SEnji Cooper 		"abc/abcdef",
146*57718be8SEnji Cooper 		"abc/abcdef/",
147*57718be8SEnji Cooper 		"abc/abcdefg",
148*57718be8SEnji Cooper 		"abc/abcdefg/",
149*57718be8SEnji Cooper 		"abc/abcdefgh",
150*57718be8SEnji Cooper 		"abc/abcdefgh/",
151*57718be8SEnji Cooper 
152*57718be8SEnji Cooper 		"abcd/",
153*57718be8SEnji Cooper 		"abcd//",
154*57718be8SEnji Cooper 		"abcd/a",
155*57718be8SEnji Cooper 		"abcd/a/",
156*57718be8SEnji Cooper 		"abcd/ab",
157*57718be8SEnji Cooper 		"abcd/ab/",
158*57718be8SEnji Cooper 		"abcd/abc",
159*57718be8SEnji Cooper 		"abcd/abc/",
160*57718be8SEnji Cooper 		"abcd/abcd",
161*57718be8SEnji Cooper 		"abcd/abcd/",
162*57718be8SEnji Cooper 		"abcd/abcde",
163*57718be8SEnji Cooper 		"abcd/abcde/",
164*57718be8SEnji Cooper 		"abcd/abcdef",
165*57718be8SEnji Cooper 		"abcd/abcdef/",
166*57718be8SEnji Cooper 		"abcd/abcdefg",
167*57718be8SEnji Cooper 		"abcd/abcdefg/",
168*57718be8SEnji Cooper 		"abcd/abcdefgh",
169*57718be8SEnji Cooper 		"abcd/abcdefgh/",
170*57718be8SEnji Cooper 
171*57718be8SEnji Cooper 		"abcde/",
172*57718be8SEnji Cooper 		"abcde//",
173*57718be8SEnji Cooper 		"abcde/a",
174*57718be8SEnji Cooper 		"abcde/a/",
175*57718be8SEnji Cooper 		"abcde/ab",
176*57718be8SEnji Cooper 		"abcde/ab/",
177*57718be8SEnji Cooper 		"abcde/abc",
178*57718be8SEnji Cooper 		"abcde/abc/",
179*57718be8SEnji Cooper 		"abcde/abcd",
180*57718be8SEnji Cooper 		"abcde/abcd/",
181*57718be8SEnji Cooper 		"abcde/abcde",
182*57718be8SEnji Cooper 		"abcde/abcde/",
183*57718be8SEnji Cooper 		"abcde/abcdef",
184*57718be8SEnji Cooper 		"abcde/abcdef/",
185*57718be8SEnji Cooper 		"abcde/abcdefg",
186*57718be8SEnji Cooper 		"abcde/abcdefg/",
187*57718be8SEnji Cooper 		"abcde/abcdefgh",
188*57718be8SEnji Cooper 		"abcde/abcdefgh/",
189*57718be8SEnji Cooper 
190*57718be8SEnji Cooper 		"abcdef/",
191*57718be8SEnji Cooper 		"abcdef//",
192*57718be8SEnji Cooper 		"abcdef/a",
193*57718be8SEnji Cooper 		"abcdef/a/",
194*57718be8SEnji Cooper 		"abcdef/ab",
195*57718be8SEnji Cooper 		"abcdef/ab/",
196*57718be8SEnji Cooper 		"abcdef/abc",
197*57718be8SEnji Cooper 		"abcdef/abc/",
198*57718be8SEnji Cooper 		"abcdef/abcd",
199*57718be8SEnji Cooper 		"abcdef/abcd/",
200*57718be8SEnji Cooper 		"abcdef/abcde",
201*57718be8SEnji Cooper 		"abcdef/abcde/",
202*57718be8SEnji Cooper 		"abcdef/abcdef",
203*57718be8SEnji Cooper 		"abcdef/abcdef/",
204*57718be8SEnji Cooper 		"abcdef/abcdefg",
205*57718be8SEnji Cooper 		"abcdef/abcdefg/",
206*57718be8SEnji Cooper 		"abcdef/abcdefgh",
207*57718be8SEnji Cooper 		"abcdef/abcdefgh/",
208*57718be8SEnji Cooper 
209*57718be8SEnji Cooper 		"abcdefg/",
210*57718be8SEnji Cooper 		"abcdefg//",
211*57718be8SEnji Cooper 		"abcdefg/a",
212*57718be8SEnji Cooper 		"abcdefg/a/",
213*57718be8SEnji Cooper 		"abcdefg/ab",
214*57718be8SEnji Cooper 		"abcdefg/ab/",
215*57718be8SEnji Cooper 		"abcdefg/abc",
216*57718be8SEnji Cooper 		"abcdefg/abc/",
217*57718be8SEnji Cooper 		"abcdefg/abcd",
218*57718be8SEnji Cooper 		"abcdefg/abcd/",
219*57718be8SEnji Cooper 		"abcdefg/abcde",
220*57718be8SEnji Cooper 		"abcdefg/abcde/",
221*57718be8SEnji Cooper 		"abcdefg/abcdef",
222*57718be8SEnji Cooper 		"abcdefg/abcdef/",
223*57718be8SEnji Cooper 		"abcdefg/abcdefg",
224*57718be8SEnji Cooper 		"abcdefg/abcdefg/",
225*57718be8SEnji Cooper 		"abcdefg/abcdefgh",
226*57718be8SEnji Cooper 		"abcdefg/abcdefgh/",
227*57718be8SEnji Cooper 
228*57718be8SEnji Cooper 		"abcdefgh/",
229*57718be8SEnji Cooper 		"abcdefgh//",
230*57718be8SEnji Cooper 		"abcdefgh/a",
231*57718be8SEnji Cooper 		"abcdefgh/a/",
232*57718be8SEnji Cooper 		"abcdefgh/ab",
233*57718be8SEnji Cooper 		"abcdefgh/ab/",
234*57718be8SEnji Cooper 		"abcdefgh/abc",
235*57718be8SEnji Cooper 		"abcdefgh/abc/",
236*57718be8SEnji Cooper 		"abcdefgh/abcd",
237*57718be8SEnji Cooper 		"abcdefgh/abcd/",
238*57718be8SEnji Cooper 		"abcdefgh/abcde",
239*57718be8SEnji Cooper 		"abcdefgh/abcde/",
240*57718be8SEnji Cooper 		"abcdefgh/abcdef",
241*57718be8SEnji Cooper 		"abcdefgh/abcdef/",
242*57718be8SEnji Cooper 		"abcdefgh/abcdefg",
243*57718be8SEnji Cooper 		"abcdefgh/abcdefg/",
244*57718be8SEnji Cooper 		"abcdefgh/abcdefgh",
245*57718be8SEnji Cooper 		"abcdefgh/abcdefgh/",
246*57718be8SEnji Cooper 	};
247*57718be8SEnji Cooper 
248*57718be8SEnji Cooper 
249*57718be8SEnji Cooper 	strchr_fn = dlsym(dlopen(0, RTLD_LAZY), "test_strchr");
250*57718be8SEnji Cooper 	if (!strchr_fn)
251*57718be8SEnji Cooper 		strchr_fn = strchr;
252*57718be8SEnji Cooper 
253*57718be8SEnji Cooper 	for (a = 3; a < 3 + sizeof(long); ++a) {
254*57718be8SEnji Cooper 		/* Put char and a \0 before the buffer */
255*57718be8SEnji Cooper 		buf[a-1] = '/';
256*57718be8SEnji Cooper 		buf[a-2] = '0';
257*57718be8SEnji Cooper 		buf[a-3] = 0xff;
258*57718be8SEnji Cooper 		for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
259*57718be8SEnji Cooper 			int len = strlen(tab[t]) + 1;
260*57718be8SEnji Cooper 			memcpy(&buf[a], tab[t], len);
261*57718be8SEnji Cooper 
262*57718be8SEnji Cooper 			/* Put the char we are looking for after the \0 */
263*57718be8SEnji Cooper 			buf[a + len] = '/';
264*57718be8SEnji Cooper 
265*57718be8SEnji Cooper 			/* Check search for NUL at end of string */
266*57718be8SEnji Cooper 			verify_strchr(buf + a, 0, t, a);
267*57718be8SEnji Cooper 
268*57718be8SEnji Cooper 			/* Then for the '/' in the strings */
269*57718be8SEnji Cooper 			verify_strchr(buf + a, '/', t, a);
270*57718be8SEnji Cooper 
271*57718be8SEnji Cooper 		   	/* check zero extension of char arg */
272*57718be8SEnji Cooper 		   	verify_strchr(buf + a, 0xffffff00 | '/', t, a);
273*57718be8SEnji Cooper 
274*57718be8SEnji Cooper 		   	/* Replace all the '/' with 0xff */
275*57718be8SEnji Cooper 		   	while ((off = slow_strchr(buf + a, '/')) != NULL)
276*57718be8SEnji Cooper 				*off = 0xff;
277*57718be8SEnji Cooper 
278*57718be8SEnji Cooper 			buf[a + len] = 0xff;
279*57718be8SEnji Cooper 
280*57718be8SEnji Cooper 			/* Check we can search for 0xff as well as '/' */
281*57718be8SEnji Cooper 			verify_strchr(buf + a, 0xff, t, a);
282*57718be8SEnji Cooper 		}
283*57718be8SEnji Cooper 	}
284*57718be8SEnji Cooper }
285*57718be8SEnji Cooper 
286*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
287*57718be8SEnji Cooper {
288*57718be8SEnji Cooper 
289*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, strchr_basic);
290*57718be8SEnji Cooper 
291*57718be8SEnji Cooper 	return atf_no_error();
292*57718be8SEnji Cooper }
293