2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23 #define RANDOM(n) (rand () % (n))
26 void assert_bitset_equal (bitset bs1
, bitset bs2
)
30 ASSERT (bitset_size (bs1
) == bitset_size (bs2
));
31 for (bitset_bindex i
= 0; i
< bitset_size (bs1
); ++i
)
32 ASSERT (bitset_test (bs1
, i
) == bitset_test (bs2
, i
));
35 void bitset_random (bitset bs
)
37 for (bitset_bindex i
= 0; i
< bitset_size (bs
); ++i
)
38 bitset_set (bs
, RANDOM (2));
41 void compare (enum bitset_attr a
, enum bitset_attr b
)
43 const int nbits
= RANDOM (256);
45 bitset asrc0
= bitset_create (nbits
, a
);
46 bitset_random (asrc0
);
47 bitset asrc1
= bitset_create (nbits
, a
);
48 bitset_random (asrc1
);
49 bitset asrc2
= bitset_create (nbits
, a
);
50 bitset_random (asrc2
);
51 bitset asrc3
= bitset_create (nbits
, a
);
52 bitset_random (asrc3
);
53 bitset adst
= bitset_create (nbits
, a
);
55 bitset bsrc0
= bitset_create (nbits
, b
);
56 bitset_copy (bsrc0
, asrc0
);
57 bitset bsrc1
= bitset_create (nbits
, b
);
58 bitset_copy (bsrc1
, asrc1
);
59 bitset bsrc2
= bitset_create (nbits
, b
);
60 bitset_copy (bsrc2
, asrc2
);
61 bitset bsrc3
= bitset_create (nbits
, b
);
62 bitset_copy (bsrc3
, asrc3
);
63 bitset bdst
= bitset_create (nbits
, b
);
65 bitset_not (adst
, asrc0
);
66 bitset_not (bdst
, bsrc0
);
67 assert_bitset_equal (adst
, bdst
);
69 bitset_and (adst
, asrc0
, asrc1
);
70 bitset_and (bdst
, bsrc0
, bsrc1
);
71 assert_bitset_equal (adst
, bdst
);
72 ASSERT (bitset_and_cmp (adst
, asrc0
, asrc1
)
73 == bitset_and_cmp (bdst
, bsrc0
, bsrc1
));
74 assert_bitset_equal (adst
, bdst
);
76 bitset_andn (adst
, asrc0
, asrc1
);
77 bitset_andn (bdst
, bsrc0
, bsrc1
);
78 assert_bitset_equal (adst
, bdst
);
79 ASSERT (bitset_andn_cmp (adst
, asrc0
, asrc1
)
80 == bitset_andn_cmp (bdst
, bsrc0
, bsrc1
));
81 assert_bitset_equal (adst
, bdst
);
83 bitset_or (adst
, asrc0
, asrc1
);
84 bitset_or (bdst
, bsrc0
, bsrc1
);
85 assert_bitset_equal (adst
, bdst
);
86 ASSERT (bitset_or_cmp (adst
, asrc0
, asrc1
)
87 == bitset_or_cmp (bdst
, bsrc0
, bsrc1
));
88 assert_bitset_equal (adst
, bdst
);
90 bitset_xor (adst
, asrc0
, asrc1
);
91 bitset_xor (bdst
, bsrc0
, bsrc1
);
92 assert_bitset_equal (adst
, bdst
);
93 ASSERT (bitset_xor_cmp (adst
, asrc0
, asrc1
)
94 == bitset_xor_cmp (bdst
, bsrc0
, bsrc1
));
95 assert_bitset_equal (adst
, bdst
);
97 bitset_and_or (adst
, asrc0
, asrc1
, asrc2
);
98 bitset_and_or (bdst
, bsrc0
, bsrc1
, bsrc2
);
99 assert_bitset_equal (adst
, bdst
);
100 ASSERT (bitset_and_or_cmp (adst
, asrc0
, asrc1
, asrc2
)
101 == bitset_and_or_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
102 assert_bitset_equal (adst
, bdst
);
104 bitset_andn_or (adst
, asrc0
, asrc1
, asrc2
);
105 bitset_andn_or (bdst
, bsrc0
, bsrc1
, bsrc2
);
106 assert_bitset_equal (adst
, bdst
);
107 ASSERT (bitset_andn_or_cmp (adst
, asrc0
, asrc1
, asrc2
)
108 == bitset_andn_or_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
109 assert_bitset_equal (adst
, bdst
);
111 bitset_or_and (adst
, asrc0
, asrc1
, asrc2
);
112 bitset_or_and (bdst
, bsrc0
, bsrc1
, bsrc2
);
113 assert_bitset_equal (adst
, bdst
);
114 ASSERT (bitset_or_and_cmp (adst
, asrc0
, asrc1
, asrc2
)
115 == bitset_or_and_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
116 assert_bitset_equal (adst
, bdst
);
119 void check_attributes (enum bitset_attr attr
)
123 bitset bs0
= bitset_create (nbits
, attr
);
124 ASSERT (bitset_size (bs0
) == 32);
125 ASSERT (bitset_count (bs0
) == 0);
126 ASSERT (bitset_empty_p (bs0
));
128 bitset bs1
= bitset_create (nbits
, attr
);
132 ASSERT (bitset_count (bs1
) == 3);
133 ASSERT (!bitset_empty_p (bs1
));
135 bitset bs2
= bitset_create (nbits
, attr
);
141 ASSERT (bitset_disjoint_p (bs1
, bs2
));
144 bitset bs
= bitset_create (nbits
, attr
);
145 bitset_and (bs
, bs1
, bs2
);
146 ASSERT (bitset_count (bs
) == 0);
149 bitset_or (bs
, bs1
, bs2
);
150 ASSERT (bitset_count (bs
) == 6);
155 check_attributes (BITSET_FIXED
);
156 check_attributes (BITSET_VARIABLE
);
157 check_attributes (BITSET_DENSE
);
158 check_attributes (BITSET_SPARSE
);
159 check_attributes (BITSET_FRUGAL
);
160 check_attributes (BITSET_GREEDY
);
162 compare (BITSET_FIXED
, BITSET_FIXED
);
163 compare (BITSET_FIXED
, BITSET_VARIABLE
);
164 compare (BITSET_FIXED
, BITSET_DENSE
);
165 compare (BITSET_FIXED
, BITSET_SPARSE
);
166 compare (BITSET_FIXED
, BITSET_FRUGAL
);
167 compare (BITSET_FIXED
, BITSET_GREEDY
);