~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to unittest/mysys/bitmap-t.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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
 
15
 
 
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
 
18
   library.
 
19
*/
 
20
 
 
21
#include <my_global.h>
 
22
#include <my_sys.h>
 
23
#include <my_bitmap.h>
 
24
#include <tap.h>
 
25
#include <m_string.h>
 
26
 
 
27
uint get_rand_bit(uint bitsize)
 
28
{
 
29
  return (rand() % bitsize);
 
30
}
 
31
 
 
32
my_bool test_set_get_clear_bit(MY_BITMAP *map, uint bitsize)
 
33
{
 
34
  uint i, test_bit;
 
35
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
36
  for (i=0; i < no_loops; i++)
 
37
  {
 
38
    test_bit= get_rand_bit(bitsize);
 
39
    bitmap_set_bit(map, test_bit);
 
40
    if (!bitmap_is_set(map, test_bit))
 
41
      goto error1;
 
42
    bitmap_clear_bit(map, test_bit);
 
43
    if (bitmap_is_set(map, test_bit))
 
44
      goto error2;
 
45
  }
 
46
  return FALSE;
 
47
error1:
 
48
  printf("Error in set bit, bit %u, bitsize = %u", test_bit, bitsize);
 
49
  return TRUE;
 
50
error2:
 
51
  printf("Error in clear bit, bit %u, bitsize = %u", test_bit, bitsize);
 
52
  return TRUE;
 
53
}
 
54
 
 
55
my_bool test_flip_bit(MY_BITMAP *map, uint bitsize)
 
56
{
 
57
  uint i, test_bit;
 
58
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
59
  for (i=0; i < no_loops; i++)
 
60
  {
 
61
    test_bit= get_rand_bit(bitsize);
 
62
    bitmap_flip_bit(map, test_bit);
 
63
    if (!bitmap_is_set(map, test_bit))
 
64
      goto error1;
 
65
    bitmap_flip_bit(map, test_bit);
 
66
    if (bitmap_is_set(map, test_bit))
 
67
      goto error2;
 
68
  }
 
69
  return FALSE;
 
70
error1:
 
71
  printf("Error in flip bit 1, bit %u, bitsize = %u", test_bit, bitsize);
 
72
  return TRUE;
 
73
error2:
 
74
  printf("Error in flip bit 2, bit %u, bitsize = %u", test_bit, bitsize);
 
75
  return TRUE;
 
76
}
 
77
 
 
78
my_bool test_operators(MY_BITMAP *map __attribute__((unused)),
 
79
                       uint bitsize __attribute__((unused)))
 
80
{
 
81
  return FALSE;
 
82
}
 
83
 
 
84
my_bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
 
85
{
 
86
  uint i;
 
87
  bitmap_set_all(map);
 
88
  if (!bitmap_is_set_all(map))
 
89
    goto error1;
 
90
  if (!bitmap_is_prefix(map, bitsize))
 
91
    goto error5;
 
92
  bitmap_clear_all(map);
 
93
  if (!bitmap_is_clear_all(map))
 
94
    goto error2;
 
95
  if (!bitmap_is_prefix(map, 0))
 
96
    goto error6;
 
97
  for (i=0; i<bitsize;i++)
 
98
    bitmap_set_bit(map, i);
 
99
  if (!bitmap_is_set_all(map))
 
100
    goto error3;
 
101
  for (i=0; i<bitsize;i++)
 
102
    bitmap_clear_bit(map, i);
 
103
  if (!bitmap_is_clear_all(map))
 
104
    goto error4;
 
105
  return FALSE;
 
106
error1:
 
107
  diag("Error in set_all, bitsize = %u", bitsize);
 
108
  return TRUE;
 
109
error2:
 
110
  diag("Error in clear_all, bitsize = %u", bitsize);
 
111
  return TRUE;
 
112
error3:
 
113
  diag("Error in bitmap_is_set_all, bitsize = %u", bitsize);
 
114
  return TRUE;
 
115
error4:
 
116
  diag("Error in bitmap_is_clear_all, bitsize = %u", bitsize);
 
117
  return TRUE;
 
118
error5:
 
119
  diag("Error in set_all through set_prefix, bitsize = %u", bitsize);
 
120
  return TRUE;
 
121
error6:
 
122
  diag("Error in clear_all through set_prefix, bitsize = %u", bitsize);
 
123
  return TRUE;
 
124
}
 
