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