Debian Codegraph @

unittest/gunit/my_bitmap-t.cc

raw symbols SCIP
1 /*
2 Copyright (c) 2006, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gtest/gtest.h>
27 #include <sys/types.h>
28
29 #include "my_bitmap.h"
30 #include "my_inttypes.h"
31 #include "my_thread.h"
32
33 namespace my_bitmap_unittest {
34
35 const uint MAX_TESTED_BITMAP_SIZE = 1024;
36
37 uint get_rand_bit(uint bitsize) { return (rand() % bitsize); }
38
39 bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize) {
40 uint i, test_bit;
41 uint no_loops = bitsize > 128 ? 128 : bitsize;
42 for (i = 0; i < no_loops; i++) {
43 test_bit = get_rand_bit(bitsize);
44 bitmap_set_bit(map, test_bit);
45 if (!bitmap_is_set(map, test_bit)) goto error1;
46 bitmap_clear_bit(map, test_bit);
47 if (bitmap_is_set(map, test_bit)) goto error2;
48 }
49 return false;
50 error1:
51 ADD_FAILURE() << "Error in set bit bit=" << test_bit;
52 return true;
53 error2:
54 ADD_FAILURE() << "Error in clear bit bit=" << test_bit;
55 return true;
56 }
57
58 bool test_flip_bit(MY_BITMAP *map, uint bitsize) {
59 uint i, test_bit;
60 uint no_loops = bitsize > 128 ? 128 : bitsize;
61 for (i = 0; i < no_loops; i++) {
62 test_bit = get_rand_bit(bitsize);
63 bitmap_flip_bit(map, test_bit);
64 if (!bitmap_is_set(map, test_bit)) goto error1;
65 bitmap_flip_bit(map, test_bit);
66 if (bitmap_is_set(map, test_bit)) goto error2;
67 }
68 return false;
69 error1:
70 ADD_FAILURE() << "Error in flip bit 1 bit=" << test_bit;
71 return true;
72 error2:
73 ADD_FAILURE() << "Error in flip bit 2 bit=" << test_bit;
74 return true;
75 }
76
77 bool test_get_all_bits(MY_BITMAP *map, uint bitsize) {
78 uint i;
79 bitmap_set_all(map);
80 if (!bitmap_is_set_all(map)) goto error1;
81 if (!bitmap_is_prefix(map, bitsize)) goto error5;
82 bitmap_clear_all(map);
83 if (!bitmap_is_clear_all(map)) goto error2;
84 if (!bitmap_is_prefix(map, 0)) goto error6;
85 for (i = 0; i < bitsize; i++) bitmap_set_bit(map, i);
86 if (!bitmap_is_set_all(map)) goto error3;
87 for (i = 0; i < bitsize; i++) bitmap_clear_bit(map, i);
88 if (!bitmap_is_clear_all(map)) goto error4;
89 return false;
90 error1:
91 ADD_FAILURE() << "Error in set_all";
92 return true;
93 error2:
94 ADD_FAILURE() << "Error in clear_all";
95 return true;
96 error3:
97 ADD_FAILURE() << "Error in bitmap_is_set_all";
98 return true;
99 error4:
100 ADD_FAILURE() << "Error in bitmap_is_clear_all";
101 return true;
102 error5:
103 ADD_FAILURE() << "Error in set_all through set_prefix";
104 return true;
105 error6:
106 ADD_FAILURE() << "Error in clear_all through set_prefix";
107 return true;
108 }
109
110 bool test_compare_operators(MY_BITMAP *map, uint bitsize) {
111 uint i, j, test_bit1, test_bit2, test_bit3, test_bit4;
112 uint no_loops = bitsize > 128 ? 128 : bitsize;
113 MY_BITMAP map2_obj, map3_obj;
114 MY_BITMAP *map2 = &map2_obj, *map3 = &map3_obj;
115 my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE];
116 my_bitmap_map map3buf[MAX_TESTED_BITMAP_SIZE];
117 bitmap_init(&map2_obj, map2buf, bitsize);
118 bitmap_init(&map3_obj, map3buf, bitsize);
119 bitmap_clear_all(map2);
120 bitmap_clear_all(map3);
121 for (i = 0; i < no_loops; i++) {
122 test_bit1 = get_rand_bit(bitsize);
123 bitmap_set_prefix(map, test_bit1);
124 test_bit2 = get_rand_bit(bitsize);
125 bitmap_set_prefix(map2, test_bit2);
126 bitmap_intersect(map, map2);
127 test_bit3 = test_bit2 < test_bit1 ? test_bit2 : test_bit1;
128 bitmap_set_prefix(map3, test_bit3);
129 if (!bitmap_cmp(map, map3)) goto error1;
130 bitmap_clear_all(map);
131 bitmap_clear_all(map2);
132 bitmap_clear_all(map3);
133 test_bit1 = get_rand_bit(bitsize);
134 test_bit2 = get_rand_bit(bitsize);
135 test_bit3 = get_rand_bit(bitsize);
136 bitmap_set_prefix(map, test_bit1);
137 bitmap_set_prefix(map2, test_bit2);
138 test_bit3 = test_bit2 > test_bit1 ? test_bit2 : test_bit1;
139 bitmap_set_prefix(map3, test_bit3);
140 bitmap_union(map, map2);
141 if (!bitmap_cmp(map, map3)) goto error2;
142 bitmap_clear_all(map);
143 bitmap_clear_all(map2);
144 bitmap_clear_all(map3);
145 test_bit1 = get_rand_bit(bitsize);
146 test_bit2 = get_rand_bit(bitsize);
147 test_bit3 = get_rand_bit(bitsize);
148 bitmap_set_prefix(map, test_bit1);
149 bitmap_set_prefix(map2, test_bit2);
150 bitmap_xor(map, map2);
151 test_bit3 = test_bit2 > test_bit1 ? test_bit2 : test_bit1;
152 test_bit4 = test_bit2 < test_bit1 ? test_bit2 : test_bit1;
153 bitmap_set_prefix(map3, test_bit3);
154 for (j = 0; j < test_bit4; j++) bitmap_clear_bit(map3, j);
155 if (!bitmap_cmp(map, map3)) goto error3;
156 bitmap_clear_all(map);
157 bitmap_clear_all(map2);
158 bitmap_clear_all(map3);
159 test_bit1 = get_rand_bit(bitsize);
160 test_bit2 = get_rand_bit(bitsize);
161 test_bit3 = get_rand_bit(bitsize);
162 bitmap_set_prefix(map, test_bit1);
163 bitmap_set_prefix(map2, test_bit2);
164 bitmap_subtract(map, map2);
165 if (test_bit2 < test_bit1) {
166 bitmap_set_prefix(map3, test_bit1);
167 for (j = 0; j < test_bit2; j++) bitmap_clear_bit(map3, j);
168 }
169 if (!bitmap_cmp(map, map3)) goto error4;
170 bitmap_clear_all(map);
171 bitmap_clear_all(map2);
172 bitmap_clear_all(map3);
173 test_bit1 = get_rand_bit(bitsize);
174 bitmap_set_prefix(map, test_bit1);
175 bitmap_invert(map);
176 bitmap_set_all(map3);
177 for (j = 0; j < test_bit1; j++) bitmap_clear_bit(map3, j);
178 if (!bitmap_cmp(map, map3)) goto error5;
179 bitmap_clear_all(map);
180 bitmap_clear_all(map3);
181 }
182 return false;
183 error1:
184 ADD_FAILURE() << "intersect error size1=" << test_bit1
185 << ",size2=" << test_bit2;
186 return true;
187 error2:
188 ADD_FAILURE() << "union error size1=" << test_bit1 << ",size2=" << test_bit2;
189 return true;
190 error3:
191 ADD_FAILURE() << "xor error size1=" << test_bit1 << ",size2=" << test_bit2;
192 return true;
193 error4:
194 ADD_FAILURE() << "subtract error size1=" << test_bit1
195 << ",size2=" << test_bit2;
196 return true;
197 error5:
198 ADD_FAILURE() << "invert error size=" << test_bit1;
199 return true;
200 }
201
202 bool test_count_bits_set(MY_BITMAP *map, uint bitsize) {
203 uint i, bit_count = 0, test_bit;
204 uint no_loops = bitsize > 128 ? 128 : bitsize;
205 for (i = 0; i < no_loops; i++) {
206 test_bit = get_rand_bit(bitsize);
207 if (!bitmap_is_set(map, test_bit)) {
208 bitmap_set_bit(map, test_bit);
209 bit_count++;
210 }
211 }
212 if (bit_count == 0 && bitsize > 0) goto error1;
213 if (bitmap_bits_set(map) != bit_count) goto error2;
214 return false;
215 error1:
216 ADD_FAILURE() << "No bits set";
217 return true;
218 error2:
219 ADD_FAILURE() << "Wrong count of bits set";
220 return true;
221 }
222
223 bool test_get_first_bit(MY_BITMAP *map, uint bitsize) {
224 uint i, test_bit = 0;
225 uint no_loops = bitsize > 128 ? 128 : bitsize;
226
227 bitmap_set_all(map);
228 for (i = 0; i < bitsize; i++) bitmap_clear_bit(map, i);
229 if (bitmap_get_first_set(map) != MY_BIT_NONE) goto error1;
230 bitmap_clear_all(map);
231 for (i = 0; i < bitsize; i++) bitmap_set_bit(map, i);
232 if (bitmap_get_first(map) != MY_BIT_NONE) goto error2;
233 bitmap_clear_all(map);
234
235 for (i = 0; i < no_loops; i++) {
236 test_bit = get_rand_bit(bitsize);
237 bitmap_set_bit(map, test_bit);
238 if (bitmap_get_first_set(map) != test_bit) goto error1;
239 bitmap_set_all(map);
240 bitmap_clear_bit(map, test_bit);
241 if (bitmap_get_first(map) != test_bit) goto error2;
242 bitmap_clear_all(map);
243 }
244 return false;
245 error1:
246 ADD_FAILURE() << "get_first_set error prefix_size=" << test_bit;
247 return true;
248 error2:
249 ADD_FAILURE() << "get_first error prefix_size=" << test_bit;
250 return true;
251 }
252
253 bool test_set_next_bit(MY_BITMAP *map, uint bitsize) {
254 uint i, j, test_bit;
255 uint no_loops = bitsize > 128 ? 128 : bitsize;
256 for (i = 0; i < no_loops; i++) {
257 test_bit = get_rand_bit(bitsize);
258 for (j = 0; j < test_bit; j++) bitmap_set_next(map);
259 if (!bitmap_is_prefix(map, test_bit)) goto error1;
260 bitmap_clear_all(map);
261 }
262 return false;
263 error1:
264 ADD_FAILURE() << "set_next error prefix_size=" << test_bit;
265 return true;
266 }
267
268 bool test_get_next_bit(MY_BITMAP *map, uint bitsize) {
269 uint i, bit_count = 0, test_bit, next_count = 0;
270 uint no_loops = bitsize > 128 ? 128 : bitsize;
271 for (i = 0; i < no_loops; i++) {
272 test_bit = get_rand_bit(bitsize);
273 if (!bitmap_is_set(map, test_bit)) {
274 bitmap_set_bit(map, test_bit);
275 bit_count++;
276 }
277 }
278 if (bit_count == 0 && bitsize > 0) goto error1;
279 if (bitmap_bits_set(map) != bit_count) goto error2;
280
281 for (test_bit = bitmap_get_first_set(map); test_bit != MY_BIT_NONE;
282 test_bit = bitmap_get_next_set(map, test_bit)) {
283 if (test_bit >= bitsize) goto error3;
284 if (!bitmap_is_set(map, test_bit)) goto error4;
285 next_count++;
286 }
287 if (next_count != bit_count) goto error5;
288 return false;
289 error1:
290 ADD_FAILURE() << "No bits set";
291 return true;
292 error2:
293 ADD_FAILURE() << "Wrong count of bits set";
294 return true;
295 error3:
296 ADD_FAILURE() << "get_next_set out of range";
297 return true;
298 error4:
299 ADD_FAILURE() << "get_next_set bit not set";
300 return true;
301 error5:
302 ADD_FAILURE() << "Wrong count get_next_set";
303 return true;
304 }
305
306 bool test_prefix(MY_BITMAP *map, uint bitsize) {
307 uint i, j, test_bit;
308 uint no_loops = bitsize > 128 ? 128 : bitsize;
309 for (i = 0; i < no_loops; i++) {
310 test_bit = get_rand_bit(bitsize);
311 bitmap_set_prefix(map, test_bit);
312 if (!bitmap_is_prefix(map, test_bit)) goto error1;
313 bitmap_clear_all(map);
314 for (j = 0; j < test_bit; j++) bitmap_set_bit(map, j);
315 if (!bitmap_is_prefix(map, test_bit)) goto error2;
316 bitmap_set_all(map);
317 for (j = bitsize - 1; ~(j - test_bit); j--) bitmap_clear_bit(map, j);
318 if (!bitmap_is_prefix(map, test_bit)) goto error3;
319 bitmap_clear_all(map);
320 }
321 for (i = 0; i < bitsize; i++) {
322 if (bitmap_is_prefix(map, i + 1)) goto error4;
323 bitmap_set_bit(map, i);
324 if (!bitmap_is_prefix(map, i + 1)) goto error5;
325 test_bit = get_rand_bit(bitsize);
326 bitmap_set_bit(map, test_bit);
327 if (test_bit <= i && !bitmap_is_prefix(map, i + 1))
328 goto error5;
329 else if (test_bit > i) {
330 if (bitmap_is_prefix(map, i + 1)) goto error4;
331 bitmap_clear_bit(map, test_bit);
332 }
333 }
334 return false;
335 error1:
336 ADD_FAILURE() << "prefix1 error prefix_size=" << test_bit;
337 return true;
338 error2:
339 ADD_FAILURE() << "prefix2 error prefix_size=" << test_bit;
340 return true;
341 error3:
342 ADD_FAILURE() << "prefix3 error prefix_size=" << test_bit;
343 return true;
344 error4:
345 ADD_FAILURE() << "prefix4 error i=" << i;
346 return true;
347 error5:
348 ADD_FAILURE() << "prefix5 error i=" << i;
349 return true;
350 }
351
352 bool test_compare(MY_BITMAP *map, uint bitsize) {
353 MY_BITMAP map2;
354 my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE];
355 uint i, test_bit;
356 uint no_loops = bitsize > 128 ? 128 : bitsize;
357 bitmap_init(&map2, map2buf, bitsize);
358
359 /* Test all 4 possible combinations of set/unset bits. */
360 for (i = 0; i < no_loops; i++) {
361 test_bit = get_rand_bit(bitsize);
362 bitmap_clear_bit(map, test_bit);
363 bitmap_clear_bit(&map2, test_bit);
364 if (!bitmap_is_subset(map, &map2)) goto error_is_subset;
365 bitmap_set_bit(map, test_bit);
366 if (bitmap_is_subset(map, &map2)) goto error_is_subset;
367 bitmap_set_bit(&map2, test_bit);
368 if (!bitmap_is_subset(map, &map2)) goto error_is_subset;
369 bitmap_clear_bit(map, test_bit);
370 if (!bitmap_is_subset(map, &map2)) goto error_is_subset;
371 /* Note that test_bit is not cleared i map2. */
372 }
373 bitmap_clear_all(map);
374 bitmap_clear_all(&map2);
375 /* Test all 4 possible combinations of set/unset bits. */
376 for (i = 0; i < no_loops; i++) {
377 test_bit = get_rand_bit(bitsize);
378 if (bitmap_is_overlapping(map, &map2)) goto error_is_overlapping;
379 bitmap_set_bit(map, test_bit);
380 if (bitmap_is_overlapping(map, &map2)) goto error_is_overlapping;
381 bitmap_set_bit(&map2, test_bit);
382 if (!bitmap_is_overlapping(map, &map2)) goto error_is_overlapping;
383 bitmap_clear_bit(map, test_bit);
384 if (bitmap_is_overlapping(map, &map2)) goto error_is_overlapping;
385 bitmap_clear_bit(&map2, test_bit);
386 /* Note that test_bit is not cleared i map2. */
387 }
388 return false;
389 error_is_subset:
390 ADD_FAILURE() << "is_subset error";
391 return true;
392 error_is_overlapping:
393 ADD_FAILURE() << "is_overlapping error";
394 return true;
395 }
396
397 bool test_intersect(MY_BITMAP *map, uint bitsize) {
398 uint bitsize2 = 1 + get_rand_bit(MAX_TESTED_BITMAP_SIZE - 1);
399 MY_BITMAP map2;
400 my_bitmap_map *map2buf = new my_bitmap_map[bitsize2];
401 bitmap_init(&map2, map2buf, bitsize2);
402
403 uint test_bit1 = get_rand_bit(bitsize);
404 uint test_bit2 = get_rand_bit(bitsize2);
405
406 if (test_bit2 < bitsize) {
407 // test_bit2 can be set in map, so the intersection is not empty iff
408 // test_bit1 == test_bit2
409 bitmap_set_bit(map, test_bit1);
410 bitmap_set_bit(&map2, test_bit2);
411 bitmap_intersect(map, &map2);
412 if (test_bit1 == test_bit2) {
413 if (!bitmap_is_set(map, test_bit1)) goto error;
414 bitmap_clear_bit(map, test_bit2);
415 } else {
416 if (bitmap_is_set(map, test_bit1)) goto error;
417 }
418 } else {
419 // test_bit2 cannot be set in map, so the intersection should be empty
420 bitmap_set_bit(map, test_bit1);
421 bitmap_set_bit(&map2, test_bit2);
422 bitmap_intersect(map, &map2);
423 }
424
425 if (!bitmap_is_clear_all(map)) goto error;
426
427 bitmap_set_all(map);
428 bitmap_set_all(&map2);
429 for (uint i = 0; i < bitsize2; i++) bitmap_clear_bit(&map2, i);
430 bitmap_intersect(map, &map2);
431 if (!bitmap_is_clear_all(map)) goto error;
432 delete[] map2buf;
433 return false;
434 error:
435 delete[] map2buf;
436 ADD_FAILURE() << "intersect error bit1=" << test_bit1
437 << ",bit2=" << test_bit2;
438 return true;
439 }
440
441 class BitMapTest : public ::testing::TestWithParam<uint> {
442 protected:
443 void SetUp() override {
444 bitsize = GetParam();
445 ASSERT_FALSE(bitmap_init(&map, buf, bitsize));
446 bitmap_clear_all(&map);
447 }
448
449 MY_BITMAP map;
450 my_bitmap_map buf[MAX_TESTED_BITMAP_SIZE];
451 uint bitsize;
452 };
453
454 const uint test_values[] = {
455 1, 2, 3, 4, 5,
456 6, 7, 8, 9, 10,
457 11, 12, 13, 14, 15,
458 16, 17, 18, 19, 20,
459 21, 22, 23, 24, 25,
460 26, 27, 28, 29, 30,
461 31, 32, 33, 34, 35,
462 36, 37, 38, 39, 40,
463 2 * 32U - 1, 2 * 32U, 2 * 32U + 1, 3 * 32U - 1, 3 * 32U,
464 3 * 32U + 1, 4 * 32U - 1, 4 * 32U, 4 * 32U + 1, MAX_TESTED_BITMAP_SIZE};
465
466 INSTANTIATE_TEST_SUITE_P(Foo, BitMapTest, ::testing::ValuesIn(test_values));
467
468 TEST_P(BitMapTest, TestSetGetClearBit) {
469 EXPECT_FALSE(test_set_get_clear_bit(&map, bitsize)) << "bitsize=" << bitsize;
470 }
471
472 TEST_P(BitMapTest, TestFlipBit) {
473 EXPECT_FALSE(test_flip_bit(&map, bitsize)) << "bitsize=" << bitsize;
474 }
475
476 TEST_P(BitMapTest, TestGetAllBits) {
477 EXPECT_FALSE(test_get_all_bits(&map, bitsize)) << "bitsize=" << bitsize;
478 }
479
480 TEST_P(BitMapTest, TestCompareOperators) {
481 EXPECT_FALSE(test_compare_operators(&map, bitsize)) << "bitsize=" << bitsize;
482 }
483
484 TEST_P(BitMapTest, TestCountBitsSet) {
485 EXPECT_FALSE(test_count_bits_set(&map, bitsize)) << "bitsize=" << bitsize;
486 }
487
488 TEST_P(BitMapTest, TestGetFirstBit) {
489 EXPECT_FALSE(test_get_first_bit(&map, bitsize)) << "bitsize=" << bitsize;
490 }
491
492 TEST_P(BitMapTest, TestSetNextBit) {
493 EXPECT_FALSE(test_set_next_bit(&map, bitsize)) << "bitsize=" << bitsize;
494 }
495
496 TEST_P(BitMapTest, TestGetNextBit) {
497 EXPECT_FALSE(test_get_next_bit(&map, bitsize)) << "bitsize=" << bitsize;
498 }
499
500 TEST_P(BitMapTest, TestPrefix) {
501 EXPECT_FALSE(test_prefix(&map, bitsize)) << "bitsize=" << bitsize;
502 }
503
504 TEST_P(BitMapTest, TestCompare) {
505 EXPECT_FALSE(test_compare(&map, bitsize)) << "bitsize=" << bitsize;
506 }
507
508 TEST_P(BitMapTest, TestIntersect) {
509 EXPECT_FALSE(test_intersect(&map, bitsize)) << "bitsize=" << bitsize;
510 }
511
512 // Bug#11761614
513
514 bool bitmap_set_prefix_t() {
515 MY_BITMAP map;
516 my_bitmap_map buf[2]; /* 64-bit buffer */
517 uint32 _max = ~((uint32)0);
518 bitmap_init(&map, buf, 32);
519
520 // set all bits in the 2nd half of the buf
521 buf[1] = _max;
522 bitmap_clear_all(&map);
523
524 /*
525 Choose prefix_size as number that is not a multiple
526 of 8, so that leftover bits in the last prefix byte
527 will be set separately.
528 */
529 bitmap_set_prefix(&map, 31);
530 // 2nd half should remain unaltered
531 return (buf[1] == _max);
532 }
533
534 TEST(BitMapTestEx, TestSetPrefixEx) { EXPECT_TRUE(bitmap_set_prefix_t()); }
535
536 } // namespace my_bitmap_unittest