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