2 Copyright (C) 2018-2024 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 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
));
36 bitset_random (bitset bs
)
38 for (bitset_bindex i
= 0; i
< bitset_size (bs
); ++i
)
39 bitset_set (bs
, RANDOM (2));
43 /* Check various operations on random bitsets with two different
47 compare (enum bitset_attr a
, enum bitset_attr b
)
49 /* bitset_list (used in many operations) uses a cache whose size is
51 const int nbits
= RANDOM (2 * BITSET_LIST_SIZE
);
53 /* Four read only random initial values of type A. */
54 const bitset asrc0
= bitset_create (nbits
, a
);
55 bitset_random (asrc0
);
56 const bitset asrc1
= bitset_create (nbits
, a
);
57 bitset_random (asrc1
);
58 const bitset asrc2
= bitset_create (nbits
, a
);
59 bitset_random (asrc2
);
60 const bitset asrc3
= bitset_create (nbits
, a
);
61 bitset_random (asrc3
);
63 /* Four read only values of type B, equal to the values of type A. */
64 const bitset bsrc0
= bitset_create (nbits
, b
);
65 bitset_copy (bsrc0
, asrc0
);
66 const bitset bsrc1
= bitset_create (nbits
, b
);
67 bitset_copy (bsrc1
, asrc1
);
68 const bitset bsrc2
= bitset_create (nbits
, b
);
69 bitset_copy (bsrc2
, asrc2
);
70 const bitset bsrc3
= bitset_create (nbits
, b
);
71 bitset_copy (bsrc3
, asrc3
);
73 /* Destinations for each operation. */
74 bitset adst
= bitset_create (nbits
, a
);
75 bitset bdst
= bitset_create (nbits
, b
);
78 ASSERT (bitset_count (asrc0
) == bitset_count (bsrc0
));
81 bitset_not (adst
, asrc0
);
82 bitset_not (bdst
, bsrc0
);
83 assert_bitset_equal (adst
, bdst
);
86 bitset_and (adst
, asrc0
, asrc1
);
87 bitset_and (bdst
, bsrc0
, bsrc1
);
88 assert_bitset_equal (adst
, bdst
);
89 ASSERT (bitset_and_cmp (adst
, asrc0
, asrc1
)
90 == bitset_and_cmp (bdst
, bsrc0
, bsrc1
));
91 assert_bitset_equal (adst
, bdst
);
94 bitset_andn (adst
, asrc0
, asrc1
);
95 bitset_andn (bdst
, bsrc0
, bsrc1
);
96 assert_bitset_equal (adst
, bdst
);
97 ASSERT (bitset_andn_cmp (adst
, asrc0
, asrc1
)
98 == bitset_andn_cmp (bdst
, bsrc0
, bsrc1
));
99 assert_bitset_equal (adst
, bdst
);
102 bitset_or (adst
, asrc0
, asrc1
);
103 bitset_or (bdst
, bsrc0
, bsrc1
);
104 assert_bitset_equal (adst
, bdst
);
105 ASSERT (bitset_or_cmp (adst
, asrc0
, asrc1
)
106 == bitset_or_cmp (bdst
, bsrc0
, bsrc1
));
107 assert_bitset_equal (adst
, bdst
);
110 bitset_xor (adst
, asrc0
, asrc1
);
111 bitset_xor (bdst
, bsrc0
, bsrc1
);
112 assert_bitset_equal (adst
, bdst
);
113 ASSERT (bitset_xor_cmp (adst
, asrc0
, asrc1
)
114 == bitset_xor_cmp (bdst
, bsrc0
, bsrc1
));
115 assert_bitset_equal (adst
, bdst
);
118 bitset_and_or (adst
, asrc0
, asrc1
, asrc2
);
119 bitset_and_or (bdst
, bsrc0
, bsrc1
, bsrc2
);
120 assert_bitset_equal (adst
, bdst
);
121 ASSERT (bitset_and_or_cmp (adst
, asrc0
, asrc1
, asrc2
)
122 == bitset_and_or_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
123 assert_bitset_equal (adst
, bdst
);
126 bitset_andn_or (adst
, asrc0
, asrc1
, asrc2
);
127 bitset_andn_or (bdst
, bsrc0
, bsrc1
, bsrc2
);
128 assert_bitset_equal (adst
, bdst
);
129 ASSERT (bitset_andn_or_cmp (adst
, asrc0
, asrc1
, asrc2
)
130 == bitset_andn_or_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
131 assert_bitset_equal (adst
, bdst
);
134 bitset_or_and (adst
, asrc0
, asrc1
, asrc2
);
135 bitset_or_and (bdst
, bsrc0
, bsrc1
, bsrc2
);
136 assert_bitset_equal (adst
, bdst
);
137 ASSERT (bitset_or_and_cmp (adst
, asrc0
, asrc1
, asrc2
)
138 == bitset_or_and_cmp (bdst
, bsrc0
, bsrc1
, bsrc2
));
139 assert_bitset_equal (adst
, bdst
);
144 assert_bitset_equal (adst
, bdst
);
149 assert_bitset_equal (adst
, bdst
);
151 /* first and last and FOR_EACH. */
152 /* Work on bdst to exercise all the bitset types (adst is
154 bitset_copy (bdst
, bsrc0
);
156 debug_bitset (bsrc0
);
157 bitset_copy (adst
, bdst
);
160 ASSERT (bitset_count (adst
) == bitset_count (bdst
));
164 bitset_bindex first
= bitset_first (adst
);
165 ASSERT (first
== bitset_first (bdst
));
167 bitset_bindex last
= bitset_last (adst
);
168 ASSERT (last
== bitset_last (bdst
));
170 ASSERT (first
<= last
);
176 bitset_iterator iter
;
178 bitset_bindex first
= bitset_first (bdst
);
179 bitset_bindex last
= bitset_last (bdst
);
180 bool seen_first
= false;
181 bool seen_last
= false;
182 BITSET_FOR_EACH (iter
, bdst
, j
, 0)
184 ASSERT (first
<= j
&& j
<= last
);
185 ASSERT (bitset_test (bdst
, j
));
191 if (first
== BITSET_BINDEX_MAX
)
193 ASSERT (!seen_first
);
203 /* FOR_EACH_REVERSE. */
205 bitset_iterator iter
;
207 bitset_bindex first
= bitset_first (bdst
);
208 bitset_bindex last
= bitset_last (bdst
);
209 bool seen_first
= false;
210 bool seen_last
= false;
211 BITSET_FOR_EACH_REVERSE (iter
, bdst
, j
, 0)
213 ASSERT (first
<= j
&& j
<= last
);
214 ASSERT (bitset_test (bdst
, j
));
220 if (first
== BITSET_BINDEX_MAX
)
222 ASSERT (!seen_first
);
235 ARRAY bitsets cannot be resized. */
236 if (bitset_type_get (bsrc0
) != BITSET_ARRAY
)
238 const int nbits_new
= RANDOM (256);
239 bitset_copy (adst
, asrc0
);
240 bitset_copy (bdst
, bsrc0
);
241 ASSERT (nbits_new
== bitset_resize (adst
, nbits_new
));
242 ASSERT (nbits_new
== bitset_resize (bdst
, nbits_new
));
243 assert_bitset_equal (adst
, bdst
);
259 /* Check empty bitsets. */
262 check_zero (bitset bs
)
264 fprintf (stderr
, "check_zero\n");
268 ASSERT (bitset_count (bs
) == 0);
271 ASSERT (bitset_first (bs
) == BITSET_BINDEX_MAX
);
272 ASSERT (bitset_last (bs
) == BITSET_BINDEX_MAX
);
276 bitset_iterator iter
;
277 _GL_UNUSED bitset_bindex i
;
278 BITSET_FOR_EACH (iter
, bs
, i
, 0)
280 BITSET_FOR_EACH_REVERSE (iter
, bs
, i
, 0)
285 /* Exercise on a single-bit values: it's easy to get the handling of
286 the most significant bit wrong. */
289 check_one_bit (bitset bs
, int bitno
)
291 fprintf (stderr
, "check_one_bit(%d)\n", bitno
);
293 bitset_set (bs
, bitno
);
296 ASSERT (bitset_count (bs
) == 1);
299 ASSERT (bitset_test (bs
, bitno
));
302 ASSERT (bitset_first (bs
) == bitno
);
303 ASSERT (bitset_last (bs
) == bitno
);
307 bitset_iterator iter
;
309 BITSET_FOR_EACH (iter
, bs
, i
, 0)
312 BITSET_FOR_EACH_REVERSE (iter
, bs
, i
, 0)
317 /* Check full bitsets. */
320 check_ones (bitset bs
)
322 fprintf (stderr
, "check_ones\n");
323 const bitset_bindex size
= bitset_size (bs
);
329 ASSERT (bitset_count (bs
) == size
);
332 ASSERT (bitset_first (bs
) == 0);
333 ASSERT (bitset_last (bs
) == size
- 1);
337 bitset_iterator iter
;
339 bitset_bindex count
= 0;
340 BITSET_FOR_EACH (iter
, bs
, i
, 0)
341 ASSERT (i
== count
++);
342 ASSERT (count
== size
);
343 BITSET_FOR_EACH_REVERSE (iter
, bs
, i
, 0)
344 ASSERT (i
== --count
);
349 /* Check various operations against expected values for a bitset
350 having attributes ATTR. */
353 check_attributes (enum bitset_attr attr
, int nbits
)
355 bitset bs0
= bitset_create (nbits
, attr
);
356 ASSERT (bitset_size (bs0
) == nbits
);
357 ASSERT (bitset_count (bs0
) == 0);
358 ASSERT (bitset_empty_p (bs0
));
360 bitset bs1
= bitset_create (nbits
, attr
);
364 ASSERT (bitset_count (bs1
) == 3);
365 ASSERT (!bitset_empty_p (bs1
));
367 bitset bs2
= bitset_create (nbits
, attr
);
373 ASSERT (bitset_disjoint_p (bs1
, bs2
));
376 bitset bs
= bitset_create (nbits
, attr
);
377 bitset_and (bs
, bs1
, bs2
);
378 ASSERT (bitset_count (bs
) == 0);
381 bitset_or (bs
, bs1
, bs2
);
382 ASSERT (bitset_count (bs
) == 6);
387 /* Exercise on all the single-bit values: it's easy to get the
388 handling of the most significant bit wrong. */
389 for (int bitno
= 0; bitno
< nbits
; ++bitno
)
390 check_one_bit (bs
, bitno
);
400 bitset_stats_enable ();
402 for (int i
= 0; i
< 4; ++i
)
404 /* table bitsets have elements that store 256 bits. bitset_list
405 (used in many operations) uses a cache whose size is
411 : (BITSET_LIST_SIZE
+ 1);
412 check_attributes (BITSET_FIXED
, nbits
);
413 check_attributes (BITSET_VARIABLE
, nbits
);
414 check_attributes (BITSET_DENSE
, nbits
);
415 check_attributes (BITSET_SPARSE
, nbits
);
416 check_attributes (BITSET_FRUGAL
, nbits
);
417 check_attributes (BITSET_GREEDY
, nbits
);
420 compare (BITSET_VARIABLE
, BITSET_FIXED
);
421 compare (BITSET_VARIABLE
, BITSET_VARIABLE
);
422 compare (BITSET_VARIABLE
, BITSET_DENSE
);
423 compare (BITSET_VARIABLE
, BITSET_SPARSE
);
424 compare (BITSET_VARIABLE
, BITSET_FRUGAL
);
425 compare (BITSET_VARIABLE
, BITSET_GREEDY
);
427 bitset_stats_dump (stderr
);
428 return test_exit_status
;