1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Handling of uchar arrays as large bitmaps.
19
API limitations (or, rather asserted safety assumptions,
20
to encourage correct programming)
22
* the internal size is a set of 32 bit words
23
* the number of bits specified in creation can be any number > 0
24
* there are THREAD safe versions of most calls called bitmap_lock_*
25
many of those are not used and not compiled normally but the code
26
already exist for them in an #ifdef:ed part. These can only be used
27
if THREAD was specified in bitmap_init
30
Make assembler THREAD safe versions of these using test-and-set instructions
32
Original version created by Sergei Golubchik 2001 - 2004.
33
New version written and test program added and some changes to the interface
34
was made by Mikael Ronström 2005, with assistance of Tomas Ulin and Mats
38
#include "mysys_priv.h"
39
#include <my_bitmap.h>
43
void create_last_word_mask(MY_BITMAP *map)
45
/* Get the number of used bits (1..8) in the last byte */
46
unsigned int const used= 1U + ((map->n_bits-1U) & 0x7U);
49
Create a mask with the upper 'unused' bits set and the lower 'used'
50
bits clear. The bits within each byte is stored in big-endian order.
52
unsigned char const mask= (~((1 << used) - 1)) & 255;
55
The first bytes are to be set to zero since they represent real bits
56
in the bitvector. The last bytes are set to 0xFF since they represent
57
bytes not used by the bitvector. Finally the last byte contains bits
58
as set by the mask above.
60
unsigned char *ptr= (unsigned char*)&map->last_word_mask;
62
map->last_word_ptr= map->bitmap + no_words_in_map(map)-1;
63
switch (no_bytes_in_map(map) & 3) {
65
map->last_word_mask= ~0U;
69
map->last_word_mask= ~0U;
74
map->last_word_mask= 0U;
79
map->last_word_mask= 0U;
86
static inline void bitmap_lock(MY_BITMAP *map __attribute__((unused)))
90
pthread_mutex_lock(map->mutex);
94
static inline void bitmap_unlock(MY_BITMAP *map __attribute__((unused)))
98
pthread_mutex_unlock(map->mutex);
103
my_bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint n_bits,
104
my_bool thread_safe __attribute__((unused)))
106
DBUG_ENTER("bitmap_init");
109
uint size_in_bytes= bitmap_buffer_size(n_bits);
114
size_in_bytes= ALIGN_SIZE(size_in_bytes);
115
extra= sizeof(pthread_mutex_t);
119
if (!(buf= (my_bitmap_map*) my_malloc(size_in_bytes+extra, MYF(MY_WME))))
124
map->mutex= (pthread_mutex_t *) ((char*) buf + size_in_bytes);
125
pthread_mutex_init(map->mutex, MY_MUTEX_INIT_FAST);
132
DBUG_ASSERT(thread_safe == 0);
138
create_last_word_mask(map);
139
bitmap_clear_all(map);
144
void bitmap_free(MY_BITMAP *map)
146
DBUG_ENTER("bitmap_free");
151
pthread_mutex_destroy(map->mutex);
153
my_free((char*) map->bitmap, MYF(0));
161
test if bit already set and set it if it was not (thread unsafe method)
164
bitmap_fast_test_and_set()
173
my_bool bitmap_fast_test_and_set(MY_BITMAP *map, uint bitmap_bit)
175
uchar *value= ((uchar*) map->bitmap) + (bitmap_bit / 8);
176
uchar bit= 1 << ((bitmap_bit) & 7);
177
uchar res= (*value) & bit;
184
test if bit already set and set it if it was not (thread safe method)
187
bitmap_fast_test_and_set()
189
bitmap_bit bit number
196
my_bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit)
199
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
201
res= bitmap_fast_test_and_set(map, bitmap_bit);
207
test if bit already set and clear it if it was set(thread unsafe method)
210
bitmap_fast_test_and_set()
219
my_bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
221
uchar *byte= (uchar*) map->bitmap + (bitmap_bit / 8);
222
uchar bit= 1 << ((bitmap_bit) & 7);
223
uchar res= (*byte) & bit;
229
my_bool bitmap_test_and_clear(MY_BITMAP *map, uint bitmap_bit)
232
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
234
res= bitmap_fast_test_and_clear(map, bitmap_bit);
240
uint bitmap_set_next(MY_BITMAP *map)
243
DBUG_ASSERT(map->bitmap);
244
if ((bit_found= bitmap_get_first(map)) != MY_BIT_NONE)
245
bitmap_set_bit(map, bit_found);
250
void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size)
252
uint prefix_bytes, prefix_bits, d;
253
uchar *m= (uchar *)map->bitmap;
255
DBUG_ASSERT(map->bitmap &&
256
(prefix_size <= map->n_bits || prefix_size == (uint) ~0));
257
set_if_smaller(prefix_size, map->n_bits);
258
if ((prefix_bytes= prefix_size / 8))
259
memset(m, 0xff, prefix_bytes);
261
if ((prefix_bits= prefix_size & 7))
262
*m++= (1 << prefix_bits)-1;
263
if ((d= no_bytes_in_map(map)-prefix_bytes))
268
my_bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size)
270
uint prefix_bits= prefix_size & 0x7, res;
271
uchar *m= (uchar*)map->bitmap;
272
uchar *end_prefix= m+prefix_size/8;
274
DBUG_ASSERT(m && prefix_size <= map->n_bits);
275
end= m+no_bytes_in_map(map);
277
while (m < end_prefix)
281
*map->last_word_ptr&= ~map->last_word_mask; /*Clear bits*/
283
if (prefix_bits && *m++ != (1 << prefix_bits)-1)
295
my_bool bitmap_is_set_all(const MY_BITMAP *map)
297
my_bitmap_map *data_ptr= map->bitmap;
298
my_bitmap_map *end= map->last_word_ptr;
299
*map->last_word_ptr |= map->last_word_mask;
300
for (; data_ptr <= end; data_ptr++)
301
if (*data_ptr != 0xFFFFFFFF)
307
my_bool bitmap_is_clear_all(const MY_BITMAP *map)
309
my_bitmap_map *data_ptr= map->bitmap;
311
if (*map->last_word_ptr & ~map->last_word_mask)
313
end= map->last_word_ptr;
314
for (; data_ptr < end; data_ptr++)
320
/* Return TRUE if map1 is a subset of map2 */
322
my_bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
324
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
326
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
327
map1->n_bits==map2->n_bits);
329
end= map1->last_word_ptr;
330
*map1->last_word_ptr &= ~map1->last_word_mask;
331
*map2->last_word_ptr &= ~map2->last_word_mask;
334
if ((*m1++) & ~(*m2++))
340
/* True if bitmaps has any common bits */
342
my_bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
344
my_bitmap_map *m1= map1->bitmap, *m2= map2->bitmap, *end;
346
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
347
map1->n_bits==map2->n_bits);
349
end= map1->last_word_ptr;
350
*map1->last_word_ptr &= ~map1->last_word_mask;
351
*map2->last_word_ptr &= ~map2->last_word_mask;
354
if ((*m1++) & (*m2++))
361
void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
363
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
364
uint len= no_words_in_map(map), len2 = no_words_in_map(map2);
366
DBUG_ASSERT(map->bitmap && map2->bitmap);
368
end= to+min(len,len2);
369
*map2->last_word_ptr&= ~map2->last_word_mask; /*Clear last bits in map2*/
383
Set/clear all bits above a bit.
387
map RETURN The bitmap to change.
388
from_byte The bitmap buffer byte offset to start with.
389
use_bit The bit value (1/0) to use for all upper bits.
392
You can only set/clear full bytes.
393
The function is meant for the situation that you copy a smaller bitmap
394
to a bigger bitmap. Bitmap lengths are always multiple of eigth (the
395
size of a byte). Using 'from_byte' saves multiplication and division
396
by eight during parameter passing.
402
void bitmap_set_above(MY_BITMAP *map, uint from_byte, uint use_bit)
404
uchar use_byte= use_bit ? 0xff : 0;
405
uchar *to= (uchar *)map->bitmap + from_byte;
406
uchar *end= (uchar *)map->bitmap + (map->n_bits+7)/8;
413
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
415
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
416
DBUG_ASSERT(map->bitmap && map2->bitmap &&
417
map->n_bits==map2->n_bits);
419
end= map->last_word_ptr;
426
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
428
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
430
DBUG_ASSERT(map->bitmap && map2->bitmap &&
431
map->n_bits==map2->n_bits);
432
end= map->last_word_ptr;
439
void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2)
441
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end= map->last_word_ptr;
442
DBUG_ASSERT(map->bitmap && map2->bitmap &&
443
map->n_bits==map2->n_bits);
449
void bitmap_invert(MY_BITMAP *map)
451
my_bitmap_map *to= map->bitmap, *end;
453
DBUG_ASSERT(map->bitmap);
454
end= map->last_word_ptr;
461
uint bitmap_bits_set(const MY_BITMAP *map)
463
uchar *m= (uchar*)map->bitmap;
464
uchar *end= m + no_bytes_in_map(map);
467
DBUG_ASSERT(map->bitmap);
468
*map->last_word_ptr&= ~map->last_word_mask; /*Reset last bits to zero*/
470
res+= my_count_bits_ushort(*m++);
475
void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2)
477
my_bitmap_map *to= map->bitmap, *from= map2->bitmap, *end;
479
DBUG_ASSERT(map->bitmap && map2->bitmap &&
480
map->n_bits==map2->n_bits);
481
end= map->last_word_ptr;
487
uint bitmap_get_first_set(const MY_BITMAP *map)
491
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
493
DBUG_ASSERT(map->bitmap);
494
data_ptr= map->bitmap;
495
*map->last_word_ptr &= ~map->last_word_mask;
497
for (i=0; data_ptr <= end; data_ptr++, i++)
501
byte_ptr= (uchar*)data_ptr;
502
for (j=0; ; j++, byte_ptr++)
508
if (*byte_ptr & (1 << k))
509
return (i*32) + (j*8) + k;
521
uint bitmap_get_first(const MY_BITMAP *map)
525
my_bitmap_map *data_ptr, *end= map->last_word_ptr;
527
DBUG_ASSERT(map->bitmap);
528
data_ptr= map->bitmap;
529
*map->last_word_ptr|= map->last_word_mask;
531
for (i=0; data_ptr <= end; data_ptr++, i++)
533
if (*data_ptr != 0xFFFFFFFF)
535
byte_ptr= (uchar*)data_ptr;
536
for (j=0; ; j++, byte_ptr++)
538
if (*byte_ptr != 0xFF)
542
if (!(*byte_ptr & (1 << k)))
543
return (i*32) + (j*8) + k;
555
uint bitmap_lock_set_next(MY_BITMAP *map)
559
bit_found= bitmap_set_next(map);
565
void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit)
568
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
569
bitmap_clear_bit(map, bitmap_bit);
575
my_bool bitmap_lock_is_prefix(const MY_BITMAP *map, uint prefix_size)
578
bitmap_lock((MY_BITMAP *)map);
579
res= bitmap_is_prefix(map, prefix_size);
580
bitmap_unlock((MY_BITMAP *)map);
585
void bitmap_lock_set_all(MY_BITMAP *map)
593
void bitmap_lock_clear_all(MY_BITMAP *map)
596
bitmap_clear_all(map);
601
void bitmap_lock_set_prefix(MY_BITMAP *map, uint prefix_size)
604
bitmap_set_prefix(map, prefix_size);
609
my_bool bitmap_lock_is_clear_all(const MY_BITMAP *map)
612
bitmap_lock((MY_BITMAP *)map);
613
res= bitmap_is_clear_all(map);
614
bitmap_unlock((MY_BITMAP *)map);
619
my_bool bitmap_lock_is_set_all(const MY_BITMAP *map)
622
bitmap_lock((MY_BITMAP *)map);
623
res= bitmap_is_set_all(map);
624
bitmap_unlock((MY_BITMAP *)map);
629
my_bool bitmap_lock_is_set(const MY_BITMAP *map, uint bitmap_bit)
632
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
633
bitmap_lock((MY_BITMAP *)map);
634
res= bitmap_is_set(map, bitmap_bit);
635
bitmap_unlock((MY_BITMAP *)map);
640
my_bool bitmap_lock_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
643
bitmap_lock((MY_BITMAP *)map1);
644
bitmap_lock((MY_BITMAP *)map2);
645
res= bitmap_is_subset(map1, map2);
646
bitmap_unlock((MY_BITMAP *)map2);
647
bitmap_unlock((MY_BITMAP *)map1);
652
my_bool bitmap_lock_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
656
DBUG_ASSERT(map1->bitmap && map2->bitmap &&
657
map1->n_bits==map2->n_bits);
658
bitmap_lock((MY_BITMAP *)map1);
659
bitmap_lock((MY_BITMAP *)map2);
660
res= bitmap_cmp(map1, map2);
661
bitmap_unlock((MY_BITMAP *)map2);
662
bitmap_unlock((MY_BITMAP *)map1);
667
void bitmap_lock_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
670
bitmap_lock((MY_BITMAP *)map2);
671
bitmap_intersect(map, map2);
672
bitmap_unlock((MY_BITMAP *)map2);
677
void bitmap_lock_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
680
bitmap_lock((MY_BITMAP *)map2);
681
bitmap_subtract(map, map2);
682
bitmap_unlock((MY_BITMAP *)map2);
687
void bitmap_lock_union(MY_BITMAP *map, const MY_BITMAP *map2)
690
bitmap_lock((MY_BITMAP *)map2);
691
bitmap_union(map, map2);
692
bitmap_unlock((MY_BITMAP *)map2);
702
Number of set bits in the bitmap.
704
uint bitmap_lock_bits_set(const MY_BITMAP *map)
707
bitmap_lock((MY_BITMAP *)map);
708
DBUG_ASSERT(map->bitmap);
709
res= bitmap_bits_set(map);
710
bitmap_unlock((MY_BITMAP *)map);
720
Number of first unset bit in the bitmap or MY_BIT_NONE if all bits are set.
722
uint bitmap_lock_get_first(const MY_BITMAP *map)
725
bitmap_lock((MY_BITMAP*)map);
726
res= bitmap_get_first(map);
727
bitmap_unlock((MY_BITMAP*)map);
732
uint bitmap_lock_get_first_set(const MY_BITMAP *map)
735
bitmap_lock((MY_BITMAP*)map);
736
res= bitmap_get_first_set(map);
737
bitmap_unlock((MY_BITMAP*)map);
742
void bitmap_lock_set_bit(MY_BITMAP *map, uint bitmap_bit)
744
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
746
bitmap_set_bit(map, bitmap_bit);
751
void bitmap_lock_flip_bit(MY_BITMAP *map, uint bitmap_bit)
753
DBUG_ASSERT(map->bitmap && bitmap_bit < map->n_bits);
755
bitmap_flip_bit(map, bitmap_bit);
761
uint get_rand_bit(uint bitsize)
763
return (rand() % bitsize);
766
bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize)
769
uint no_loops= bitsize > 128 ? 128 : bitsize;
770
for (i=0; i < no_loops; i++)
772
test_bit= get_rand_bit(bitsize);
773
bitmap_set_bit(map, test_bit);
774
if (!bitmap_is_set(map, test_bit))
776
bitmap_clear_bit(map, test_bit);
777
if (bitmap_is_set(map, test_bit))
782
printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
785
printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
789
bool test_flip_bit(MY_BITMAP *map, uint bitsize)
792
uint no_loops= bitsize > 128 ? 128 : bitsize;
793
for (i=0; i < no_loops; i++)
795
test_bit= get_rand_bit(bitsize);
796
bitmap_flip_bit(map, test_bit);
797
if (!bitmap_is_set(map, test_bit))
799
bitmap_flip_bit(map, test_bit);
800
if (bitmap_is_set(map, test_bit))
805
printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
808
printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
812
bool test_operators(MY_BITMAP *map __attribute__((unused)),
813
uint bitsize __attribute__((unused)))
818
bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
822
if (!bitmap_is_set_all(map))
824
if (!bitmap_is_prefix(map, bitsize))
826
bitmap_clear_all(map);
827
if (!bitmap_is_clear_all(map))
829
if (!bitmap_is_prefix(map, 0))
831
for (i=0; i<bitsize;i++)
832
bitmap_set_bit(map, i);
833
if (!bitmap_is_set_all(map))
835
for (i=0; i<bitsize;i++)
836
bitmap_clear_bit(map, i);
837
if (!bitmap_is_clear_all(map))
841
printf("Error in set_all, bitsize = %u", bitsize);
844
printf("Error in clear_all, bitsize = %u", bitsize);
847
printf("Error in bitmap_is_set_all, bitsize = %u", bitsize);
850
printf("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
853
printf("Error in set_all through set_prefix, bitsize = %u", bitsize);
856
printf("Error in clear_all through set_prefix, bitsize = %u", bitsize);
860
bool test_compare_operators(MY_BITMAP *map, uint bitsize)
862
uint i, j, test_bit1, test_bit2, test_bit3,test_bit4;
863
uint no_loops= bitsize > 128 ? 128 : bitsize;
864
MY_BITMAP map2_obj, map3_obj;
865
MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
866
my_bitmap_map map2buf[1024];
867
my_bitmap_map map3buf[1024];
868
bitmap_init(&map2_obj, map2buf, bitsize, FALSE);
869
bitmap_init(&map3_obj, map3buf, bitsize, FALSE);
870
bitmap_clear_all(map2);
871
bitmap_clear_all(map3);
872
for (i=0; i < no_loops; i++)
874
test_bit1=get_rand_bit(bitsize);
875
bitmap_set_prefix(map, test_bit1);
876
test_bit2=get_rand_bit(bitsize);
877
bitmap_set_prefix(map2, test_bit2);
878
bitmap_intersect(map, map2);
879
test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
880
bitmap_set_prefix(map3, test_bit3);
881
if (!bitmap_cmp(map, map3))
883
bitmap_clear_all(map);
884
bitmap_clear_all(map2);
885
bitmap_clear_all(map3);
886
test_bit1=get_rand_bit(bitsize);
887
test_bit2=get_rand_bit(bitsize);
888
test_bit3=get_rand_bit(bitsize);
889
bitmap_set_prefix(map, test_bit1);
890
bitmap_set_prefix(map2, test_bit2);
891
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
892
bitmap_set_prefix(map3, test_bit3);
893
bitmap_union(map, map2);
894
if (!bitmap_cmp(map, map3))
896
bitmap_clear_all(map);
897
bitmap_clear_all(map2);
898
bitmap_clear_all(map3);
899
test_bit1=get_rand_bit(bitsize);
900
test_bit2=get_rand_bit(bitsize);
901
test_bit3=get_rand_bit(bitsize);
902
bitmap_set_prefix(map, test_bit1);
903
bitmap_set_prefix(map2, test_bit2);
904
bitmap_xor(map, map2);
905
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
906
test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
907
bitmap_set_prefix(map3, test_bit3);
908
for (j=0; j < test_bit4; j++)
909
bitmap_clear_bit(map3, j);
910
if (!bitmap_cmp(map, map3))
912
bitmap_clear_all(map);
913
bitmap_clear_all(map2);
914
bitmap_clear_all(map3);
915
test_bit1=get_rand_bit(bitsize);
916
test_bit2=get_rand_bit(bitsize);
917
test_bit3=get_rand_bit(bitsize);
918
bitmap_set_prefix(map, test_bit1);
919
bitmap_set_prefix(map2, test_bit2);
920
bitmap_subtract(map, map2);
921
if (test_bit2 < test_bit1)
923
bitmap_set_prefix(map3, test_bit1);
924
for (j=0; j < test_bit2; j++)
925
bitmap_clear_bit(map3, j);
927
if (!bitmap_cmp(map, map3))
929
bitmap_clear_all(map);
930
bitmap_clear_all(map2);
931
bitmap_clear_all(map3);
932
test_bit1=get_rand_bit(bitsize);
933
bitmap_set_prefix(map, test_bit1);
935
bitmap_set_all(map3);
936
for (j=0; j < test_bit1; j++)
937
bitmap_clear_bit(map3, j);
938
if (!bitmap_cmp(map, map3))
940
bitmap_clear_all(map);
941
bitmap_clear_all(map3);
945
printf("intersect error bitsize=%u,size1=%u,size2=%u", bitsize,
946
test_bit1,test_bit2);
949
printf("union error bitsize=%u,size1=%u,size2=%u", bitsize,
950
test_bit1,test_bit2);
953
printf("xor error bitsize=%u,size1=%u,size2=%u", bitsize,
954
test_bit1,test_bit2);
957
printf("subtract error bitsize=%u,size1=%u,size2=%u", bitsize,
958
test_bit1,test_bit2);
961
printf("invert error bitsize=%u,size=%u", bitsize,
966
bool test_count_bits_set(MY_BITMAP *map, uint bitsize)
968
uint i, bit_count=0, test_bit;
969
uint no_loops= bitsize > 128 ? 128 : bitsize;
970
for (i=0; i < no_loops; i++)
972
test_bit=get_rand_bit(bitsize);
973
if (!bitmap_is_set(map, test_bit))
975
bitmap_set_bit(map, test_bit);
979
if (bit_count==0 && bitsize > 0)
981
if (bitmap_bits_set(map) != bit_count)
985
printf("No bits set bitsize = %u", bitsize);
988
printf("Wrong count of bits set, bitsize = %u", bitsize);
992
bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
995
uint no_loops= bitsize > 128 ? 128 : bitsize;
996
for (i=0; i < no_loops; i++)
998
test_bit=get_rand_bit(bitsize);
999
bitmap_set_bit(map, test_bit);
1000
if (bitmap_get_first_set(map) != test_bit)
1002
bitmap_set_all(map);
1003
bitmap_clear_bit(map, test_bit);
1004
if (bitmap_get_first(map) != test_bit)
1006
bitmap_clear_all(map);
1010
printf("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
1013
printf("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
1017
bool test_get_next_bit(MY_BITMAP *map, uint bitsize)
1019
uint i, j, test_bit;
1020
uint no_loops= bitsize > 128 ? 128 : bitsize;
1021
for (i=0; i < no_loops; i++)
1023
test_bit=get_rand_bit(bitsize);
1024
for (j=0; j < test_bit; j++)
1025
bitmap_set_next(map);
1026
if (!bitmap_is_prefix(map, test_bit))
1028
bitmap_clear_all(map);
1032
printf("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit);
1036
bool test_prefix(MY_BITMAP *map, uint bitsize)
1038
uint i, j, test_bit;
1039
uint no_loops= bitsize > 128 ? 128 : bitsize;
1040
for (i=0; i < no_loops; i++)
1042
test_bit=get_rand_bit(bitsize);
1043
bitmap_set_prefix(map, test_bit);
1044
if (!bitmap_is_prefix(map, test_bit))
1046
bitmap_clear_all(map);
1047
for (j=0; j < test_bit; j++)
1048
bitmap_set_bit(map, j);
1049
if (!bitmap_is_prefix(map, test_bit))
1051
bitmap_set_all(map);
1052
for (j=bitsize - 1; ~(j-test_bit); j--)
1053
bitmap_clear_bit(map, j);
1054
if (!bitmap_is_prefix(map, test_bit))
1056
bitmap_clear_all(map);
1060
printf("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1063
printf("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1066
printf("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
1071
bool do_test(uint bitsize)
1074
my_bitmap_map buf[1024];
1075
if (bitmap_init(&map, buf, bitsize, FALSE))
1077
printf("init error for bitsize %d", bitsize);
1080
if (test_set_get_clear_bit(&map,bitsize))
1082
bitmap_clear_all(&map);
1083
if (test_flip_bit(&map,bitsize))
1085
bitmap_clear_all(&map);
1086
if (test_operators(&map,bitsize))
1088
bitmap_clear_all(&map);
1089
if (test_get_all_bits(&map, bitsize))
1091
bitmap_clear_all(&map);
1092
if (test_compare_operators(&map,bitsize))
1094
bitmap_clear_all(&map);
1095
if (test_count_bits_set(&map,bitsize))
1097
bitmap_clear_all(&map);
1098
if (test_get_first_bit(&map,bitsize))
1100
bitmap_clear_all(&map);
1101
if (test_get_next_bit(&map,bitsize))
1103
if (test_prefix(&map,bitsize))
1114
for (i= 1; i < 4096; i++)
1116
printf("Start test for bitsize=%u\n",i);
1127
will build the bitmap tests and ./test_bitmap will execute it