~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2006 MySQL AB
 
1
/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
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
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
15
15
 
16
16
   This test was copied from the unit test inside the
17
17
   mysys/my_bitmap.c file and adapted by Mats Kindahl to use the mytap
24
24
#include <tap.h>
25
25
#include <m_string.h>
26
26
 
 
27
#define MAX_TESTED_BITMAP_SIZE 1024
 
28
 
27
29
uint get_rand_bit(uint bitsize)
28
30
{
29
31
  return (rand() % bitsize);
75
77
  return TRUE;
76
78
}
77
79
 
78
 
my_bool test_operators(MY_BITMAP *map __attribute__((unused)),
79
 
                       uint bitsize __attribute__((unused)))
80
 
{
81
 
  return FALSE;
82
 
}
83
 
 
84
80
my_bool test_get_all_bits(MY_BITMAP *map, uint bitsize)
85
81
{
86
82
  uint i;
129
125
  uint no_loops= bitsize > 128 ? 128 : bitsize;
130
126
  MY_BITMAP map2_obj, map3_obj;
131
127
  MY_BITMAP *map2= &map2_obj, *map3= &map3_obj;
132
 
  uint32 map2buf[1024];
133
 
  uint32 map3buf[1024];
 
128
  uint32 map2buf[MAX_TESTED_BITMAP_SIZE];
 
129
  uint32 map3buf[MAX_TESTED_BITMAP_SIZE];
134
130
  bitmap_init(&map2_obj, map2buf, bitsize, FALSE);
135
131
  bitmap_init(&map3_obj, map3buf, bitsize, FALSE);
136
132
  bitmap_clear_all(map2);
257
253
 
258
254
my_bool test_get_first_bit(MY_BITMAP *map, uint bitsize)
259
255
{
260
 
  uint i, test_bit;
 
256
  uint i, test_bit= 0;
261
257
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
258
 
 
259
  bitmap_set_all(map);
 
260
  for (i=0; i < bitsize; i++)
 
261
    bitmap_clear_bit(map, i);
 
262
  if (bitmap_get_first_set(map) != MY_BIT_NONE)
 
263
    goto error1;
 
264
  bitmap_clear_all(map);
 
265
  for (i=0; i < bitsize; i++)
 
266
    bitmap_set_bit(map, i);
 
267
  if (bitmap_get_first(map) != MY_BIT_NONE)
 
268
    goto error2;
 
269
  bitmap_clear_all(map);
 
270
 
262
271
  for (i=0; i < no_loops; i++)
263
272
  {
264
273
    test_bit=get_rand_bit(bitsize);
321
330
      goto error3;
322
331
    bitmap_clear_all(map);
323
332
  }
 
333
  for (i=0; i < bitsize; i++)
 
334
  {
 
335
    if (bitmap_is_prefix(map, i + 1))
 
336
      goto error4;
 
337
    bitmap_set_bit(map, i);
 
338
    if (!bitmap_is_prefix(map, i + 1))
 
339
      goto error5;
 
340
    test_bit=get_rand_bit(bitsize);
 
341
    bitmap_set_bit(map, test_bit);
 
342
    if (test_bit <= i && !bitmap_is_prefix(map, i + 1))
 
343
      goto error5;
 
344
    else if (test_bit > i)
 
345
    {
 
346
      if (bitmap_is_prefix(map, i + 1))
 
347
        goto error4;
 
348
      bitmap_clear_bit(map, test_bit);
 
349
    }
 
350
  }
324
351
  return FALSE;
325
352
error1:
326
353
  diag("prefix1 error  bitsize = %u, prefix_size = %u", bitsize,test_bit);
331
358
error3:
332
359
  diag("prefix3 error  bitsize = %u, prefix_size = %u", bitsize,test_bit);
333
360
  return TRUE;
334
 
}
335
 
 
 
361
error4:
 
362
  diag("prefix4 error  bitsize = %u, i = %u", bitsize,i);
 
363
  return TRUE;
 
364
error5:
 
365
  diag("prefix5 error  bitsize = %u, i = %u", bitsize,i);
 
366
  return TRUE;
 
367
}
 
368
 
 
369
my_bool test_compare(MY_BITMAP *map, uint bitsize)
 
