# HG changeset patch # User Akim Demaille # Date 1543380981 -3600 # Node ID e562c3b5b9c41ecd92eec7613fb68eba94611193 # Parent ad2f51bb31a0dfa87ef91b285d06b9adf667f739 bitset: check the operations * tests/test-bitset.c (bitset_random): New. Use it. * lib/bitset/expandable.c (ebitset_not): Fix typo. diff -r ad2f51bb31a0 -r e562c3b5b9c4 ChangeLog --- a/ChangeLog Wed Nov 28 21:55:00 2018 +0100 +++ b/ChangeLog Wed Nov 28 05:56:21 2018 +0100 @@ -1,3 +1,10 @@ +2018-11-28 Akim Demaille + + bitset: check the operations + * tests/test-bitset.c (bitset_random): New. + Use it. + * lib/bitset/expandable.c (ebitset_not): Fix typo. + 2018-11-28 Akim Demaille bitset: properly use false/true instead of 0/1 for Booleans diff -r ad2f51bb31a0 -r e562c3b5b9c4 lib/bitset/expandable.c --- a/lib/bitset/expandable.c Wed Nov 28 21:55:00 2018 +0100 +++ b/lib/bitset/expandable.c Wed Nov 28 05:56:21 2018 +0100 @@ -844,7 +844,7 @@ /* Create new elements for dst if they cannot be found or substitute zero elements if src elements not found. */ ebitset_elt *selt = - ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_SUBST); + ebitset_elt_find (src, j * EBITSET_ELT_BITS, EBITSET_SUBST); ebitset_elt *delt = ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE); diff -r ad2f51bb31a0 -r e562c3b5b9c4 tests/test-bitset.c --- a/tests/test-bitset.c Wed Nov 28 21:55:00 2018 +0100 +++ b/tests/test-bitset.c Wed Nov 28 05:56:21 2018 +0100 @@ -20,6 +20,102 @@ #include "macros.h" +#define RANDOM(n) (rand () % (n)) + +static +void assert_bitset_equal (bitset bs1, bitset bs2) +{ + debug_bitset (bs1); + debug_bitset (bs2); + ASSERT (bitset_size (bs1) == bitset_size (bs2)); + for (bitset_bindex i = 0; i < bitset_size (bs1); ++i) + ASSERT (bitset_test (bs1, i) == bitset_test (bs2, i)); +} + +void bitset_random (bitset bs) +{ + for (bitset_bindex i = 0; i < bitset_size (bs); ++i) + bitset_set (bs, RANDOM (2)); +} + +void compare (enum bitset_attr a, enum bitset_attr b) +{ + const int nbits = RANDOM (256); + + bitset asrc0 = bitset_create (nbits, a); + bitset_random (asrc0); + bitset asrc1 = bitset_create (nbits, a); + bitset_random (asrc1); + bitset asrc2 = bitset_create (nbits, a); + bitset_random (asrc2); + bitset asrc3 = bitset_create (nbits, a); + bitset_random (asrc3); + bitset adst = bitset_create (nbits, a); + + bitset bsrc0 = bitset_create (nbits, b); + bitset_copy (bsrc0, asrc0); + bitset bsrc1 = bitset_create (nbits, b); + bitset_copy (bsrc1, asrc1); + bitset bsrc2 = bitset_create (nbits, b); + bitset_copy (bsrc2, asrc2); + bitset bsrc3 = bitset_create (nbits, b); + bitset_copy (bsrc3, asrc3); + bitset bdst = bitset_create (nbits, b); + + bitset_not (adst, asrc0); + bitset_not (bdst, bsrc0); + assert_bitset_equal (adst, bdst); + + bitset_and (adst, asrc0, asrc1); + bitset_and (bdst, bsrc0, bsrc1); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_and_cmp (adst, asrc0, asrc1) + == bitset_and_cmp (bdst, bsrc0, bsrc1)); + assert_bitset_equal (adst, bdst); + + bitset_andn (adst, asrc0, asrc1); + bitset_andn (bdst, bsrc0, bsrc1); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_andn_cmp (adst, asrc0, asrc1) + == bitset_andn_cmp (bdst, bsrc0, bsrc1)); + assert_bitset_equal (adst, bdst); + + bitset_or (adst, asrc0, asrc1); + bitset_or (bdst, bsrc0, bsrc1); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_or_cmp (adst, asrc0, asrc1) + == bitset_or_cmp (bdst, bsrc0, bsrc1)); + assert_bitset_equal (adst, bdst); + + bitset_xor (adst, asrc0, asrc1); + bitset_xor (bdst, bsrc0, bsrc1); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_xor_cmp (adst, asrc0, asrc1) + == bitset_xor_cmp (bdst, bsrc0, bsrc1)); + assert_bitset_equal (adst, bdst); + + bitset_and_or (adst, asrc0, asrc1, asrc2); + bitset_and_or (bdst, bsrc0, bsrc1, bsrc2); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_and_or_cmp (adst, asrc0, asrc1, asrc2) + == bitset_and_or_cmp (bdst, bsrc0, bsrc1, bsrc2)); + assert_bitset_equal (adst, bdst); + + bitset_andn_or (adst, asrc0, asrc1, asrc2); + bitset_andn_or (bdst, bsrc0, bsrc1, bsrc2); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_andn_or_cmp (adst, asrc0, asrc1, asrc2) + == bitset_andn_or_cmp (bdst, bsrc0, bsrc1, bsrc2)); + assert_bitset_equal (adst, bdst); + + bitset_or_and (adst, asrc0, asrc1, asrc2); + bitset_or_and (bdst, bsrc0, bsrc1, bsrc2); + assert_bitset_equal (adst, bdst); + ASSERT (bitset_or_and_cmp (adst, asrc0, asrc1, asrc2) + == bitset_or_and_cmp (bdst, bsrc0, bsrc1, bsrc2)); + assert_bitset_equal (adst, bdst); +} + void check_attributes (enum bitset_attr attr) { enum { nbits = 32 }; @@ -62,5 +158,12 @@ check_attributes (BITSET_SPARSE); check_attributes (BITSET_FRUGAL); check_attributes (BITSET_GREEDY); + + compare (BITSET_FIXED, BITSET_FIXED); + compare (BITSET_FIXED, BITSET_VARIABLE); + compare (BITSET_FIXED, BITSET_DENSE); + compare (BITSET_FIXED, BITSET_SPARSE); + compare (BITSET_FIXED, BITSET_FRUGAL); + compare (BITSET_FIXED, BITSET_GREEDY); return 0; }