1
/* Copyright (C) 2006 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
16
This test was copied from the unit test inside the
17
mysys/my_bitmap.c file and adapted by Mats Kindahl to use the mytap
21
#include <my_global.h>
23
#include <my_bitmap.h>
27
uint get_rand_bit(uint bitsize)
29
return (rand() % bitsize);
32
my_bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize)
35
uint no_loops= bitsize > 128 ? 128 : bitsize;
36
for (i=0; i < no_loops; i++)
38
test_bit= get_rand_bit(bitsize);
39
bitmap_set_bit(map, test_bit);
40
if (!bitmap_is_set(map, test_bit))
42
bitmap_clear_bit(map, test_bit);
43
if (bitmap_is_set(map, test_bit))
48
printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
51
printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
55
my_bool test_flip_bit(MY_BITMAP *map, uint bitsize)
58
uint no_loops= bitsize > 128 ? 128 : bitsize;
59
for (i=0; i < no_loops; i++)
61
test_bit= get_rand_bit(bitsize);
62
bitmap_flip_bit(map, test_bit);
63
if (!bitmap_is_set(map, test_bit))
65
bitmap_flip_bit(map, test_bit);
66
if (bitmap_is_set(map, test_bit))
71
printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
74
printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
78
my_bool test_operators(MY_BITMAP *map __attribute__((unused)),
79
uint bitsize __attribute__((unused)))
84
my_bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
88
if (!bitmap_is_set_all(map))
90
if (!bitmap_is_prefix(map, bitsize))
92
bitmap_clear_all(map);
93
if (!bitmap_is_clear_all(map))
95
if (!bitmap_is_prefix(map, 0))
97
for (i=0; i<bitsize;i++)
98
bitmap_set_bit(map, i);
99
if (!bitmap_is_set_all(map))
101
for (i=0; i<bitsize;i++)
102
bitmap_clear_bit(map, i);
103
if (!bitmap_is_clear_all(map))
107
diag("Error in set_all, bitsize = %u", bitsize);
110
diag("Error in clear_all, bitsize = %u", bitsize);
113
diag("Error in bitmap_is_set_all, bitsize = %u", bitsize);
116
diag("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
119
diag("Error in set_all through set_prefix, bitsize = %u", bitsize);
122
diag("Error in clear_all through set_prefix, bitsize = %u", bitsize);
126
my_bool test_compare_operators(MY_BITMAP *map, uint bitsize)
128
uint i, j, test_bit1, test_bit2, test_bit3,test_bit4;
129
uint no_loops= bitsize > 128 ? 128 : bitsize;
130
MY_BITMAP map2_obj, map3_obj;
131
MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
132
uint32 map2buf[1024];
133
uint32 map3buf[1024];
134
bitmap_init(&map2_obj, map2buf, bitsize, FALSE);
135
bitmap_init(&map3_obj, map3buf, bitsize, FALSE);
136
bitmap_clear_all(map2);
137
bitmap_clear_all(map3);
138
for (i=0; i < no_loops; i++)
140
test_bit1=get_rand_bit(bitsize);
141
bitmap_set_prefix(map, test_bit1);
142
test_bit2=get_rand_bit(bitsize);
143
bitmap_set_prefix(map2, test_bit2);
144
bitmap_intersect(map, map2);
145
test_bit3= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
146
bitmap_set_prefix(map3, test_bit3);
147
if (!bitmap_cmp(map, map3))
149
bitmap_clear_all(map);
150
bitmap_clear_all(map2);
151
bitmap_clear_all(map3);
152
test_bit1=get_rand_bit(bitsize);
153
test_bit2=get_rand_bit(bitsize);
154
test_bit3=get_rand_bit(bitsize);
155
bitmap_set_prefix(map, test_bit1);
156
bitmap_set_prefix(map2, test_bit2);
157
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
158
bitmap_set_prefix(map3, test_bit3);
159
bitmap_union(map, map2);
160
if (!bitmap_cmp(map, map3))
162
bitmap_clear_all(map);
163
bitmap_clear_all(map2);
164
bitmap_clear_all(map3);
165
test_bit1=get_rand_bit(bitsize);
166
test_bit2=get_rand_bit(bitsize);
167
test_bit3=get_rand_bit(bitsize);
168
bitmap_set_prefix(map, test_bit1);
169
bitmap_set_prefix(map2, test_bit2);
170
bitmap_xor(map, map2);
171
test_bit3= test_bit2 > test_bit1 ? test_bit2 : test_bit1;
172
test_bit4= test_bit2 < test_bit1 ? test_bit2 : test_bit1;
173
bitmap_set_prefix(map3, test_bit3);
174
for (j=0; j < test_bit4; j++)
175
bitmap_clear_bit(map3, j);
176
if (!bitmap_cmp(map, map3))
178
bitmap_clear_all(map);
179
bitmap_clear_all(map2);
180
bitmap_clear_all(map3);
181
test_bit1=get_rand_bit(bitsize);
182
test_bit2=get_rand_bit(bitsize);
183
test_bit3=get_rand_bit(bitsize);
184
bitmap_set_prefix(map, test_bit1);
185
bitmap_set_prefix(map2, test_bit2);
186
bitmap_subtract(map, map2);
187
if (test_bit2 < test_bit1)
189
bitmap_set_prefix(map3, test_bit1);
190
for (j=0; j < test_bit2; j++)
191
bitmap_clear_bit(map3, j);
193
if (!bitmap_cmp(map, map3))
195
bitmap_clear_all(map);
196
bitmap_clear_all(map2);
197
bitmap_clear_all(map3);
198
test_bit1=get_rand_bit(bitsize);
199
bitmap_set_prefix(map, test_bit1);
201
bitmap_set_all(map3);
202
for (j=0; j < test_bit1; j++)
203
bitmap_clear_bit(map3, j);
204
if (!bitmap_cmp(map, map3))
206
bitmap_clear_all(map);
207
bitmap_clear_all(map3);
211
diag("intersect error bitsize=%u,size1=%u,size2=%u", bitsize,
212
test_bit1,test_bit2);
215
diag("union error bitsize=%u,size1=%u,size2=%u", bitsize,
216
test_bit1,test_bit2);
219
diag("xor error bitsize=%u,size1=%u,size2=%u", bitsize,
220
test_bit1,test_bit2);
223
diag("subtract error bitsize=%u,size1=%u,size2=%u", bitsize,
224
test_bit1,test_bit2);
227
diag("invert error bitsize=%u,size=%u", bitsize,
232
my_bool test_count_bits_set(MY_BITMAP *map, uint bitsize)
234
uint i, bit_count=0, test_bit;
235
uint no_loops= bitsize > 128 ? 128 : bitsize;
236
for (i=0; i < no_loops; i++)
238
test_bit=get_rand_bit(bitsize);
239
if (!bitmap_is_set(map, test_bit))
241
bitmap_set_bit(map, test_bit);
245
if (bit_count==0 && bitsize > 0)
247
if (bitmap_bits_set(map) != bit_count)
251
diag("No bits set bitsize = %u", bitsize);
254
diag("Wrong count of bits set, bitsize = %u", bitsize);
258
my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
261
uint no_loops= bitsize > 128 ? 128 : bitsize;
262
for (i=0; i < no_loops; i++)
264
test_bit=get_rand_bit(bitsize);
265
bitmap_set_bit(map, test_bit);
266
if (bitmap_get_first_set(map) != test_bit)
269
bitmap_clear_bit(map, test_bit);
270
if (bitmap_get_first(map) != test_bit)
272
bitmap_clear_all(map);
276
diag("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
279
diag("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
283
my_bool test_get_next_bit(MY_BITMAP *map, uint bitsize)
286
uint no_loops= bitsize > 128 ? 128 : bitsize;
287
for (i=0; i < no_loops; i++)
289
test_bit=get_rand_bit(bitsize);
290
for (j=0; j < test_bit; j++)
291
bitmap_set_next(map);
292
if (!bitmap_is_prefix(map, test_bit))
294
bitmap_clear_all(map);
298
diag("get_next error bitsize= %u, prefix_size= %u", bitsize,test_bit);
302
my_bool test_prefix(MY_BITMAP *map, uint bitsize)
305
uint no_loops= bitsize > 128 ? 128 : bitsize;
306
for (i=0; i < no_loops; i++)
308
test_bit=get_rand_bit(bitsize);
309
bitmap_set_prefix(map, test_bit);
310
if (!bitmap_is_prefix(map, test_bit))
312
bitmap_clear_all(map);
313
for (j=0; j < test_bit; j++)
314
bitmap_set_bit(map, j);
315
if (!bitmap_is_prefix(map, test_bit))
318
for (j=bitsize - 1; ~(j-test_bit); j--)
319
bitmap_clear_bit(map, j);
320
if (!bitmap_is_prefix(map, test_bit))
322
bitmap_clear_all(map);
326
diag("prefix1 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
329
diag("prefix2 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
332
diag("prefix3 error bitsize = %u, prefix_size = %u", bitsize,test_bit);
337
my_bool do_test(uint bitsize)
341
if (bitmap_init(&map, buf, bitsize, FALSE))
343
diag("init error for bitsize %d", bitsize);
346
if (test_set_get_clear_bit(&map,bitsize))
348
bitmap_clear_all(&map);
349
if (test_flip_bit(&map,bitsize))
351
bitmap_clear_all(&map);
352
if (test_operators(&map,bitsize))
354
bitmap_clear_all(&map);
355
if (test_get_all_bits(&map, bitsize))
357
bitmap_clear_all(&map);
358
if (test_compare_operators(&map,bitsize))
360
bitmap_clear_all(&map);
361
if (test_count_bits_set(&map,bitsize))
363
bitmap_clear_all(&map);
364
if (test_get_first_bit(&map,bitsize))
366
bitmap_clear_all(&map);
367
if (test_get_next_bit(&map,bitsize))
369
if (test_prefix(&map,bitsize))
379
int const min_size = 1;
380
int const max_size = 1024;
383
plan(max_size - min_size);
384
for (i= min_size; i < max_size; i++)
385
ok(do_test(i) == 0, "bitmap size %d", i);
386
return exit_status();