2 * Copyright (c) 2014 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/param.h>
33 #include <bitstring.h>
38 typedef void (testfunc_t)(bitstr_t *bstr, int nbits, const char *memloc);
41 bitstring_run_stack_test(testfunc_t *test, int nbits)
43 bitstr_t bit_decl(bitstr, nbits);
45 test(bitstr, nbits, "stack");
49 bitstring_run_heap_test(testfunc_t *test, int nbits)
51 bitstr_t *bitstr = bit_alloc(nbits);
53 test(bitstr, nbits, "heap");
57 bitstring_test_runner(testfunc_t *test)
59 const int bitstr_sizes[] = {
71 for (unsigned long i = 0; i < nitems(bitstr_sizes); i++) {
72 bitstring_run_stack_test(test, bitstr_sizes[i]);
73 bitstring_run_heap_test(test, bitstr_sizes[i]);
77 #define BITSTRING_TC_DEFINE(name) \
78 ATF_TC_WITHOUT_HEAD(name); \
79 static testfunc_t name ## _test; \
81 ATF_TC_BODY(name, tc) \
83 bitstring_test_runner(name ## _test); \
87 name ## _test(bitstr_t *bitstr, int nbits, const char *memloc)
89 #define BITSTRING_TC_ADD(tp, name) \
91 ATF_TP_ADD_TC(tp, name); \
94 ATF_TC_WITHOUT_HEAD(bitstr_in_struct);
95 ATF_TC_BODY(bitstr_in_struct, tc)
97 struct bitstr_containing_struct {
98 bitstr_t bit_decl(bitstr, 8);
101 bit_nclear(test_struct.bitstr, 0, 8);
104 ATF_TC_WITHOUT_HEAD(bitstr_size);
105 ATF_TC_BODY(bitstr_size, tc)
107 size_t sob = sizeof(bitstr_t);
109 ATF_CHECK_EQ(0, bitstr_size(0));
110 ATF_CHECK_EQ(sob, bitstr_size(1));
111 ATF_CHECK_EQ(sob, bitstr_size(sob * 8));
112 ATF_CHECK_EQ(2 * sob, bitstr_size(sob * 8 + 1));
115 BITSTRING_TC_DEFINE(bit_set)
116 /* bitstr_t *bitstr, int nbits, const char *memloc */
118 memset(bitstr, 0, bitstr_size(nbits));
120 for (int i = 0; i < nbits; i++) {
123 for (int j = 0; j < nbits; j++) {
124 ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 1 : 0,
125 "bit_set_%d_%s: Failed on bit %d",
129 bit_clear(bitstr, i);
133 BITSTRING_TC_DEFINE(bit_clear)
134 /* bitstr_t *bitstr, int nbits, const char *memloc */
138 memset(bitstr, 0xFF, bitstr_size(nbits));
139 for (i = 0; i < nbits; i++) {
140 bit_clear(bitstr, i);
142 for (j = 0; j < nbits; j++) {
143 ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 0 : 1,
144 "bit_clear_%d_%s: Failed on bit %d",
152 BITSTRING_TC_DEFINE(bit_ffs)
153 /* bitstr_t *bitstr, int nbits, const char *memloc */
158 memset(bitstr, 0, bitstr_size(nbits));
159 bit_ffs(bitstr, nbits, &found_set_bit);
160 ATF_REQUIRE_MSG(found_set_bit == -1,
161 "bit_ffs_%d_%s: Failed all clear bits.", nbits, memloc);
163 for (i = 0; i < nbits; i++) {
164 memset(bitstr, 0xFF, bitstr_size(nbits));
166 bit_nclear(bitstr, 0, i - 1);
168 bit_ffs(bitstr, nbits, &found_set_bit);
169 ATF_REQUIRE_MSG(found_set_bit == i,
170 "bit_ffs_%d_%s: Failed on bit %d, Result %d",
171 nbits, memloc, i, found_set_bit);
175 BITSTRING_TC_DEFINE(bit_ffc)
176 /* bitstr_t *bitstr, int nbits, const char *memloc */
181 memset(bitstr, 0xFF, bitstr_size(nbits));
182 bit_ffc(bitstr, nbits, &found_clear_bit);
183 ATF_REQUIRE_MSG(found_clear_bit == -1,
184 "bit_ffc_%d_%s: Failed all set bits.", nbits, memloc);
186 for (i = 0; i < nbits; i++) {
187 memset(bitstr, 0, bitstr_size(nbits));
189 bit_nset(bitstr, 0, i - 1);
191 bit_ffc(bitstr, nbits, &found_clear_bit);
192 ATF_REQUIRE_MSG(found_clear_bit == i,
193 "bit_ffc_%d_%s: Failed on bit %d, Result %d",
194 nbits, memloc, i, found_clear_bit);
198 BITSTRING_TC_DEFINE(bit_ffs_at)
199 /* bitstr_t *bitstr, int nbits, const char *memloc */
204 memset(bitstr, 0xFF, bitstr_size(nbits));
205 for (i = 0; i < nbits; i++) {
206 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
207 ATF_REQUIRE_MSG(found_set_bit == i,
208 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
209 nbits, memloc, i, found_set_bit);
212 memset(bitstr, 0, bitstr_size(nbits));
213 for (i = 0; i < nbits; i++) {
214 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
215 ATF_REQUIRE_MSG(found_set_bit == -1,
216 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
217 nbits, memloc, i, found_set_bit);
220 memset(bitstr, 0x55, bitstr_size(nbits));
221 for (i = 0; i < nbits; i++) {
222 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
223 if (i == nbits - 1 && (nbits & 1) == 0) {
224 ATF_REQUIRE_MSG(found_set_bit == -1,
225 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
226 nbits, memloc, i, found_set_bit);
228 ATF_REQUIRE_MSG(found_set_bit == i + (i & 1),
229 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
230 nbits, memloc, i, found_set_bit);
234 memset(bitstr, 0xAA, bitstr_size(nbits));
235 for (i = 0; i < nbits; i++) {
236 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
237 if (i == nbits - 1 && (nbits & 1) != 0) {
238 ATF_REQUIRE_MSG(found_set_bit == -1,
239 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
240 nbits, memloc, i, found_set_bit);
243 found_set_bit == i + ((i & 1) ? 0 : 1),
244 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
245 nbits, memloc, i, found_set_bit);
249 /* Pass a start value beyond the size of the bit string */
250 bit_ffs_at(bitstr, nbits, nbits, &found_set_bit);
251 ATF_REQUIRE_MSG(found_set_bit == -1,
252 "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
253 nbits, memloc, nbits, found_set_bit);
255 bit_ffs_at(bitstr, nbits + 3, nbits, &found_set_bit);
256 ATF_REQUIRE_MSG(found_set_bit == -1,
257 "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
258 nbits, memloc, nbits + 3, found_set_bit);
261 BITSTRING_TC_DEFINE(bit_ffc_at)
262 /* bitstr_t *bitstr, int nbits, const char *memloc */
264 int i, found_clear_bit;
266 memset(bitstr, 0, bitstr_size(nbits));
267 for (i = 0; i < nbits; i++) {
268 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
269 ATF_REQUIRE_MSG(found_clear_bit == i,
270 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
271 nbits, memloc, i, found_clear_bit);
274 memset(bitstr, 0xFF, bitstr_size(nbits));
275 for (i = 0; i < nbits; i++) {
276 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
277 ATF_REQUIRE_MSG(found_clear_bit == -1,
278 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
279 nbits, memloc, i, found_clear_bit);
282 memset(bitstr, 0x55, bitstr_size(nbits));
283 for (i = 0; i < nbits; i++) {
284 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
285 if (i == nbits - 1 && (nbits & 1) != 0) {
286 ATF_REQUIRE_MSG(found_clear_bit == -1,
287 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
288 nbits, memloc, i, found_clear_bit);
291 found_clear_bit == i + ((i & 1) ? 0 : 1),
292 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
293 nbits, memloc, i, found_clear_bit);
297 memset(bitstr, 0xAA, bitstr_size(nbits));
298 for (i = 0; i < nbits; i++) {
299 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
300 if (i == nbits - 1 && (nbits & 1) == 0) {
301 ATF_REQUIRE_MSG(found_clear_bit == -1,
302 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
303 nbits, memloc, i, found_clear_bit);
305 ATF_REQUIRE_MSG(found_clear_bit == i + (i & 1),
306 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
307 nbits, memloc, i, found_clear_bit);
311 /* Pass a start value beyond the size of the bit string */
312 bit_ffc_at(bitstr, nbits, nbits, &found_clear_bit);
313 ATF_REQUIRE_MSG(found_clear_bit == -1,
314 "bit_ffc_at_%d_%s: Failed with high start value, Result %d",
315 nbits, memloc, found_clear_bit);
317 bit_ffc_at(bitstr, nbits + 3, nbits, &found_clear_bit);
318 ATF_REQUIRE_MSG(found_clear_bit == -1,
319 "bit_ffc_at_%d_%s: Failed with high start value of %d, Result %d",
320 nbits, memloc, nbits + 3, found_clear_bit);
323 BITSTRING_TC_DEFINE(bit_ffc_area_at_all_or_nothing)
324 /* bitstr_t *bitstr, int nbits, const char *memloc */
328 memset(bitstr, 0, bitstr_size(nbits));
329 if (nbits % _BITSTR_BITS != 0)
330 bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
332 for (int start = 0; start < nbits; start++) {
333 for (int size = 1; size < nbits - start; size++) {
334 bit_ffc_area_at(bitstr, start, nbits, size, &found);
335 ATF_REQUIRE_EQ_MSG(start, found,
336 "bit_ffc_area_at_%d_%s: "
337 "Did not find %d clear bits at %d",
338 nbits, memloc, size, start);
342 memset(bitstr, 0xff, bitstr_size(nbits));
343 if (nbits % _BITSTR_BITS != 0)
344 bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
346 for (int start = 0; start < nbits; start++) {
347 for (int size = 1; size < nbits - start; size++) {
348 bit_ffc_area_at(bitstr, start, nbits, size, &found);
349 ATF_REQUIRE_EQ_MSG(-1, found,
350 "bit_ffc_area_at_%d_%s: "
351 "Found %d clear bits at %d",
352 nbits, memloc, size, start);
357 BITSTRING_TC_DEFINE(bit_ffs_area_at_all_or_nothing)
358 /* bitstr_t *bitstr, int nbits, const char *memloc */
362 memset(bitstr, 0, bitstr_size(nbits));
363 if (nbits % _BITSTR_BITS != 0)
364 bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
366 for (int start = 0; start < nbits; start++) {
367 for (int size = 1; size < nbits - start; size++) {
368 bit_ffs_area_at(bitstr, start, nbits, size, &found);
369 ATF_REQUIRE_EQ_MSG(-1, found,
370 "bit_ffs_area_at_%d_%s: "
371 "Found %d set bits at %d",
372 nbits, memloc, size, start);
376 memset(bitstr, 0xff, bitstr_size(nbits));
377 if (nbits % _BITSTR_BITS != 0)
378 bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
380 for (int start = 0; start < nbits; start++) {
381 for (int size = 1; size < nbits - start; size++) {
382 bit_ffs_area_at(bitstr, start, nbits, size, &found);
383 ATF_REQUIRE_EQ_MSG(start, found,
384 "bit_ffs_area_at_%d_%s: "
385 "Did not find %d set bits at %d",
386 nbits, memloc, size, start);
391 ATF_TC_WITHOUT_HEAD(bit_ffs_area);
392 ATF_TC_BODY(bit_ffs_area, tc)
394 const int nbits = 72;
395 bitstr_t bit_decl(bitstr, nbits);
398 memset(bitstr, 0, bitstr_size(nbits));
400 bit_nset(bitstr, 5, 6);
403 bit_ffs_area(bitstr, nbits, 3, &location);
404 ATF_REQUIRE_EQ_MSG(-1, location,
405 "bit_ffs_area: found location of size 3 when only 2 bits are set");
406 ATF_REQUIRE_EQ_MSG(0, bit_ntest(bitstr, 5, 7, 1),
407 "bit_ntest: found location of size 3 when only 2 bits are set");
412 bit_ffs_area(bitstr, nbits, 3, &location);
413 ATF_REQUIRE_EQ_MSG(5, location,
414 "bit_ffs_area: failed to find location of size 3 %d", location);
415 ATF_REQUIRE_EQ_MSG(1, bit_ntest(bitstr, 5, 7, 1),
416 "bit_ntest: failed to find all 3 bits set");
421 bit_ffs_area(bitstr, nbits, 3, &location);
422 ATF_REQUIRE_EQ_MSG(5, location,
423 "bit_ffs_area: failed to find location of size 3");
426 bit_ffs_area_at(bitstr, 2, nbits, 3, &location);
427 ATF_REQUIRE_EQ_MSG(5, location,
428 "bit_ffs_area_at: failed to find location of size 3");
431 bit_ffs_area_at(bitstr, 6, nbits, 3, &location);
432 ATF_REQUIRE_EQ_MSG(6, location,
433 "bit_ffs_area_at: failed to find location of size 3");
436 bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
437 ATF_REQUIRE_EQ_MSG(-1, location,
438 "bit_ffs_area_at: found invalid location");
440 bit_nset(bitstr, 69, 71);
443 bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
444 ATF_REQUIRE_EQ_MSG(69, location,
445 "bit_ffs_area_at: failed to find location of size 3");
448 bit_ffs_area_at(bitstr, 69, nbits, 3, &location);
449 ATF_REQUIRE_EQ_MSG(69, location,
450 "bit_ffs_area_at: failed to find location of size 3");
453 bit_ffs_area_at(bitstr, 70, nbits, 3, &location);
454 ATF_REQUIRE_EQ_MSG(-1, location,
455 "bit_ffs_area_at: found invalid location");
458 bit_ffs_area_at(bitstr, 72, nbits, 3, &location);
459 ATF_REQUIRE_EQ_MSG(-1, location,
460 "bit_ffs_area_at: found invalid location");
462 bit_nset(bitstr, 59, 67);
465 bit_ffs_area(bitstr, nbits, 9, &location);
466 ATF_REQUIRE_EQ_MSG(59, location,
467 "bit_ffs_area: failed to find location of size 9");
470 bit_ffs_area(bitstr, nbits, 10, &location);
471 ATF_REQUIRE_EQ_MSG(-1, location,
472 "bit_ffs_area: found invalid location");
475 ATF_TC_WITHOUT_HEAD(bit_ffc_area);
476 ATF_TC_BODY(bit_ffc_area, tc)
478 const int nbits = 80;
479 bitstr_t bit_decl(bitstr, nbits);
483 memset(bitstr, 0xFF, bitstr_size(nbits));
485 bit_clear(bitstr, 7);
486 bit_clear(bitstr, 8);
489 bit_ffc_area(bitstr, nbits, 3, &location);
490 ATF_REQUIRE_EQ_MSG(-1, location,
491 "bit_ffc_area: found location of size 3 when only 2 bits are set");
493 bit_clear(bitstr, 9);
496 bit_ffc_area(bitstr, nbits, 3, &location);
497 ATF_REQUIRE_EQ_MSG(7, location,
498 "bit_ffc_area: failed to find location of size 3");
500 bit_clear(bitstr, 10);
503 bit_ffc_area(bitstr, nbits, 3, &location);
504 ATF_REQUIRE_EQ_MSG(7, location,
505 "bit_ffc_area: failed to find location of size 3");
508 bit_ffc_area_at(bitstr, 2, nbits, 3, &location);
509 ATF_REQUIRE_EQ_MSG(7, location,
510 "bit_ffc_area_at: failed to find location of size 3");
513 bit_ffc_area_at(bitstr, 8, nbits, 3, &location);
514 ATF_REQUIRE_EQ_MSG(8, location,
515 "bit_ffc_area_at: failed to find location of size 3");
518 bit_ffc_area_at(bitstr, 9, nbits, 3, &location);
519 ATF_REQUIRE_EQ_MSG(-1, location,
520 "bit_ffc_area_at: found invalid bit location");
522 bit_clear(bitstr, 77);
523 bit_clear(bitstr, 78);
524 bit_clear(bitstr, 79);
527 bit_ffc_area_at(bitstr, 12, nbits, 3, &location);
528 ATF_REQUIRE_EQ_MSG(77, location,
529 "bit_ffc_area_at: failed to find location of size 3");
532 bit_ffc_area_at(bitstr, 77, nbits, 3, &location);
533 ATF_REQUIRE_EQ_MSG(77, location,
534 "bit_ffc_area_at: failed to find location of size 3");
537 bit_ffc_area_at(bitstr, 78, nbits, 3, &location);
538 ATF_REQUIRE_EQ_MSG(-1, location,
539 "bit_ffc_area_at: found invalid location");
542 bit_ffc_area_at(bitstr, 85, nbits, 3, &location);
543 ATF_REQUIRE_EQ_MSG(-1, location,
544 "bit_ffc_area_at: found invalid location");
547 BITSTRING_TC_DEFINE(bit_nclear)
548 /* bitstr_t *bitstr, int nbits, const char *memloc */
554 for (i = 0; i < nbits; i++) {
555 for (j = i; j < nbits; j++) {
556 memset(bitstr, 0xFF, bitstr_size(nbits));
557 bit_nclear(bitstr, i, j);
559 bit_ffc(bitstr, nbits, &found_clear_bit);
561 found_clear_bit == i,
562 "bit_nclear_%d_%d_%d%s: Failed with result %d",
563 nbits, i, j, memloc, found_clear_bit);
565 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
567 (j + 1 < nbits) ? found_set_bit == j + 1 : -1,
568 "bit_nset_%d_%d_%d%s: Failed with result %d",
569 nbits, i, j, memloc, found_set_bit);
574 BITSTRING_TC_DEFINE(bit_nset)
575 /* bitstr_t *bitstr, int nbits, const char *memloc */
581 for (i = 0; i < nbits; i++) {
582 for (j = i; j < nbits; j++) {
583 memset(bitstr, 0, bitstr_size(nbits));
584 bit_nset(bitstr, i, j);
586 bit_ffs(bitstr, nbits, &found_set_bit);
589 "bit_nset_%d_%d_%d%s: Failed with result %d",
590 nbits, i, j, memloc, found_set_bit);
592 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
594 (j + 1 < nbits) ? found_clear_bit == j + 1 : -1,
595 "bit_nset_%d_%d_%d%s: Failed with result %d",
596 nbits, i, j, memloc, found_clear_bit);
601 BITSTRING_TC_DEFINE(bit_count)
602 /* bitstr_t *bitstr, int nbits, const char *memloc */
604 int result, s, e, expected;
607 memset(bitstr, 0, bitstr_size(nbits));
608 bit_count(bitstr, 0, nbits, &result);
609 ATF_CHECK_MSG(0 == result,
610 "bit_count_%d_%s_%s: Failed with result %d",
611 nbits, "clear", memloc, result);
614 memset(bitstr, 0xFF, bitstr_size(nbits));
615 bit_count(bitstr, 0, nbits, &result);
616 ATF_CHECK_MSG(nbits == result,
617 "bit_count_%d_%s_%s: Failed with result %d",
618 nbits, "set", memloc, result);
620 /* Invalid _start value */
621 memset(bitstr, 0xFF, bitstr_size(nbits));
622 bit_count(bitstr, nbits, nbits, &result);
623 ATF_CHECK_MSG(0 == result,
624 "bit_count_%d_%s_%s: Failed with result %d",
625 nbits, "invalid_start", memloc, result);
627 /* Alternating bitstr, starts with 0 */
628 memset(bitstr, 0xAA, bitstr_size(nbits));
629 bit_count(bitstr, 0, nbits, &result);
630 ATF_CHECK_MSG(nbits / 2 == result,
631 "bit_count_%d_%s_%d_%s: Failed with result %d",
632 nbits, "alternating", 0, memloc, result);
634 /* Alternating bitstr, starts with 1 */
635 memset(bitstr, 0x55, bitstr_size(nbits));
636 bit_count(bitstr, 0, nbits, &result);
637 ATF_CHECK_MSG((nbits + 1) / 2 == result,
638 "bit_count_%d_%s_%d_%s: Failed with result %d",
639 nbits, "alternating", 1, memloc, result);
641 /* Varying start location */
642 memset(bitstr, 0xAA, bitstr_size(nbits));
643 for (s = 0; s < nbits; s++) {
644 expected = s % 2 == 0 ? (nbits - s) / 2 : (nbits - s + 1) / 2;
645 bit_count(bitstr, s, nbits, &result);
646 ATF_CHECK_MSG(expected == result,
647 "bit_count_%d_%s_%d_%s: Failed with result %d",
648 nbits, "vary_start", s, memloc, result);
651 /* Varying end location */
652 memset(bitstr, 0xAA, bitstr_size(nbits));
653 for (e = 0; e < nbits; e++) {
654 bit_count(bitstr, 0, e, &result);
655 ATF_CHECK_MSG(e / 2 == result,
656 "bit_count_%d_%s_%d_%s: Failed with result %d",
657 nbits, "vary_end", e, memloc, result);
662 BITSTRING_TC_DEFINE(bit_foreach)
663 /* bitstr_t *bitstr, int nbits, const char *memloc */
668 memset(bitstr, 0x00, bitstr_size(nbits));
669 bit_foreach (bitstr, nbits, set_bit) {
670 atf_tc_fail("bit_foreach_%d_%s_%s: Failed at location %d",
671 nbits, "clear", memloc, set_bit);
676 memset(bitstr, 0xFF, bitstr_size(nbits));
677 bit_foreach(bitstr, nbits, set_bit) {
678 ATF_REQUIRE_MSG(set_bit == i,
679 "bit_foreach_%d_%s_%s: Failed on turn %d at location %d",
680 nbits, "set", memloc, i, set_bit);
683 ATF_REQUIRE_MSG(i == nbits,
684 "bit_foreach_%d_%s_%s: Invalid number of turns %d",
685 nbits, "set", memloc, i);
687 /* Alternating bitstr, starts with 0 */
689 memset(bitstr, 0xAA, bitstr_size(nbits));
690 bit_foreach(bitstr, nbits, set_bit) {
691 ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
692 "bit_foreach_%d_%s_%d_%s: "
693 "Failed on turn %d at location %d",
694 nbits, "alternating", 0, memloc, i, set_bit);
697 ATF_REQUIRE_MSG(i == nbits / 2,
698 "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
699 nbits, "alternating", 0, memloc, i);
701 /* Alternating bitstr, starts with 1 */
703 memset(bitstr, 0x55, bitstr_size(nbits));
704 bit_foreach(bitstr, nbits, set_bit) {
705 ATF_REQUIRE_MSG(set_bit == i * 2,
706 "bit_foreach_%d_%s_%d_%s: "
707 "Failed on turn %d at location %d",
708 nbits, "alternating", 1, memloc, i, set_bit);
711 ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
712 "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
713 nbits, "alternating", 1, memloc, i);
716 BITSTRING_TC_DEFINE(bit_foreach_at)
717 /* bitstr_t *bitstr, int nbits, const char *memloc */
719 int i, s, e, set_bit;
721 /* Invalid _start value */
722 memset(bitstr, 0xFF, bitstr_size(nbits));
723 bit_foreach_at(bitstr, nbits, nbits, set_bit) {
724 atf_tc_fail("bit_foreach_at_%d_%s_%s: Failed at location %d",
725 nbits, "invalid_start", memloc, set_bit);
728 /* Varying start location */
729 memset(bitstr, 0xAA, bitstr_size(nbits));
730 for (s = 0; s < nbits; s++) {
732 bit_foreach_at(bitstr, s, nbits, set_bit) {
733 ATF_REQUIRE_MSG(set_bit == (i + s / 2) * 2 + 1,
734 "bit_foreach_at_%d_%s_%d_%s: "
735 "Failed on turn %d at location %d",
736 nbits, "vary_start", s, memloc, i, set_bit);
739 ATF_REQUIRE_MSG(i == nbits / 2 - s / 2,
740 "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
741 nbits, "vary_start", s, memloc, i);
744 /* Varying end location */
745 memset(bitstr, 0xAA, bitstr_size(nbits));
746 for (e = 0; e < nbits; e++) {
748 bit_foreach_at(bitstr, 0, e, set_bit) {
749 ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
750 "bit_foreach_at_%d_%s_%d_%s: "
751 "Failed on turn %d at location %d",
752 nbits, "vary_end", e, memloc, i, set_bit);
755 ATF_REQUIRE_MSG(i == e / 2,
756 "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
757 nbits, "vary_end", e, memloc, i);
761 BITSTRING_TC_DEFINE(bit_foreach_unset)
762 /* bitstr_t *bitstr, int nbits, const char *memloc */
768 memset(bitstr, 0, bitstr_size(nbits));
769 bit_foreach_unset(bitstr, nbits, unset_bit) {
770 ATF_REQUIRE_MSG(unset_bit == i,
771 "bit_foreach_unset_%d_%s_%s: "
772 "Failed on turn %d at location %d",
773 nbits, "clear", memloc, i, unset_bit);
776 ATF_REQUIRE_MSG(i == nbits,
777 "bit_foreach_unset_%d_%s_%s: Invalid number of turns %d",
778 nbits, "set", memloc, i);
781 memset(bitstr, 0xFF, bitstr_size(nbits));
782 bit_foreach_unset(bitstr, nbits, unset_bit) {
783 atf_tc_fail("bit_foreach_unset_%d_%s_%s: "
784 "Failed at location %d",
785 nbits, "set", memloc, unset_bit);
788 /* Alternating bitstr, starts with 0 */
790 memset(bitstr, 0xAA, bitstr_size(nbits));
791 bit_foreach_unset(bitstr, nbits, unset_bit) {
792 ATF_REQUIRE_MSG(unset_bit == i * 2,
793 "bit_foreach_unset_%d_%s_%d_%s: "
794 "Failed on turn %d at location %d",
795 nbits, "alternating", 0, memloc, i, unset_bit);
798 ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
799 "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
800 nbits, "alternating", 0, memloc, i);
802 /* Alternating bitstr, starts with 1 */
804 memset(bitstr, 0x55, bitstr_size(nbits));
805 bit_foreach_unset(bitstr, nbits, unset_bit) {
806 ATF_REQUIRE_MSG(unset_bit == i * 2 + 1,
807 "bit_foreach_unset_%d_%s_%d_%s: "
808 "Failed on turn %d at location %d",
809 nbits, "alternating", 1, memloc, i, unset_bit);
812 ATF_REQUIRE_MSG(i == nbits / 2,
813 "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
814 nbits, "alternating", 1, memloc, i);
817 BITSTRING_TC_DEFINE(bit_foreach_unset_at)
818 /* bitstr_t *bitstr, int nbits, const char *memloc */
820 int i, s, e, unset_bit;
822 /* Invalid _start value */
823 memset(bitstr, 0, bitstr_size(nbits));
824 bit_foreach_unset_at(bitstr, nbits, nbits, unset_bit) {
825 atf_tc_fail("bit_foreach_unset_at_%d_%s_%s: "
826 "Failed at location %d",
827 nbits, "invalid_start", memloc, unset_bit);
830 /* Varying start location */
831 memset(bitstr, 0xAA, bitstr_size(nbits));
832 for (s = 0; s < nbits; s++) {
834 bit_foreach_unset_at(bitstr, s, nbits, unset_bit) {
835 ATF_REQUIRE_MSG(unset_bit == (i + (s + 1) / 2) * 2,
836 "bit_foreach_unset_at_%d_%s_%d_%s: "
837 "Failed on turn %d at location %d",
838 nbits, "vary_start", s, memloc, i, unset_bit);
841 ATF_REQUIRE_MSG(i == (nbits + 1) / 2 - (s + 1) / 2,
842 "bit_foreach_unset_at_%d_%s_%d_%s: "
843 "Invalid number of turns %d",
844 nbits, "vary_start", s, memloc, i);
847 /* Varying end location */
848 memset(bitstr, 0xAA, bitstr_size(nbits));
849 for (e = 0; e < nbits; e++) {
851 bit_foreach_unset_at(bitstr, 0, e, unset_bit) {
852 ATF_REQUIRE_MSG(unset_bit == i * 2,
853 "bit_foreach_unset_at_%d_%s_%d_%s: "
854 "Failed on turn %d at location %d",
855 nbits, "vary_end", e, memloc, i, unset_bit);
858 ATF_REQUIRE_MSG(i == (e + 1) / 2,
859 "bit_foreach_unset_at_%d_%s_%d_%s: "
860 "Invalid number of turns %d",
861 nbits, "vary_end", e, memloc, i);
868 ATF_TP_ADD_TC(tp, bitstr_in_struct);
869 ATF_TP_ADD_TC(tp, bitstr_size);
870 ATF_TP_ADD_TC(tp, bit_ffc_area);
871 ATF_TP_ADD_TC(tp, bit_ffs_area);
872 BITSTRING_TC_ADD(tp, bit_set);
873 BITSTRING_TC_ADD(tp, bit_clear);
874 BITSTRING_TC_ADD(tp, bit_ffs);
875 BITSTRING_TC_ADD(tp, bit_ffc);
876 BITSTRING_TC_ADD(tp, bit_ffs_at);
877 BITSTRING_TC_ADD(tp, bit_ffc_at);
878 BITSTRING_TC_ADD(tp, bit_nclear);
879 BITSTRING_TC_ADD(tp, bit_nset);
880 BITSTRING_TC_ADD(tp, bit_count);
881 BITSTRING_TC_ADD(tp, bit_ffs_area_at_all_or_nothing);
882 BITSTRING_TC_ADD(tp, bit_ffc_area_at_all_or_nothing);
883 BITSTRING_TC_ADD(tp, bit_foreach);
884 BITSTRING_TC_ADD(tp, bit_foreach_at);
885 BITSTRING_TC_ADD(tp, bit_foreach_unset);
886 BITSTRING_TC_ADD(tp, bit_foreach_unset_at);
888 return (atf_no_error());