Changeset cf8e43eb3296c5f2e2681f89658e5b9b3c0dd263

Show
Ignore:
Timestamp:
04/27/08 17:17:43 (8 months ago)
Author:
David Balmain <dbalmain@…>
Parents:
88f53b244ed21fc4833bef05fb376eb4497a2740
Children:
2142bb1f97bcd9f2283306e84852774fef71d55e
git-committer:
David Balmain <dbalmain@gmail.com> / 2008-04-27T17:17:43Z+1000
Message:

Improved BitVector? coverage to 100%

Location:
c
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • c/test/test_bitvector.c

    r48290f rcf8e43  
    147147} 
    148148 
     149#define test_bveq(_bv1, _bv2)                                       \ 
     150do {                                                                \ 
     151    BitVector *_not_bv1, *_not_bv2;                                 \ 
     152    Assert(bv_eq(_bv1, _bv2), "BitVectors are equal");              \ 
     153    Assert(bv_eq(_bv2, _bv1), "BitVectors are equal");              \ 
     154    Assert(bv_eq(_bv1, _bv1), "bv_eq on self should work");         \ 
     155    Aiequal(bv_hash(_bv1), bv_hash(_bv2));                          \ 
     156    /* test flipped bitvectors */                                   \ 
     157    _not_bv1 = bv_not(_bv1); _not_bv2 = bv_not(_bv2);               \ 
     158    bv_set(_not_bv1, 1100); /* should make no difference */         \ 
     159    Assert(bv_eq(_not_bv1, _not_bv2), "BitVectors are equal");      \ 
     160    Assert(bv_eq(_not_bv2, _not_bv1), "BitVectors are equal");      \ 
     161    Assert(bv_eq(_not_bv1, _not_bv1), "bv_eq on self should work"); \ 
     162    Aiequal(bv_hash(_not_bv1), bv_hash(_not_bv2));                  \ 
     163    bv_destroy(_not_bv1); bv_destroy(_not_bv2);                     \ 
     164} while (0) 
     165 
     166#define test_bvneq(_bv1, _bv2)                                      \ 
     167do {                                                                \ 
     168    BitVector *_not_bv1, *_not_bv2;                                 \ 
     169    Assert(!bv_eq(_bv1, _bv2), "BitVectors are not equal");         \ 
     170    Assert(!bv_eq(_bv2, _bv1), "BitVectors are not equal");         \ 
     171    Assert(bv_hash(_bv1) != bv_hash(_bv2), "BitVectors not equal"); \ 
     172    /* test flipped bitvectors */                                   \ 
     173    _not_bv1 = bv_not(_bv1); _not_bv2 = bv_not(_bv2);               \ 
     174    Assert(!bv_eq(_not_bv1, _not_bv2), "BitVectors are not equal"); \ 
     175    Assert(!bv_eq(_not_bv2, _not_bv1), "BitVectors are not equal"); \ 
     176    Assert(bv_hash(_not_bv1) != bv_hash(_not_bv2), "Bitvectors !=");\ 
     177    bv_destroy(_not_bv1); bv_destroy(_not_bv2);                     \ 
     178} while (0) 
     179 
    149180static void test_bv_eq_hash(TestCase *tc, void *data) 
    150181{ 
     
    155186    (void)data; 
    156187 
    157     Assert(bv_eq(bv1, bv2), "BitVectors are both empty"); 
     188    test_bveq(bv1, bv2); 
    158189    Assert(bv_eq(bv1, bv1), "bv_eq on self should work"); 
    159     Aiequal(bv_hash(bv1), bv_hash(bv2)); 
    160190 
    161191    bv_set(bv1, 1); 
    162     Assert(!bv_eq(bv1, bv2), "BitVectors are not equal"); 
    163     Assert(bv_hash(bv1) != bv_hash(bv2), "BitVectors are not equal"); 
     192    test_bvneq(bv1, bv2); 
    164193 
    165194    bv_set(bv2, 11); 
    166     Assert(!bv_eq(bv1, bv2), "BitVectors are not equal"); 
    167     Assert(bv_hash(bv1) != bv_hash(bv2), "BitVectors are not equal"); 
     195    test_bvneq(bv1, bv2); 
    168196 
    169197    bv_set(bv1, 11); 
    170198    bv_set(bv2, 1); 
    171     Assert(bv_eq(bv1, bv2), "BitVectors are equal"); 
    172     Aiequal(bv_hash(bv1), bv_hash(bv2)); 
    173  
    174     bv_unset(bv1, 1000); 
    175     Assert(bv_eq(bv1, bv2), "BitVectors are equal"); 
    176     Aiequal(bv_hash(bv1), bv_hash(bv2)); 
     199    test_bveq(bv1, bv2); 
     200 
     201    /* This will increase size of bv1 to 1000 */ 
     202    bv_unset(bv1, 1000);  
     203    /* difference in size shouldn't matter */ 
     204    test_bveq(bv1, bv2); 
    177205 
    178206    for (i = 0; i < COUNT; i++) { 
     
    181209        bv_set(bv2, bit); 
    182210    } 
    183  
    184     Assert(bv_eq(bv1, bv2), "BitVectors are equal"); 
    185     Aiequal(bv_hash(bv1), bv_hash(bv2)); 
     211    test_bveq(bv1, bv2); 
     212 
    186213    bv_destroy(bv1); 
    187214    bv_destroy(bv2); 
    188215 
     216    /* although the saet bits will be equal, the extension will be different*/ 
    189217    bv1 = set_bits(bv_new(), "1, 3, 5"); 
    190218    bv2 = bv_not_x(set_bits(bv_new(), "0, 2, 4")); 
    191219    bv_set(bv2, 5); 
    192     Assert(!bv_eq(bv1, bv2), "BitVectors have different extension"); 
    193     Assert(bv_hash(bv1) != bv_hash(bv2), "BitVectors have different extension"); 
     220    test_bvneq(bv1, bv2); 
     221 
     222    bv_destroy(bv2); 
     223    bv2 = set_bits(bv_new(), "1, 3, 5"); 
     224    bv1 = bv_not_x(bv1); 
     225    bv2 = bv_not_x(bv2); 
     226    bv_unset(bv1, 1000); 
     227    test_bvneq(bv1, bv2); 
     228 
    194229    bv_destroy(bv1); 
    195230    bv_destroy(bv2); 
     
    203238    BitVector *bv1 = bv_new(); 
    204239    BitVector *bv2 = bv_new(); 
     240    BitVector *not_bv1, *not_bv2, *or_bv, *not_and_bv; 
    205241    char set1[AND_SIZE]; 
    206242    char set2[AND_SIZE]; 
     
    225261    } 
    226262 
     263    not_bv1 = bv_not(bv1); not_bv2 = bv_not(bv2); 
     264    and_bv = bv_and(not_bv1, not_bv2); 
     265    not_and_bv = bv_not(and_bv); 
     266    or_bv = bv_or(bv1, bv2); 
     267    Assert(bv_eq(not_and_bv, or_bv), "BitVectors should be equal"); 
     268    bv_destroy(not_bv1); bv_destroy(not_bv2); 
     269    bv_destroy(and_bv); 
     270    bv_destroy(not_and_bv); 
     271    bv_destroy(or_bv); 
     272 
    227273    and_bv = bv_and(bv1, bv2); 
    228274 
     
    231277        Aiequal(set2[i], bv_get(and_bv, i)); 
    232278    } 
     279     
    233280 
    234281    bv1 = bv_and_x(bv1, bv2); 
     
    469516    (void)data; /* suppress unused argument warning */ 
    470517 
    471     /* 
    472     clock_t t = clock(), total_t = 0; 
    473     */ 
    474  
    475518    for (i = BV_SCAN_INC; i < BV_SCAN_SIZE; i += BV_SCAN_INC) { 
    476519        bv_set_fast(bv, i); 
     
    481524 
    482525    not_bv = bv_not(bv); 
    483     /* 
    484     t = clock() - t; 
    485     total_t += t; 
    486     printf("bv_set took %0.3f secs\n", (double) (t) / CLOCKS_PER_SEC); 
    487     t = clock(); 
    488     */ 
    489526 
    490527    for (i = BV_SCAN_INC; i < BV_SCAN_SIZE; i += BV_SCAN_INC) { 
     
    495532    Aiequal(-1, bv_scan_next_unset_from(not_bv, i - BV_SCAN_INC / 2)); 
    496533 
    497     /* 
    498     t = clock() - t; 
    499     total_t += t; 
    500     printf("bv_scan_next_from took %0.3f secs\n", (double) (t) / CLOCKS_PER_SEC); 
    501     t = clock(); 
    502     */ 
     534    /* test scan_next_from where size is actually greater than the highest set 
     535     * bit */ 
     536    bv->size++; 
     537    not_bv->size++; 
     538 
     539    bv_scan_reset(bv); 
     540    bv_scan_reset(not_bv); 
     541    for (i = BV_SCAN_INC; i < BV_SCAN_SIZE; i += BV_SCAN_INC) { 
     542        Aiequal(i, bv_scan_next_from(bv, i - BV_SCAN_INC / 2)); 
     543        Aiequal(i, bv_scan_next_unset_from(not_bv, i - BV_SCAN_INC / 2)); 
     544    } 
     545    Aiequal(-1, bv_scan_next_from(bv, i - BV_SCAN_INC / 2)); 
     546    Aiequal(-1, bv_scan_next_unset_from(not_bv, i - BV_SCAN_INC / 2)); 
    503547 
    504548    bv_scan_reset(bv); 
     
    511555    Aiequal(-1, bv_scan_next_unset(not_bv)); 
    512556 
    513     /* 
    514     t = clock() - t; 
    515     total_t += t; 
    516     printf("bv_scan_next took %0.3f secs\n", (double) (t) / CLOCKS_PER_SEC); 
    517     */ 
    518  
    519557    bv_clear(bv); 
    520558    bv_destroy(not_bv); 
     
    524562    not_bv = bv_not(bv); 
    525563 
    526     /* 
    527     t = clock(); 
    528     */ 
    529  
    530564    for (i = 0; i < BV_DENSE_SCAN_SIZE; i++) { 
    531565        Aiequal(i, bv_scan_next_from(bv, i)); 
     
    534568    Aiequal(-1, bv_scan_next_from(bv, i)); 
    535569    Aiequal(-1, bv_scan_next_unset_from(not_bv, i)); 
    536  
    537     /* 
    538     t = clock() - t; 
    539     total_t += t; 
    540     printf("dense bv_scan_next_from took %0.3f secs\n", (double) (t) / CLOCKS_PER_SEC); 
    541     t = clock(); 
    542     */ 
    543570 
    544571    bv_scan_reset(bv); 
     
    550577    Aiequal(-1, bv_scan_next(bv)); 
    551578    Aiequal(-1, bv_scan_next_unset(not_bv)); 
    552  
    553     /* 
    554     t = clock() - t; 
    555     total_t += t; 
    556     printf("dense bv_scan_next took %0.3f secs\n", (double) t / CLOCKS_PER_SEC); 
    557     printf("total time %0.3f seconds\n", 
    558            (double) total_t / CLOCKS_PER_SEC); 
    559     */ 
    560579 
    561580    bv_destroy(bv);