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.
33 #include <sys/param.h>
35 #include <bitstring.h>
40 typedef void (testfunc_t)(bitstr_t *bstr, int nbits, const char *memloc);
43 bitstring_run_stack_test(testfunc_t *test, int nbits)
45 bitstr_t bit_decl(bitstr, nbits);
47 test(bitstr, nbits, "stack");
51 bitstring_run_heap_test(testfunc_t *test, int nbits)
53 bitstr_t *bitstr = bit_alloc(nbits);
55 test(bitstr, nbits, "heap");
59 bitstring_test_runner(testfunc_t *test)
61 const int bitstr_sizes[] = {
73 for (unsigned long i = 0; i < nitems(bitstr_sizes); i++) {
74 bitstring_run_stack_test(test, bitstr_sizes[i]);
75 bitstring_run_heap_test(test, bitstr_sizes[i]);
79 #define BITSTRING_TC_DEFINE(name) \
80 ATF_TC_WITHOUT_HEAD(name); \
81 static testfunc_t name ## _test; \
83 ATF_TC_BODY(name, tc) \
85 bitstring_test_runner(name ## _test); \
89 name ## _test(bitstr_t *bitstr, int nbits, const char *memloc)
91 #define BITSTRING_TC_ADD(tp, name) \
93 ATF_TP_ADD_TC(tp, name); \
96 ATF_TC_WITHOUT_HEAD(bitstr_in_struct);
97 ATF_TC_BODY(bitstr_in_struct, tc)
99 struct bitstr_containing_struct {
100 bitstr_t bit_decl(bitstr, 8);
103 bit_nclear(test_struct.bitstr, 0, 8);
106 ATF_TC_WITHOUT_HEAD(bitstr_size);
107 ATF_TC_BODY(bitstr_size, tc)
109 size_t sob = sizeof(bitstr_t);
111 ATF_CHECK_EQ(0, bitstr_size(0));
112 ATF_CHECK_EQ(sob, bitstr_size(1));
113 ATF_CHECK_EQ(sob, bitstr_size(sob * 8));
114 ATF_CHECK_EQ(2 * sob, bitstr_size(sob * 8 + 1));
117 BITSTRING_TC_DEFINE(bit_set)
118 /* bitstr_t *bitstr, int nbits, const char *memloc */
120 memset(bitstr, 0, bitstr_size(nbits));
122 for (int i = 0; i < nbits; i++) {
125 for (int j = 0; j < nbits; j++) {
126 ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 1 : 0,
127 "bit_set_%d_%s: Failed on bit %d",
131 bit_clear(bitstr, i);
135 BITSTRING_TC_DEFINE(bit_clear)
136 /* bitstr_t *bitstr, int nbits, const char *memloc */
140 memset(bitstr, 0xFF, bitstr_size(nbits));
141 for (i = 0; i < nbits; i++) {
142 bit_clear(bitstr, i);
144 for (j = 0; j < nbits; j++) {
145 ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 0 : 1,
146 "bit_clear_%d_%s: Failed on bit %d",
154 BITSTRING_TC_DEFINE(bit_ffs)
155 /* bitstr_t *bitstr, int nbits, const char *memloc */
160 memset(bitstr, 0, bitstr_size(nbits));
161 bit_ffs(bitstr, nbits, &found_set_bit);
162 ATF_REQUIRE_MSG(found_set_bit == -1,
163 "bit_ffs_%d_%s: Failed all clear bits.", nbits, memloc);
165 for (i = 0; i < nbits; i++) {
166 memset(bitstr, 0xFF, bitstr_size(nbits));
168 bit_nclear(bitstr, 0, i - 1);
170 bit_ffs(bitstr, nbits, &found_set_bit);
171 ATF_REQUIRE_MSG(found_set_bit == i,
172 "bit_ffs_%d_%s: Failed on bit %d, Result %d",
173 nbits, memloc, i, found_set_bit);
177 BITSTRING_TC_DEFINE(bit_ffc)
178 /* bitstr_t *bitstr, int nbits, const char *memloc */
183 memset(bitstr, 0xFF, bitstr_size(nbits));
184 bit_ffc(bitstr, nbits, &found_clear_bit);
185 ATF_REQUIRE_MSG(found_clear_bit == -1,
186 "bit_ffc_%d_%s: Failed all set bits.", nbits, memloc);
188 for (i = 0; i < nbits; i++) {
189 memset(bitstr, 0, bitstr_size(nbits));
191 bit_nset(bitstr, 0, i - 1);
193 bit_ffc(bitstr, nbits, &found_clear_bit);
194 ATF_REQUIRE_MSG(found_clear_bit == i,
195 "bit_ffc_%d_%s: Failed on bit %d, Result %d",
196 nbits, memloc, i, found_clear_bit);
200 BITSTRING_TC_DEFINE(bit_ffs_at)
201 /* bitstr_t *bitstr, int nbits, const char *memloc */
206 memset(bitstr, 0xFF, bitstr_size(nbits));
207 for (i = 0; i < nbits; i++) {
208 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
209 ATF_REQUIRE_MSG(found_set_bit == i,
210 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
211 nbits, memloc, i, found_set_bit);
214 memset(bitstr, 0, bitstr_size(nbits));
215 for (i = 0; i < nbits; i++) {
216 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
217 ATF_REQUIRE_MSG(found_set_bit == -1,
218 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
219 nbits, memloc, i, found_set_bit);
222 memset(bitstr, 0x55, bitstr_size(nbits));
223 for (i = 0; i < nbits; i++) {
224 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
225 if (i == nbits - 1 && (nbits & 1) == 0) {
226 ATF_REQUIRE_MSG(found_set_bit == -1,
227 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
228 nbits, memloc, i, found_set_bit);
230 ATF_REQUIRE_MSG(found_set_bit == i + (i & 1),
231 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
232 nbits, memloc, i, found_set_bit);
236 memset(bitstr, 0xAA, bitstr_size(nbits));
237 for (i = 0; i < nbits; i++) {
238 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
239 if (i == nbits - 1 && (nbits & 1) != 0) {
240 ATF_REQUIRE_MSG(found_set_bit == -1,
241 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
242 nbits, memloc, i, found_set_bit);
245 found_set_bit == i + ((i & 1) ? 0 : 1),
246 "bit_ffs_at_%d_%s: Failed on bit %d, Result %d",
247 nbits, memloc, i, found_set_bit);
251 /* Pass a start value beyond the size of the bit string */
252 bit_ffs_at(bitstr, nbits, nbits, &found_set_bit);
253 ATF_REQUIRE_MSG(found_set_bit == -1,
254 "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
255 nbits, memloc, nbits, found_set_bit);
257 bit_ffs_at(bitstr, nbits + 3, nbits, &found_set_bit);
258 ATF_REQUIRE_MSG(found_set_bit == -1,
259 "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d",
260 nbits, memloc, nbits + 3, found_set_bit);
263 BITSTRING_TC_DEFINE(bit_ffc_at)
264 /* bitstr_t *bitstr, int nbits, const char *memloc */
266 int i, found_clear_bit;
268 memset(bitstr, 0, bitstr_size(nbits));
269 for (i = 0; i < nbits; i++) {
270 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
271 ATF_REQUIRE_MSG(found_clear_bit == i,
272 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
273 nbits, memloc, i, found_clear_bit);
276 memset(bitstr, 0xFF, bitstr_size(nbits));
277 for (i = 0; i < nbits; i++) {
278 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
279 ATF_REQUIRE_MSG(found_clear_bit == -1,
280 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
281 nbits, memloc, i, found_clear_bit);
284 memset(bitstr, 0x55, bitstr_size(nbits));
285 for (i = 0; i < nbits; i++) {
286 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
287 if (i == nbits - 1 && (nbits & 1) != 0) {
288 ATF_REQUIRE_MSG(found_clear_bit == -1,
289 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
290 nbits, memloc, i, found_clear_bit);
293 found_clear_bit == i + ((i & 1) ? 0 : 1),
294 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
295 nbits, memloc, i, found_clear_bit);
299 memset(bitstr, 0xAA, bitstr_size(nbits));
300 for (i = 0; i < nbits; i++) {
301 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
302 if (i == nbits - 1 && (nbits & 1) == 0) {
303 ATF_REQUIRE_MSG(found_clear_bit == -1,
304 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
305 nbits, memloc, i, found_clear_bit);
307 ATF_REQUIRE_MSG(found_clear_bit == i + (i & 1),
308 "bit_ffc_at_%d_%s: Failed on bit %d, Result %d",
309 nbits, memloc, i, found_clear_bit);
313 /* Pass a start value beyond the size of the bit string */
314 bit_ffc_at(bitstr, nbits, nbits, &found_clear_bit);
315 ATF_REQUIRE_MSG(found_clear_bit == -1,
316 "bit_ffc_at_%d_%s: Failed with high start value, Result %d",
317 nbits, memloc, found_clear_bit);
319 bit_ffc_at(bitstr, nbits + 3, nbits, &found_clear_bit);
320 ATF_REQUIRE_MSG(found_clear_bit == -1,
321 "bit_ffc_at_%d_%s: Failed with high start value of %d, Result %d",
322 nbits, memloc, nbits + 3, found_clear_bit);
325 BITSTRING_TC_DEFINE(bit_ffc_area_at_all_or_nothing)
326 /* bitstr_t *bitstr, int nbits, const char *memloc */
330 memset(bitstr, 0, bitstr_size(nbits));
331 if (nbits % _BITSTR_BITS != 0)
332 bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
334 for (int start = 0; start < nbits; start++) {
335 for (int size = 1; size < nbits - start; size++) {
336 bit_ffc_area_at(bitstr, start, nbits, size, &found);
337 ATF_REQUIRE_EQ_MSG(start, found,
338 "bit_ffc_area_at_%d_%s: "
339 "Did not find %d clear bits at %d",
340 nbits, memloc, size, start);
344 memset(bitstr, 0xff, bitstr_size(nbits));
345 if (nbits % _BITSTR_BITS != 0)
346 bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
348 for (int start = 0; start < nbits; start++) {
349 for (int size = 1; size < nbits - start; size++) {
350 bit_ffc_area_at(bitstr, start, nbits, size, &found);
351 ATF_REQUIRE_EQ_MSG(-1, found,
352 "bit_ffc_area_at_%d_%s: "
353 "Found %d clear bits at %d",
354 nbits, memloc, size, start);
359 BITSTRING_TC_DEFINE(bit_ffs_area_at_all_or_nothing)
360 /* bitstr_t *bitstr, int nbits, const char *memloc */
364 memset(bitstr, 0, bitstr_size(nbits));
365 if (nbits % _BITSTR_BITS != 0)
366 bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
368 for (int start = 0; start < nbits; start++) {
369 for (int size = 1; size < nbits - start; size++) {
370 bit_ffs_area_at(bitstr, start, nbits, size, &found);
371 ATF_REQUIRE_EQ_MSG(-1, found,
372 "bit_ffs_area_at_%d_%s: "
373 "Found %d set bits at %d",
374 nbits, memloc, size, start);
378 memset(bitstr, 0xff, bitstr_size(nbits));
379 if (nbits % _BITSTR_BITS != 0)
380 bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1);
382 for (int start = 0; start < nbits; start++) {
383 for (int size = 1; size < nbits - start; size++) {
384 bit_ffs_area_at(bitstr, start, nbits, size, &found);
385 ATF_REQUIRE_EQ_MSG(start, found,
386 "bit_ffs_area_at_%d_%s: "
387 "Did not find %d set bits at %d",
388 nbits, memloc, size, start);
393 ATF_TC_WITHOUT_HEAD(bit_ffs_area);
394 ATF_TC_BODY(bit_ffs_area, tc)
396 const int nbits = 72;
397 bitstr_t bit_decl(bitstr, nbits);
400 memset(bitstr, 0, bitstr_size(nbits));
402 bit_nset(bitstr, 5, 6);
405 bit_ffs_area(bitstr, nbits, 3, &location);
406 ATF_REQUIRE_EQ_MSG(-1, location,
407 "bit_ffs_area: found location of size 3 when only 2 bits are set");
408 ATF_REQUIRE_EQ_MSG(0, bit_ntest(bitstr, 5, 7, 1),
409 "bit_ntest: found location of size 3 when only 2 bits are set");
414 bit_ffs_area(bitstr, nbits, 3, &location);
415 ATF_REQUIRE_EQ_MSG(5, location,
416 "bit_ffs_area: failed to find location of size 3 %d", location);
417 ATF_REQUIRE_EQ_MSG(1, bit_ntest(bitstr, 5, 7, 1),
418 "bit_ntest: failed to find all 3 bits set");
423 bit_ffs_area(bitstr, nbits, 3, &location);
424 ATF_REQUIRE_EQ_MSG(5, location,
425 "bit_ffs_area: failed to find location of size 3");
428 bit_ffs_area_at(bitstr, 2, nbits, 3, &location);
429 ATF_REQUIRE_EQ_MSG(5, location,
430 "bit_ffs_area_at: failed to find location of size 3");
433 bit_ffs_area_at(bitstr, 6, nbits, 3, &location);
434 ATF_REQUIRE_EQ_MSG(6, location,
435 "bit_ffs_area_at: failed to find location of size 3");
438 bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
439 ATF_REQUIRE_EQ_MSG(-1, location,
440 "bit_ffs_area_at: found invalid location");
442 bit_nset(bitstr, 69, 71);
445 bit_ffs_area_at(bitstr, 8, nbits, 3, &location);
446 ATF_REQUIRE_EQ_MSG(69, location,
447 "bit_ffs_area_at: failed to find location of size 3");
450 bit_ffs_area_at(bitstr, 69, nbits, 3, &location);
451 ATF_REQUIRE_EQ_MSG(69, location,
452 "bit_ffs_area_at: failed to find location of size 3");
455 bit_ffs_area_at(bitstr, 70, nbits, 3, &location);
456 ATF_REQUIRE_EQ_MSG(-1, location,
457 "bit_ffs_area_at: found invalid location");
460 bit_ffs_area_at(bitstr, 72, nbits, 3, &location);
461 ATF_REQUIRE_EQ_MSG(-1, location,
462 "bit_ffs_area_at: found invalid location");
464 bit_nset(bitstr, 59, 67);
467 bit_ffs_area(bitstr, nbits, 9, &location);
468 ATF_REQUIRE_EQ_MSG(59, location,
469 "bit_ffs_area: failed to find location of size 9");
472 bit_ffs_area(bitstr, nbits, 10, &location);
473 ATF_REQUIRE_EQ_MSG(-1, location,
474 "bit_ffs_area: found invalid location");
477 ATF_TC_WITHOUT_HEAD(bit_ffc_area);
478 ATF_TC_BODY(bit_ffc_area, tc)
480 const int nbits = 80;
481 bitstr_t bit_decl(bitstr, nbits);
485 memset(bitstr, 0xFF, bitstr_size(nbits));
487 bit_clear(bitstr, 7);
488 bit_clear(bitstr, 8);
491 bit_ffc_area(bitstr, nbits, 3, &location);
492 ATF_REQUIRE_EQ_MSG(-1, location,
493 "bit_ffc_area: found location of size 3 when only 2 bits are set");
495 bit_clear(bitstr, 9);
498 bit_ffc_area(bitstr, nbits, 3, &location);
499 ATF_REQUIRE_EQ_MSG(7, location,
500 "bit_ffc_area: failed to find location of size 3");
502 bit_clear(bitstr, 10);
505 bit_ffc_area(bitstr, nbits, 3, &location);
506 ATF_REQUIRE_EQ_MSG(7, location,
507 "bit_ffc_area: failed to find location of size 3");
510 bit_ffc_area_at(bitstr, 2, nbits, 3, &location);
511 ATF_REQUIRE_EQ_MSG(7, location,
512 "bit_ffc_area_at: failed to find location of size 3");
515 bit_ffc_area_at(bitstr, 8, nbits, 3, &location);
516 ATF_REQUIRE_EQ_MSG(8, location,
517 "bit_ffc_area_at: failed to find location of size 3");
520 bit_ffc_area_at(bitstr, 9, nbits, 3, &location);
521 ATF_REQUIRE_EQ_MSG(-1, location,
522 "bit_ffc_area_at: found invalid bit location");
524 bit_clear(bitstr, 77);
525 bit_clear(bitstr, 78);
526 bit_clear(bitstr, 79);
529 bit_ffc_area_at(bitstr, 12, nbits, 3, &location);
530 ATF_REQUIRE_EQ_MSG(77, location,
531 "bit_ffc_area_at: failed to find location of size 3");
534 bit_ffc_area_at(bitstr, 77, nbits, 3, &location);
535 ATF_REQUIRE_EQ_MSG(77, location,
536 "bit_ffc_area_at: failed to find location of size 3");
539 bit_ffc_area_at(bitstr, 78, nbits, 3, &location);
540 ATF_REQUIRE_EQ_MSG(-1, location,
541 "bit_ffc_area_at: found invalid location");
544 bit_ffc_area_at(bitstr, 85, nbits, 3, &location);
545 ATF_REQUIRE_EQ_MSG(-1, location,
546 "bit_ffc_area_at: found invalid location");
549 BITSTRING_TC_DEFINE(bit_nclear)
550 /* bitstr_t *bitstr, int nbits, const char *memloc */
556 for (i = 0; i < nbits; i++) {
557 for (j = i; j < nbits; j++) {
558 memset(bitstr, 0xFF, bitstr_size(nbits));
559 bit_nclear(bitstr, i, j);
561 bit_ffc(bitstr, nbits, &found_clear_bit);
563 found_clear_bit == i,
564 "bit_nclear_%d_%d_%d%s: Failed with result %d",
565 nbits, i, j, memloc, found_clear_bit);
567 bit_ffs_at(bitstr, i, nbits, &found_set_bit);
569 (j + 1 < nbits) ? found_set_bit == j + 1 : -1,
570 "bit_nset_%d_%d_%d%s: Failed with result %d",
571 nbits, i, j, memloc, found_set_bit);
576 BITSTRING_TC_DEFINE(bit_nset)
577 /* bitstr_t *bitstr, int nbits, const char *memloc */
583 for (i = 0; i < nbits; i++) {
584 for (j = i; j < nbits; j++) {
585 memset(bitstr, 0, bitstr_size(nbits));
586 bit_nset(bitstr, i, j);
588 bit_ffs(bitstr, nbits, &found_set_bit);
591 "bit_nset_%d_%d_%d%s: Failed with result %d",
592 nbits, i, j, memloc, found_set_bit);
594 bit_ffc_at(bitstr, i, nbits, &found_clear_bit);
596 (j + 1 < nbits) ? found_clear_bit == j + 1 : -1,
597 "bit_nset_%d_%d_%d%s: Failed with result %d",
598 nbits, i, j, memloc, found_clear_bit);
603 BITSTRING_TC_DEFINE(bit_count)
604 /* bitstr_t *bitstr, int nbits, const char *memloc */
606 int result, s, e, expected;
609 memset(bitstr, 0, bitstr_size(nbits));
610 bit_count(bitstr, 0, nbits, &result);
611 ATF_CHECK_MSG(0 == result,
612 "bit_count_%d_%s_%s: Failed with result %d",
613 nbits, "clear", memloc, result);
616 memset(bitstr, 0xFF, bitstr_size(nbits));
617 bit_count(bitstr, 0, nbits, &result);
618 ATF_CHECK_MSG(nbits == result,
619 "bit_count_%d_%s_%s: Failed with result %d",
620 nbits, "set", memloc, result);
622 /* Invalid _start value */
623 memset(bitstr, 0xFF, bitstr_size(nbits));
624 bit_count(bitstr, nbits, nbits, &result);
625 ATF_CHECK_MSG(0 == result,
626 "bit_count_%d_%s_%s: Failed with result %d",
627 nbits, "invalid_start", memloc, result);
629 /* Alternating bitstr, starts with 0 */
630 memset(bitstr, 0xAA, bitstr_size(nbits));
631 bit_count(bitstr, 0, nbits, &result);
632 ATF_CHECK_MSG(nbits / 2 == result,
633 "bit_count_%d_%s_%d_%s: Failed with result %d",
634 nbits, "alternating", 0, memloc, result);
636 /* Alternating bitstr, starts with 1 */
637 memset(bitstr, 0x55, bitstr_size(nbits));
638 bit_count(bitstr, 0, nbits, &result);
639 ATF_CHECK_MSG((nbits + 1) / 2 == result,
640 "bit_count_%d_%s_%d_%s: Failed with result %d",
641 nbits, "alternating", 1, memloc, result);
643 /* Varying start location */
644 memset(bitstr, 0xAA, bitstr_size(nbits));
645 for (s = 0; s < nbits; s++) {
646 expected = s % 2 == 0 ? (nbits - s) / 2 : (nbits - s + 1) / 2;
647 bit_count(bitstr, s, nbits, &result);
648 ATF_CHECK_MSG(expected == result,
649 "bit_count_%d_%s_%d_%s: Failed with result %d",
650 nbits, "vary_start", s, memloc, result);
653 /* Varying end location */
654 memset(bitstr, 0xAA, bitstr_size(nbits));
655 for (e = 0; e < nbits; e++) {
656 bit_count(bitstr, 0, e, &result);
657 ATF_CHECK_MSG(e / 2 == result,
658 "bit_count_%d_%s_%d_%s: Failed with result %d",
659 nbits, "vary_end", e, memloc, result);
664 BITSTRING_TC_DEFINE(bit_foreach)
665 /* bitstr_t *bitstr, int nbits, const char *memloc */
670 memset(bitstr, 0x00, bitstr_size(nbits));
671 bit_foreach (bitstr, nbits, set_bit) {
672 atf_tc_fail("bit_foreach_%d_%s_%s: Failed at location %d",
673 nbits, "clear", memloc, set_bit);
678 memset(bitstr, 0xFF, bitstr_size(nbits));
679 bit_foreach(bitstr, nbits, set_bit) {
680 ATF_REQUIRE_MSG(set_bit == i,
681 "bit_foreach_%d_%s_%s: Failed on turn %d at location %d",
682 nbits, "set", memloc, i, set_bit);
685 ATF_REQUIRE_MSG(i == nbits,
686 "bit_foreach_%d_%s_%s: Invalid number of turns %d",
687 nbits, "set", memloc, i);
689 /* Alternating bitstr, starts with 0 */
691 memset(bitstr, 0xAA, bitstr_size(nbits));
692 bit_foreach(bitstr, nbits, set_bit) {
693 ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
694 "bit_foreach_%d_%s_%d_%s: "
695 "Failed on turn %d at location %d",
696 nbits, "alternating", 0, memloc, i, set_bit);
699 ATF_REQUIRE_MSG(i == nbits / 2,
700 "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
701 nbits, "alternating", 0, memloc, i);
703 /* Alternating bitstr, starts with 1 */
705 memset(bitstr, 0x55, bitstr_size(nbits));
706 bit_foreach(bitstr, nbits, set_bit) {
707 ATF_REQUIRE_MSG(set_bit == i * 2,
708 "bit_foreach_%d_%s_%d_%s: "
709 "Failed on turn %d at location %d",
710 nbits, "alternating", 1, memloc, i, set_bit);
713 ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
714 "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d",
715 nbits, "alternating", 1, memloc, i);
718 BITSTRING_TC_DEFINE(bit_foreach_at)
719 /* bitstr_t *bitstr, int nbits, const char *memloc */
721 int i, s, e, set_bit;
723 /* Invalid _start value */
724 memset(bitstr, 0xFF, bitstr_size(nbits));
725 bit_foreach_at(bitstr, nbits, nbits, set_bit) {
726 atf_tc_fail("bit_foreach_at_%d_%s_%s: Failed at location %d",
727 nbits, "invalid_start", memloc, set_bit);
730 /* Varying start location */
731 memset(bitstr, 0xAA, bitstr_size(nbits));
732 for (s = 0; s < nbits; s++) {
734 bit_foreach_at(bitstr, s, nbits, set_bit) {
735 ATF_REQUIRE_MSG(set_bit == (i + s / 2) * 2 + 1,
736 "bit_foreach_at_%d_%s_%d_%s: "
737 "Failed on turn %d at location %d",
738 nbits, "vary_start", s, memloc, i, set_bit);
741 ATF_REQUIRE_MSG(i == nbits / 2 - s / 2,
742 "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
743 nbits, "vary_start", s, memloc, i);
746 /* Varying end location */
747 memset(bitstr, 0xAA, bitstr_size(nbits));
748 for (e = 0; e < nbits; e++) {
750 bit_foreach_at(bitstr, 0, e, set_bit) {
751 ATF_REQUIRE_MSG(set_bit == i * 2 + 1,
752 "bit_foreach_at_%d_%s_%d_%s: "
753 "Failed on turn %d at location %d",
754 nbits, "vary_end", e, memloc, i, set_bit);
757 ATF_REQUIRE_MSG(i == e / 2,
758 "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d",
759 nbits, "vary_end", e, memloc, i);
763 BITSTRING_TC_DEFINE(bit_foreach_unset)
764 /* bitstr_t *bitstr, int nbits, const char *memloc */
770 memset(bitstr, 0, bitstr_size(nbits));
771 bit_foreach_unset(bitstr, nbits, unset_bit) {
772 ATF_REQUIRE_MSG(unset_bit == i,
773 "bit_foreach_unset_%d_%s_%s: "
774 "Failed on turn %d at location %d",
775 nbits, "clear", memloc, i, unset_bit);
778 ATF_REQUIRE_MSG(i == nbits,
779 "bit_foreach_unset_%d_%s_%s: Invalid number of turns %d",
780 nbits, "set", memloc, i);
783 memset(bitstr, 0xFF, bitstr_size(nbits));
784 bit_foreach_unset(bitstr, nbits, unset_bit) {
785 atf_tc_fail("bit_foreach_unset_%d_%s_%s: "
786 "Failed at location %d",
787 nbits, "set", memloc, unset_bit);
790 /* Alternating bitstr, starts with 0 */
792 memset(bitstr, 0xAA, bitstr_size(nbits));
793 bit_foreach_unset(bitstr, nbits, unset_bit) {
794 ATF_REQUIRE_MSG(unset_bit == i * 2,
795 "bit_foreach_unset_%d_%s_%d_%s: "
796 "Failed on turn %d at location %d",
797 nbits, "alternating", 0, memloc, i, unset_bit);
800 ATF_REQUIRE_MSG(i == (nbits + 1) / 2,
801 "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
802 nbits, "alternating", 0, memloc, i);
804 /* Alternating bitstr, starts with 1 */
806 memset(bitstr, 0x55, bitstr_size(nbits));
807 bit_foreach_unset(bitstr, nbits, unset_bit) {
808 ATF_REQUIRE_MSG(unset_bit == i * 2 + 1,
809 "bit_foreach_unset_%d_%s_%d_%s: "
810 "Failed on turn %d at location %d",
811 nbits, "alternating", 1, memloc, i, unset_bit);
814 ATF_REQUIRE_MSG(i == nbits / 2,
815 "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d",
816 nbits, "alternating", 1, memloc, i);
819 BITSTRING_TC_DEFINE(bit_foreach_unset_at)
820 /* bitstr_t *bitstr, int nbits, const char *memloc */
822 int i, s, e, unset_bit;
824 /* Invalid _start value */
825 memset(bitstr, 0, bitstr_size(nbits));
826 bit_foreach_unset_at(bitstr, nbits, nbits, unset_bit) {
827 atf_tc_fail("bit_foreach_unset_at_%d_%s_%s: "
828 "Failed at location %d",
829 nbits, "invalid_start", memloc, unset_bit);
832 /* Varying start location */
833 memset(bitstr, 0xAA, bitstr_size(nbits));
834 for (s = 0; s < nbits; s++) {
836 bit_foreach_unset_at(bitstr, s, nbits, unset_bit) {
837 ATF_REQUIRE_MSG(unset_bit == (i + (s + 1) / 2) * 2,
838 "bit_foreach_unset_at_%d_%s_%d_%s: "
839 "Failed on turn %d at location %d",
840 nbits, "vary_start", s, memloc, i, unset_bit);
843 ATF_REQUIRE_MSG(i == (nbits + 1) / 2 - (s + 1) / 2,
844 "bit_foreach_unset_at_%d_%s_%d_%s: "
845 "Invalid number of turns %d",
846 nbits, "vary_start", s, memloc, i);
849 /* Varying end location */
850 memset(bitstr, 0xAA, bitstr_size(nbits));
851 for (e = 0; e < nbits; e++) {
853 bit_foreach_unset_at(bitstr, 0, e, unset_bit) {
854 ATF_REQUIRE_MSG(unset_bit == i * 2,
855 "bit_foreach_unset_at_%d_%s_%d_%s: "
856 "Failed on turn %d at location %d",
857 nbits, "vary_end", e, memloc, i, unset_bit);
860 ATF_REQUIRE_MSG(i == (e + 1) / 2,
861 "bit_foreach_unset_at_%d_%s_%d_%s: "
862 "Invalid number of turns %d",
863 nbits, "vary_end", e, memloc, i);
870 ATF_TP_ADD_TC(tp, bitstr_in_struct);
871 ATF_TP_ADD_TC(tp, bitstr_size);
872 ATF_TP_ADD_TC(tp, bit_ffc_area);
873 ATF_TP_ADD_TC(tp, bit_ffs_area);
874 BITSTRING_TC_ADD(tp, bit_set);
875 BITSTRING_TC_ADD(tp, bit_clear);
876 BITSTRING_TC_ADD(tp, bit_ffs);
877 BITSTRING_TC_ADD(tp, bit_ffc);
878 BITSTRING_TC_ADD(tp, bit_ffs_at);
879 BITSTRING_TC_ADD(tp, bit_ffc_at);
880 BITSTRING_TC_ADD(tp, bit_nclear);
881 BITSTRING_TC_ADD(tp, bit_nset);
882 BITSTRING_TC_ADD(tp, bit_count);
883 BITSTRING_TC_ADD(tp, bit_ffs_area_at_all_or_nothing);
884 BITSTRING_TC_ADD(tp, bit_ffc_area_at_all_or_nothing);
885 BITSTRING_TC_ADD(tp, bit_foreach);
886 BITSTRING_TC_ADD(tp, bit_foreach_at);
887 BITSTRING_TC_ADD(tp, bit_foreach_unset);
888 BITSTRING_TC_ADD(tp, bit_foreach_unset_at);
890 return (atf_no_error());