1*54bb1074Sjruoho /* $NetBSD: t_strrchr.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
2*54bb1074Sjruoho
3*54bb1074Sjruoho /*
4*54bb1074Sjruoho * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*54bb1074Sjruoho * Public domain.
6*54bb1074Sjruoho */
7*54bb1074Sjruoho
8*54bb1074Sjruoho #include <atf-c.h>
9*54bb1074Sjruoho #include <string.h>
10*54bb1074Sjruoho #include <unistd.h>
11*54bb1074Sjruoho #include <stdio.h>
12*54bb1074Sjruoho #include <stdlib.h>
13*54bb1074Sjruoho
14*54bb1074Sjruoho ATF_TC(strrchr_basic);
ATF_TC_HEAD(strrchr_basic,tc)15*54bb1074Sjruoho ATF_TC_HEAD(strrchr_basic, tc)
16*54bb1074Sjruoho {
17*54bb1074Sjruoho atf_tc_set_md_var(tc, "descr", "Test strrchr(3) results");
18*54bb1074Sjruoho }
19*54bb1074Sjruoho
ATF_TC_BODY(strrchr_basic,tc)20*54bb1074Sjruoho ATF_TC_BODY(strrchr_basic, tc)
21*54bb1074Sjruoho {
22*54bb1074Sjruoho /* try to trick the compiler */
23*54bb1074Sjruoho char * (*f)(const char *, int) = strrchr;
24*54bb1074Sjruoho
25*54bb1074Sjruoho unsigned int a, t;
26*54bb1074Sjruoho char *off, *off2;
27*54bb1074Sjruoho char buf[32];
28*54bb1074Sjruoho
29*54bb1074Sjruoho struct tab {
30*54bb1074Sjruoho const char* val;
31*54bb1074Sjruoho char match;
32*54bb1074Sjruoho ssize_t f_off; /* offset of first match */
33*54bb1074Sjruoho ssize_t l_off; /* offset of last match */
34*54bb1074Sjruoho };
35*54bb1074Sjruoho
36*54bb1074Sjruoho const struct tab tab[] = {
37*54bb1074Sjruoho { "", 0, 0, 0 },
38*54bb1074Sjruoho { "a", 0, 0, 0 },
39*54bb1074Sjruoho { "aa", 0, 0, 0 },
40*54bb1074Sjruoho { "abc", 0, 0, 0 },
41*54bb1074Sjruoho { "abcd", 0, 0, 0 },
42*54bb1074Sjruoho { "abcde", 0, 0, 0 },
43*54bb1074Sjruoho { "abcdef", 0, 0, 0 },
44*54bb1074Sjruoho { "abcdefg", 0, 0, 0 },
45*54bb1074Sjruoho { "abcdefgh", 0, 0, 0 },
46*54bb1074Sjruoho
47*54bb1074Sjruoho { "/", 1, 0, 0 },
48*54bb1074Sjruoho { "//", 1, 0, 1 },
49*54bb1074Sjruoho { "/a", 1, 0, 0 },
50*54bb1074Sjruoho { "/a/", 1, 0, 2 },
51*54bb1074Sjruoho { "/ab", 1, 0, 0 },
52*54bb1074Sjruoho { "/ab/", 1, 0, 3 },
53*54bb1074Sjruoho { "/abc", 1, 0, 0 },
54*54bb1074Sjruoho { "/abc/", 1, 0, 4 },
55*54bb1074Sjruoho { "/abcd", 1, 0, 0 },
56*54bb1074Sjruoho { "/abcd/", 1, 0, 5 },
57*54bb1074Sjruoho { "/abcde", 1, 0, 0 },
58*54bb1074Sjruoho { "/abcde/", 1, 0, 6 },
59*54bb1074Sjruoho { "/abcdef", 1, 0, 0 },
60*54bb1074Sjruoho { "/abcdef/", 1, 0, 7 },
61*54bb1074Sjruoho { "/abcdefg", 1, 0, 0 },
62*54bb1074Sjruoho { "/abcdefg/", 1, 0, 8 },
63*54bb1074Sjruoho { "/abcdefgh", 1, 0, 0 },
64*54bb1074Sjruoho { "/abcdefgh/", 1, 0, 9 },
65*54bb1074Sjruoho
66*54bb1074Sjruoho { "a/", 1, 1, 1 },
67*54bb1074Sjruoho { "a//", 1, 1, 2 },
68*54bb1074Sjruoho { "a/a", 1, 1, 1 },
69*54bb1074Sjruoho { "a/a/", 1, 1, 3 },
70*54bb1074Sjruoho { "a/ab", 1, 1, 1 },
71*54bb1074Sjruoho { "a/ab/", 1, 1, 4 },
72*54bb1074Sjruoho { "a/abc", 1, 1, 1 },
73*54bb1074Sjruoho { "a/abc/", 1, 1, 5 },
74*54bb1074Sjruoho { "a/abcd", 1, 1, 1 },
75*54bb1074Sjruoho { "a/abcd/", 1, 1, 6 },
76*54bb1074Sjruoho { "a/abcde", 1, 1, 1 },
77*54bb1074Sjruoho { "a/abcde/", 1, 1, 7 },
78*54bb1074Sjruoho { "a/abcdef", 1, 1, 1 },
79*54bb1074Sjruoho { "a/abcdef/", 1, 1, 8 },
80*54bb1074Sjruoho { "a/abcdefg", 1, 1, 1 },
81*54bb1074Sjruoho { "a/abcdefg/", 1, 1, 9 },
82*54bb1074Sjruoho { "a/abcdefgh", 1, 1, 1 },
83*54bb1074Sjruoho { "a/abcdefgh/", 1, 1, 10 },
84*54bb1074Sjruoho
85*54bb1074Sjruoho { "ab/", 1, 2, 2 },
86*54bb1074Sjruoho { "ab//", 1, 2, 3 },
87*54bb1074Sjruoho { "ab/a", 1, 2, 2 },
88*54bb1074Sjruoho { "ab/a/", 1, 2, 4 },
89*54bb1074Sjruoho { "ab/ab", 1, 2, 2 },
90*54bb1074Sjruoho { "ab/ab/", 1, 2, 5 },
91*54bb1074Sjruoho { "ab/abc", 1, 2, 2 },
92*54bb1074Sjruoho { "ab/abc/", 1, 2, 6 },
93*54bb1074Sjruoho { "ab/abcd", 1, 2, 2 },
94*54bb1074Sjruoho { "ab/abcd/", 1, 2, 7 },
95*54bb1074Sjruoho { "ab/abcde", 1, 2, 2 },
96*54bb1074Sjruoho { "ab/abcde/", 1, 2, 8 },
97*54bb1074Sjruoho { "ab/abcdef", 1, 2, 2 },
98*54bb1074Sjruoho { "ab/abcdef/", 1, 2, 9 },
99*54bb1074Sjruoho { "ab/abcdefg", 1, 2, 2 },
100*54bb1074Sjruoho { "ab/abcdefg/", 1, 2, 10 },
101*54bb1074Sjruoho { "ab/abcdefgh", 1, 2, 2 },
102*54bb1074Sjruoho { "ab/abcdefgh/", 1, 2, 11 },
103*54bb1074Sjruoho
104*54bb1074Sjruoho { "abc/", 1, 3, 3 },
105*54bb1074Sjruoho { "abc//", 1, 3, 4 },
106*54bb1074Sjruoho { "abc/a", 1, 3, 3 },
107*54bb1074Sjruoho { "abc/a/", 1, 3, 5 },
108*54bb1074Sjruoho { "abc/ab", 1, 3, 3 },
109*54bb1074Sjruoho { "abc/ab/", 1, 3, 6 },
110*54bb1074Sjruoho { "abc/abc", 1, 3, 3 },
111*54bb1074Sjruoho { "abc/abc/", 1, 3, 7 },
112*54bb1074Sjruoho { "abc/abcd", 1, 3, 3 },
113*54bb1074Sjruoho { "abc/abcd/", 1, 3, 8 },
114*54bb1074Sjruoho { "abc/abcde", 1, 3, 3 },
115*54bb1074Sjruoho { "abc/abcde/", 1, 3, 9 },
116*54bb1074Sjruoho { "abc/abcdef", 1, 3, 3 },
117*54bb1074Sjruoho { "abc/abcdef/", 1, 3, 10 },
118*54bb1074Sjruoho { "abc/abcdefg", 1, 3, 3 },
119*54bb1074Sjruoho { "abc/abcdefg/", 1, 3, 11 },
120*54bb1074Sjruoho { "abc/abcdefgh", 1, 3, 3 },
121*54bb1074Sjruoho { "abc/abcdefgh/", 1, 3, 12 },
122*54bb1074Sjruoho
123*54bb1074Sjruoho { "abcd/", 1, 4, 4 },
124*54bb1074Sjruoho { "abcd//", 1, 4, 5 },
125*54bb1074Sjruoho { "abcd/a", 1, 4, 4 },
126*54bb1074Sjruoho { "abcd/a/", 1, 4, 6 },
127*54bb1074Sjruoho { "abcd/ab", 1, 4, 4 },
128*54bb1074Sjruoho { "abcd/ab/", 1, 4, 7 },
129*54bb1074Sjruoho { "abcd/abc", 1, 4, 4 },
130*54bb1074Sjruoho { "abcd/abc/", 1, 4, 8 },
131*54bb1074Sjruoho { "abcd/abcd", 1, 4, 4 },
132*54bb1074Sjruoho { "abcd/abcd/", 1, 4, 9 },
133*54bb1074Sjruoho { "abcd/abcde", 1, 4, 4 },
134*54bb1074Sjruoho { "abcd/abcde/", 1, 4, 10 },
135*54bb1074Sjruoho { "abcd/abcdef", 1, 4, 4 },
136*54bb1074Sjruoho { "abcd/abcdef/", 1, 4, 11 },
137*54bb1074Sjruoho { "abcd/abcdefg", 1, 4, 4 },
138*54bb1074Sjruoho { "abcd/abcdefg/", 1, 4, 12 },
139*54bb1074Sjruoho { "abcd/abcdefgh", 1, 4, 4 },
140*54bb1074Sjruoho { "abcd/abcdefgh/", 1, 4, 13 },
141*54bb1074Sjruoho
142*54bb1074Sjruoho { "abcde/", 1, 5, 5 },
143*54bb1074Sjruoho { "abcde//", 1, 5, 6 },
144*54bb1074Sjruoho { "abcde/a", 1, 5, 5 },
145*54bb1074Sjruoho { "abcde/a/", 1, 5, 7 },
146*54bb1074Sjruoho { "abcde/ab", 1, 5, 5 },
147*54bb1074Sjruoho { "abcde/ab/", 1, 5, 8 },
148*54bb1074Sjruoho { "abcde/abc", 1, 5, 5 },
149*54bb1074Sjruoho { "abcde/abc/", 1, 5, 9 },
150*54bb1074Sjruoho { "abcde/abcd", 1, 5, 5 },
151*54bb1074Sjruoho { "abcde/abcd/", 1, 5, 10 },
152*54bb1074Sjruoho { "abcde/abcde", 1, 5, 5 },
153*54bb1074Sjruoho { "abcde/abcde/", 1, 5, 11 },
154*54bb1074Sjruoho { "abcde/abcdef", 1, 5, 5 },
155*54bb1074Sjruoho { "abcde/abcdef/", 1, 5, 12 },
156*54bb1074Sjruoho { "abcde/abcdefg", 1, 5, 5 },
157*54bb1074Sjruoho { "abcde/abcdefg/", 1, 5, 13 },
158*54bb1074Sjruoho { "abcde/abcdefgh", 1, 5, 5 },
159*54bb1074Sjruoho { "abcde/abcdefgh/", 1, 5, 14 },
160*54bb1074Sjruoho
161*54bb1074Sjruoho { "abcdef/", 1, 6, 6 },
162*54bb1074Sjruoho { "abcdef//", 1, 6, 7 },
163*54bb1074Sjruoho { "abcdef/a", 1, 6, 6 },
164*54bb1074Sjruoho { "abcdef/a/", 1, 6, 8 },
165*54bb1074Sjruoho { "abcdef/ab", 1, 6, 6 },
166*54bb1074Sjruoho { "abcdef/ab/", 1, 6, 9 },
167*54bb1074Sjruoho { "abcdef/abc", 1, 6, 6 },
168*54bb1074Sjruoho { "abcdef/abc/", 1, 6, 10 },
169*54bb1074Sjruoho { "abcdef/abcd", 1, 6, 6 },
170*54bb1074Sjruoho { "abcdef/abcd/", 1, 6, 11 },
171*54bb1074Sjruoho { "abcdef/abcde", 1, 6, 6 },
172*54bb1074Sjruoho { "abcdef/abcde/", 1, 6, 12 },
173*54bb1074Sjruoho { "abcdef/abcdef", 1, 6, 6 },
174*54bb1074Sjruoho { "abcdef/abcdef/", 1, 6, 13 },
175*54bb1074Sjruoho { "abcdef/abcdefg", 1, 6, 6 },
176*54bb1074Sjruoho { "abcdef/abcdefg/", 1, 6, 14 },
177*54bb1074Sjruoho { "abcdef/abcdefgh", 1, 6, 6 },
178*54bb1074Sjruoho { "abcdef/abcdefgh/", 1, 6, 15 },
179*54bb1074Sjruoho
180*54bb1074Sjruoho { "abcdefg/", 1, 7, 7 },
181*54bb1074Sjruoho { "abcdefg//", 1, 7, 8 },
182*54bb1074Sjruoho { "abcdefg/a", 1, 7, 7 },
183*54bb1074Sjruoho { "abcdefg/a/", 1, 7, 9 },
184*54bb1074Sjruoho { "abcdefg/ab", 1, 7, 7 },
185*54bb1074Sjruoho { "abcdefg/ab/", 1, 7, 10 },
186*54bb1074Sjruoho { "abcdefg/abc", 1, 7, 7 },
187*54bb1074Sjruoho { "abcdefg/abc/", 1, 7, 11 },
188*54bb1074Sjruoho { "abcdefg/abcd", 1, 7, 7 },
189*54bb1074Sjruoho { "abcdefg/abcd/", 1, 7, 12 },
190*54bb1074Sjruoho { "abcdefg/abcde", 1, 7, 7 },
191*54bb1074Sjruoho { "abcdefg/abcde/", 1, 7, 13 },
192*54bb1074Sjruoho { "abcdefg/abcdef", 1, 7, 7 },
193*54bb1074Sjruoho { "abcdefg/abcdef/", 1, 7, 14 },
194*54bb1074Sjruoho { "abcdefg/abcdefg", 1, 7, 7 },
195*54bb1074Sjruoho { "abcdefg/abcdefg/", 1, 7, 15 },
196*54bb1074Sjruoho { "abcdefg/abcdefgh", 1, 7, 7 },
197*54bb1074Sjruoho { "abcdefg/abcdefgh/", 1, 7, 16 },
198*54bb1074Sjruoho
199*54bb1074Sjruoho { "abcdefgh/", 1, 8, 8 },
200*54bb1074Sjruoho { "abcdefgh//", 1, 8, 9 },
201*54bb1074Sjruoho { "abcdefgh/a", 1, 8, 8 },
202*54bb1074Sjruoho { "abcdefgh/a/", 1, 8, 10 },
203*54bb1074Sjruoho { "abcdefgh/ab", 1, 8, 8 },
204*54bb1074Sjruoho { "abcdefgh/ab/", 1, 8, 11 },
205*54bb1074Sjruoho { "abcdefgh/abc", 1, 8, 8 },
206*54bb1074Sjruoho { "abcdefgh/abc/", 1, 8, 12 },
207*54bb1074Sjruoho { "abcdefgh/abcd", 1, 8, 8 },
208*54bb1074Sjruoho { "abcdefgh/abcd/", 1, 8, 13 },
209*54bb1074Sjruoho { "abcdefgh/abcde", 1, 8, 8 },
210*54bb1074Sjruoho { "abcdefgh/abcde/", 1, 8, 14 },
211*54bb1074Sjruoho { "abcdefgh/abcdef", 1, 8, 8 },
212*54bb1074Sjruoho { "abcdefgh/abcdef/", 1, 8, 15 },
213*54bb1074Sjruoho { "abcdefgh/abcdefg", 1, 8, 8 },
214*54bb1074Sjruoho { "abcdefgh/abcdefg/", 1, 8, 16 },
215*54bb1074Sjruoho { "abcdefgh/abcdefgh", 1, 8, 8 },
216*54bb1074Sjruoho { "abcdefgh/abcdefgh/", 1, 8, 17 },
217*54bb1074Sjruoho };
218*54bb1074Sjruoho
219*54bb1074Sjruoho for (a = 0; a < sizeof(long); ++a) {
220*54bb1074Sjruoho for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
221*54bb1074Sjruoho strcpy(&buf[a], tab[t].val);
222*54bb1074Sjruoho
223*54bb1074Sjruoho off = f(&buf[a], '/');
224*54bb1074Sjruoho if (tab[t].match == 0) {
225*54bb1074Sjruoho if (off != 0) {
226*54bb1074Sjruoho fprintf(stderr, "a %d, t %d\n", a, t);
227*54bb1074Sjruoho atf_tc_fail("strrchr should not have "
228*54bb1074Sjruoho "found the character");
229*54bb1074Sjruoho }
230*54bb1074Sjruoho } else if (tab[t].match == 1) {
231*54bb1074Sjruoho if (tab[t].l_off != (off - &buf[a])) {
232*54bb1074Sjruoho fprintf(stderr, "a %d, t %d\n", a, t);
233*54bb1074Sjruoho atf_tc_fail("strrchr returns wrong "
234*54bb1074Sjruoho "offset");
235*54bb1074Sjruoho }
236*54bb1074Sjruoho } else {
237*54bb1074Sjruoho fprintf(stderr, "a %d, t %d\n", a, t);
238*54bb1074Sjruoho atf_tc_fail("bad test case data");
239*54bb1074Sjruoho }
240*54bb1074Sjruoho
241*54bb1074Sjruoho /* check zero extension of char arg */
242*54bb1074Sjruoho off2 = f(&buf[a], 0xffffff00 | '/');
243*54bb1074Sjruoho if (off != off2) {
244*54bb1074Sjruoho fprintf(stderr, "a %d, t %d\n", a, t);
245*54bb1074Sjruoho atf_tc_fail("zero extension of char arg fails");
246*54bb1074Sjruoho }
247*54bb1074Sjruoho }
248*54bb1074Sjruoho }
249*54bb1074Sjruoho }
250*54bb1074Sjruoho
ATF_TP_ADD_TCS(tp)251*54bb1074Sjruoho ATF_TP_ADD_TCS(tp)
252*54bb1074Sjruoho {
253*54bb1074Sjruoho
254*54bb1074Sjruoho ATF_TP_ADD_TC(tp, strrchr_basic);
255*54bb1074Sjruoho
256*54bb1074Sjruoho return atf_no_error();
257*54bb1074Sjruoho }
258