18 #include "my_config.h"
19 #include <gtest/gtest.h>
21 #include <my_global.h>
22 #include <my_pthread.h>
23 #include <my_bitmap.h>
25 namespace my_bitmap_unittest {
27 const uint MAX_TESTED_BITMAP_SIZE= 1024;
29 uint get_rand_bit(uint bitsize)
31 return (rand() % bitsize);
34 bool test_set_get_clear_bit(
MY_BITMAP *map, uint bitsize)
37 uint no_loops= bitsize > 128 ? 128 : bitsize;
38 for (i=0; i < no_loops; i++)
40 test_bit= get_rand_bit(bitsize);
41 bitmap_set_bit(map, test_bit);
42 if (!bitmap_is_set(map, test_bit))
44 bitmap_clear_bit(map, test_bit);
45 if (bitmap_is_set(map, test_bit))
50 ADD_FAILURE() <<
"Error in set bit bit=" << test_bit;
53 ADD_FAILURE() <<
"Error in clear bit bit=" << test_bit;
57 bool test_flip_bit(
MY_BITMAP *map, uint bitsize)
60 uint no_loops= bitsize > 128 ? 128 : bitsize;
61 for (i=0; i < no_loops; i++)
63 test_bit= get_rand_bit(bitsize);
64 bitmap_flip_bit(map, test_bit);
65 if (!bitmap_is_set(map, test_bit))
67 bitmap_flip_bit(map, test_bit);
68 if (bitmap_is_set(map, test_bit))
73 ADD_FAILURE() <<
"Error in flip bit 1 bit=" << test_bit;
76 ADD_FAILURE() <<
"Error in flip bit 2 bit=" << test_bit;
80 bool test_get_all_bits(
MY_BITMAP *map, uint bitsize)
84 if (!bitmap_is_set_all(map))
86 if (!bitmap_is_prefix(map, bitsize))
88 bitmap_clear_all(map);
89 if (!bitmap_is_clear_all(map))
91 if (!bitmap_is_prefix(map, 0))
93 for (i=0; i<bitsize;i++)
94 bitmap_set_bit(map, i);
95 if (!bitmap_is_set_all(map))
97 for (i=0; i<bitsize;i++)
98 bitmap_clear_bit(map, i);
99 if (!bitmap_is_clear_all(map))
103 ADD_FAILURE() <<
"Error in set_all";
106 ADD_FAILURE() <<
"Error in clear_all";
109 ADD_FAILURE() <<
"Error in bitmap_is_set_all";
112 ADD_FAILURE() <<
"Error in bitmap_is_clear_all";
115 ADD_FAILURE() <<
"Error in set_all through set_prefix";
118 ADD_FAILURE() <<
"Error in clear_all through set_prefix";
122 bool test_compare_operators(
MY_BITMAP *map, uint bitsize)
124 uint
i, j, test_bit1, test_bit2, test_bit3,test_bit4;
125 uint no_loops= bitsize > 128 ? 128 : bitsize;
127 MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
128 my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE];
129 my_bitmap_map map3buf[MAX_TESTED_BITMAP_SIZE];
130 bitmap_init(&map2_obj, map2buf, bitsize,
false);
131 bitmap_init(&map3_obj, map3buf, bitsize,
false);
132 bitmap_clear_all(map2);
133 bitmap_clear_all(map3);
134 for (i=0; i < no_loops; i++)
136 test_bit1=get_rand_bit(bitsize);
137 bitmap_set_prefix(map, test_bit1);
138 test_bit2=get_rand_bit(bitsize);
139 bitmap_set_prefix(map2, test_bit2);
140 bitmap_intersect(map, map2);
141 test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
142 bitmap_set_prefix(map3, test_bit3);
143 if (!bitmap_cmp(map, map3))
145 bitmap_clear_all(map);
146 bitmap_clear_all(map2);
147 bitmap_clear_all(map3);
148 test_bit1=get_rand_bit(bitsize);
149 test_bit2=get_rand_bit(bitsize);
150 test_bit3=get_rand_bit(bitsize);
151 bitmap_set_prefix(map, test_bit1);
152 bitmap_set_prefix(map2, test_bit2);
153 test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
154 bitmap_set_prefix(map3, test_bit3);
155 bitmap_union(map, map2);
156 if (!bitmap_cmp(map, map3))
158 bitmap_clear_all(map);
159 bitmap_clear_all(map2);
160 bitmap_clear_all(map3);
161 test_bit1=get_rand_bit(bitsize);
162 test_bit2=get_rand_bit(bitsize);
163 test_bit3=get_rand_bit(bitsize);
164 bitmap_set_prefix(map, test_bit1);
165 bitmap_set_prefix(map2, test_bit2);
166 bitmap_xor(map, map2);
167 test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
168 test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
169 bitmap_set_prefix(map3, test_bit3);
170 for (j=0; j < test_bit4; j++)
171 bitmap_clear_bit(map3, j);
172 if (!bitmap_cmp(map, map3))
174 bitmap_clear_all(map);
175 bitmap_clear_all(map2);
176 bitmap_clear_all(map3);
177 test_bit1=get_rand_bit(bitsize);
178 test_bit2=get_rand_bit(bitsize);
179 test_bit3=get_rand_bit(bitsize);
180 bitmap_set_prefix(map, test_bit1);
181 bitmap_set_prefix(map2, test_bit2);
182 bitmap_subtract(map, map2);
183 if (test_bit2 < test_bit1)
185 bitmap_set_prefix(map3, test_bit1);
186 for (j=0; j < test_bit2; j++)
187 bitmap_clear_bit(map3, j);
189 if (!bitmap_cmp(map, map3))
191 bitmap_clear_all(map);
192 bitmap_clear_all(map2);
193 bitmap_clear_all(map3);
194 test_bit1=get_rand_bit(bitsize);
195 bitmap_set_prefix(map, test_bit1);
197 bitmap_set_all(map3);
198 for (j=0; j < test_bit1; j++)
199 bitmap_clear_bit(map3, j);
200 if (!bitmap_cmp(map, map3))
202 bitmap_clear_all(map);
203 bitmap_clear_all(map3);
207 ADD_FAILURE() <<
"intersect error size1=" << test_bit1
208 <<
",size2=" << test_bit2;
211 ADD_FAILURE() <<
"union error size1=" << test_bit1
212 <<
",size2=" << test_bit2;
215 ADD_FAILURE() <<
"xor error size1=" << test_bit1
216 <<
",size2=" << test_bit2;
219 ADD_FAILURE() <<
"subtract error size1=" << test_bit1
220 <<
",size2=" << test_bit2;
223 ADD_FAILURE() <<
"invert error size=" << test_bit1;
227 bool test_count_bits_set(
MY_BITMAP *map, uint bitsize)
229 uint
i, bit_count=0, test_bit;
230 uint no_loops= bitsize > 128 ? 128 : bitsize;
231 for (i=0; i < no_loops; i++)
233 test_bit=get_rand_bit(bitsize);
234 if (!bitmap_is_set(map, test_bit))
236 bitmap_set_bit(map, test_bit);
240 if (bit_count==0 && bitsize > 0)
242 if (bitmap_bits_set(map) != bit_count)
246 ADD_FAILURE() <<
"No bits set";
249 ADD_FAILURE() <<
"Wrong count of bits set";
253 bool test_get_first_bit(
MY_BITMAP *map, uint bitsize)
256 uint no_loops= bitsize > 128 ? 128 : bitsize;
259 for (i=0; i < bitsize; i++)
260 bitmap_clear_bit(map, i);
261 if (bitmap_get_first_set(map) != MY_BIT_NONE)
263 bitmap_clear_all(map);
264 for (i=0; i < bitsize; i++)
265 bitmap_set_bit(map, i);
266 if (bitmap_get_first(map) != MY_BIT_NONE)
268 bitmap_clear_all(map);
270 for (i=0; i < no_loops; i++)
272 test_bit=get_rand_bit(bitsize);
273 bitmap_set_bit(map, test_bit);
274 if (bitmap_get_first_set(map) != test_bit)
277 bitmap_clear_bit(map, test_bit);
278 if (bitmap_get_first(map) != test_bit)
280 bitmap_clear_all(map);
284 ADD_FAILURE() <<
"get_first_set error prefix_size=" << test_bit;
287 ADD_FAILURE() <<
"get_first error prefix_size=" << test_bit;
291 bool test_set_next_bit(
MY_BITMAP *map, uint bitsize)
294 uint no_loops= bitsize > 128 ? 128 : bitsize;
295 for (i=0; i < no_loops; i++)
297 test_bit=get_rand_bit(bitsize);
298 for (j=0; j < test_bit; j++)
299 bitmap_set_next(map);
300 if (!bitmap_is_prefix(map, test_bit))
302 bitmap_clear_all(map);
306 ADD_FAILURE() <<
"set_next error prefix_size=" << test_bit;
310 bool test_get_next_bit(
MY_BITMAP *map, uint bitsize)
312 uint
i, bit_count=0, test_bit, next_count=0;
313 uint no_loops= bitsize > 128 ? 128 : bitsize;
314 for (i=0; i < no_loops; i++)
316 test_bit=get_rand_bit(bitsize);
317 if (!bitmap_is_set(map, test_bit))
319 bitmap_set_bit(map, test_bit);
323 if (bit_count==0 && bitsize > 0)
325 if (bitmap_bits_set(map) != bit_count)
328 for (test_bit= bitmap_get_first_set(map);
329 test_bit != MY_BIT_NONE;
330 test_bit= bitmap_get_next_set(map, test_bit))
332 if (test_bit >= bitsize)
334 if (!bitmap_is_set(map, test_bit))
338 if (next_count != bit_count)
342 ADD_FAILURE() <<
"No bits set";
345 ADD_FAILURE() <<
"Wrong count of bits set";
348 ADD_FAILURE() <<
"get_next_set out of range";
351 ADD_FAILURE() <<
"get_next_set bit not set";
354 ADD_FAILURE() <<
"Wrong count get_next_set";
358 bool test_prefix(
MY_BITMAP *map, uint bitsize)
361 uint no_loops= bitsize > 128 ? 128 : bitsize;
362 for (i=0; i < no_loops; i++)
364 test_bit=get_rand_bit(bitsize);
365 bitmap_set_prefix(map, test_bit);
366 if (!bitmap_is_prefix(map, test_bit))
368 bitmap_clear_all(map);
369 for (j=0; j < test_bit; j++)
370 bitmap_set_bit(map, j);
371 if (!bitmap_is_prefix(map, test_bit))
374 for (j=bitsize - 1; ~(j-test_bit); j--)
375 bitmap_clear_bit(map, j);
376 if (!bitmap_is_prefix(map, test_bit))
378 bitmap_clear_all(map);
380 for (i=0; i < bitsize; i++)
382 if (bitmap_is_prefix(map, i + 1))
384 bitmap_set_bit(map, i);
385 if (!bitmap_is_prefix(map, i + 1))
387 test_bit=get_rand_bit(bitsize);
388 bitmap_set_bit(map, test_bit);
389 if (test_bit <= i && !bitmap_is_prefix(map, i + 1))
391 else if (test_bit > i)
393 if (bitmap_is_prefix(map, i + 1))
395 bitmap_clear_bit(map, test_bit);
400 ADD_FAILURE() <<
"prefix1 error prefix_size=" << test_bit;
403 ADD_FAILURE() <<
"prefix2 error prefix_size=" << test_bit;
406 ADD_FAILURE() <<
"prefix3 error prefix_size=" << test_bit;
409 ADD_FAILURE() <<
"prefix4 error i=" <<
i;
412 ADD_FAILURE() <<
"prefix5 error i=" <<
i;
416 bool test_compare(
MY_BITMAP *map, uint bitsize)
419 my_bitmap_map map2buf[MAX_TESTED_BITMAP_SIZE];
421 uint no_loops= bitsize > 128 ? 128 : bitsize;
422 bitmap_init(&map2, map2buf, bitsize,
false);
425 for (i=0; i < no_loops; i++)
427 test_bit=get_rand_bit(bitsize);
428 bitmap_clear_bit(map, test_bit);
429 bitmap_clear_bit(&map2, test_bit);
430 if (!bitmap_is_subset(map, &map2))
431 goto error_is_subset;
432 bitmap_set_bit(map, test_bit);
433 if (bitmap_is_subset(map, &map2))
434 goto error_is_subset;
435 bitmap_set_bit(&map2, test_bit);
436 if (!bitmap_is_subset(map, &map2))
437 goto error_is_subset;
438 bitmap_clear_bit(map, test_bit);
439 if (!bitmap_is_subset(map, &map2))
440 goto error_is_subset;
443 bitmap_clear_all(map);
444 bitmap_clear_all(&map2);
446 for (i=0; i < no_loops; i++)
448 test_bit=get_rand_bit(bitsize);
449 if (bitmap_is_overlapping(map, &map2))
450 goto error_is_overlapping;
451 bitmap_set_bit(map, test_bit);
452 if (bitmap_is_overlapping(map, &map2))
453 goto error_is_overlapping;
454 bitmap_set_bit(&map2, test_bit);
455 if (!bitmap_is_overlapping(map, &map2))
456 goto error_is_overlapping;
457 bitmap_clear_bit(map, test_bit);
458 if (bitmap_is_overlapping(map, &map2))
459 goto error_is_overlapping;
460 bitmap_clear_bit(&map2, test_bit);
465 ADD_FAILURE() <<
"is_subset error";
467 error_is_overlapping:
468 ADD_FAILURE() <<
"is_overlapping error";
472 bool test_intersect(
MY_BITMAP *map, uint bitsize)
474 uint bitsize2 = 1 + get_rand_bit(MAX_TESTED_BITMAP_SIZE - 1);
476 my_bitmap_map *map2buf=
new my_bitmap_map[bitsize2];
477 uint
i, test_bit1, test_bit2, test_bit3;
478 bitmap_init(&map2, map2buf, bitsize2,
false);
480 test_bit1= get_rand_bit(bitsize);
481 test_bit2= get_rand_bit(bitsize);
482 bitmap_set_bit(map, test_bit1);
483 bitmap_set_bit(map, test_bit2);
484 test_bit3= get_rand_bit(bitsize2);
485 bitmap_set_bit(&map2, test_bit3);
486 if (test_bit2 < bitsize2)
487 bitmap_set_bit(&map2, test_bit2);
489 bitmap_intersect(map, &map2);
490 if (test_bit2 < bitsize2)
492 if (!bitmap_is_set(map, test_bit2))
494 bitmap_clear_bit(map, test_bit2);
496 if (test_bit1 == test_bit3)
498 if (!bitmap_is_set(map, test_bit1))
500 bitmap_clear_bit(map, test_bit1);
502 if (!bitmap_is_clear_all(map))
506 bitmap_set_all(&map2);
507 for (i=0; i < bitsize2; i++)
508 bitmap_clear_bit(&map2, i);
509 bitmap_intersect(map, &map2);
510 if (!bitmap_is_clear_all(map))
515 ADD_FAILURE() <<
"intersect error bit1=" << test_bit1
516 <<
",bit2=" << test_bit2 <<
",bit3=" << test_bit3;
520 #if defined(GTEST_HAS_PARAM_TEST)
522 class BitMapTest :
public ::testing::TestWithParam<uint>
528 ASSERT_FALSE(bitmap_init(&map,
buf, bitsize,
false));
529 bitmap_clear_all(&map);
533 my_bitmap_map
buf[MAX_TESTED_BITMAP_SIZE];
537 const uint test_values[]=
539 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
540 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
541 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
542 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
543 2*32
U - 1, 2*32
U, 2*32U + 1,
544 3*32U - 1, 3*32U, 3*32U + 1,
545 4*32U - 1, 4*32U, 4*32U + 1,
546 MAX_TESTED_BITMAP_SIZE
549 INSTANTIATE_TEST_CASE_P(Foo, BitMapTest,
550 ::testing::ValuesIn(test_values));
552 TEST_P(BitMapTest, TestSetGetClearBit)
554 EXPECT_FALSE(test_set_get_clear_bit(&map, bitsize)) <<
"bitsize=" << bitsize;
557 TEST_P(BitMapTest, TestFlipBit)
559 EXPECT_FALSE(test_flip_bit(&map, bitsize)) <<
"bitsize=" << bitsize;
562 TEST_P(BitMapTest, TestGetAllBits)
564 EXPECT_FALSE(test_get_all_bits(&map, bitsize)) <<
"bitsize=" << bitsize;
567 TEST_P(BitMapTest, TestCompareOperators)
569 EXPECT_FALSE(test_compare_operators(&map, bitsize)) <<
"bitsize=" << bitsize;
572 TEST_P(BitMapTest, TestCountBitsSet)
574 EXPECT_FALSE(test_count_bits_set(&map, bitsize)) <<
"bitsize=" << bitsize;
577 TEST_P(BitMapTest, TestGetFirstBit)
579 EXPECT_FALSE(test_get_first_bit(&map, bitsize)) <<
"bitsize=" << bitsize;
582 TEST_P(BitMapTest, TestSetNextBit)
584 EXPECT_FALSE(test_set_next_bit(&map, bitsize)) <<
"bitsize=" << bitsize;
587 TEST_P(BitMapTest, TestGetNextBit)
589 EXPECT_FALSE(test_get_next_bit(&map, bitsize)) <<
"bitsize=" << bitsize;
592 TEST_P(BitMapTest, TestPrefix)
594 EXPECT_FALSE(test_prefix(&map, bitsize)) <<
"bitsize=" << bitsize;
597 TEST_P(BitMapTest, TestCompare)
599 EXPECT_FALSE(test_compare(&map, bitsize)) <<
"bitsize=" << bitsize;
602 TEST_P(BitMapTest, TestIntersect)
604 EXPECT_FALSE(test_intersect(&map, bitsize)) <<
"bitsize=" << bitsize;