1*11be35a1SLionel Sambuc /* $NetBSD: t_bitstring.c,v 1.4 2012/03/25 06:54:04 joerg Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 1993, 2008, 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <assert.h>
30*11be35a1SLionel Sambuc #include <bitstring.h>
31*11be35a1SLionel Sambuc #include <stdio.h>
32*11be35a1SLionel Sambuc #include <stdlib.h>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <atf-c.h>
35*11be35a1SLionel Sambuc
36*11be35a1SLionel Sambuc static void
clearbits(bitstr_t * b,int n)37*11be35a1SLionel Sambuc clearbits(bitstr_t *b, int n)
38*11be35a1SLionel Sambuc {
39*11be35a1SLionel Sambuc int i = bitstr_size(n);
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc while(i--)
42*11be35a1SLionel Sambuc *(b + i) = 0;
43*11be35a1SLionel Sambuc }
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc static void
printbits(FILE * file,bitstr_t * b,int n)46*11be35a1SLionel Sambuc printbits(FILE *file, bitstr_t *b, int n)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc int i;
49*11be35a1SLionel Sambuc int jc, js;
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc bit_ffc(b, n, &jc);
52*11be35a1SLionel Sambuc bit_ffs(b, n, &js);
53*11be35a1SLionel Sambuc
54*11be35a1SLionel Sambuc (void) fprintf(file, "%3d %3d ", jc, js);
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc for (i=0; i < n; i++) {
57*11be35a1SLionel Sambuc (void) fprintf(file, "%c", (bit_test(b, i) ? '1' : '0'));
58*11be35a1SLionel Sambuc }
59*11be35a1SLionel Sambuc
60*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '\n');
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc
63*11be35a1SLionel Sambuc static void
calculate_data(FILE * file,const int test_length)64*11be35a1SLionel Sambuc calculate_data(FILE *file, const int test_length)
65*11be35a1SLionel Sambuc {
66*11be35a1SLionel Sambuc int i;
67*11be35a1SLionel Sambuc bitstr_t *bs;
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc assert(test_length >= 4);
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc (void) fprintf(file, "Testing with TEST_LENGTH = %d\n\n", test_length);
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc (void) fprintf(file, "test _bit_byte, _bit_mask, and bitstr_size\n");
74*11be35a1SLionel Sambuc (void) fprintf(file, " i _bit_byte(i) _bit_mask(i) bitstr_size(i)\n");
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
77*11be35a1SLionel Sambuc (void) fprintf(file, "%3d%15u%15u%15zu\n",
78*11be35a1SLionel Sambuc i, _bit_byte(i), _bit_mask(i), bitstr_size(i));
79*11be35a1SLionel Sambuc }
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc bs = bit_alloc(test_length);
82*11be35a1SLionel Sambuc clearbits(bs, test_length);
83*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest bit_alloc, clearbits, bit_ffc, bit_ffs\n");
84*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 -1 ");
85*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
86*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '0');
87*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
88*11be35a1SLionel Sambuc printbits(file, bs, test_length);
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest bit_set\n");
91*11be35a1SLionel Sambuc for (i=0; i < test_length; i+=3)
92*11be35a1SLionel Sambuc bit_set(bs, i);
93*11be35a1SLionel Sambuc (void) fprintf(file, "be: 1 0 ");
94*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
95*11be35a1SLionel Sambuc (void) fprintf(file, "%c", "100"[i % 3]);
96*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
97*11be35a1SLionel Sambuc printbits(file, bs, test_length);
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest bit_clear\n");
100*11be35a1SLionel Sambuc for (i=0; i < test_length; i+=6)
101*11be35a1SLionel Sambuc bit_clear(bs, i);
102*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 3 ");
103*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
104*11be35a1SLionel Sambuc (void) fprintf(file, "%c", "000100"[i % 6]);
105*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
106*11be35a1SLionel Sambuc printbits(file, bs, test_length);
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest bit_test using previous bitstring\n");
109*11be35a1SLionel Sambuc (void) fprintf(file, " i bit_test(i)\n");
110*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
111*11be35a1SLionel Sambuc (void) fprintf(file, "%3d%15d\n", i, bit_test(bs, i));
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc clearbits(bs, test_length);
114*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest clearbits\n");
115*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 -1 ");
116*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
117*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '0');
118*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
119*11be35a1SLionel Sambuc printbits(file, bs, test_length);
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc (void) fprintf(file, "\ntest bit_nset and bit_nclear\n");
122*11be35a1SLionel Sambuc bit_nset(bs, 1, test_length - 2);
123*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 1 0");
124*11be35a1SLionel Sambuc for (i=0; i < test_length - 2; i++)
125*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '1');
126*11be35a1SLionel Sambuc (void) fprintf(file, "0\nis: ");
127*11be35a1SLionel Sambuc printbits(file, bs, test_length);
128*11be35a1SLionel Sambuc
129*11be35a1SLionel Sambuc bit_nclear(bs, 2, test_length - 3);
130*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 1 01");
131*11be35a1SLionel Sambuc for (i=0; i < test_length - 4; i++)
132*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '0');
133*11be35a1SLionel Sambuc (void) fprintf(file, "10\nis: ");
134*11be35a1SLionel Sambuc printbits(file, bs, test_length);
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1);
137*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 -1 ");
138*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
139*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '0');
140*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
141*11be35a1SLionel Sambuc printbits(file, bs, test_length);
142*11be35a1SLionel Sambuc bit_nset(bs, 0, test_length - 2);
143*11be35a1SLionel Sambuc (void) fprintf(file, "be: %3d 0 ",test_length - 1);
144*11be35a1SLionel Sambuc for (i=0; i < test_length - 1; i++)
145*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '1');
146*11be35a1SLionel Sambuc fprintf(file, "%c", '0');
147*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
148*11be35a1SLionel Sambuc printbits(file, bs, test_length);
149*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1);
150*11be35a1SLionel Sambuc (void) fprintf(file, "be: 0 -1 ");
151*11be35a1SLionel Sambuc for (i=0; i < test_length; i++)
152*11be35a1SLionel Sambuc (void) fprintf(file, "%c", '0');
153*11be35a1SLionel Sambuc (void) fprintf(file, "\nis: ");
154*11be35a1SLionel Sambuc printbits(file, bs, test_length);
155*11be35a1SLionel Sambuc
156*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
157*11be35a1SLionel Sambuc (void) fprintf(file, "first 1 bit should move right 1 position each line\n");
158*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
159*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1);
160*11be35a1SLionel Sambuc bit_nset(bs, i, test_length - 1);
161*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc
164*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
165*11be35a1SLionel Sambuc (void) fprintf(file, "first 0 bit should move right 1 position each line\n");
166*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
167*11be35a1SLionel Sambuc bit_nset(bs, 0, test_length - 1);
168*11be35a1SLionel Sambuc bit_nclear(bs, i, test_length - 1);
169*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
170*11be35a1SLionel Sambuc }
171*11be35a1SLionel Sambuc
172*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
173*11be35a1SLionel Sambuc (void) fprintf(file, "first 0 bit should move left 1 position each line\n");
174*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
175*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1);
176*11be35a1SLionel Sambuc bit_nset(bs, 0, test_length - 1 - i);
177*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
178*11be35a1SLionel Sambuc }
179*11be35a1SLionel Sambuc
180*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
181*11be35a1SLionel Sambuc (void) fprintf(file, "first 1 bit should move left 1 position each line\n");
182*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
183*11be35a1SLionel Sambuc bit_nset(bs, 0, test_length - 1);
184*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1 - i);
185*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
186*11be35a1SLionel Sambuc }
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
189*11be35a1SLionel Sambuc (void) fprintf(file, "0 bit should move right 1 position each line\n");
190*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
191*11be35a1SLionel Sambuc bit_nset(bs, 0, test_length - 1);
192*11be35a1SLionel Sambuc bit_nclear(bs, i, i);
193*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
194*11be35a1SLionel Sambuc }
195*11be35a1SLionel Sambuc
196*11be35a1SLionel Sambuc (void) fprintf(file, "\n");
197*11be35a1SLionel Sambuc (void) fprintf(file, "1 bit should move right 1 position each line\n");
198*11be35a1SLionel Sambuc for (i=0; i < test_length; i++) {
199*11be35a1SLionel Sambuc bit_nclear(bs, 0, test_length - 1);
200*11be35a1SLionel Sambuc bit_nset(bs, i, i);
201*11be35a1SLionel Sambuc (void) fprintf(file, "%3d ", i); printbits(file, bs, test_length);
202*11be35a1SLionel Sambuc }
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc (void) free(bs);
205*11be35a1SLionel Sambuc }
206*11be35a1SLionel Sambuc
207*11be35a1SLionel Sambuc static void
one_check(const atf_tc_t * tc,const int test_length)208*11be35a1SLionel Sambuc one_check(const atf_tc_t *tc, const int test_length)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc FILE *out;
211*11be35a1SLionel Sambuc char command[1024];
212*11be35a1SLionel Sambuc
213*11be35a1SLionel Sambuc ATF_REQUIRE((out = fopen("out", "w")) != NULL);
214*11be35a1SLionel Sambuc calculate_data(out, test_length);
215*11be35a1SLionel Sambuc fclose(out);
216*11be35a1SLionel Sambuc
217*11be35a1SLionel Sambuc /* XXX The following is a huge hack that was added to simplify the
218*11be35a1SLionel Sambuc * conversion of these tests from src/regress/ to src/tests/. The
219*11be35a1SLionel Sambuc * tests in this file should be checking their own results, without
220*11be35a1SLionel Sambuc * having to resort to external data files. */
221*11be35a1SLionel Sambuc snprintf(command, sizeof(command), "diff -u %s/d_bitstring_%d.out out",
222*11be35a1SLionel Sambuc atf_tc_get_config_var(tc, "srcdir"), test_length);
223*11be35a1SLionel Sambuc if (system(command) != EXIT_SUCCESS)
224*11be35a1SLionel Sambuc atf_tc_fail("Test failed; see output for details");
225*11be35a1SLionel Sambuc }
226*11be35a1SLionel Sambuc
227*11be35a1SLionel Sambuc ATF_TC(bits_8);
ATF_TC_HEAD(bits_8,tc)228*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_8, tc)
229*11be35a1SLionel Sambuc {
230*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 8-bit long bitstrings");
231*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_8,tc)232*11be35a1SLionel Sambuc ATF_TC_BODY(bits_8, tc)
233*11be35a1SLionel Sambuc {
234*11be35a1SLionel Sambuc one_check(tc, 8);
235*11be35a1SLionel Sambuc }
236*11be35a1SLionel Sambuc
237*11be35a1SLionel Sambuc ATF_TC(bits_27);
ATF_TC_HEAD(bits_27,tc)238*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_27, tc)
239*11be35a1SLionel Sambuc {
240*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 27-bit long bitstrings");
241*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_27,tc)242*11be35a1SLionel Sambuc ATF_TC_BODY(bits_27, tc)
243*11be35a1SLionel Sambuc {
244*11be35a1SLionel Sambuc one_check(tc, 27);
245*11be35a1SLionel Sambuc }
246*11be35a1SLionel Sambuc
247*11be35a1SLionel Sambuc ATF_TC(bits_32);
ATF_TC_HEAD(bits_32,tc)248*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_32, tc)
249*11be35a1SLionel Sambuc {
250*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 32-bit long bitstrings");
251*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_32,tc)252*11be35a1SLionel Sambuc ATF_TC_BODY(bits_32, tc)
253*11be35a1SLionel Sambuc {
254*11be35a1SLionel Sambuc one_check(tc, 32);
255*11be35a1SLionel Sambuc }
256*11be35a1SLionel Sambuc
257*11be35a1SLionel Sambuc ATF_TC(bits_49);
ATF_TC_HEAD(bits_49,tc)258*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_49, tc)
259*11be35a1SLionel Sambuc {
260*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 49-bit long bitstrings");
261*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_49,tc)262*11be35a1SLionel Sambuc ATF_TC_BODY(bits_49, tc)
263*11be35a1SLionel Sambuc {
264*11be35a1SLionel Sambuc one_check(tc, 49);
265*11be35a1SLionel Sambuc }
266*11be35a1SLionel Sambuc
267*11be35a1SLionel Sambuc ATF_TC(bits_64);
ATF_TC_HEAD(bits_64,tc)268*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_64, tc)
269*11be35a1SLionel Sambuc {
270*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 64-bit long bitstrings");
271*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_64,tc)272*11be35a1SLionel Sambuc ATF_TC_BODY(bits_64, tc)
273*11be35a1SLionel Sambuc {
274*11be35a1SLionel Sambuc one_check(tc, 64);
275*11be35a1SLionel Sambuc }
276*11be35a1SLionel Sambuc
277*11be35a1SLionel Sambuc ATF_TC(bits_67);
ATF_TC_HEAD(bits_67,tc)278*11be35a1SLionel Sambuc ATF_TC_HEAD(bits_67, tc)
279*11be35a1SLionel Sambuc {
280*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks 67-bit long bitstrings");
281*11be35a1SLionel Sambuc }
ATF_TC_BODY(bits_67,tc)282*11be35a1SLionel Sambuc ATF_TC_BODY(bits_67, tc)
283*11be35a1SLionel Sambuc {
284*11be35a1SLionel Sambuc one_check(tc, 67);
285*11be35a1SLionel Sambuc }
286*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)287*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
288*11be35a1SLionel Sambuc {
289*11be35a1SLionel Sambuc
290*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_8);
291*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_27);
292*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_32);
293*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_49);
294*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_64);
295*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, bits_67);
296*11be35a1SLionel Sambuc
297*11be35a1SLionel Sambuc return atf_no_error();
298*11be35a1SLionel Sambuc }
299