125
 
 
126
my_bool test_compare_operators(MY_BITMAP *map, uint bitsize)
 
127
{
 
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++)
 
139
  {
 
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))
 
148
      goto error1;
 
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))
 
161
      goto error2;
 
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))
 
177
      goto error3;
 
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)
 
188
    {
 
189
      bitmap_set_prefix(map3, test_bit1);
 
190
      for (j=0; j < test_bit2; j++)
 
191
        bitmap_clear_bit(map3, j);
 
192
    }
 
193
    if (!bitmap_cmp(map, map3))
 
194
      goto error4;
 
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);
 
200
    bitmap_invert(map);
 
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))
 
205
      goto error5;
 
206
    bitmap_clear_all(map);
 
207
    bitmap_clear_all(map3);
 
208
  }
 
209
  return FALSE;
 
210
error1:
 
211
  diag("intersect error  bitsize=%u,size1=%u,size2=%u", bitsize,
 
212
  test_bit1,test_bit2);
 
213
  return TRUE;
 
214
error2:
 
215
  diag("union error  bitsize=%u,size1=%u,size2=%u", bitsize,
 
216
  test_bit1,test_bit2);
 
217
  return TRUE;
 
218
error3:
 
219
  diag("xor error  bitsize=%u,size1=%u,size2=%u", bitsize,
 
220
  test_bit1,test_bit2);
 
221
  return TRUE;
 
222
error4:
 
223
  diag("subtract error  bitsize=%u,size1=%u,size2=%u", bitsize,
 
224
  test_bit1,test_bit2);
 
225
  return TRUE;
 
226
error5:
 
227
  diag("invert error  bitsize=%u,size=%u", bitsize,
 
228
  test_bit1);
 
229
  return TRUE;
 
230
}
 
231
 
 
232
my_bool test_count_bits_set(MY_BITMAP *map, uint bitsize)
 
233
{
 
234
  uint i, bit_count=0, test_bit;
 
235
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
236
  for (i=0; i < no_loops; i++)
 
237
  {
 
238
    test_bit=get_rand_bit(bitsize);
 
239
    if (!bitmap_is_set(map, test_bit))
 
240
    {
 
241
      bitmap_set_bit(map, test_bit);
 
242
      bit_count++;
 
243
    }
 
244
  }
 
245
  if (bit_count==0 && bitsize > 0)
 
246
    goto error1;
 
247
  if (bitmap_bits_set(map) != bit_count)
 
248
    goto error2;
 
249
  return FALSE;
 
250
error1:
 
251
  diag("No bits set  bitsize = %u", bitsize);
 
252
  return TRUE;
 
253
error2:
 
254
  diag("Wrong count of bits set, bitsize = %u", bitsize);
 
255
  return TRUE;
 
256
}
 
257
 
 
258
my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
 
259
{
 
260
  uint i, test_bit;
 
261
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
262
  for (i=0; i < no_loops; i++)
 
263
  {
 
264
    test_bit=get_rand_bit(bitsize);
 
265
    bitmap_set_bit(map, test_bit);
 
266
    if (bitmap_get_first_set(map) != test_bit)
 
267
      goto error1;
 
268
    bitmap_set_all(map);
 
269
    bitmap_clear_bit(map, test_bit);
 
270
    if (bitmap_get_first(map) != test_bit)
 
271
      goto error2;
 
272
    bitmap_clear_all(map);
 
273
  }
 
274
  return FALSE;
 
275
error1:
 
276
  diag("get_first_set error bitsize=%u,prefix_size=%u",bitsize,test_bit);
 
277
  return TRUE;
 
278
error2:
 
279
  diag("get_first error bitsize= %u, prefix_size= %u",bitsize,test_bit);
 
280
  return TRUE;
 
281
}
 