370
{
 
371
  MY_BITMAP map2;
 
372
  uint32 map2buf[MAX_TESTED_BITMAP_SIZE];
 
373
  uint i, test_bit;
 
374
  uint no_loops= bitsize > 128 ? 128 : bitsize;
 
375
  if (bitmap_init(&map2, map2buf, bitsize, FALSE))
 
376
  {
 
377
    diag("init error for bitsize %d", bitsize);
 
378
    return TRUE;
 
379
  }
 
380
  /* Test all 4 possible combinations of set/unset bits. */
 
381
  for (i=0; i < no_loops; i++)
 
382
  {
 
383
    test_bit=get_rand_bit(bitsize);
 
384
    bitmap_clear_bit(map, test_bit);
 
385
    bitmap_clear_bit(&map2, test_bit);
 
386
    if (!bitmap_is_subset(map, &map2))
 
387
      goto error_is_subset;
 
388
    bitmap_set_bit(map, test_bit);
 
389
    if (bitmap_is_subset(map, &map2))
 
390
      goto error_is_subset;
 
391
    bitmap_set_bit(&map2, test_bit);
 
392
    if (!bitmap_is_subset(map, &map2))
 
393
      goto error_is_subset;
 
394
    bitmap_clear_bit(map, test_bit);
 
395
    if (!bitmap_is_subset(map, &map2))
 
396
      goto error_is_subset;
 
397
    /* Note that test_bit is not cleared i map2. */
 
398
  }
 
399
  bitmap_clear_all(map);
 
400
  bitmap_clear_all(&map2);
 
401
  /* Test all 4 possible combinations of set/unset bits. */
 
402
  for (i=0; i < no_loops; i++)
 
403
  {
 
404
    test_bit=get_rand_bit(bitsize);
 
405
    if (bitmap_is_overlapping(map, &map2))
 
406
      goto error_is_overlapping;
 
407
    bitmap_set_bit(map, test_bit);
 
408
    if (bitmap_is_overlapping(map, &map2))
 
409
      goto error_is_overlapping;
 
410
    bitmap_set_bit(&map2, test_bit);
 
411
    if (!bitmap_is_overlapping(map, &map2))
 
412
      goto error_is_overlapping;
 
413
    bitmap_clear_bit(map, test_bit);
 
414
    if (bitmap_is_overlapping(map, &map2))
 
415
      goto error_is_overlapping;
 
416
    bitmap_clear_bit(&map2, test_bit);
 
417
    /* Note that test_bit is not cleared i map2. */
 
418
  }
 
419
  return FALSE;
 
420
error_is_subset:
 
421
  diag("is_subset error  bitsize = %u", bitsize);
 
422
  return TRUE;
 
423
error_is_overlapping:
 
424
  diag("is_overlapping error  bitsize = %u", bitsize);
 
425
  return TRUE;
 
426
}
 
427
 
 
428
my_bool test_intersect(MY_BITMAP *map, uint bitsize)
 
429
{
 
430
  uint bitsize2 = 1 + get_rand_bit(MAX_TESTED_BITMAP_SIZE - 1);
 
431
  MY_BITMAP map2;
 
432
  uint32 map2buf[bitsize2];
 
433
  uint i, test_bit1, test_bit2, test_bit3;
 
434
  if (bitmap_init(&map2, map2buf, bitsize2, FALSE))
 
435
  {
 
436
    diag("init error for bitsize %d", bitsize2);
 
437
    return TRUE;
 
438
  }
 
439
  test_bit1= get_rand_bit(bitsize);
 
440
  test_bit2= get_rand_bit(bitsize);
 
441
  bitmap_set_bit(map, test_bit1);
 
442
  bitmap_set_bit(map, test_bit2);
 
443
  test_bit3= get_rand_bit(bitsize2);
 
444
  bitmap_set_bit(&map2, test_bit3);
 
445
  if (test_bit2 < bitsize2)
 
446
    bitmap_set_bit(&map2, test_bit2);
 
447
 
 
448
  bitmap_intersect(map, &map2);
 
449
  if (test_bit2 < bitsize2)
 
450
  {
 
451
    if (!bitmap_is_set(map, test_bit2))
 
452
      goto error;
 
453
    bitmap_clear_bit(map, test_bit2);
 
454
  }
 
455
  if (test_bit1 == test_bit3)
 
456
  {
 
457
    if (!bitmap_is_set(map, test_bit1))
 
458
      goto error;
 
459
    bitmap_clear_bit(map, test_bit1);
 
460
  }
 
461
  if (!bitmap_is_clear_all(map))
 
462
    goto error;
 
463
 
 
464
  bitmap_set_all(map);
 
465
  bitmap_set_all(&map2);
 
466
  for (i=0; i < bitsize2; i++)
 
467
    bitmap_clear_bit(&map2, i);
 
468
  bitmap_intersect(map, &map2);
 
469
  if (!bitmap_is_clear_all(map))
 
470
    goto error;
 
471
  return FALSE;
 
472
error:
 
473
  diag("intersect error  bitsize = %u, bit1 = %u, bit2 = %u, bit3 = %u",
 
474
       bitsize, test_bit1, test_bit2, test_bit3);
 
475
  return TRUE;
 
476
}
336
477
 
337
478
my_bool do_test(uint bitsize)
338
479
{
339
480
  MY_BITMAP map;
340
 
  uint32 buf[1024];
 
481
  uint32 buf[MAX_TESTED_BITMAP_SIZE];
341
482
  if (bitmap_init(&map, buf, bitsize, FALSE))
342
483
  {
343
484
    diag("init error for bitsize %d", bitsize);
349
490
  if (test_flip_bit(&map,bitsize))
350
491
    goto error;
351
492
  bitmap_clear_all(&map);
352
 
  if (test_operators(&map,bitsize))
353
 
    goto error;
354
 
  bitmap_clear_all(&map);
355
493
  if (test_get_all_bits(&map, bitsize))
356
494
    goto error;
357
495
  bitmap_clear_all(&map);
366
504
  bitmap_clear_all(&map);
367
505
  if (test_get_next_bit(&map,bitsize))
368
506
    goto error;
 
507
  bitmap_clear_all(&map);
369
508
  if (test_prefix(&map,bitsize))
370
509
    goto error;
 
510
  bitmap_clear_all(&map);
 
511
  if (test_compare(&map,bitsize))
 
512
    goto error;
 
513
  bitmap_clear_all(&map);
 
514
  if (test_intersect(&map,bitsize))
 
515
    goto error;
371
516
  return FALSE;
372
517
error:
373
518
  return TRUE;
377
522
{
378
523
  int i;
379
524
  int const min_size = 1;
380
 
  int const max_size = 1024;
 
525
  int const max_size = MAX_TESTED_BITMAP_SIZE;
381
526
  MY_INIT("bitmap-t");
382
527
 
383
528
  plan(max_size - min_size);