xref: /freebsd/tests/sys/sys/bitstring_test.c (revision 53b70c86)
1 /*-
2  * Copyright (c) 2014 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  *
17  * NO WARRANTY
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGES.
29  *
30  * $FreeBSD$
31  */
32 #include <sys/param.h>
33 
34 #include <bitstring.h>
35 #include <stdio.h>
36 
37 #include <atf-c.h>
38 
39 typedef void (testfunc_t)(bitstr_t *bstr, int nbits, const char *memloc);
40 
41 static void
42 bitstring_run_stack_test(testfunc_t *test, int nbits)
43 {
44 	bitstr_t bit_decl(bitstr, nbits);
45 
46 	test(bitstr, nbits, "stack");
47 }
48 
49 static void
50 bitstring_run_heap_test(testfunc_t *test, int nbits)
51 {
52 	bitstr_t *bitstr = bit_alloc(nbits);
53 
54 	test(bitstr, nbits, "heap");
55 }
56 
57 static void
58 bitstring_test_runner(testfunc_t *test)
59 {
60 	const int bitstr_sizes[] = {
61 		0,
62 		1,
63 		_BITSTR_BITS - 1,
64 		_BITSTR_BITS,
65 		_BITSTR_BITS + 1,
66 		2 * _BITSTR_BITS - 1,
67 		2 * _BITSTR_BITS,
68 		1023,
69 		1024
70 	};
71 
72 	for (unsigned long i = 0; i < nitems(bitstr_sizes); i++) {
73 		bitstring_run_stack_test(test, bitstr_sizes[i]);
74 		bitstring_run_heap_test(test, bitstr_sizes[i]);
75 	}
76 }
77 
78 #define	BITSTRING_TC_DEFINE(name)				\
79 ATF_TC_WITHOUT_HEAD(name);					\
80 static testfunc_t name ## _test;				\
81 								\
82 ATF_TC_BODY(name, tc)						\
83 {								\
84 	bitstring_test_runner(name ## _test);			\
85 }								\
86 								\
87 static void							\
88 name ## _test(bitstr_t *bitstr, int nbits, const char *memloc)
89 
90 #define	BITSTRING_TC_ADD(tp, name)				\
91 do {								\
92 	ATF_TP_ADD_TC(tp, name);				\
93 } while (0)
94 
95 ATF_TC_WITHOUT_HEAD(bitstr_in_struct);
96 ATF_TC_BODY(bitstr_in_struct, tc)
97 {
98 	struct bitstr_containing_struct {
99 		bitstr_t bit_decl(bitstr, 8);
100 	} test_struct;
101 
102 	bit_nclear(test_struct.bitstr, 0, 8);
103 }
104 
105 ATF_TC_WITHOUT_HEAD(bitstr_size);
106 ATF_TC_BODY(bitstr_size, tc)
107 {
108 	size_t sob = sizeof(bitstr_t);
109 
110 	ATF_CHECK_EQ(0, bitstr_size(0));
111 	ATF_CHECK_EQ(sob, bitstr_size(1));
112 	ATF_CHECK_EQ(sob, bitstr_size(sob * 8));
113 	ATF_CHECK_EQ(2 * sob, bitstr_size(sob * 8 + 1));
114 }
115 
116 BITSTRING_TC_DEFINE(bit_set)
117 /* bitstr_t *bitstr, int nbits, const char *memloc */
118 {
119 	memset(bitstr, 0, bitstr_size(nbits));
120 
121 	for (int i = 0; i < nbits; i++) {
122 		bit_set(bitstr, i);
123 
124 		for (int j = 0; j < nbits; j++) {
125 			ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 1 : 0,
126 			    "bit_set_%d_%s: Failed on bit %d",
127 			    nbits, memloc, i);
128 		}
129 
130 		bit_clear(bitstr, i);
131 	}
132 }
133 
134 BITSTRING_TC_DEFINE(bit_clear)
135 /* bitstr_t *bitstr, int nbits, const char *memloc */
136 {
137 	int i, j;
138 
139 	memset(bitstr, 0xFF, bitstr_size(nbits));
140 	for (i = 0; i < nbits; i++) {
141 		bit_clear(bitstr, i);
142 
143 		for (j = 0; j < nbits; j++) {
144 			ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 0 : 1,
145 			    "bit_clear_%d_%s: Failed on bit %d",
146 			    nbits, memloc, i);
147 		}
148 
149 		bit_set(bitstr, i);
150 	}
151 }
152 
153 BITSTRING_TC_DEFINE(bit_ffs)
154 /* bitstr_t *bitstr, int nbits, const char *memloc */
155 {
156 	int i;
157 	int found_set_bit;
158 
159 	memset(bitstr, 0, bitstr_size(nbits));
160 	bit_ffs(bitstr, nbits, &found_set_bit);
161 	ATF_REQUIRE_MSG(found_set_bit == -1,
162 	    "bit_ffs_%d_%s: Failed all clear bits.", nbits, memloc);
163 
164 	for (i = 0; i < nbits; i++) {
165 		memset(bitstr, 0xFF, bitstr_size(nbits));
166 		if (i > 0)
167 			bit_nclear(bitstr, 0, i - 1);
168 
169 		bit_ffs(bitstr, nbits, &found_set_bit);
170 		ATF_REQUIRE_MSG(found_set_bit == i,
171 		    "bit_ffs_%d_%s: Failed on bit %d, Result %d",
172 		    nbits, memloc, i, found_set_bit);
173 	}
174 }
175 
176 BITSTRING_TC_DEFINE(bit_ffc)
177 /* bitstr_t *bitstr, int nbits, const char *memloc */
178 {
179 	int i;
180 	int found_clear_bit;
181 
182 	memset(bitstr, 0xFF, bitstr_size(nbits));
183 	bit_ffc(bitstr, nbits, &found_clear_bit);
184 	ATF_REQUIRE_MSG(found_clear_bit == -1,
185 	    "bit_ffc_%d_%s: Failed all set bits.", nbits, memloc);
186 
187 	for (i = 0; i < nbits; i++) {
188 		memset(bitstr, 0, bitstr_size(nbits));
189 		if (i > 0)
190 			bit_nset(bitstr, 0, i - 1);
191 
192 		bit_ffc(bitstr, nbits, &found_clear_bit);
193 		ATF_REQUIRE_MSG(found_clear_bit == i,
194 		    "bit_ffc_%d_%s: Failed on bit %d, Result %d",
195 		    nbits, memloc, i, found_clear_bit);
196 	}
197 }
198 
199 BITSTRING_TC_DEFINE(bit_ffs_at)
200 /* bitstr_t *bitstr, int nbits, const char *memloc */
201 {
202 	int i;
203 	int found_set_bit;
204 
205 	memset(bitstr, 0xFF, bitstr_size(nbits));
206 	for (i = 0; i < nbits; i++) {
207 		bit_ffs_at(bitstr, i, nbits, &found_set_bit);
208 		ATF_REQUIRE_MSG(found_set_bit == i,
209 		    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
210 		    nbits, memloc, i, found_set_bit);
211 	}
212 
213 	memset(bitstr, 0, bitstr_size(nbits));
214 	for (i = 0; i < nbits; i++) {
215 		bit_ffs_at(bitstr, i, nbits, &found_set_bit);
216 		ATF_REQUIRE_MSG(found_set_bit == -1,
217 		    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
218 		    nbits, memloc, i, found_set_bit);
219 	}
220 
221 	memset(bitstr, 0x55, bitstr_size(nbits));
222 	for (i = 0; i < nbits; i++) {
223 		bit_ffs_at(bitstr, i, nbits, &found_set_bit);
224 		if (i == nbits - 1 && (nbits & 1) == 0) {
225 			ATF_REQUIRE_MSG(found_set_bit == -1,
226 			    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
227 			    nbits, memloc, i, found_set_bit);
228 		} else {
229 			ATF_REQUIRE_MSG(found_set_bit == i + (i & 1),
230 			    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
231 			    nbits, memloc, i, found_set_bit);
232 		}
233 	}
234 
235 	memset(bitstr, 0xAA, bitstr_size(nbits));
236 	for (i = 0; i < nbits; i++) {
237 		bit_ffs_at(bitstr, i, nbits, &found_set_bit);
238 		if (i == nbits - 1 && (nbits & 1) != 0) {
239 			ATF_REQUIRE_MSG(found_set_bit == -1,
240 			    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
241 			    nbits, memloc, i, found_set_bit);
242 		} else {
243 			ATF_REQUIRE_MSG(
244 			    found_set_bit == i + ((i & 1) ? 0 : 1),
245 			    "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
246 			    nbits, memloc, i, found_set_bit);
247 		}
248 	}
249 
250 	/* Pass a start value beyond the size of the bit string */
251 	bit_ffs_at(bitstr, nbits, nbits, &found_set_bit);
252 	ATF_REQUIRE_MSG(found_set_bit == -1,
253 			"bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
254 			nbits, memloc, nbits, found_set_bit);
255 
256 	bit_ffs_at(bitstr, nbits + 3, nbits, &found_set_bit);
257 	ATF_REQUIRE_MSG(found_set_bit == -1,
258 			"bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
259 			nbits, memloc, nbits + 3, found_set_bit);
260 }
261 
262 BITSTRING_TC_DEFINE(bit_ffc_at)
263 /* bitstr_t *bitstr, int nbits, const char *memloc */
264 {
265 	int i, found_clear_bit;
266 
267 	memset(bitstr, 0, bitstr_size(nbits));
268 	for (i = 0; i < nbits; i++) {
269 		bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
270 		ATF_REQUIRE_MSG(found_clear_bit == i,
271 		    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
272 		    nbits, memloc, i, found_clear_bit);
273 	}
274 
275 	memset(bitstr, 0xFF, bitstr_size(nbits));
276 	for (i = 0; i < nbits; i++) {
277 		bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
278 		ATF_REQUIRE_MSG(found_clear_bit == -1,
279 		    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
280 		    nbits, memloc, i, found_clear_bit);
281 	}
282 
283 	memset(bitstr, 0x55, bitstr_size(nbits));
284 	for (i = 0; i < nbits; i++) {
285 		bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
286 		if (i == nbits - 1 && (nbits & 1) != 0) {
287 			ATF_REQUIRE_MSG(found_clear_bit == -1,
288 			    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
289 			    nbits, memloc, i, found_clear_bit);
290 		} else {
291 			ATF_REQUIRE_MSG(
292 			    found_clear_bit == i + ((i & 1) ? 0 : 1),
293 			    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
294 			    nbits, memloc, i, found_clear_bit);
295 		}
296 	}
297 
298 	memset(bitstr, 0xAA, bitstr_size(nbits));
299 	for (i = 0; i < nbits; i++) {
300 		bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
301 		if (i == nbits - 1 && (nbits & 1) == 0) {
302 			ATF_REQUIRE_MSG(found_clear_bit == -1,
303 			    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
304 			    nbits, memloc, i, found_clear_bit);
305 		} else {
306 			ATF_REQUIRE_MSG(found_clear_bit == i + (i & 1),
307 			    "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
308 			    nbits, memloc, i, found_clear_bit);
309 		}
310 	}
311 
312 	/* Pass a start value beyond the size of the bit string */
313 	bit_ffc_at(bitstr, nbits, nbits, &found_clear_bit);
314 	ATF_REQUIRE_MSG(found_clear_bit == -1,
315 			"bit_ffc_at_%d_%s: Failed with high start value, Result %d",
316 			nbits, memloc, found_clear_bit);
317 
318 	bit_ffc_at(bitstr, nbits + 3, nbits, &found_clear_bit);
319 	ATF_REQUIRE_MSG(found_clear_bit == -1,
320 			"bit_ffc_at_%d_%s: Failed with high start value of %d, Result %d",
321 			nbits, memloc, nbits + 3, found_clear_bit);
322 }
323 
324 BITSTRING_TC_DEFINE(bit_ffc_area_no_match)
325 /* bitstr_t *bitstr, int nbits, const char *memloc */
326 {
327 	int found_clear_bits;
328 
329 	memset(bitstr, 0xFF, bitstr_size(nbits));
330 	bit_ffc_area(bitstr, nbits, 2, &found_clear_bits);
331 	ATF_REQUIRE_EQ_MSG(-1, found_clear_bits,
332 		"bit_ffc_area_%d_%s: Failed all set bits.", nbits, memloc);
333 }
334 
335 BITSTRING_TC_DEFINE(bit_ffs_area_no_match)
336 /* bitstr_t *bitstr, int nbits, const char *memloc */
337 {
338 	int found_clear_bits;
339 
340 	memset(bitstr, 0, bitstr_size(nbits));
341 	bit_ffs_area(bitstr, nbits, 2, &found_clear_bits);
342 	ATF_REQUIRE_EQ_MSG(-1, found_clear_bits,
343 		"bit_ffs_area_%d_%s: Failed all clear bits.", nbits, memloc);
344 }
345 
346 ATF_TC_WITHOUT_HEAD(bit_ffs_area);
347 ATF_TC_BODY(bit_ffs_area, tc)
348 {
349 	const int nbits = 72;
350 	bitstr_t bit_decl(bitstr, nbits);
351 	int location;
352 
353 	memset(bitstr, 0, bitstr_size(nbits));
354 
355 	bit_set(bitstr, 5);
356 	bit_set(bitstr, 6);
357 
358 	location = 0;
359 	bit_ffs_area(bitstr, nbits, 3, &location);
360 	ATF_REQUIRE_EQ_MSG(-1, location,
361 			"bit_ffs_area: found location of size 3 when only 2 bits are set");
362 
363 	bit_set(bitstr, 7);
364 
365 	location = 0;
366 	bit_ffs_area(bitstr, nbits, 3, &location);
367 	ATF_REQUIRE_EQ_MSG(5, location,
368 			"bit_ffs_area: failed to find location of size 3");
369 
370 	bit_set(bitstr, 8);
371 
372 	location = 0;
373 	bit_ffs_area(bitstr, nbits, 3, &location);
374 	ATF_REQUIRE_EQ_MSG(5, location,
375 			"bit_ffs_area: failed to find location of size 3");
376 
377 	location = 0;
378 	bit_ffs_area_at(bitstr, 2, nbits, 3, &location);
379 	ATF_REQUIRE_EQ_MSG(5, location,
380 			"bit_ffs_area_at: failed to find location of size 3");
381 
382 	location = 0;
383 	bit_ffs_area_at(bitstr, 6, nbits, 3, &location);
384 	ATF_REQUIRE_EQ_MSG(6, location,
385 			"bit_ffs_area_at: failed to find location of size 3");
386 
387 	location = 0;
388 	bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
389 	ATF_REQUIRE_EQ_MSG(-1, location,
390 			"bit_ffs_area_at: found invalid location");
391 
392 	bit_set(bitstr, 69);
393 	bit_set(bitstr, 70);
394 	bit_set(bitstr, 71);
395 
396 	location = 0;
397 	bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
398 	ATF_REQUIRE_EQ_MSG(69, location,
399 			"bit_ffs_area_at: failed to find location of size 3");
400 
401 	location = 0;
402 	bit_ffs_area_at(bitstr, 69, nbits, 3, &location);
403 	ATF_REQUIRE_EQ_MSG(69, location,
404 			"bit_ffs_area_at: failed to find location of size 3");
405 
406 	location = 0;
407 	bit_ffs_area_at(bitstr, 70, nbits, 3, &location);
408 	ATF_REQUIRE_EQ_MSG(-1, location,
409 			"bit_ffs_area_at: found invalid location");
410 
411 	location = 0;
412 	bit_ffs_area_at(bitstr, 72, nbits, 3, &location);
413 	ATF_REQUIRE_EQ_MSG(-1, location,
414 			"bit_ffs_area_at: found invalid location");
415 }
416 
417 ATF_TC_WITHOUT_HEAD(bit_ffc_area);
418 ATF_TC_BODY(bit_ffc_area, tc)
419 {
420 	const int nbits = 80;
421 	bitstr_t bit_decl(bitstr, nbits);
422 	int location;
423 
424 	/* set all bits */
425 	memset(bitstr, 0xFF, bitstr_size(nbits));
426 
427 	bit_clear(bitstr, 7);
428 	bit_clear(bitstr, 8);
429 
430 	location = 0;
431 	bit_ffc_area(bitstr, nbits, 3, &location);
432 	ATF_REQUIRE_EQ_MSG(-1, location,
433 			"bit_ffc_area: found location of size 3 when only 2 bits are set");
434 
435 	bit_clear(bitstr, 9);
436 
437 	location = 0;
438 	bit_ffc_area(bitstr, nbits, 3, &location);
439 	ATF_REQUIRE_EQ_MSG(7, location,
440 			"bit_ffc_area: failed to find location of size 3");
441 
442 	bit_clear(bitstr, 10);
443 
444 	location = 0;
445 	bit_ffc_area(bitstr, nbits, 3, &location);
446 	ATF_REQUIRE_EQ_MSG(7, location,
447 			"bit_ffc_area: failed to find location of size 3");
448 
449 	location = 0;
450 	bit_ffc_area_at(bitstr, 2, nbits, 3, &location);
451 	ATF_REQUIRE_EQ_MSG(7, location,
452 			"bit_ffc_area_at: failed to find location of size 3");
453 
454 	location = 0;
455 	bit_ffc_area_at(bitstr, 8, nbits, 3, &location);
456 	ATF_REQUIRE_EQ_MSG(8, location,
457 			"bit_ffc_area_at: failed to find location of size 3");
458 
459 	location = 0;
460 	bit_ffc_area_at(bitstr, 9, nbits, 3, &location);
461 	ATF_REQUIRE_EQ_MSG(-1, location,
462 			"bit_ffc_area_at: found invalid bit location");
463 
464 	bit_clear(bitstr, 77);
465 	bit_clear(bitstr, 78);
466 	bit_clear(bitstr, 79);
467 
468 	location = 0;
469 	bit_ffc_area_at(bitstr, 12, nbits, 3, &location);
470 	ATF_REQUIRE_EQ_MSG(77, location,
471 			"bit_ffc_area_at: failed to find location of size 3");
472 
473 	location = 0;
474 	bit_ffc_area_at(bitstr, 77, nbits, 3, &location);
475 	ATF_REQUIRE_EQ_MSG(77, location,
476 			"bit_ffc_area_at: failed to find location of size 3");
477 
478 	location = 0;
479 	bit_ffc_area_at(bitstr, 78, nbits, 3, &location);
480 	ATF_REQUIRE_EQ_MSG(-1, location,
481 			"bit_ffc_area_at: found invalid location");
482 
483 	location = 0;
484 	bit_ffc_area_at(bitstr, 85, nbits, 3, &location);
485 	ATF_REQUIRE_EQ_MSG(-1, location,
486 			"bit_ffc_area_at: found invalid location");
487 }
488 
489 BITSTRING_TC_DEFINE(bit_nclear)
490 /* bitstr_t *bitstr, int nbits, const char *memloc */
491 {
492 	int i, j;
493 	int found_set_bit;
494 	int found_clear_bit;
495 
496 	for (i = 0; i < nbits; i++) {
497 		for (j = i; j < nbits; j++) {
498 			memset(bitstr, 0xFF, bitstr_size(nbits));
499 			bit_nclear(bitstr, i, j);
500 
501 			bit_ffc(bitstr, nbits, &found_clear_bit);
502 			ATF_REQUIRE_MSG(
503 			    found_clear_bit == i,
504 			    "bit_nclear_%d_%d_%d%s: Failed with result %d",
505 			    nbits, i, j, memloc, found_clear_bit);
506 
507 			bit_ffs_at(bitstr, i, nbits, &found_set_bit);
508 			ATF_REQUIRE_MSG(
509 			    (j + 1 < nbits) ? found_set_bit == j + 1 : -1,
510 			    "bit_nset_%d_%d_%d%s: Failed with result %d",
511 			    nbits, i, j, memloc, found_set_bit);
512 		}
513 	}
514 }
515 
516 BITSTRING_TC_DEFINE(bit_nset)
517 /* bitstr_t *bitstr, int nbits, const char *memloc */
518 {
519 	int i, j;
520 	int found_set_bit;
521 	int found_clear_bit;
522 
523 	for (i = 0; i < nbits; i++) {
524 		for (j = i; j < nbits; j++) {
525 			memset(bitstr, 0, bitstr_size(nbits));
526 			bit_nset(bitstr, i, j);
527 
528 			bit_ffs(bitstr, nbits, &found_set_bit);
529 			ATF_REQUIRE_MSG(
530 			    found_set_bit == i,
531 			    "bit_nset_%d_%d_%d%s: Failed with result %d",
532 			    nbits, i, j, memloc, found_set_bit);
533 
534 			bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
535 			ATF_REQUIRE_MSG(
536 			    (j + 1 < nbits) ? found_clear_bit == j + 1 : -1,
537 			    "bit_nset_%d_%d_%d%s: Failed with result %d",
538 			    nbits, i, j, memloc, found_clear_bit);
539 		}
540 	}
541 }
542 
543 BITSTRING_TC_DEFINE(bit_count)
544 /* bitstr_t *bitstr, int nbits, const char *memloc */
545 {
546 	int result, s, e, expected;
547 
548 	/* Empty bitstr */
549 	memset(bitstr, 0, bitstr_size(nbits));
550 	bit_count(bitstr, 0, nbits, &result);
551 	ATF_CHECK_MSG(0 == result,
552 			"bit_count_%d_%s_%s: Failed with result %d",
553 			nbits, "clear", memloc, result);
554 
555 	/* Full bitstr */
556 	memset(bitstr, 0xFF, bitstr_size(nbits));
557 	bit_count(bitstr, 0, nbits, &result);
558 	ATF_CHECK_MSG(nbits == result,
559 			"bit_count_%d_%s_%s: Failed with result %d",
560 			nbits, "set", memloc, result);
561 
562 	/* Invalid _start value */
563 	memset(bitstr, 0xFF, bitstr_size(nbits));
564 	bit_count(bitstr, nbits, nbits, &result);
565 	ATF_CHECK_MSG(0 == result,
566 			"bit_count_%d_%s_%s: Failed with result %d",
567 			nbits, "invalid_start", memloc, result);
568 
569 	/* Alternating bitstr, starts with 0 */
570 	memset(bitstr, 0xAA, bitstr_size(nbits));
571 	bit_count(bitstr, 0, nbits, &result);
572 	ATF_CHECK_MSG(nbits / 2 == result,
573 			"bit_count_%d_%s_%d_%s: Failed with result %d",
574 			nbits, "alternating", 0, memloc, result);
575 
576 	/* Alternating bitstr, starts with 1 */
577 	memset(bitstr, 0x55, bitstr_size(nbits));
578 	bit_count(bitstr, 0, nbits, &result);
579 	ATF_CHECK_MSG((nbits + 1) / 2 == result,
580 			"bit_count_%d_%s_%d_%s: Failed with result %d",
581 			nbits, "alternating", 1, memloc, result);
582 
583 	/* Varying start location */
584 	memset(bitstr, 0xAA, bitstr_size(nbits));
585 	for (s = 0; s < nbits; s++) {
586 		expected = s % 2 == 0 ? (nbits - s) / 2 : (nbits - s + 1) / 2;
587 		bit_count(bitstr, s, nbits, &result);
588 		ATF_CHECK_MSG(expected == result,
589 				"bit_count_%d_%s_%d_%s: Failed with result %d",
590 				nbits, "vary_start", s, memloc, result);
591 	}
592 
593 	/* Varying end location */
594 	memset(bitstr, 0xAA, bitstr_size(nbits));
595 	for (e = 0; e < nbits; e++) {
596 		bit_count(bitstr, 0, e, &result);
597 		ATF_CHECK_MSG(e / 2 == result,
598 				"bit_count_%d_%s_%d_%s: Failed with result %d",
599 				nbits, "vary_end", e, memloc, result);
600 	}
601 
602 }
603 
604 BITSTRING_TC_DEFINE(bit_foreach)
605 /* bitstr_t *bitstr, int nbits, const char *memloc */
606 {
607 	int i, set_bit;
608 
609 	/* Empty bitstr */
610 	memset(bitstr, 0x00, bitstr_size(nbits));
611 	bit_foreach (bitstr, nbits, set_bit) {
612 		atf_tc_fail("bit_foreach_%d_%s_%s: Failed at location %d",
613 		    nbits, "clear", memloc, set_bit);
614 	}
615 
616 	/* Full bitstr */
617 	i = 0;
618 	memset(bitstr, 0xFF, bitstr_size(nbits));
619 	bit_foreach(bitstr, nbits, set_bit) {
620 		ATF_REQUIRE_MSG(set_bit == i,
621 		    "bit_foreach_%d_%s_%s: Failed on turn %d at location %d",
622 		    nbits, "set", memloc, i, set_bit);
623 		i++;
624 	}
625 	ATF_REQUIRE_MSG(i == nbits,
626 	    "bit_foreach_%d_%s_%s: Invalid number of turns %d",
627 	    nbits, "set", memloc, i);
628 
629 	/* Alternating bitstr, starts with 0 */
630 	i = 0;
631 	memset(bitstr, 0xAA, bitstr_size(nbits));
632 	bit_foreach(bitstr, nbits, set_bit) {
633 		ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
634 		    "bit_foreach_%d_%s_%d_%s: "
635 		    "Failed on turn %d at location %d",
636 		    nbits, "alternating", 0,  memloc, i, set_bit);
637 		i++;
638 	}
639 	ATF_REQUIRE_MSG(i == nbits / 2,
640 	    "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
641 	    nbits, "alternating", 0, memloc, i);
642 
643 	/* Alternating bitstr, starts with 1 */
644 	i = 0;
645 	memset(bitstr, 0x55, bitstr_size(nbits));
646 	bit_foreach(bitstr, nbits, set_bit) {
647 		ATF_REQUIRE_MSG(set_bit == i * 2,
648 		    "bit_foreach_%d_%s_%d_%s: "
649 		    "Failed on turn %d at location %d",
650 		    nbits, "alternating", 1, memloc, i, set_bit);
651 		i++;
652 	}
653 	ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
654 	    "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
655 	    nbits, "alternating", 1, memloc, i);
656 }
657 
658 BITSTRING_TC_DEFINE(bit_foreach_at)
659 /* bitstr_t *bitstr, int nbits, const char *memloc */
660 {
661 	int i, s, e, set_bit;
662 
663 	/* Invalid _start value */
664 	memset(bitstr, 0xFF, bitstr_size(nbits));
665 	bit_foreach_at(bitstr, nbits, nbits, set_bit) {
666 		atf_tc_fail("bit_foreach_at_%d_%s_%s: Failed at location %d",
667 		    nbits, "invalid_start", memloc, set_bit);
668 	}
669 
670 	/* Varying start location */
671 	memset(bitstr, 0xAA, bitstr_size(nbits));
672 	for (s = 0; s < nbits; s++) {
673 		i = 0;
674 		bit_foreach_at(bitstr, s, nbits, set_bit) {
675 			ATF_REQUIRE_MSG(set_bit == (i + s / 2) * 2 + 1,
676 			    "bit_foreach_at_%d_%s_%d_%s: "
677 			    "Failed on turn %d at location %d",
678 			    nbits, "vary_start", s,  memloc, i, set_bit);
679 			i++;
680 		}
681 		ATF_REQUIRE_MSG(i == nbits / 2 - s / 2,
682 		    "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
683 		    nbits, "vary_start", s, memloc, i);
684 	}
685 
686 	/* Varying end location */
687 	memset(bitstr, 0xAA, bitstr_size(nbits));
688 	for (e = 0; e < nbits; e++) {
689 		i = 0;
690 		bit_foreach_at(bitstr, 0, e, set_bit) {
691 			ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
692 			    "bit_foreach_at_%d_%s_%d_%s: "
693 			    "Failed on turn %d at location %d",
694 			    nbits, "vary_end", e,  memloc, i, set_bit);
695 			i++;
696 		}
697 		ATF_REQUIRE_MSG(i == e / 2,
698 		    "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
699 		    nbits, "vary_end", e, memloc, i);
700 	}
701 }
702 
703 BITSTRING_TC_DEFINE(bit_foreach_unset)
704 /* bitstr_t *bitstr, int nbits, const char *memloc */
705 {
706 	int i, unset_bit;
707 
708 	/* Empty bitstr */
709 	i = 0;
710 	memset(bitstr, 0, bitstr_size(nbits));
711 	bit_foreach_unset(bitstr, nbits, unset_bit) {
712 		ATF_REQUIRE_MSG(unset_bit == i,
713 		    "bit_foreach_unset_%d_%s_%s: "
714 		    "Failed on turn %d at location %d",
715 		    nbits, "clear", memloc, i, unset_bit);
716 		i++;
717 	}
718 	ATF_REQUIRE_MSG(i == nbits,
719 	    "bit_foreach_unset_%d_%s_%s: Invalid number of turns %d",
720 	    nbits, "set", memloc, i);
721 
722 	/* Full bitstr */
723 	memset(bitstr, 0xFF, bitstr_size(nbits));
724 	bit_foreach_unset(bitstr, nbits, unset_bit) {
725 		atf_tc_fail("bit_foreach_unset_%d_%s_%s: "
726 		    "Failed at location %d",
727 		    nbits, "set", memloc, unset_bit);
728 	}
729 
730 	/* Alternating bitstr, starts with 0 */
731 	i = 0;
732 	memset(bitstr, 0xAA, bitstr_size(nbits));
733 	bit_foreach_unset(bitstr, nbits, unset_bit) {
734 		ATF_REQUIRE_MSG(unset_bit == i * 2,
735 		    "bit_foreach_unset_%d_%s_%d_%s: "
736 		    "Failed on turn %d at location %d",
737 		    nbits, "alternating", 0,  memloc, i, unset_bit);
738 		i++;
739 	}
740 	ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
741 	    "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
742 	    nbits, "alternating", 0, memloc, i);
743 
744 	/* Alternating bitstr, starts with 1 */
745 	i = 0;
746 	memset(bitstr, 0x55, bitstr_size(nbits));
747 	bit_foreach_unset(bitstr, nbits, unset_bit) {
748 		ATF_REQUIRE_MSG(unset_bit == i * 2 + 1,
749 		    "bit_foreach_unset_%d_%s_%d_%s: "
750 		    "Failed on turn %d at location %d",
751 		    nbits, "alternating", 1, memloc, i, unset_bit);
752 		i++;
753 	}
754 	ATF_REQUIRE_MSG(i == nbits / 2,
755 	    "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
756 	    nbits, "alternating", 1, memloc, i);
757 }
758 
759 BITSTRING_TC_DEFINE(bit_foreach_unset_at)
760 /* bitstr_t *bitstr, int nbits, const char *memloc */
761 {
762 	int i, s, e, unset_bit;
763 
764 	/* Invalid _start value */
765 	memset(bitstr, 0, bitstr_size(nbits));
766 	bit_foreach_unset_at(bitstr, nbits, nbits, unset_bit) {
767 		atf_tc_fail("bit_foreach_unset_at_%d_%s_%s: "
768 		    "Failed at location %d",
769 		    nbits, "invalid_start", memloc, unset_bit);
770 	}
771 
772 	/* Varying start location */
773 	memset(bitstr, 0xAA, bitstr_size(nbits));
774 	for (s = 0; s < nbits; s++) {
775 		i = 0;
776 		bit_foreach_unset_at(bitstr, s, nbits, unset_bit) {
777 			ATF_REQUIRE_MSG(unset_bit == (i + (s + 1) / 2) * 2,
778 			    "bit_foreach_unset_at_%d_%s_%d_%s: "
779 			    "Failed on turn %d at location %d",
780 			    nbits, "vary_start", s,  memloc, i, unset_bit);
781 			i++;
782 		}
783 		ATF_REQUIRE_MSG(i == (nbits + 1) / 2 - (s + 1) / 2,
784 		    "bit_foreach_unset_at_%d_%s_%d_%s: "
785 		    "Invalid number of turns %d",
786 		    nbits, "vary_start", s, memloc, i);
787 	}
788 
789 	/* Varying end location */
790 	memset(bitstr, 0xAA, bitstr_size(nbits));
791 	for (e = 0; e < nbits; e++) {
792 		i = 0;
793 		bit_foreach_unset_at(bitstr, 0, e, unset_bit) {
794 			ATF_REQUIRE_MSG(unset_bit == i * 2,
795 			    "bit_foreach_unset_at_%d_%s_%d_%s: "
796 			    "Failed on turn %d at location %d",
797 			    nbits, "vary_end", e,  memloc, i, unset_bit);
798 			i++;
799 		}
800 		ATF_REQUIRE_MSG(i == (e + 1) / 2,
801 		    "bit_foreach_unset_at_%d_%s_%d_%s: "
802 		    "Invalid number of turns %d",
803 		    nbits, "vary_end", e, memloc, i);
804 	}
805 }
806 
807 ATF_TP_ADD_TCS(tp)
808 {
809 
810 	ATF_TP_ADD_TC(tp, bitstr_in_struct);
811 	ATF_TP_ADD_TC(tp, bitstr_size);
812 	ATF_TP_ADD_TC(tp, bit_ffc_area);
813 	ATF_TP_ADD_TC(tp, bit_ffs_area);
814 	BITSTRING_TC_ADD(tp, bit_set);
815 	BITSTRING_TC_ADD(tp, bit_clear);
816 	BITSTRING_TC_ADD(tp, bit_ffs);
817 	BITSTRING_TC_ADD(tp, bit_ffc);
818 	BITSTRING_TC_ADD(tp, bit_ffs_at);
819 	BITSTRING_TC_ADD(tp, bit_ffc_at);
820 	BITSTRING_TC_ADD(tp, bit_nclear);
821 	BITSTRING_TC_ADD(tp, bit_nset);
822 	BITSTRING_TC_ADD(tp, bit_count);
823 	BITSTRING_TC_ADD(tp, bit_ffs_area_no_match);
824 	BITSTRING_TC_ADD(tp, bit_ffc_area_no_match);
825 	BITSTRING_TC_ADD(tp, bit_foreach);
826 	BITSTRING_TC_ADD(tp, bit_foreach_at);
827 	BITSTRING_TC_ADD(tp, bit_foreach_unset);
828 	BITSTRING_TC_ADD(tp, bit_foreach_unset_at);
829 
830 	return (atf_no_error());
831 }
832