282
 
 
283
my_bool test_get_next_bit(MY_BITMAP *map, uint bitsize)
 
284
{
 
285
  uint i, j, test_bit;
 
286
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
287
  for (i=0; i < no_loops; i++)
 
288
  {
 
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))
 
293
      goto error1;
 
294
    bitmap_clear_all(map);
 
295
  }
 
296
  return FALSE;
 
297
error1:
 
298
  diag("get_next error  bitsize= %u, prefix_size= %u", bitsize,test_bit);
 
299
  return TRUE;
 
300
}
 
301
 
 
302
my_bool test_prefix(MY_BITMAP *map, uint bitsize)
 
303
{
 
304
  uint i, j, test_bit;
 
305
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
306
  for (i=0; i < no_loops; i++)
 
307
  {
 
308
    test_bit=get_rand_bit(bitsize);
 
309
    bitmap_set_prefix(map, test_bit);
 
310
    if (!bitmap_is_prefix(map, test_bit))
 
311
      goto error1;
 
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))
 
316
      goto error2;
 
317
    bitmap_set_all(map);
 
318
    for (j=bitsize - 1; ~(j-test_bit); j--)
 
319
      bitmap_clear_bit(map, j);
 
320
    if (!bitmap_is_prefix(map, test_bit))
 
321
      goto error3;
 
322
    bitmap_clear_all(map);
 
323
  }
 
324
  return FALSE;
 
325
error1:
 
326
  diag("prefix1 error  bitsize = %u, prefix_size = %u", bitsize,test_bit);
 
327
  return TRUE;
 
328
error2:
 
329
  diag("prefix2 error  bitsize = %u, prefix_size = %u", bitsize,test_bit);
 
330
  return TRUE;
 
331
error3:
 
332
  diag("prefix3 error  bitsize = %u, prefix_size = %u", bitsize,test_bit);
 
333
  return TRUE;
 
334
}
 
335
 
 
336
 
 
337
my_bool do_test(uint bitsize)
 
338
{
 
339
  MY_BITMAP map;
 
340
  uint32 buf[1024];
 
341
  if (bitmap_init(&map, buf, bitsize, FALSE))
 
342
  {
 
343
    diag("init error for bitsize %d", bitsize);
 
344
    goto error;
 
345
  }
 
346
  if (test_set_get_clear_bit(&map,bitsize))
 
347
    goto error;
 
348
  bitmap_clear_all(&map);
 
349
  if (test_flip_bit(&map,bitsize))
 
350
    goto error;
 
351
  bitmap_clear_all(&map);
 
352
  if (test_operators(&map,bitsize))
 
353
    goto error;
 
354
  bitmap_clear_all(&map);
 
355
  if (test_get_all_bits(&map, bitsize))
 
356
    goto error;
 
357
  bitmap_clear_all(&map);
 
358
  if (test_compare_operators(&map,bitsize))
 
359
    goto error;
 
360
  bitmap_clear_all(&map);
 
361
  if (test_count_bits_set(&map,bitsize))
 
362
    goto error;
 
363
  bitmap_clear_all(&map);
 
364
  if (test_get_first_bit(&map,bitsize))
 
365
    goto error;
 
366
  bitmap_clear_all(&map);
 
367
  if (test_get_next_bit(&map,bitsize))
 
368
    goto error;
 
369
  if (test_prefix(&map,bitsize))
 
370
    goto error;
 
371
  return FALSE;
 
372
error:
 
373
  return TRUE;
 
374
}
 
375
 
 
376
int main()
 
377
{
 
378
  int i;
 
379
  int const min_size = 1;
 
380
  int const max_size = 1024;
 
381
  MY_INIT("bitmap-t");
 
382
 
 
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();
 
387
}