1*11be35a1SLionel Sambuc /* $NetBSD: t_memchr.c,v 1.3 2012/04/06 07:53:10 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*11be35a1SLionel Sambuc * Public domain.
6*11be35a1SLionel Sambuc */
7*11be35a1SLionel Sambuc
8*11be35a1SLionel Sambuc #include <atf-c.h>
9*11be35a1SLionel Sambuc #include <string.h>
10*11be35a1SLionel Sambuc #include <unistd.h>
11*11be35a1SLionel Sambuc #include <stdio.h>
12*11be35a1SLionel Sambuc #include <stdlib.h>
13*11be35a1SLionel Sambuc
14*11be35a1SLionel Sambuc ATF_TC(memchr_basic);
ATF_TC_HEAD(memchr_basic,tc)15*11be35a1SLionel Sambuc ATF_TC_HEAD(memchr_basic, tc)
16*11be35a1SLionel Sambuc {
17*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test memchr(3) results, #1");
18*11be35a1SLionel Sambuc }
19*11be35a1SLionel Sambuc
ATF_TC_BODY(memchr_basic,tc)20*11be35a1SLionel Sambuc ATF_TC_BODY(memchr_basic, tc)
21*11be35a1SLionel Sambuc {
22*11be35a1SLionel Sambuc /* try to trick the compiler */
23*11be35a1SLionel Sambuc void * (*f)(const void *, int, size_t) = memchr;
24*11be35a1SLionel Sambuc
25*11be35a1SLionel Sambuc unsigned int a, t;
26*11be35a1SLionel Sambuc void *off, *off2;
27*11be35a1SLionel Sambuc char buf[32];
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc struct tab {
30*11be35a1SLionel Sambuc const char *val;
31*11be35a1SLionel Sambuc size_t len;
32*11be35a1SLionel Sambuc char match;
33*11be35a1SLionel Sambuc ssize_t off;
34*11be35a1SLionel Sambuc };
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc const struct tab tab[] = {
37*11be35a1SLionel Sambuc { "", 0, 0, 0 },
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambuc { "/", 0, 0, 0 },
40*11be35a1SLionel Sambuc { "/", 1, 1, 0 },
41*11be35a1SLionel Sambuc { "/a", 2, 1, 0 },
42*11be35a1SLionel Sambuc { "/ab", 3, 1, 0 },
43*11be35a1SLionel Sambuc { "/abc", 4, 1, 0 },
44*11be35a1SLionel Sambuc { "/abcd", 5, 1, 0 },
45*11be35a1SLionel Sambuc { "/abcde", 6, 1, 0 },
46*11be35a1SLionel Sambuc { "/abcdef", 7, 1, 0 },
47*11be35a1SLionel Sambuc { "/abcdefg", 8, 1, 0 },
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc { "a/", 1, 0, 0 },
50*11be35a1SLionel Sambuc { "a/", 2, 1, 1 },
51*11be35a1SLionel Sambuc { "a/b", 3, 1, 1 },
52*11be35a1SLionel Sambuc { "a/bc", 4, 1, 1 },
53*11be35a1SLionel Sambuc { "a/bcd", 5, 1, 1 },
54*11be35a1SLionel Sambuc { "a/bcde", 6, 1, 1 },
55*11be35a1SLionel Sambuc { "a/bcdef", 7, 1, 1 },
56*11be35a1SLionel Sambuc { "a/bcdefg", 8, 1, 1 },
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc { "ab/", 2, 0, 0 },
59*11be35a1SLionel Sambuc { "ab/", 3, 1, 2 },
60*11be35a1SLionel Sambuc { "ab/c", 4, 1, 2 },
61*11be35a1SLionel Sambuc { "ab/cd", 5, 1, 2 },
62*11be35a1SLionel Sambuc { "ab/cde", 6, 1, 2 },
63*11be35a1SLionel Sambuc { "ab/cdef", 7, 1, 2 },
64*11be35a1SLionel Sambuc { "ab/cdefg", 8, 1, 2 },
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc { "abc/", 3, 0, 0 },
67*11be35a1SLionel Sambuc { "abc/", 4, 1, 3 },
68*11be35a1SLionel Sambuc { "abc/d", 5, 1, 3 },
69*11be35a1SLionel Sambuc { "abc/de", 6, 1, 3 },
70*11be35a1SLionel Sambuc { "abc/def", 7, 1, 3 },
71*11be35a1SLionel Sambuc { "abc/defg", 8, 1, 3 },
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc { "abcd/", 4, 0, 0 },
74*11be35a1SLionel Sambuc { "abcd/", 5, 1, 4 },
75*11be35a1SLionel Sambuc { "abcd/e", 6, 1, 4 },
76*11be35a1SLionel Sambuc { "abcd/ef", 7, 1, 4 },
77*11be35a1SLionel Sambuc { "abcd/efg", 8, 1, 4 },
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambuc { "abcde/", 5, 0, 0 },
80*11be35a1SLionel Sambuc { "abcde/", 6, 1, 5 },
81*11be35a1SLionel Sambuc { "abcde/f", 7, 1, 5 },
82*11be35a1SLionel Sambuc { "abcde/fg", 8, 1, 5 },
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc { "abcdef/", 6, 0, 0 },
85*11be35a1SLionel Sambuc { "abcdef/", 7, 1, 6 },
86*11be35a1SLionel Sambuc { "abcdef/g", 8, 1, 6 },
87*11be35a1SLionel Sambuc
88*11be35a1SLionel Sambuc { "abcdefg/", 7, 0, 0 },
89*11be35a1SLionel Sambuc { "abcdefg/", 8, 1, 7 },
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc { "\xff\xff\xff\xff" "efg/", 8, 1, 7 },
92*11be35a1SLionel Sambuc { "a" "\xff\xff\xff\xff" "fg/", 8, 1, 7 },
93*11be35a1SLionel Sambuc { "ab" "\xff\xff\xff\xff" "g/", 8, 1, 7 },
94*11be35a1SLionel Sambuc { "abc" "\xff\xff\xff\xff" "/", 8, 1, 7 },
95*11be35a1SLionel Sambuc };
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc for (a = 1; a < 1 + sizeof(long); ++a) {
98*11be35a1SLionel Sambuc for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
99*11be35a1SLionel Sambuc buf[a-1] = '/';
100*11be35a1SLionel Sambuc strcpy(&buf[a], tab[t].val);
101*11be35a1SLionel Sambuc
102*11be35a1SLionel Sambuc off = f(&buf[a], '/', tab[t].len);
103*11be35a1SLionel Sambuc if (tab[t].match == 0) {
104*11be35a1SLionel Sambuc if (off != 0) {
105*11be35a1SLionel Sambuc fprintf(stderr, "a = %d, t = %d\n",
106*11be35a1SLionel Sambuc a, t);
107*11be35a1SLionel Sambuc atf_tc_fail("should not have found "
108*11be35a1SLionel Sambuc " char past len");
109*11be35a1SLionel Sambuc }
110*11be35a1SLionel Sambuc } else if (tab[t].match == 1) {
111*11be35a1SLionel Sambuc if (tab[t].off != ((char*)off - &buf[a])) {
112*11be35a1SLionel Sambuc fprintf(stderr, "a = %d, t = %d\n",
113*11be35a1SLionel Sambuc a, t);
114*11be35a1SLionel Sambuc atf_tc_fail("char not found at "
115*11be35a1SLionel Sambuc "correct offset");
116*11be35a1SLionel Sambuc }
117*11be35a1SLionel Sambuc } else {
118*11be35a1SLionel Sambuc fprintf(stderr, "a = %d, t = %d\n", a, t);
119*11be35a1SLionel Sambuc atf_tc_fail("Corrupt test case data");
120*11be35a1SLionel Sambuc }
121*11be35a1SLionel Sambuc
122*11be35a1SLionel Sambuc /* check zero extension of char arg */
123*11be35a1SLionel Sambuc off2 = f(&buf[a], 0xffffff00 | '/', tab[t].len);
124*11be35a1SLionel Sambuc if (off2 != off)
125*11be35a1SLionel Sambuc atf_tc_fail("zero extension of char arg "
126*11be35a1SLionel Sambuc "failed");
127*11be35a1SLionel Sambuc }
128*11be35a1SLionel Sambuc }
129*11be35a1SLionel Sambuc }
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc ATF_TC(memchr_simple);
ATF_TC_HEAD(memchr_simple,tc)132*11be35a1SLionel Sambuc ATF_TC_HEAD(memchr_simple, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test memchr(3) results, #2");
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc
ATF_TC_BODY(memchr_simple,tc)137*11be35a1SLionel Sambuc ATF_TC_BODY(memchr_simple, tc)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc char buf[] = "abcdefg";
140*11be35a1SLionel Sambuc short i = 7;
141*11be35a1SLionel Sambuc
142*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'a', 0) == NULL);
143*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'g', 0) == NULL);
144*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'x', 7) == NULL);
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc ATF_CHECK(memchr("\0", 'x', 0) == NULL);
147*11be35a1SLionel Sambuc ATF_CHECK(memchr("\0", 'x', 1) == NULL);
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc while (i <= 14) {
150*11be35a1SLionel Sambuc
151*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'a', i) == buf + 0);
152*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'b', i) == buf + 1);
153*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'c', i) == buf + 2);
154*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'd', i) == buf + 3);
155*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'e', i) == buf + 4);
156*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'f', i) == buf + 5);
157*11be35a1SLionel Sambuc ATF_CHECK(memchr(buf, 'g', i) == buf + 6);
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc i *= 2;
160*11be35a1SLionel Sambuc }
161*11be35a1SLionel Sambuc }
162*11be35a1SLionel Sambuc
163*11be35a1SLionel Sambuc ATF_TC(memrchr_simple);
ATF_TC_HEAD(memrchr_simple,tc)164*11be35a1SLionel Sambuc ATF_TC_HEAD(memrchr_simple, tc)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test memrchr(3) results");
167*11be35a1SLionel Sambuc }
168*11be35a1SLionel Sambuc
ATF_TC_BODY(memrchr_simple,tc)169*11be35a1SLionel Sambuc ATF_TC_BODY(memrchr_simple, tc)
170*11be35a1SLionel Sambuc {
171*11be35a1SLionel Sambuc char buf[] = "abcdabcd";
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'a', 0) == NULL);
174*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'g', 0) == NULL);
175*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'x', 8) == NULL);
176*11be35a1SLionel Sambuc
177*11be35a1SLionel Sambuc ATF_CHECK(memrchr("\0", 'x', 0) == NULL);
178*11be35a1SLionel Sambuc ATF_CHECK(memrchr("\0", 'x', 1) == NULL);
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'a', 8) == buf + 4);
181*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'b', 8) == buf + 5);
182*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'c', 8) == buf + 6);
183*11be35a1SLionel Sambuc ATF_CHECK(memrchr(buf, 'd', 8) == buf + 7);
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)186*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
187*11be35a1SLionel Sambuc {
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, memchr_basic);
190*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, memchr_simple);
191*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, memrchr_simple);
192*11be35a1SLionel Sambuc
193*11be35a1SLionel Sambuc return atf_no_error();
194*11be35a1SLionel Sambuc }
195