src/util/bitset/stress_test/stress.cpp

Go to the documentation of this file.
00001 /*  $Id: stress.cpp 105976 2007-06-20 15:19:13Z kuznets $
00002 * ===========================================================================
00003 *
00004 *                            PUBLIC DOMAIN NOTICE
00005 *               National Center for Biotechnology Information
00006 *
00007 *  This software/database is a "United States Government Work" under the
00008 *  terms of the United States Copyright Act.  It was written as part of
00009 *  the author's official duties as a United States Government employee and
00010 *  thus cannot be copyrighted.  This software/database is freely available
00011 *  to the public for use. The National Library of Medicine and the U.S.
00012 *  Government have not placed any restriction on its use or reproduction.
00013 *
00014 *  Although all reasonable efforts have been taken to ensure the accuracy
00015 *  and reliability of the software and data, the NLM and the U.S.
00016 *  Government do not and cannot warrant the performance or results that
00017 *  may be obtained by using this software or data. The NLM and the U.S.
00018 *  Government disclaim all warranties, express or implied, including
00019 *  warranties of performance, merchantability or fitness for any particular
00020 *  purpose.
00021 *
00022 *  Please cite the author in any work or product based on this material.
00023 *
00024 * ===========================================================================
00025 *
00026 * Author:  Anatoliy Kuznetsov
00027 *
00028 *
00029 */
00030 
00031 //#define BM64OPT
00032 //#define BM_SET_MMX_GUARD
00033 //#define BMSSE2OPT
00034 //#define BMCOUNTOPT
00035 
00036 #include <ncbi_pch.hpp>
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <assert.h>
00040 #include <memory.h>
00041 #include <iostream>
00042 #include <time.h>
00043 #include <util/bitset/bm.h>
00044 #include <util/bitset/bmalgo.h>
00045 #include <util/bitset/bmserial.h>
00046 #include <util/bitset/bitset_debug.hpp>
00047 
00048 using namespace bm;
00049 using namespace std;
00050 
00051 #include "rlebtv.h"
00052 #include <util/bitset/encoding.h>
00053 #include <limits.h>
00054 
00055 
00056 
00057 #define POOL_SIZE 5000
00058 
00059 //#define MEM_POOL
00060 
00061 
00062 template<class T> T* pool_allocate(T** pool, int& i, size_t n)
00063 {
00064     return i ? pool[i--] : (T*) ::malloc(n * sizeof(T));
00065 }
00066 
00067 inline void* pool_allocate2(void** pool, int& i, size_t n)
00068 {
00069     return i ? pool[i--] : malloc(n * sizeof(void*));
00070 }
00071 
00072 
00073 
00074 template<class T> void pool_free(T** pool, int& i, T* p)
00075 {
00076     i < POOL_SIZE ? (free(p),(void*)0) : pool[++i]=p;
00077 }
00078 
00079 
00080 class pool_block_allocator
00081 {
00082 public:
00083 
00084     static bm::word_t* allocate(size_t n, const void *)
00085     {
00086         int *idx = 0;
00087         bm::word_t** pool = 0;
00088 
00089         switch (n)
00090         {
00091         case bm::set_block_size:
00092             idx = &bit_blocks_idx_;
00093             pool = free_bit_blocks_;
00094             break;
00095 
00096         case 64:
00097             idx = &gap_blocks_idx0_;
00098             pool = gap_bit_blocks0_;
00099             break;
00100 
00101         case 128:
00102             idx = &gap_blocks_idx1_;
00103             pool = gap_bit_blocks1_;
00104             break;
00105         
00106         case 256:
00107             idx = &gap_blocks_idx2_;
00108             pool = gap_bit_blocks2_;
00109             break;
00110 
00111         case 512:
00112             idx = &gap_blocks_idx3_;
00113             pool = gap_bit_blocks3_;
00114             break;
00115 
00116         default:
00117             assert(0);
00118         }
00119 
00120         return pool_allocate(pool, *idx, n);
00121     }
00122 
00123     static void deallocate(bm::word_t* p, size_t n)
00124     {
00125         int *idx = 0;
00126         bm::word_t** pool = 0;
00127 
00128         switch (n)
00129         {
00130         case bm::set_block_size:
00131             idx = &bit_blocks_idx_;
00132             pool = free_bit_blocks_;
00133             break;
00134 
00135         case 64:
00136             idx = &gap_blocks_idx0_;
00137             pool = gap_bit_blocks0_;
00138             break;
00139 
00140         case 128:
00141             idx = &gap_blocks_idx1_;
00142             pool = gap_bit_blocks1_;
00143             break;
00144         
00145         case 256:
00146             idx = &gap_blocks_idx2_;
00147             pool = gap_bit_blocks2_;
00148             break;
00149 
00150         case 512:
00151             idx = &gap_blocks_idx3_;
00152             pool = gap_bit_blocks3_;
00153             break;
00154 
00155         default:
00156             assert(0);
00157         }
00158 
00159         pool_free(pool, *idx, p);
00160     }
00161 
00162 private:
00163     static bm::word_t* free_bit_blocks_[];
00164     static int         bit_blocks_idx_;
00165 
00166     static bm::word_t* gap_bit_blocks0_[];
00167     static int         gap_blocks_idx0_;
00168 
00169     static bm::word_t* gap_bit_blocks1_[];
00170     static int         gap_blocks_idx1_;
00171 
00172     static bm::word_t* gap_bit_blocks2_[];
00173     static int         gap_blocks_idx2_;
00174 
00175     static bm::word_t* gap_bit_blocks3_[];
00176     static int         gap_blocks_idx3_;
00177 };
00178 
00179 bm::word_t* pool_block_allocator::free_bit_blocks_[POOL_SIZE];
00180 int pool_block_allocator::bit_blocks_idx_ = 0;
00181 
00182 bm::word_t* pool_block_allocator::gap_bit_blocks0_[POOL_SIZE];
00183 int pool_block_allocator::gap_blocks_idx0_ = 0;
00184 
00185 bm::word_t* pool_block_allocator::gap_bit_blocks1_[POOL_SIZE];
00186 int pool_block_allocator::gap_blocks_idx1_ = 0;
00187 
00188 bm::word_t* pool_block_allocator::gap_bit_blocks2_[POOL_SIZE];
00189 int pool_block_allocator::gap_blocks_idx2_ = 0;
00190 
00191 bm::word_t* pool_block_allocator::gap_bit_blocks3_[POOL_SIZE];
00192 int pool_block_allocator::gap_blocks_idx3_ = 0;
00193 
00194 
00195 
00196 
00197 class pool_ptr_allocator
00198 {
00199 public:
00200 
00201     static void* allocate(size_t n, const void *)
00202     {
00203         return pool_allocate2(free_ptr_blocks_, ptr_blocks_idx_, n);
00204     }
00205 
00206     static void deallocate(void* p, size_t)
00207     {
00208         pool_free(free_ptr_blocks_, ptr_blocks_idx_, p);
00209     }
00210 
00211 private:
00212     static void*  free_ptr_blocks_[];
00213     static int    ptr_blocks_idx_;
00214 };
00215 
00216 void* pool_ptr_allocator::free_ptr_blocks_[POOL_SIZE];
00217 int pool_ptr_allocator::ptr_blocks_idx_ = 0;
00218 
00219 
00220 //#define MEM_DEBUG
00221  
00222 #ifdef MEM_DEBUG
00223 
00224 
00225 class dbg_block_allocator
00226 {
00227 public:
00228 static unsigned na_;
00229 static unsigned nf_;
00230 
00231     static bm::word_t* allocate(size_t n, const void *)
00232     {
00233         ++na_;
00234         assert(n);
00235         bm::word_t* p =
00236             (bm::word_t*) ::malloc((n+1) * sizeof(bm::word_t));
00237         *p = n;
00238         return ++p;
00239     }
00240 
00241     static void deallocate(bm::word_t* p, size_t n)
00242     {
00243         ++nf_;
00244         --p;
00245         if(*p != n)
00246         {
00247             printf("Block memory deallocation error!\n");
00248             exit(1);
00249         }
00250         ::free(p);
00251     }
00252 
00253     static int balance()
00254     {
00255         return nf_ - na_;
00256     }
00257 };
00258 
00259 unsigned dbg_block_allocator::na_ = 0;
00260 unsigned dbg_block_allocator::nf_ = 0;
00261 
00262 class dbg_ptr_allocator
00263 {
00264 public:
00265 static unsigned na_;
00266 static unsigned nf_;
00267 
00268     static void* allocate(size_t n, const void *)
00269     {
00270         ++na_;
00271         assert(sizeof(size_t) == sizeof(void*));
00272         void* p = ::malloc((n+1) * sizeof(void*));
00273         size_t* s = (size_t*) p;
00274         *s = n;
00275         return (void*)++s;
00276     }
00277 
00278     static void deallocate(void* p, size_t n)
00279     {
00280         ++nf_;
00281         size_t* s = (size_t*) p;
00282         --s;
00283         if(*s != n)
00284         {
00285             printf("Ptr memory deallocation error!\n");
00286             exit(1);
00287         }
00288         ::free(s);
00289     }
00290 
00291     static int balance()
00292     {
00293         return nf_ - na_;
00294     }
00295 
00296 };
00297 
00298 unsigned dbg_ptr_allocator::na_ = 0;
00299 unsigned dbg_ptr_allocator::nf_ = 0;
00300 
00301 
00302 typedef mem_alloc<dbg_block_allocator, dbg_ptr_allocator> dbg_alloc;
00303 
00304 typedef bm::bvector<dbg_alloc> bvect;
00305 typedef bm::bvector_mini<dbg_block_allocator> bvect_mini;
00306 
00307 #else
00308 
00309 #ifdef MEM_POOL
00310 
00311 typedef mem_alloc<pool_block_allocator, pool_ptr_allocator> pool_alloc;
00312 typedef bm::bvector<pool_alloc> bvect;
00313 typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
00314 
00315 
00316 #else
00317 
00318 typedef bm::bvector<> bvect;
00319 typedef bm::bvector_mini<bm::block_allocator> bvect_mini;
00320 
00321 #endif
00322 
00323 #endif
00324 
00325 //const unsigned BITVECT_SIZE = 100000000 * 8;
00326 
00327 // This this setting program will consume around 150M of RAM
00328 const unsigned BITVECT_SIZE = 100000000 * 2;
00329 
00330 const unsigned ITERATIONS = 100000;
00331 const unsigned PROGRESS_PRINT = 2000000;
00332 
00333 
00334 
00335 void CheckVectors(bvect_mini &bvect_min, 
00336                   bvect      &bvect_full,
00337                   unsigned size,
00338                   bool     detailed = false);
00339 
00340 
00341 unsigned random_minmax(unsigned min, unsigned max)
00342 {
00343     unsigned r = (rand() << 16) | rand();
00344     return r % (max-min) + min;
00345 }
00346 
00347 
00348 void FillSets(bvect_mini* bvect_min, 
00349               bvect* bvect_full,
00350               unsigned min, 
00351               unsigned max,
00352               unsigned fill_factor)
00353 {
00354     unsigned i;
00355     unsigned id;
00356 
00357     //Random filling
00358     if(fill_factor == 0)
00359     {
00360         unsigned n_id = (max - min) / 100;
00361         printf("random filling : %i\n", n_id);
00362         for (i = 0; i < n_id; i++)
00363         {
00364             id = random_minmax(min, max);
00365             bvect_min->set_bit(id);
00366             bvect_full->set_bit(id);
00367             if (PROGRESS_PRINT)
00368             {
00369                 if ( (i % PROGRESS_PRINT) == 0)
00370                 {
00371                     cout << "+" << flush;
00372                 }
00373             }
00374         }
00375         cout << endl;
00376     }
00377     else
00378     {
00379         printf("fill_factor random filling : factor = %i\n", fill_factor);
00380 
00381         for(i = 0; i < fill_factor; i++)
00382         {
00383             int k = rand() % 10;
00384             if (k == 0)
00385                 k+=2;
00386 
00387             //Calculate start
00388             unsigned start = min + (max - min) / (fill_factor * k);
00389 
00390             //Randomize start
00391             start += random_minmax(1, (max - min) / (fill_factor * 10));
00392 
00393             if (start > max)
00394             {
00395                 start = min;
00396             }
00397             
00398             //Calculate end 
00399             unsigned end = start + (max - start) / (fill_factor *2);
00400 
00401             //Randomize end
00402             end -= random_minmax(1, (max - start) / (fill_factor * 10));
00403 
00404             if (end > max )
00405             {
00406                 end = max;
00407             }
00408 
00409             
00410             if (fill_factor > 1)
00411             {
00412                 for(; start < end;)
00413                 {
00414                     int r = rand() % 8;
00415 
00416                     if (r > 7)
00417                     {
00418                         int inc = rand() % 3;
00419                         ++inc;
00420                         unsigned end2 = start + rand() % 1000;
00421                         if (end2 > end)
00422                             end2 = end;
00423                         while (start < end2)
00424                         {
00425                             bvect_min->set_bit(start);
00426                             bvect_full->set_bit(start);  
00427                             start += inc;
00428                         }
00429 
00430                         if (PROGRESS_PRINT)
00431                         {
00432                             if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
00433                             {
00434                                 cout << "+" << flush;
00435                             }
00436                         }
00437 
00438                         continue;
00439                     }
00440 
00441                     if (r)
00442                     {
00443                         bvect_min->set_bit(start);
00444                         bvect_full->set_bit(start);
00445                         ++start;
00446                     }
00447                     else
00448                     {
00449                         start+=r;
00450                         bvect_min->set_bit(start);
00451                         bvect_full->set_bit(start);
00452                     }
00453 
00454                     if (PROGRESS_PRINT)
00455                     {
00456                         if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
00457                         {
00458                             cout << "+" << flush;
00459                         }
00460                     }
00461                 }
00462 
00463             }
00464             else
00465             {
00466                 int c = rand() % 15;
00467                 if (c == 0)
00468                     ++c;
00469                 for(; start < end; ++start)
00470                 {
00471                     bvect_min->set_bit(start);
00472                     bvect_full->set_bit(start);
00473 
00474                     if (start % c)
00475                     {
00476                         start += c;
00477                     }
00478 
00479                     if (PROGRESS_PRINT)
00480                     {
00481                         if ( ( ((i+1)*(end-start))  % PROGRESS_PRINT) == 0)
00482                         {
00483                             cout << "+" << flush;
00484                         }
00485                     }
00486 
00487                 }
00488             }
00489             cout << endl;
00490 
00491         }
00492     }
00493 }
00494 
00495 //
00496 // Interval filling.
00497 // 111........111111........111111..........11111111.......1111111...
00498 //
00499 
00500 
00501 void FillSetsIntervals(bvect_mini* bvect_min, 
00502               bvect* bvect_full,
00503               unsigned min, 
00504               unsigned max,
00505               unsigned fill_factor,
00506               bool set_flag=true)
00507 {
00508 
00509     while(fill_factor==0)
00510     {
00511         fill_factor=rand()%10;
00512     }
00513 
00514     cout << "Intervals filling. Factor=" 
00515          <<  fill_factor << endl << endl;
00516 
00517     unsigned i, j;
00518     unsigned factor = 70 * fill_factor;
00519     for (i = min; i < max; ++i)
00520     {
00521         unsigned len, end; 
00522 
00523         do
00524         {
00525             len = rand() % factor;
00526             end = i+len;
00527             
00528         } while (end >= max);
00529 /*
00530         if (set_flag == false)
00531         {
00532             cout << "Cleaning: " << i << "-" << end << endl;
00533         }
00534         else
00535         {
00536             cout << "Add: " << i << "-" << end << endl;
00537         }
00538 */
00539         if (i < end)
00540         {
00541             bvect_full->set_range(i, end-1, set_flag);
00542         }
00543        
00544         for (j = i; j < end; ++j)
00545         {
00546             if (set_flag)
00547             {
00548                 bvect_min->set_bit(j);
00549                 //bvect_full->set_bit(j);
00550             }
00551             else
00552             {
00553                 bvect_min->clear_bit(j);
00554                 //bvect_full->clear_bit(j);
00555 /*
00556         if (g_cnt_check)
00557         {
00558             bool b = bvect_full->count_check();
00559             if(!b)
00560             {
00561                 cout << "Count check failed (clear)!" << endl;
00562                 cout << "bit=" << j << endl;
00563                 exit(1);
00564             }
00565         }
00566 */
00567             }
00568 
00569                            
00570         } // j
00571 
00572 //cout << "Checking range filling " << from << "-" << to << endl;
00573 //CheckVectors(*bvect_min, *bvect_full, 100000000);
00574 
00575 
00576         i = end;
00577 
00578 
00579         len = rand() % (factor* 10 * bm::gap_max_bits);
00580         if (len % 2)
00581         {
00582             len *= rand() % (factor * 10);
00583         }
00584 
00585         i+=len;
00586 
00587         if ( (len % 6) == 0)  
00588         {
00589 /*
00590 if (set_flag == false)
00591 {
00592     cout << "Additional Cleaning: " << i << "-" << end << endl;
00593 }
00594 */
00595             for(unsigned k=0; k < 1000 && i < max; k+=3,i+=3)
00596             {
00597                 if (set_flag)
00598                 {
00599                     bvect_min->set_bit(i);
00600                     bvect_full->set_bit(i);            
00601                 }
00602                 else
00603                 {
00604                     bvect_min->clear_bit(j);
00605                     bvect_full->clear_bit(j);
00606                 }
00607 
00608             }
00609         }
00610 
00611     } // for i
00612 
00613 }
00614 
00615 void FillSetClearIntervals(bvect_mini* bvect_min, 
00616               bvect* bvect_full,
00617               unsigned min, 
00618               unsigned max,
00619               unsigned fill_factor)
00620 {
00621     FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, true);
00622     FillSetsIntervals(bvect_min, bvect_full, min, max, fill_factor, false);
00623 }
00624 
00625 
00626 void FillSetsRandom(bvect_mini* bvect_min, 
00627               bvect* bvect_full,
00628               unsigned min, 
00629               unsigned max,
00630               unsigned fill_factor)
00631 {
00632     unsigned diap = max - min;
00633 
00634     unsigned count;
00635 
00636 
00637     switch (fill_factor)
00638     {
00639     case 0:
00640         count = diap / 1000;
00641         break;
00642     case 1:
00643         count = diap / 100;
00644         break;
00645     default:
00646         count = diap / 10;
00647         break;
00648 
00649     }
00650 
00651     for (unsigned i = 0; i < count; ++i)
00652     {
00653         unsigned bn = rand() % count;
00654         bn += min;
00655 
00656         if (bn > max)
00657         {
00658             bn = max;
00659         }
00660         bvect_min->set_bit(bn);
00661         bvect_full->set_bit(bn);   
00662         
00663         if ( (i  % PROGRESS_PRINT) == 0)
00664         {
00665             cout << "+" << flush;
00666         }
00667     }
00668     cout << "Ok" << endl;
00669 
00670 }
00671 
00672 
00673 //
00674 //  Quasi random filling with choosing randomizing method.
00675 //
00676 //
00677 void FillSetsRandomMethod(bvect_mini* bvect_min, 
00678                           bvect* bvect_full,
00679                           unsigned min, 
00680                           unsigned max,
00681                           int optimize = 0,
00682                           int method = -1)
00683 {
00684     if (method == -1)
00685     {
00686         method = rand() % 5;
00687     }
00688     unsigned factor;
00689 //method = 4;
00690     switch (method)
00691     {
00692 
00693     case 0:
00694         cout << "Random filling: method - FillSets - factor(0)" << endl;
00695         FillSets(bvect_min, bvect_full, min, max, 0);
00696         break;
00697 
00698     case 1:
00699         cout << "Random filling: method - FillSets - factor(random)" << endl;
00700         factor = rand()%3;
00701         FillSets(bvect_min, bvect_full, min, max, factor?factor:1);
00702         break;
00703 
00704     case 2:
00705         cout << "Random filling: method - Set-Clear Intervals - factor(random)" << endl;
00706         factor = rand()%10;
00707         FillSetClearIntervals(bvect_min, bvect_full, min, max, factor);
00708         break;
00709     case 3:
00710         cout << "Random filling: method - FillRandom - factor(random)" << endl;
00711         factor = rand()%3;
00712         FillSetsRandom(bvect_min, bvect_full, min, max, factor?factor:1);
00713         break;
00714 
00715     default:
00716         cout << "Random filling: method - Set Intervals - factor(random)" << endl;
00717         factor = rand()%10;
00718         FillSetsIntervals(bvect_min, bvect_full, min, max, factor);
00719         break;
00720 
00721     } // switch
00722 
00723     if (optimize && (method <= 1))
00724     {
00725         cout << "Vector optimization..." << flush;
00726         bvect_full->optimize();
00727         cout << "OK" << endl;
00728     }
00729 }
00730 
00731 // do logical operation through serialization
00732 unsigned SerializationOperation(bvect*             bv_target,
00733                                 /*const*/ bvect&   bv1,
00734                                 /*const*/ bvect&   bv2,
00735                                 set_operation      op,
00736                                 bool               check_reverse=false)
00737 {
00738     bvect bv_tmp;
00739     if (!bv_target)
00740     {
00741         bv_target = &bv_tmp;
00742     }
00743 
00744     if (op == set_COUNT_SUB_AB ||
00745         op == set_COUNT_SUB_BA)
00746     {
00747         check_reverse = false;
00748     }
00749 
00750     // serialize input vectors
00751     bvect::statistics *st1_op, *st2_op;
00752     st1_op = new bvect::statistics;
00753     st2_op = new bvect::statistics;
00754 
00755     bv1.optimize(0, bvect::opt_compress, st1_op);
00756     bv2.optimize(0, bvect::opt_compress, st2_op);
00757 
00758 
00759    struct bvect::statistics st1, st2;
00760    bv1.calc_stat(&st1);
00761    bv2.calc_stat(&st2);
00762 
00763 
00764    if (st1.max_serialize_mem > st1_op->max_serialize_mem)
00765    {
00766        cout << "Optimize failed to compute max_serialize_mem" << endl;
00767        cout << "calc_stat=" << st1.max_serialize_mem << endl;
00768        cout << "optimize=" << st1_op->max_serialize_mem << endl;
00769        exit(1);
00770    }
00771    if (st2.max_serialize_mem > st2_op->max_serialize_mem)
00772    {
00773        cout << "Optimize failed to compute max_serialize_mem" << endl;
00774        cout << "calc_stat=" << st2.max_serialize_mem << endl;
00775        cout << "optimize=" << st2_op->max_serialize_mem << endl;
00776        exit(1);
00777    }
00778 
00779    delete st1_op;
00780    delete st2_op;
00781 
00782    unsigned char* smem1 = new unsigned char[st1.max_serialize_mem];
00783    unsigned char* smem2 = new unsigned char[st2.max_serialize_mem];
00784 
00785    unsigned slen1 = bm::serialize(bv1, smem1);
00786    unsigned slen2 = bm::serialize(bv2, smem2);
00787 
00788    unsigned count =
00789        operation_deserializer<bvect>::deserialize(*bv_target,
00790                                                   smem1,
00791                                                   0,
00792                                                   set_ASSIGN);
00793    int res = bv1.compare(*bv_target);
00794    if (res != 0)
00795    {
00796        cout << "set_ASSIGN 1 failed!" << endl;
00797        exit (1);
00798    }
00799 //   PrintSet(cout, *bv_target);
00800 //bv2.stat();
00801 
00802     count=
00803        operation_deserializer<bvect>::deserialize(*bv_target,
00804                                                   smem2,
00805                                                   0,
00806                                                   op);
00807 
00808    if (check_reverse)
00809    {
00810         bvect bv_tmp2(BM_GAP);
00811         operation_deserializer<bvect>::deserialize(bv_tmp2,
00812                                                    smem2,
00813                                                    0,
00814                                                    set_ASSIGN);
00815         int res = bv_tmp2.compare(bv2);
00816         if (res != 0)
00817         {
00818             cout << "set_ASSIGN failed 2! " << endl;
00819             exit(1);
00820         }
00821 //        PrintSet(cout, bv_tmp2);
00822 //bv1.stat();
00823         unsigned count_rev =
00824         operation_deserializer<bvect>::deserialize(bv_tmp2,
00825                                                    smem1,
00826                                                    0,
00827                                                    op);
00828         if (count != count_rev)
00829         {
00830             bv1.stat();
00831             cout << "Serialization operation reverse check failed"
00832                  << " count = " << count 
00833                  << " count rev= " << count_rev
00834                  << endl;
00835             exit(1);
00836         }
00837 
00838    }
00839 
00840    delete [] smem1;
00841    delete [] smem2;
00842 
00843    return count;
00844 }
00845 
00846 void SerializationOperation2Test(bvect*        bv_target,
00847                                  bvect&        bv1,
00848                                  bvect&        bv2,
00849                                  unsigned      predicted_count,
00850                                  set_operation op_count,
00851                                  set_operation op_combine)
00852 {
00853     bv_target->clear(true);
00854     unsigned scount1 = SerializationOperation(0, 
00855                                               bv1,
00856                                               bv2,
00857                                               op_count,
00858                                               true /*reverse check*/);
00859 
00860     unsigned scount2 = SerializationOperation(bv_target, 
00861                                               bv1,
00862                                               bv2,
00863                                               op_combine);
00864     scount2 = bv_target->count();
00865     if (predicted_count != scount2 || scount1 != scount2)
00866     {
00867         cout << "Serialization count != predicted" << endl 
00868              << " predicted=" << predicted_count 
00869              << " scount1="   << scount1
00870              << " scount2="   << scount2
00871              << endl;
00872 
00873         cout << endl << "target:" << endl;
00874         bv_target->stat();
00875         cout << endl << endl << "Reference" << endl;
00876         if (op_combine == set_AND)
00877         {
00878             bv1 &= bv2;
00879             bv1.stat();
00880         }
00881 
00882         exit(1);
00883     }
00884 }
00885 
00886 
00887 void print_mv(const bvect_mini &bvect_min, unsigned size)
00888 {
00889     unsigned i;
00890     for (i = 0; i < size; ++i)
00891     {
00892         bool bflag = bvect_min.is_bit_true(i) != 0; 
00893 
00894         if (bflag)
00895             printf("1");
00896         else
00897             printf("0");
00898         if ((i % 31) == 0 && (i != 0))
00899             printf(".");
00900     }
00901 
00902     printf("\n");
00903 }
00904 
00905 void print_gap(const gap_vector& gap_vect, unsigned size)
00906 {
00907     const gap_word_t *buf = gap_vect.get_buf();
00908     unsigned len = gap_length(buf);
00909     printf("[%i:]", *buf++ & 1);
00910 
00911     for (unsigned i = 1; i < len; ++i)
00912     {
00913         printf("%i,", *buf++);
00914     }
00915 
00916     printf("\n");
00917 }
00918 
00919 void CheckGAPMin(const gap_vector& gapv, const bvect_mini& bvect_min, unsigned len)
00920 {
00921     for (unsigned i = 0; i < len; ++i)
00922     {
00923         int bit1 = (gapv.is_bit_true(i) == 1);
00924         int bit2 = (bvect_min.is_bit_true(i) != 0);
00925         if(bit1 != bit2)
00926         {
00927            cout << "Bit comparison failed. " << "Bit N=" << i << endl;
00928            assert(0);
00929            exit(1);
00930         }
00931     }
00932 }
00933 
00934 void CheckIntervals(const bvect& bv, unsigned max_bit)
00935 {
00936     unsigned cnt0 = count_intervals(bv);
00937     unsigned cnt1 = 1;
00938     bool bit_prev = bv.test(0);
00939     for (unsigned i = 1; i < max_bit; ++i)
00940     {
00941         bool bit = bv.test(i);
00942         cnt1 += bit_prev ^ bit;
00943         bit_prev = bit;
00944     }
00945     if (cnt0 != cnt1)
00946     {
00947         cout << "CheckIntervals error. " << "bm count=" << cnt0
00948              << " Control = " << cnt1 << endl;
00949         exit(1);
00950     }
00951 }
00952 
00953 template<class T> void CheckCountRange(const T& vect, 
00954                                        unsigned left, 
00955                                        unsigned right,
00956                                        unsigned* block_count_arr=0)
00957 {
00958     unsigned cnt1 = vect.count_range(left, right, block_count_arr);
00959     unsigned cnt2 = 0;
00960 //cout << endl;
00961     for (unsigned i = left; i <= right; ++i)
00962     {
00963         if (vect.test(i))
00964         {
00965 //            cout << i << " " << flush;
00966             ++cnt2;
00967         }
00968     }
00969 //cout << endl;
00970     if (cnt1 != cnt2)
00971     {
00972         cout << "Bitcount range failed!" << "left=" << left 
00973              << " right=" << right << endl
00974              << "count_range()=" << cnt1 
00975              << " check=" << cnt2;
00976         exit(1);
00977     }
00978 }
00979 
00980 
00981 unsigned BitCountChange(unsigned word)
00982 {
00983     unsigned count = 1;
00984     unsigned bit_prev = word & 1;
00985     word >>= 1;
00986     for (unsigned i = 1; i < 32; ++i)
00987     {
00988         unsigned bit = word & 1;
00989         count += bit ^ bit_prev;
00990         bit_prev = bit;
00991         word >>= 1;
00992     }
00993     return count;
00994 }
00995 
00996 
00997 void DetailedCheckVectors(const bvect_mini &bvect_min, 
00998                           const bvect      &bvect_full,
00999                           unsigned size)
01000 {
01001     cout << "Detailed check" << endl;
01002 
01003     //bvect_full.stat();
01004 
01005     // detailed bit by bit comparison. Paranoia check.
01006 
01007     unsigned i;
01008     for (i = 0; i < size; ++i)
01009     {
01010         bool bv_m_flag = bvect_min.is_bit_true(i) != 0; 
01011         bool bv_f_flag = bvect_full.get_bit(i) != 0;
01012 
01013         if (bv_m_flag != bv_f_flag)
01014         {
01015             printf("Bit %u is non conformant. vect_min=%i vect_full=%i\n",
01016                 i, (int)bv_m_flag, (int)bv_f_flag);
01017 
01018             cout << "Non-conformant block number is: " << unsigned(i >>  bm::set_block_shift) << endl;
01019 //            throw 110;
01020             exit(1);
01021         }
01022 
01023         if (PROGRESS_PRINT)
01024         {
01025             if ( (i % PROGRESS_PRINT) == 0)
01026             {
01027                 printf(".");
01028             }
01029         }
01030              
01031     }
01032     
01033     printf("\n detailed check ok.\n");
01034 
01035 }
01036 
01037 
01038 // vectors comparison check
01039 
01040 void CheckVectors(bvect_mini &bvect_min, 
01041                   bvect      &bvect_full,
01042                   unsigned size,
01043                   bool     detailed)
01044 {
01045     cout << "\nVectors checking...bits to compare = " << size << endl;
01046 
01047     cout << "Bitcount summary : " << endl;
01048     unsigned min_count = bvect_min.bit_count();
01049     cout << "minvector count = " << min_count << endl;
01050     unsigned count = bvect_full.count();
01051     unsigned full_count = bvect_full.recalc_count();
01052     cout << "fullvector re-count = " << full_count << endl;
01053     
01054     if (min_count != full_count)
01055     {
01056         cout << "fullvector count = " << count << endl;
01057         cout << "Count comparison failed !!!!" << endl;
01058         bvect_full.stat();
01059         DetailedCheckVectors(bvect_min, bvect_full, size);
01060 
01061         exit(1);  
01062     } 
01063 
01064     if (full_count)
01065     {
01066         bool any = bvect_full.any();
01067         if (!any)
01068         {
01069             cout << "Anycheck failed!" << endl;
01070             exit(1);
01071         }
01072     }
01073 
01074     // get_next comparison
01075 
01076     cout << "Positive bits comparison..." << flush;
01077     unsigned nb_min = bvect_min.get_first();
01078     unsigned nb_ful = bvect_full.get_first();
01079 
01080     bvect::counted_enumerator en = bvect_full.first();
01081     unsigned nb_en = *en;
01082     if (nb_min != nb_ful)
01083     {
01084          cout << "!!!! First bit comparison failed. Full id = " 
01085               << nb_ful << " Min id = " << nb_min 
01086               << endl;
01087 
01088          bool bit_f = bvect_full.get_bit(nb_ful);
01089          cout << "Full vector'd bit #" << nb_ful << "is:" 
01090               << bit_f << endl;
01091 
01092          bool bit_m = (bvect_min.is_bit_true(nb_min) == 1);
01093          cout << "Min vector'd bit #" << nb_min << "is:" 
01094               << bit_m << endl;
01095 
01096 
01097          bvect_full.stat();
01098 
01099          DetailedCheckVectors(bvect_min, bvect_full, size);
01100 
01101          exit(1);
01102     }
01103 
01104     if (full_count)
01105     {
01106        unsigned bit_count = 1;
01107        unsigned en_prev = nb_en;
01108 
01109        do
01110        {
01111            nb_min = bvect_min.get_next(nb_min);
01112            if (nb_min == 0)
01113            {
01114                break;
01115            }
01116 
01117            en_prev = nb_en;
01118            ++en;
01119 
01120            nb_en = *en;
01121 //           nb_en = bvect_full.get_next(nb_en);
01122 
01123            ++bit_count;
01124 
01125            if (nb_en != nb_min)
01126            {
01127                nb_ful = bvect_full.get_next(en_prev);
01128                cout << "!!!!! next bit comparison failed. Full id = " 
01129                     << nb_ful << " Min id = " << nb_min 
01130                     << " Enumerator = " << nb_en
01131                     << endl;
01132 
01133      //          bvect_full.stat();
01134 
01135      //          DetailedCheckVectors(bvect_min, bvect_full, size);
01136 
01137                exit(1);
01138            }
01139             if ( (bit_count % PROGRESS_PRINT) == 0)
01140            {
01141                 cout << "." << flush;
01142             }
01143 
01144        } while (en.valid());
01145        if (bit_count != min_count)
01146        {
01147            cout << " Bit count failed."  
01148                 << " min = " << min_count 
01149                 << " bit = " << bit_count 
01150                 << endl;
01151            exit(1);
01152        }
01153     }
01154 
01155     cout << "OK" << endl;
01156 
01157     return;
01158 }
01159 
01160 
01161 void ClearAllTest()
01162 {
01163     bvect     bvect_full;
01164 
01165     for (int i = 0; i < 100000; ++i)
01166     {
01167         bvect_full.set_bit(i);
01168     }
01169     bvect_full.optimize();
01170     bvect_full.clear();
01171 
01172     bvect_full.stat();
01173 
01174     int count = bvect_full.count();
01175     assert(count == 0);
01176     bvect_full.stat();
01177 }
01178 
01179 
01180 void WordCmpTest()
01181 {
01182     cout << "---------------------------- WordCmp test" << endl;
01183 
01184     for (int i = 0; i < 10000000; ++i)
01185     {
01186         unsigned w1 = rand();
01187         unsigned w2 = rand();
01188         int res = wordcmp0(w1, w2);
01189         int res2 = wordcmp(w1, w2);
01190         if (res != res2)
01191         {
01192             printf("WordCmp failed !\n");
01193             exit(1);
01194         }
01195 
01196         res = wordcmp0((unsigned)0U, (unsigned)w2);
01197         res2 = wordcmp((unsigned)0U, (unsigned)w2);
01198 
01199         if (res != res2)
01200         {
01201             printf("WordCmp 0 test failed !\n");
01202             exit(1);
01203         }
01204 
01205         res = wordcmp0((unsigned)~0U, (unsigned)w2);
01206         res2 = wordcmp((unsigned)~0U, (unsigned)w2);
01207 
01208         if (res != res2)
01209         {
01210             printf("WordCmp ~0 test failed !\n");
01211             exit(1);
01212         }
01213 
01214         res = wordcmp0((unsigned)w2, (unsigned)0);
01215         res2 = wordcmp((unsigned)w2, (unsigned)0);
01216 
01217         if (res != res2)
01218         {
01219             printf("WordCmp 0-2 test failed !\n");
01220             exit(1);
01221         }
01222 
01223     }
01224 
01225     cout << "Ok." << endl;
01226 }
01227 
01228 void BasicFunctionalityTest()
01229 {
01230     cout << "---------------------------- Basic functinality test" << endl;
01231 
01232     assert(ITERATIONS < BITVECT_SIZE);
01233 
01234 
01235     bvect_mini     bvect_min(BITVECT_SIZE);
01236     bvect          bvect_full;
01237     bvect          bvect_full1;
01238 
01239     printf("\nBasic functionality test.\n");
01240     
01241     // filling vectors with regular values
01242 
01243     unsigned i;
01244     for (i = 0; i < ITERATIONS; ++i)
01245     {
01246         bvect_min.set_bit(i);
01247         bvect_full.set_bit(i);
01248     }
01249     
01250     bvect_full1.set_range(0, ITERATIONS-1);
01251     
01252     CheckCountRange(bvect_full, 0, ITERATIONS);
01253     CheckCountRange(bvect_full, 10, ITERATIONS+10);
01254 
01255     if (bvect_full1 != bvect_full)
01256     {
01257         cout << "set_range failed!" << endl;
01258         bvect_full1.stat();
01259         exit(1);
01260     }
01261 
01262     bvect_full.stat();
01263     bvect_full1.stat();
01264 
01265     // checking the results
01266     unsigned count_min = 0;
01267     for (i = 0; i < ITERATIONS; ++i)
01268     {
01269         if (bvect_min.is_bit_true(i))
01270             ++count_min;
01271     }
01272 
01273 
01274     
01275     unsigned count_full = bvect_full.count();
01276 
01277     if (count_min == count_full)
01278     {
01279         printf("simple count test ok.\n");
01280     }
01281     else
01282     {
01283         printf("simple count test failed count_min = %i  count_full = %i\n", 
01284                count_min, count_full);
01285         exit(1);
01286     }
01287 
01288 
01289     // detailed vectors verification
01290 
01291     CheckVectors(bvect_min, bvect_full, ITERATIONS);
01292 
01293     // now clearning
01294 
01295     for (i = 0; i < ITERATIONS; i+=2)
01296     {
01297         bvect_min.clear_bit(i);
01298         bvect_full.clear_bit(i);
01299         bvect_full1.set_range(i, i, false);
01300     }
01301 
01302     CheckVectors(bvect_min, bvect_full, ITERATIONS);
01303     CheckVectors(bvect_min, bvect_full1, ITERATIONS);
01304 
01305     for (i = 0; i < ITERATIONS; ++i)
01306     {
01307         bvect_min.clear_bit(i);
01308     }
01309     bvect_full.clear();
01310 
01311     CheckVectors(bvect_min, bvect_full, ITERATIONS);
01312 
01313     cout << "Random step filling" << endl;
01314 
01315     for (i = rand()%10; i < ITERATIONS; i+=rand()%10)
01316     {
01317         bvect_min.clear_bit(i);
01318         bvect_full.clear_bit(i);
01319     }
01320     
01321     CheckVectors(bvect_min, bvect_full, ITERATIONS);
01322 
01323     bvect bv1;
01324     bvect bv2;
01325 
01326     bv1[10] = true;
01327     bv1[1000] = true;
01328 
01329     bv2[200] = bv2[700] = bv2[500] = true;
01330 
01331     bv1.swap(bv2);
01332 
01333     if (bv1.count() != 3)
01334     {
01335         cout << "Swap test failed!" << endl;
01336         exit(1);
01337     }
01338 
01339     if (bv2.count() != 2)
01340     {
01341         cout << "Swap test failed!" << endl;
01342         exit(1);
01343     }
01344 }
01345 
01346 void SimpleRandomFillTest()
01347 {
01348     assert(ITERATIONS < BITVECT_SIZE);
01349 
01350     cout << "-------------------------- SimpleRandomFillTest" << endl;
01351 
01352     {
01353     printf("Simple random fill test 1.");
01354     bvect_mini   bvect_min(BITVECT_SIZE);
01355     bvect      bvect_full;
01356     bvect_full.set_new_blocks_strat(bm::BM_BIT);
01357 
01358 
01359     unsigned iter = ITERATIONS / 5;
01360 
01361     printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
01362 
01363     bvect_min.set_bit(0);
01364     bvect_full.set_bit(0);
01365 
01366     unsigned i;
01367     for (i = 0; i < iter; ++i)
01368     {
01369         unsigned num = ::rand() % iter;
01370         bvect_min.set_bit(num);
01371         bvect_full.set_bit(num);
01372         if ((i % 1000) == 0) cout << "." << flush;
01373         CheckCountRange(bvect_full, 0, num);
01374         CheckCountRange(bvect_full, num, num+iter);
01375     }
01376 
01377     CheckVectors(bvect_min, bvect_full, iter);
01378     CheckCountRange(bvect_full, 0, iter);
01379 
01380     printf("Simple random fill test 2.");
01381 
01382     for(i = 0; i < iter; ++i)
01383     {
01384         unsigned num = ::rand() % iter;
01385         bvect_min.clear_bit(num);
01386         bvect_full.clear_bit(num);
01387     }
01388 
01389     CheckVectors(bvect_min, bvect_full, iter);
01390     }
01391 
01392 
01393     {
01394     printf("\nSimple random fill test 3.\n");
01395     bvect_mini   bvect_min(BITVECT_SIZE);
01396     bvect      bvect_full(bm::BM_GAP);
01397 
01398 
01399     unsigned iter = ITERATIONS;
01400 
01401     printf("\nSimple Random fill test ITERATIONS = %i\n", iter);
01402 
01403     unsigned i;
01404     for(i = 0; i < iter; ++i)
01405     {
01406         unsigned num = ::rand() % iter;
01407         bvect_min.set_bit(num);
01408         bvect_full.set_bit(num);
01409         CheckCountRange(bvect_full, 0, 65535);
01410         CheckCountRange(bvect_full, 0, num);
01411         CheckCountRange(bvect_full, num, num+iter);
01412         if ((i % 1000) == 0) cout << "." << flush;
01413     }
01414 
01415     CheckVectors(bvect_min, bvect_full, iter);
01416 
01417     printf("Simple random fill test 4.");
01418 
01419     for(i = 0; i < iter; ++i)
01420     {
01421         unsigned num = ::rand() % iter;
01422         bvect_min.clear_bit(num);
01423         bvect_full.clear_bit(num);
01424         CheckCountRange(bvect_full, 0, num);
01425         CheckCountRange(bvect_full, num, num+iter);
01426         if ((i % 1000) == 0) cout << "." << flush;
01427     }
01428 
01429     CheckVectors(bvect_min, bvect_full, iter);
01430     CheckCountRange(bvect_full, 0, iter);
01431     }
01432 
01433 
01434 }
01435 
01436 
01437 
01438 
01439 void RangeRandomFillTest()
01440 {
01441     assert(ITERATIONS < BITVECT_SIZE);
01442 
01443     cout << "----------------------------------- RangeRandomFillTest" << endl;
01444 
01445     {
01446     bvect_mini   bvect_min(BITVECT_SIZE);
01447     bvect     bvect_full;
01448 
01449     printf("Range Random fill test\n");
01450 
01451     unsigned min = BITVECT_SIZE / 2;
01452     unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
01453     if (max > BITVECT_SIZE) 
01454         max = BITVECT_SIZE - 1;
01455 
01456     FillSets(&bvect_min, &bvect_full, min, max, 0);
01457 
01458     CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
01459     CheckCountRange(bvect_full, min, max);
01460 
01461     }
01462 
01463     
01464     {
01465     bvect_mini   bvect_min(BITVECT_SIZE);
01466     bvect     bvect_full;
01467 
01468     printf("Range Random fill test\n");
01469 
01470     unsigned min = BITVECT_SIZE / 2;
01471     unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
01472     if (max > BITVECT_SIZE) 
01473         max = BITVECT_SIZE - 1;
01474 
01475     FillSetsIntervals(&bvect_min, &bvect_full, min, max, 4);
01476 
01477     CheckVectors(bvect_min, bvect_full, BITVECT_SIZE);
01478     CheckCountRange(bvect_full, min, max);
01479     }
01480     
01481 
01482 }
01483 
01484 
01485 
01486 void AndOperationsTest()
01487 {
01488     assert(ITERATIONS < BITVECT_SIZE);
01489 
01490     cout << "----------------------------------- AndOperationTest" << endl;
01491 
01492     {
01493 
01494     bvect_mini   bvect_min1(256);
01495     bvect_mini   bvect_min2(256);
01496     bvect        bvect_full1;
01497     bvect        bvect_full2;
01498 
01499     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01500     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01501 
01502 
01503 
01504     printf("AND test\n");
01505 
01506     bvect_min1.set_bit(1);
01507     bvect_min1.set_bit(12);
01508     bvect_min1.set_bit(13);
01509 
01510     bvect_min2.set_bit(12);
01511     bvect_min2.set_bit(13);
01512 
01513     bvect_min1.combine_and(bvect_min2);
01514 
01515     bvect_full1.set_bit(1);
01516     bvect_full1.set_bit(12);
01517     bvect_full1.set_bit(13);
01518 
01519     bvect_full2.set_bit(12);
01520     bvect_full2.set_bit(13);
01521 
01522     bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
01523 
01524     bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
01525     if (predicted_any == 0 && predicted_count != 0)
01526     {
01527         cout << "Predicted any error!" << endl;
01528         exit(1);
01529     }
01530 
01531     bvect    bv_target_s;
01532     SerializationOperation2Test(&bv_target_s,
01533                                 bvect_full1,
01534                                 bvect_full2,
01535                                 predicted_count,
01536                                 set_COUNT_AND,
01537                                 set_AND);
01538 
01539 
01540     bvect_full1.bit_and(bvect_full2);
01541 
01542     bm::id_t count = bvect_full1.count();
01543     if (count != predicted_count)
01544     {
01545         cout << "Predicted count error!" << endl;
01546         exit(1);
01547     }
01548 
01549     CheckVectors(bvect_min1, bvect_full1, 256);
01550     CheckVectors(bvect_min1, bv_target_s, 256);
01551     CheckCountRange(bvect_full1, 0, 256);
01552 
01553     }
01554 
01555     {
01556 
01557     bvect_mini   bvect_min1(BITVECT_SIZE);
01558     bvect_mini   bvect_min2(BITVECT_SIZE);
01559     bvect        bvect_full1;
01560     bvect        bvect_full2;
01561 
01562 
01563     printf("AND test stage 1.\n");
01564 
01565     for (int i = 0; i < 112; ++i)
01566     {
01567         bvect_min1.set_bit(i);
01568         bvect_full1.set_bit(i);
01569 
01570         bvect_min2.set_bit(i);
01571         bvect_full2.set_bit(i);
01572 
01573     }
01574 
01575     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
01576     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
01577 
01578 //    FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
01579 //    FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
01580 
01581     bvect_min1.combine_and(bvect_min2);
01582 
01583     bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
01584     bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
01585     if (predicted_any == 0 && predicted_count != 0)
01586     {
01587         cout << "Predicted any error!" << endl;
01588         exit(1);
01589     }
01590 
01591     bvect    bv_target_s;
01592     SerializationOperation2Test(&bv_target_s,
01593                                 bvect_full1,
01594                                 bvect_full2,
01595                                 predicted_count,
01596                                 set_COUNT_AND,
01597                                 set_AND);
01598 
01599     bvect_full1.bit_and(bvect_full2);
01600 
01601     bm::id_t count = bvect_full1.count();
01602     if (count != predicted_count)
01603     {
01604         cout << "Predicted count error!" << endl;
01605         exit(1);
01606     }
01607 
01608     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
01609     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
01610     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
01611 
01612     }
01613 
01614 
01615     {
01616 
01617     bvect_mini   bvect_min1(BITVECT_SIZE);
01618     bvect_mini   bvect_min2(BITVECT_SIZE);
01619     bvect        bvect_full1;
01620     bvect        bvect_full2;
01621 
01622     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01623     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01624 
01625     printf("AND test stage 2.\n");
01626 
01627 
01628     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
01629     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
01630 
01631     bm::id_t predicted_count = bm::count_and(bvect_full1,bvect_full2);
01632     bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
01633     if (predicted_any == 0 && predicted_count != 0)
01634     {
01635         cout << "Predicted any error!" << endl;
01636         exit(1);
01637     }
01638 
01639     bvect    bv_target_s;
01640     SerializationOperation2Test(&bv_target_s,
01641                                 bvect_full1,
01642                                 bvect_full2,
01643                                 predicted_count,
01644                                 set_COUNT_AND,
01645                                 set_AND);
01646 
01647     bvect_min1.combine_and(bvect_min2);
01648 
01649     bvect_full1.bit_and(bvect_full2);
01650 
01651     bm::id_t count = bvect_full1.count();
01652     if (count != predicted_count)
01653     {
01654         cout << "Predicted count error!" << endl;
01655         bvect_full1.stat();
01656         exit(1);
01657     }
01658 
01659     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
01660     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
01661     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
01662 
01663     }
01664 
01665     {
01666 
01667     bvect_mini   bvect_min1(BITVECT_SIZE);
01668     bvect_mini   bvect_min2(BITVECT_SIZE);
01669     bvect        bvect_full1;
01670     bvect        bvect_full2;
01671 
01672     bvect_full1.set_new_blocks_strat(bm::BM_BIT);
01673     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
01674 
01675     cout << "------------------------------" << endl;
01676     printf("AND test stage 3.\n");
01677 
01678 
01679     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
01680     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
01681 
01682     bvect_min1.combine_and(bvect_min2);
01683 
01684     bm::id_t predicted_count = bm::count_and(bvect_full1, bvect_full2);
01685     bm::id_t predicted_any = bm::any_and(bvect_full1, bvect_full2);
01686     if (predicted_any == 0 && predicted_count != 0)
01687     {
01688         cout << "Predicted any error!" << endl;
01689         exit(1);
01690     }
01691     
01692     bvect    bv_target_s;
01693     SerializationOperation2Test(&bv_target_s,
01694                                 bvect_full1,
01695                                 bvect_full2,
01696                                 predicted_count,
01697                                 set_COUNT_AND,
01698                                 set_AND);
01699 
01700     bvect_full1.bit_and(bvect_full2);
01701 
01702     bm::id_t count = bvect_full1.count();
01703     if (count != predicted_count)
01704     {
01705         cout << "Predicted count error!" << endl;
01706         exit(1);
01707     }
01708 
01709     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01710     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
01711     CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
01712 
01713     bvect_full1.optimize();
01714     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01715     CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
01716     CheckCountRange(bvect_full1, BITVECT_SIZE/2, BITVECT_SIZE);
01717 
01718     }
01719 
01720     cout << "------------------------------" << endl;
01721     printf("AND test stage 4. combine_and_sorted\n");
01722     {
01723     unsigned ids[] = {0, 1, 2, 3, 10, 65535, 65536, 65535*2, 65535*3};
01724     unsigned to_add = sizeof(ids)/sizeof(unsigned);
01725     bvect        bvect_full1;
01726     bvect        bvect_full2;    
01727     bvect_mini   bvect_min1(BITVECT_SIZE);
01728     bvect_mini   bvect_min2(BITVECT_SIZE);
01729 
01730     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01731     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01732     
01733     for (unsigned i = 2; i < to_add; ++i)
01734     {
01735         bvect_full1.set(ids[i]);
01736         bvect_min1.set_bit(ids[i]);
01737         bvect_full2.set(ids[i]);
01738         bvect_min2.set_bit(ids[i]);
01739     }
01740     
01741     unsigned* first = ids;
01742     unsigned* last = ids + to_add;
01743     
01744     bvect_min1.combine_and(bvect_min2);
01745 
01746     bm::combine_and_sorted(bvect_full1, first, last);
01747     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01748     }
01749 
01750 }
01751 
01752 
01753 void OrOperationsTest()
01754 {
01755     assert(ITERATIONS < BITVECT_SIZE);
01756 
01757     cout << "----------------------------------- OrOperationTest" << endl;
01758 
01759     {
01760 
01761     bvect_mini   bvect_min1(256);
01762     bvect_mini   bvect_min2(256);
01763     bvect        bvect_full1;
01764     bvect        bvect_full2;
01765 
01766     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01767     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01768 
01769 
01770 
01771     printf("OR test\n");
01772 
01773     bvect_min1.set_bit(1);
01774     bvect_min1.set_bit(12);
01775     bvect_min1.set_bit(13);
01776 
01777     bvect_min2.set_bit(12);
01778     bvect_min2.set_bit(13);
01779 
01780     bvect_min1.combine_or(bvect_min2);
01781 
01782     bvect_full1.set_bit(1);
01783     bvect_full1.set_bit(12);
01784     bvect_full1.set_bit(13);
01785 
01786     bvect_full2.set_bit(12);
01787     bvect_full2.set_bit(13);
01788     
01789     bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
01790     bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
01791     if (predicted_any == 0 && predicted_count != 0)
01792     {
01793         cout << "Predicted any error!" << endl;
01794         exit(1);
01795     }
01796 
01797     bvect    bv_target_s;
01798     SerializationOperation2Test(&bv_target_s,
01799                                 bvect_full1,
01800                                 bvect_full2,
01801                                 predicted_count,
01802                                 set_COUNT_OR,
01803                                 set_OR);
01804 
01805 
01806     bvect_full1.bit_or(bvect_full2);
01807 
01808     bm::id_t count = bvect_full1.count();
01809     if (count != predicted_count)
01810     {
01811         cout << "Predicted count error!" << endl;
01812         cout << predicted_count << " " << count << endl;
01813         bvect_full1.stat();
01814         exit(1);
01815     }
01816 
01817 
01818     CheckVectors(bvect_min1, bvect_full1, 256);
01819     CheckVectors(bvect_min1, bv_target_s, 256);
01820     CheckCountRange(bvect_full1, 0, 256);
01821     CheckCountRange(bvect_full1, 128, 256);
01822     }
01823 
01824     {
01825 
01826     bvect_mini   bvect_min1(BITVECT_SIZE);
01827     bvect_mini   bvect_min2(BITVECT_SIZE);
01828     bvect        bvect_full1;
01829     bvect        bvect_full2;
01830 
01831     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01832     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01833 
01834     printf("OR test stage 2.\n");
01835 
01836 
01837     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
01838     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
01839 
01840     bvect_min1.combine_or(bvect_min2);
01841 
01842     bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
01843     bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
01844     if (predicted_any == 0 && predicted_count != 0)
01845     {
01846         cout << "Predicted any error!" << endl;
01847         exit(1);
01848     }
01849 
01850     bvect    bv_target_s;
01851     SerializationOperation2Test(&bv_target_s,
01852                                 bvect_full1,
01853                                 bvect_full2,
01854                                 predicted_count,
01855                                 set_COUNT_OR,
01856                                 set_OR);
01857 
01858 
01859     bvect_full1.bit_or(bvect_full2);
01860 
01861     bm::id_t count = bvect_full1.count();
01862     if (count != predicted_count)
01863     {
01864         cout << "Predicted count error!" << endl;
01865         exit(1);
01866     }
01867 
01868     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
01869     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
01870     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
01871 
01872     }
01873 
01874     {
01875 
01876     bvect_mini   bvect_min1(BITVECT_SIZE);
01877     bvect_mini   bvect_min2(BITVECT_SIZE);
01878     bvect        bvect_full1;
01879     bvect        bvect_full2;
01880 
01881     bvect_full1.set_new_blocks_strat(bm::BM_BIT);
01882     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
01883 
01884     cout << "------------------------------" << endl;
01885     printf("OR test stage 3.\n");
01886 
01887 
01888     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
01889     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
01890 
01891     bvect_min1.combine_or(bvect_min2);
01892     
01893     bm::id_t predicted_count = bm::count_or(bvect_full1, bvect_full2);    
01894     bm::id_t predicted_any = bm::any_or(bvect_full1, bvect_full2);
01895     if (predicted_any == 0 && predicted_count != 0)
01896     {
01897         cout << "Predicted any error!" << endl;
01898         exit(1);
01899     }
01900 
01901     bvect    bv_target_s;
01902     SerializationOperation2Test(&bv_target_s,
01903                                 bvect_full1,
01904                                 bvect_full2,
01905                                 predicted_count,
01906                                 set_COUNT_OR,
01907                                 set_OR);
01908 
01909     bvect_full1.bit_or(bvect_full2);
01910 
01911     bm::id_t count = bvect_full1.count();
01912     if (count != predicted_count)
01913     {
01914         cout << "Predicted count error!" << endl;
01915         exit(1);
01916     }
01917 
01918     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01919 
01920     bvect_full1.optimize();
01921 
01922     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01923     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
01924     CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
01925 
01926 
01927     }
01928     
01929     cout << "Testing combine_or" << endl;
01930     
01931     {
01932     
01933     bvect        bvect_full1;
01934     bvect        bvect_full2;
01935     bvect_mini   bvect_min1(BITVECT_SIZE);
01936     
01937     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01938     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01939 
01940     unsigned ids[10000];
01941     unsigned to_add = 10000;
01942     
01943     unsigned bn = 0;
01944     for (unsigned i = 0; i < to_add; ++i)
01945     {
01946         ids[i] = bn;
01947         bvect_full2.set(bn);
01948         bvect_min1.set_bit(bn);
01949         bn += 15;
01950     }
01951     
01952     unsigned* first = ids;
01953     unsigned* last = ids + to_add;
01954     
01955     bm::combine_or(bvect_full1, first, last);
01956 
01957     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01958     
01959     bm::combine_or(bvect_full1, first, last);
01960     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01961     
01962     }
01963     
01964     
01965     {
01966     unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
01967     unsigned to_add = sizeof(ids)/sizeof(unsigned);
01968     bvect        bvect_full1;
01969     bvect        bvect_full2;    
01970     bvect_mini   bvect_min1(BITVECT_SIZE);
01971 
01972     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
01973     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
01974     
01975     unsigned bn = 0;
01976     for (unsigned i = 0; i < to_add; ++i)
01977     {
01978         ids[i] = bn;
01979         bvect_full2.set(bn);
01980         bvect_min1.set_bit(bn);
01981         bn += 15;
01982     }
01983     
01984     unsigned* first = ids;
01985     unsigned* last = ids + to_add;
01986     
01987     bm::combine_or(bvect_full1, first, last);
01988     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
01989 
01990     bm::combine_or(bvect_full1, first, last);
01991     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);    
01992     }
01993     
01994 
01995 }
01996 
01997 
01998 
01999 void SubOperationsTest()
02000 {
02001     assert(ITERATIONS < BITVECT_SIZE);
02002 
02003     cout << "----------------------------------- SubOperationTest" << endl;
02004 
02005     {
02006 
02007     bvect_mini   bvect_min1(256);
02008     bvect_mini   bvect_min2(256);
02009     bvect        bvect_full1;
02010     bvect        bvect_full2;
02011 
02012     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02013     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02014 
02015 
02016 
02017     printf("SUB test\n");
02018 
02019     bvect_min1.set_bit(1);
02020     bvect_min1.set_bit(12);
02021     bvect_min1.set_bit(13);
02022 
02023     bvect_min2.set_bit(12);
02024     bvect_min2.set_bit(13);
02025 
02026     bvect_min1.combine_sub(bvect_min2);
02027 
02028     bvect_full1.set_bit(1);
02029     bvect_full1.set_bit(12);
02030     bvect_full1.set_bit(13);
02031 
02032     bvect_full2.set_bit(12);
02033     bvect_full2.set_bit(13);
02034 
02035     bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
02036     bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
02037     if (predicted_any == 0 && predicted_count != 0)
02038     {
02039         cout << "Predicted any error!" << endl;
02040         exit(1);
02041     }
02042 
02043     bvect    bv_target_s;
02044     SerializationOperation2Test(&bv_target_s,
02045                                 bvect_full1,
02046                                 bvect_full2,
02047                                 predicted_count,
02048                                 set_COUNT_SUB_AB,
02049                                 set_SUB);
02050 
02051 
02052     bvect_full1.bit_sub(bvect_full2);
02053     
02054     bm::id_t count = bvect_full1.count();
02055     if (count != predicted_count)
02056     {
02057         cout << "Predicted count error!" << endl;
02058         exit(1);
02059     }
02060 
02061     CheckVectors(bvect_min1, bvect_full1, 256);
02062     CheckVectors(bvect_min1, bv_target_s, 256);
02063     CheckCountRange(bvect_full1, 0, 256);
02064 
02065     }
02066 
02067     {
02068 
02069     bvect_mini   bvect_min1(BITVECT_SIZE);
02070     bvect_mini   bvect_min2(BITVECT_SIZE);
02071     bvect        bvect_full1;
02072     bvect        bvect_full2;
02073 
02074     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02075     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02076 
02077     printf("SUB test stage 2.\n");
02078 
02079 
02080     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
02081     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
02082 
02083     bvect_min1.combine_sub(bvect_min2);
02084 
02085     bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
02086     bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
02087     if (predicted_any == 0 && predicted_count != 0)
02088     {
02089         cout << "Predicted any error!" << endl;
02090         exit(1);
02091     }
02092 
02093     bvect    bv_target_s;
02094     SerializationOperation2Test(&bv_target_s,
02095                                 bvect_full1,
02096                                 bvect_full2,
02097                                 predicted_count,
02098                                 set_COUNT_SUB_AB,
02099                                 set_SUB);
02100 
02101     bvect_full1.bit_sub(bvect_full2);
02102     
02103     bm::id_t count = bvect_full1.count();
02104     if (count != predicted_count)
02105     {
02106         cout << "Predicted count error!" << endl;
02107         cout << predicted_count << " " << count << endl;
02108 bvect_full1.stat();    
02109         
02110         exit(1);
02111     }
02112     
02113 
02114     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
02115     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
02116     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
02117 
02118     }
02119 
02120     {
02121 
02122     bvect_mini   bvect_min1(BITVECT_SIZE);
02123     bvect_mini   bvect_min2(BITVECT_SIZE);
02124     bvect        bvect_full1;
02125     bvect        bvect_full2;
02126 
02127     bvect_full1.set_new_blocks_strat(bm::BM_BIT);
02128     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
02129 
02130     cout << "------------------------------" << endl;
02131     printf("SUB test stage 3.\n");
02132 
02133 
02134     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
02135     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
02136 
02137     bvect_min1.combine_sub(bvect_min2);
02138     
02139     bm::id_t predicted_count = bm::count_sub(bvect_full1, bvect_full2);
02140     bm::id_t predicted_any = bm::any_sub(bvect_full1, bvect_full2);
02141     if (predicted_any == 0 && predicted_count != 0)
02142     {
02143         cout << "Predicted any error!" << endl;
02144         exit(1);
02145     }
02146 
02147     bvect    bv_target_s;
02148     SerializationOperation2Test(&bv_target_s,
02149                                 bvect_full1,
02150                                 bvect_full2,
02151                                 predicted_count,
02152                                 set_COUNT_SUB_AB,
02153                                 set_SUB);
02154 
02155     bvect_full1.bit_sub(bvect_full2);
02156 
02157     bm::id_t count = bvect_full1.count();
02158     if (count != predicted_count)
02159     {
02160         cout << "Predicted count error!" << endl;
02161         exit(1);
02162     }
02163 
02164 
02165     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02166 
02167     bvect_full1.optimize();
02168     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02169     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
02170     CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
02171 
02172     }
02173 
02174 }
02175 
02176 
02177 
02178 void XorOperationsTest()
02179 {
02180     assert(ITERATIONS < BITVECT_SIZE);
02181 
02182     cout << "----------------------------------- XorOperationTest" << endl;
02183     {
02184 
02185     bvect_mini   bvect_min1(256);
02186     bvect_mini   bvect_min2(256);
02187     bvect        bvect_full1;
02188     bvect        bvect_full2;
02189 
02190     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02191     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02192 
02193 
02194 
02195     printf("XOR test\n");
02196 
02197     bvect_min1.set_bit(1);
02198     bvect_min1.set_bit(12);
02199     bvect_min1.set_bit(13);
02200 
02201     bvect_min2.set_bit(12);
02202     bvect_min2.set_bit(13);
02203 
02204     bvect_min1.combine_xor(bvect_min2);
02205 
02206     bvect_full1.set_bit(1);
02207     bvect_full1.set_bit(12);
02208     bvect_full1.set_bit(13);
02209 
02210     bvect_full2.set_bit(12);
02211     bvect_full2.set_bit(13);
02212 
02213     bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
02214     bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
02215     if (predicted_any == 0 && predicted_count != 0)
02216     {
02217         cout << "Predicted any error!" << endl;
02218         exit(1);
02219     }
02220 
02221     bvect    bv_target_s;
02222     SerializationOperation2Test(&bv_target_s,
02223                                 bvect_full1,
02224                                 bvect_full2,
02225                                 predicted_count,
02226                                 set_COUNT_XOR,
02227                                 set_XOR);
02228 
02229 
02230     bvect_full1.bit_xor(bvect_full2);
02231 
02232     bm::id_t count = bvect_full1.count();
02233     if (count != predicted_count)
02234     {
02235         cout << "1.Predicted count error!" << endl;
02236         exit(1);
02237     }
02238 
02239     CheckVectors(bvect_min1, bvect_full1, 256);
02240     CheckVectors(bvect_min1, bv_target_s, 256);
02241     CheckCountRange(bvect_full1, 0, 256);
02242     CheckCountRange(bvect_full1, 128, 256);
02243 
02244     }
02245     {
02246         bvect  bvect1;
02247         bvect_mini  bvect_min1(BITVECT_SIZE);
02248 
02249         bvect  bvect2;
02250         bvect_mini  bvect_min2(BITVECT_SIZE);
02251 
02252 
02253         for (int i = 0; i < 150000; ++i)
02254         {
02255             bvect2.set_bit(i);
02256             bvect_min2.set_bit(i);
02257         }
02258 
02259         bvect2.optimize();
02260 
02261         bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
02262         bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
02263         if (predicted_any == 0 && predicted_count != 0)
02264         {
02265             cout << "Predicted any error!" << endl;
02266             exit(1);
02267         }
02268 
02269         bvect    bv_target_s;
02270         SerializationOperation2Test(&bv_target_s,
02271                                     bvect1,
02272                                     bvect2,
02273                                     predicted_count,
02274                                     set_COUNT_XOR,
02275                                     set_XOR);
02276 
02277         bvect1.bit_xor(bvect2);
02278         
02279         bm::id_t count = bvect1.count();
02280         if (count != predicted_count)
02281         {
02282             cout << "2.Predicted count error!" << endl;
02283             exit(1);
02284         }
02285         
02286         bvect_min1.combine_xor(bvect_min2);
02287         CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
02288         CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, true);
02289         CheckCountRange(bvect1, 0, BITVECT_SIZE);
02290     }
02291 
02292 
02293     {
02294         bvect  bvect1;
02295         bvect_mini  bvect_min1(BITVECT_SIZE);
02296 
02297         bvect  bvect2;
02298         bvect_mini  bvect_min2(BITVECT_SIZE);
02299 
02300 
02301         for (int i = 0; i < 150000; ++i)
02302         {
02303             bvect1.set_bit(i);
02304             bvect_min1.set_bit(i);
02305         }
02306 
02307         bvect1.optimize();
02308         
02309         bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
02310         bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
02311         if (predicted_any == 0 && predicted_count != 0)
02312         {
02313             cout << "Predicted any error!" << endl;
02314             exit(1);
02315         }
02316 
02317         bvect    bv_target_s;
02318         SerializationOperation2Test(&bv_target_s,
02319                                     bvect1,
02320                                     bvect2,
02321                                     predicted_count,
02322                                     set_COUNT_XOR,
02323                                     set_XOR);
02324 
02325         bvect1.bit_xor(bvect2);
02326 
02327         bm::id_t count = bvect1.count();
02328         if (count != predicted_count)
02329         {
02330             cout << "3.Predicted count error!" << endl;
02331             exit(1);
02332         }
02333         
02334         bvect_min1.combine_xor(bvect_min2);
02335         CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
02336         CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE, true);
02337     }
02338 
02339 
02340     {
02341         bvect  bvect1;
02342         bvect_mini  bvect_min1(BITVECT_SIZE);
02343 
02344         bvect  bvect2;
02345         bvect_mini  bvect_min2(BITVECT_SIZE);
02346 
02347 
02348         for (int i = 0; i < 150000; ++i)
02349         {
02350             bvect1.set_bit(i);
02351             bvect_min1.set_bit(i);
02352             bvect2.set_bit(i);
02353             bvect_min2.set_bit(i);
02354         }
02355 
02356         bvect1.optimize();
02357         
02358         bm::id_t predicted_count = bm::count_xor(bvect1, bvect2);
02359         bm::id_t predicted_any = bm::any_xor(bvect1, bvect2);
02360         if (predicted_any == 0 && predicted_count != 0)
02361         {
02362             cout << "Predicted any error!" << endl;
02363             exit(1);
02364         }
02365 
02366         bvect    bv_target_s;
02367         SerializationOperation2Test(&bv_target_s,
02368                                     bvect1,
02369                                     bvect2,
02370                                     predicted_count,
02371                                     set_COUNT_XOR,
02372                                     set_XOR);
02373 
02374         bvect1.bit_xor(bvect2);
02375 
02376         bm::id_t count = bvect1.count();
02377         if (count != predicted_count)
02378         {
02379             cout << "4.Predicted count error!" << endl;
02380             cout << count << " " << predicted_count << endl;
02381             
02382             exit(1);
02383         }
02384         
02385         bvect_min1.combine_xor(bvect_min2);
02386         CheckVectors(bvect_min1, bvect1, BITVECT_SIZE, true);
02387     }
02388 
02389 
02390 
02391     {
02392 
02393     bvect_mini   bvect_min1(BITVECT_SIZE);
02394     bvect_mini   bvect_min2(BITVECT_SIZE);
02395     bvect        bvect_full1;
02396     bvect        bvect_full2;
02397 
02398     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02399     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02400 
02401     printf("XOR test stage 2.\n");
02402 
02403     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/7, 0);
02404     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/7, 0);
02405 
02406     bvect_min1.combine_xor(bvect_min2);
02407     
02408     bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
02409     bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
02410     if (predicted_any == 0 && predicted_count != 0)
02411     {
02412         cout << "Predicted any error!" << endl;
02413         exit(1);
02414     }
02415 
02416     bvect    bv_target_s;
02417     SerializationOperation2Test(&bv_target_s,
02418                                 bvect_full1,
02419                                 bvect_full2,
02420                                 predicted_count,
02421                                 set_COUNT_XOR,
02422                                 set_XOR);
02423 
02424 
02425     bvect_full1.bit_xor(bvect_full2);
02426     
02427     bm::id_t count = bvect_full1.count();
02428     if (count != predicted_count)
02429     {
02430         cout << "5.Predicted count error!" << endl;
02431         cout << count << " " << predicted_count << endl;
02432         bvect_full1.stat();
02433         exit(1);
02434     }
02435 
02436     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE/10+10);
02437     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE/10+10);
02438     CheckCountRange(bvect_full1, 0, BITVECT_SIZE/10+10);
02439 
02440     }
02441 
02442     {
02443 
02444     bvect_mini   bvect_min1(BITVECT_SIZE);
02445     bvect_mini   bvect_min2(BITVECT_SIZE);
02446     bvect        bvect_full1;
02447     bvect        bvect_full2;
02448 
02449     bvect_full1.set_new_blocks_strat(bm::BM_BIT);
02450     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
02451 
02452     cout << "------------------------------" << endl;
02453     printf("XOR test stage 3.\n");
02454 
02455 
02456     FillSets(&bvect_min1, &bvect_full1, 1, BITVECT_SIZE/5, 2);
02457     FillSets(&bvect_min2, &bvect_full2, 1, BITVECT_SIZE/5, 2);
02458 
02459     bm::id_t predicted_count = bm::count_xor(bvect_full1, bvect_full2);
02460     bm::id_t predicted_any = bm::any_xor(bvect_full1, bvect_full2);
02461     if (predicted_any == 0 && predicted_count != 0)
02462     {
02463         cout << "Predicted any error!" << endl;
02464         exit(1);
02465     }
02466 
02467     bvect    bv_target_s;
02468     SerializationOperation2Test(&bv_target_s,
02469                                 bvect_full1,
02470                                 bvect_full2,
02471                                 predicted_count,
02472                                 set_COUNT_XOR,
02473                                 set_XOR);
02474 
02475     bvect_min1.combine_xor(bvect_min2);
02476 
02477     bvect_full1.bit_xor(bvect_full2);
02478 
02479     bm::id_t count = bvect_full1.count();
02480     if (count != predicted_count)
02481     {
02482         cout << "6.Predicted count error!" << endl;
02483         exit(1);
02484     }
02485 
02486 
02487     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02488 
02489     bvect_full1.optimize();
02490     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02491     CheckVectors(bvect_min1, bv_target_s, BITVECT_SIZE);
02492     CheckCountRange(bvect_full1, 0, BITVECT_SIZE);
02493 
02494 
02495     }
02496 
02497 
02498     cout << "Testing combine_xor" << endl;
02499     
02500     {
02501     
02502     bvect        bvect_full1;
02503     bvect        bvect_full2;
02504     bvect_mini   bvect_min1(BITVECT_SIZE);
02505     
02506     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02507     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02508 
02509     unsigned ids[10000];
02510     unsigned to_add = 10000;
02511     
02512     unsigned bn = 0;
02513     for (unsigned i = 0; i < to_add; ++i)
02514     {
02515         ids[i] = bn;
02516         bvect_full2.set(bn);
02517         bvect_min1.set_bit(bn);
02518         bn += 15;
02519     }
02520     
02521     unsigned* first = ids;
02522     unsigned* last = ids + to_add;
02523     
02524     bm::combine_xor(bvect_full1, first, last);
02525 
02526     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02527     
02528     bm::combine_xor(bvect_full1, first, last);
02529     if (bvect_full1.count())
02530     {
02531         cout << "combine_xor count failed!" << endl;
02532         exit(1);
02533     }
02534     
02535     }
02536 
02537     {
02538     
02539     bvect        bvect_full1;
02540     bvect        bvect_full2;
02541     bvect_mini   bvect_min1(BITVECT_SIZE);
02542     
02543     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02544     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02545 
02546     unsigned ids[10000]={0,};
02547     unsigned to_add = 10000;
02548     
02549     for (unsigned i = 0; i < to_add; i+=100)
02550     {
02551         ids[i] = i;
02552         bvect_full2.set(i);
02553         bvect_min1.set_bit(i);
02554     }
02555     unsigned* first = ids;
02556     unsigned* last = ids + to_add;
02557     
02558     bm::combine_xor(bvect_full1, first, last);
02559 
02560     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02561     
02562     bm::combine_xor(bvect_full1, first, last);
02563     if (bvect_full1.count())
02564     {
02565         cout << "combine_xor count failed!" << endl;
02566         exit(1);
02567     }
02568     
02569     }
02570 
02571     
02572     {
02573     unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
02574     unsigned to_add = sizeof(ids)/sizeof(unsigned);
02575     bvect        bvect_full1;
02576     bvect        bvect_full2;    
02577     bvect_mini   bvect_min1(BITVECT_SIZE);
02578 
02579     bvect_full1.set_new_blocks_strat(bm::BM_BIT);
02580     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
02581     
02582     unsigned bn = 0;
02583     for (unsigned i = 0; i < to_add; ++i)
02584     {
02585         ids[i] = bn;
02586         bvect_full2.set(bn);
02587         bvect_min1.set_bit(bn);
02588         bn += 15;
02589     }
02590     
02591     unsigned* first = ids;
02592     unsigned* last = ids + to_add;
02593     
02594     bm::combine_xor(bvect_full1, first, last);
02595     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02596 
02597     bm::combine_xor(bvect_full1, first, last);
02598     if (bvect_full1.count())
02599     {
02600         cout << "combine_xor count failed!" << endl;
02601         exit(1);
02602     }
02603     }
02604     
02605     
02606     {
02607     unsigned ids[] = {0, 65536, 65535, 65535*3, 65535*2, 10};
02608     unsigned to_add = sizeof(ids)/sizeof(unsigned);
02609     bvect        bvect_full1;
02610     bvect        bvect_full2;    
02611     bvect_mini   bvect_min1(BITVECT_SIZE);
02612 
02613     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
02614     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
02615     
02616     unsigned bn = 0;
02617     for (unsigned i = 0; i < to_add; ++i)
02618     {
02619         ids[i] = bn;
02620         bvect_full2.set(bn);
02621         bvect_min1.set_bit(bn);
02622         bn += 15;
02623     }
02624     
02625     unsigned* first = ids;
02626     unsigned* last = ids + to_add;
02627     
02628     bm::combine_xor(bvect_full1, first, last);
02629     CheckVectors(bvect_min1, bvect_full1, BITVECT_SIZE);
02630 
02631     bm::combine_xor(bvect_full1, first, last);
02632     if (bvect_full1.count())
02633     {
02634         cout << "combine_xor count failed!" << endl;
02635         exit(1);
02636     }
02637     }
02638 
02639 }
02640 
02641 
02642 void ComparisonTest()
02643 {
02644     cout << "-------------------------------------- ComparisonTest" << endl;
02645 
02646     bvect_mini   bvect_min1(BITVECT_SIZE);
02647     bvect_mini   bvect_min2(BITVECT_SIZE);
02648     bvect        bvect_full1;
02649     bvect        bvect_full2;
02650     int res1, res2;
02651 
02652     bvect_full1.set_bit(31); 
02653     bvect_full2.set_bit(63); 
02654 
02655     res1 = bvect_full1.compare(bvect_full2);
02656     if (res1 != 1)
02657     {
02658         printf("Comparison test failed 1\n");
02659         exit(1);
02660     }
02661 
02662     bvect_full1.clear();
02663     bvect_full2.clear();
02664 
02665     bvect_min1.set_bit(10);
02666     bvect_min2.set_bit(10);
02667 
02668     bvect_full1.set_bit(10);
02669     bvect_full2.set_bit(10);
02670 
02671     res1 = bvect_min1.compare(bvect_min2);
02672     res2 = bvect_full1.compare(bvect_full2);
02673 
02674     if (res1 != res2)
02675     {
02676         printf("Comparison test failed 1\n");
02677         exit(1);
02678     }
02679 
02680     printf("Comparison 2.\n");
02681 
02682     bvect_min1.set_bit(11);
02683     bvect_full1.set_bit(11);
02684 
02685     res1 = bvect_min1.compare(bvect_min2);
02686     res2 = bvect_full1.compare(bvect_full2);
02687 
02688     if (res1 != res2 && res1 != 1)
02689     {
02690         printf("Comparison test failed 2\n");
02691         exit(1);
02692     }
02693 
02694     res1 = bvect_min2.compare(bvect_min1);
02695     res2 = bvect_full2.compare(bvect_full1);
02696 
02697     if (res1 != res2 && res1 != -1)
02698     {
02699         printf("Comparison test failed 2.1\n");
02700         exit(1);
02701     }
02702 
02703     printf("Comparison 3.\n");
02704 
02705     bvect_full1.optimize();
02706 
02707     res1 = bvect_min1.compare(bvect_min2);
02708     res2 = bvect_full1.compare(bvect_full2);
02709 
02710     if (res1 != res2 && res1 != 1)
02711     {
02712         printf("Comparison test failed 3\n");
02713         exit(1);
02714     }
02715 
02716     res1 = bvect_min2.compare(bvect_min1);
02717     res2 = bvect_full2.compare(bvect_full1);
02718 
02719     if (res1 != res2 && res1 != -1)
02720     {
02721         printf("Comparison test failed 3.1\n");
02722         exit(1);
02723     }
02724 
02725     printf("Comparison 4.\n");
02726 
02727     bvect_full2.optimize();
02728 
02729     res1 = bvect_min1.compare(bvect_min2);
02730     res2 = bvect_full1.compare(bvect_full2);
02731 
02732     if (res1 != res2 && res1 != 1)
02733     {
02734         printf("Comparison test failed 4\n");
02735         exit(1);
02736     }
02737 
02738     res1 = bvect_min2.compare(bvect_min1);
02739     res2 = bvect_full2.compare(bvect_full1);
02740 
02741     if (res1 != res2 && res1 != -1)
02742     {
02743         printf("Comparison test failed 4.1\n");
02744         exit(1);
02745     }
02746 
02747     printf("Comparison 5.\n");
02748 
02749     unsigned i;
02750     for (i = 0; i < 65536; ++i)
02751     {
02752         bvect_full1.set_bit(i);
02753     }
02754 
02755     res1 = bvect_min1.compare(bvect_min2);
02756     res2 = bvect_full1.compare(bvect_full2);
02757 
02758     if (res1 != res2 && res1 != 1)
02759     {
02760         printf("Comparison test failed 5\n");
02761         exit(1);
02762     }
02763 
02764     bvect_full1.optimize();
02765 
02766     res1 = bvect_min2.compare(bvect_min1);
02767     res2 = bvect_full2.compare(bvect_full1);
02768 
02769     if (res1 != res2 && res1 != -1)
02770     {
02771         printf("Comparison test failed 5.1\n");
02772         exit(1);
02773     }
02774 
02775 }
02776 
02777 void DesrializationTest2()
02778 {
02779    bvect  bvtotal;
02780    unsigned size = BITVECT_SIZE - 10;
02781 
02782 
02783    bvect  bv1;
02784    bvect  bv2;
02785    int i;
02786    for (i = 10; i < 165536; i+=2)
02787    {
02788       bv1.set_bit(i);
02789    }
02790 
02791    bv1.optimize();
02792    bv1.stat();
02793 
02794    struct bvect::statistics st1;
02795    bv1.calc_stat(&st1);
02796 
02797 
02798    unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
02799 
02800    unsigned slen2 = bm::serialize(bv1, sermem);
02801    assert(slen2);
02802    slen2 = 0;
02803 
02804    bm::deserialize(bvtotal, sermem);
02805     bvect  bv_target_s;
02806     operation_deserializer<bvect>::deserialize(bv_target_s,
02807                                                 sermem,
02808                                                 0,
02809                                                 set_OR);
02810 
02811    bvtotal.optimize();
02812    int res = bvtotal.compare(bv_target_s);
02813    if (res != 0)
02814    {
02815        cout << "Operation deserialization error 1" << endl;
02816        exit(1);
02817    }
02818 
02819    for (i = 55000; i < 165536; ++i)
02820    {
02821       bv2.set_bit(i);
02822    }
02823    bv2.optimize();
02824    bv2.stat();
02825 
02826    struct bvect::statistics st2;
02827    bv2.calc_stat(&st2);
02828 
02829    unsigned char* sermem2 = new unsigned char[st2.max_serialize_mem];
02830 
02831    unsigned slen = bm::serialize(bv2, sermem2);
02832    assert(slen);
02833    slen = 0;
02834 
02835    bm::deserialize(bvtotal, sermem2);
02836    bvtotal.stat();
02837     operation_deserializer<bvect>::deserialize(bv_target_s,
02838                                                sermem2,
02839                                                0,
02840                                                set_OR);
02841     res = bvtotal.compare(bv_target_s);
02842     if (res != 0)
02843     {
02844         cout << "Operation deserialization error 2" << endl;
02845         exit(1);
02846     }
02847 
02848 //   bvtotal.optimize();
02849  //  bvtotal.stat();
02850 
02851    bm::deserialize(bvtotal, sermem2);
02852 
02853    bm::deserialize(bvtotal, sermem);
02854 
02855     operation_deserializer<bvect>::deserialize(bv_target_s,
02856                                                sermem2,
02857                                                0,
02858                                                set_OR);
02859     operation_deserializer<bvect>::deserialize(bv_target_s,
02860                                                sermem,
02861                                                0,
02862                                                set_OR);
02863 
02864     res = bvtotal.compare(bv_target_s);
02865     if (res != 0)
02866     {
02867         cout << "Deserialization test failed! 3" << endl;
02868         exit(1);
02869     }
02870 
02871    delete [] sermem;
02872    delete [] sermem2;
02873 
02874 
02875    bvtotal.clear();
02876    bv_target_s.clear(false);
02877 
02878    int clcnt = 0;
02879 
02880    int repetitions = 25;
02881    for (i = 0; i < repetitions; ++i)
02882    {
02883         cout << endl << "Deserialization STEP " << i << endl;
02884 
02885         bvect_mini*   bvect_min1= new bvect_mini(size);
02886         bvect*        bvect_full1= new bvect();
02887 
02888         FillSetsRandomMethod(bvect_min1, bvect_full1, 1, size, 1);
02889 
02890        struct bvect::statistics st;
02891        bvect_full1->calc_stat(&st);
02892 
02893        unsigned char* sermem = new unsigned char[st.max_serialize_mem];
02894 
02895        unsigned slen = bm::serialize(*bvect_full1, sermem);
02896 
02897        unsigned char* smem = new unsigned char[slen];
02898        ::memcpy(smem, sermem, slen);
02899 
02900 //       cout << "Serialized vector" << endl;
02901 //       bvect_full1->stat();
02902 
02903 //       cout << "Before deserialization" << endl;
02904 //       bvtotal.stat();
02905        bm::deserialize(bvtotal, smem);
02906         operation_deserializer<bvect>::deserialize(bv_target_s,
02907                                                 smem,
02908                                                 0,
02909                                                 set_OR);
02910         res = bvtotal.compare(bv_target_s);
02911         if (res != 0)
02912         {
02913             cout << "Operation deserialization error 2" << endl;
02914             exit(1);
02915         }
02916 
02917 //       cout << "After deserialization" << endl;
02918 //       bvtotal.stat();
02919 
02920        bvtotal.optimize();
02921        bv_target_s.optimize();
02922 
02923 //       cout << "After optimization" << endl;
02924 //       bvtotal.stat();
02925 
02926 
02927        if (++clcnt == 5)
02928        {
02929           clcnt = 0;
02930           bvtotal.clear();
02931           bv_target_s.clear();
02932 
02933 //          cout << "Post clear." << endl;
02934 //          bvtotal.stat();
02935 
02936        }
02937 
02938        delete [] sermem;
02939        delete [] smem;
02940        delete bvect_min1;
02941        delete bvect_full1;
02942 
02943    } // for i
02944 
02945 }
02946 
02947 
02948 void StressTest(int repetitions)
02949 {
02950 
02951    unsigned RatioSum = 0;
02952    unsigned SRatioSum = 0;
02953    unsigned DeltaSum = 0;
02954    unsigned SDeltaSum = 0;
02955 
02956    unsigned clear_count = 0;
02957 
02958    bvect  bvtotal;
02959    bvtotal.set_new_blocks_strat(bm::BM_GAP);
02960 
02961 
02962    cout << "----------------------------StressTest" << endl;
02963 
02964    unsigned size = BITVECT_SIZE - 10;
02965 //size = BITVECT_SIZE / 10;
02966    int i;
02967    for (i = 0; i < repetitions; ++i)
02968    {
02969         cout << endl << " - - - - - - - - - - - - STRESS STEP " << i << endl;
02970 
02971         switch (rand() % 3)
02972         {
02973         case 0:
02974             size = BITVECT_SIZE / 10;
02975             break;
02976         case 1:
02977             size = BITVECT_SIZE / 2;
02978             break;
02979         default:
02980             size = BITVECT_SIZE - 10;
02981             break;
02982         } // switch
02983 
02984 
02985         bvect_mini*   bvect_min1= new bvect_mini(size);
02986         bvect_mini*   bvect_min2= new bvect_mini(size);
02987         bvect*        bvect_full1= new bvect();
02988         bvect*        bvect_full2= new bvect();
02989 
02990         bvect_full1->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
02991         bvect_full2->set_new_blocks_strat(i&1 ? bm::BM_GAP : bm::BM_BIT);
02992 
02993         int opt = rand() % 2;
02994 
02995         unsigned start1 = 0;
02996 
02997         switch (rand() % 3)
02998         {
02999         case 1:
03000             start1 += size / 5;
03001             break;
03002         default:
03003             break;
03004         }
03005 
03006         unsigned start2 = 0;
03007 
03008         switch (rand() % 3)
03009         {
03010         case 1:
03011             start2 += size / 5;
03012             break;
03013         default:
03014             break;
03015         }
03016 /*
03017         if (i == 3)
03018         {
03019             g_cnt_check = 1;
03020         }
03021 */
03022         FillSetsRandomMethod(bvect_min1, bvect_full1, start1, size, opt);
03023         FillSetsRandomMethod(bvect_min2, bvect_full2, start2, size, opt);
03024 
03025 
03026 
03027         unsigned arr[bm::set_total_blocks]={0,};
03028         bm::id_t cnt = bvect_full1->count();
03029         unsigned last_block = bvect_full1->count_blocks(arr);
03030         unsigned sum = bm::sum_arr(&arr[0], &arr[last_block+1]);
03031 
03032         if (sum != cnt)
03033         {
03034             cout << "Error in function count_blocks." << endl;
03035             cout << "Array sum = " << sum << endl;
03036             cout << "BitCount = " << cnt << endl;
03037             cnt = bvect_full1->count();
03038             for (unsigned i = 0; i <= last_block; ++i)
03039             {
03040                 if (arr[i])
03041                 {
03042                     cout << "[" << i << ":" << arr[i] << "]";
03043                 }
03044             }
03045             cout << endl;
03046             cout << "================" << endl;
03047             bvect_full1->stat();
03048 
03049 
03050             exit(1);
03051         }
03052 
03053         CheckCountRange(*bvect_full1, start1, BITVECT_SIZE, arr);
03054         CheckIntervals(*bvect_full1, BITVECT_SIZE);
03055 
03056 
03057         
03058 
03059         CheckCountRange(*bvect_full2, start2, BITVECT_SIZE);
03060 
03061         CheckCountRange(*bvect_full1, 0, start1, arr);
03062         CheckCountRange(*bvect_full2, 0, start2);
03063 
03064 
03065 /*        
03066         cout << "!!!!!!!!!!!!!!!" << endl;
03067         CheckVectors(*bvect_min1, *bvect_full1, size);
03068         cout << "!!!!!!!!!!!!!!!" << endl;
03069         CheckVectors(*bvect_min2, *bvect_full2, size);
03070         cout << "!!!!!!!!!!!!!!!" << endl;
03071  
03072         
03073          bvect_full1->stat();
03074          cout << " --" << endl;
03075          bvect_full2->stat();
03076 */
03077 
03078         int operation = rand()%4;
03079 //operation = 2;
03080 
03081         switch(operation)
03082         {
03083         case 0:
03084             cout << "Operation OR" << endl;
03085             bvect_min1->combine_or(*bvect_min2);
03086             break;
03087 
03088         case 1:
03089             cout << "Operation SUB" << endl;
03090             bvect_min1->combine_sub(*bvect_min2);
03091             break;
03092 
03093         case 2:
03094             cout << "Operation XOR" << endl;
03095             bvect_min1->combine_xor(*bvect_min2);
03096             break;
03097 
03098         default:
03099             cout << "Operation AND" << endl;
03100             bvect_min1->combine_and(*bvect_min2);
03101             break;
03102         }
03103 
03104         int cres1 = bvect_min1->compare(*bvect_min2);
03105 
03106         delete bvect_min2;
03107 
03108         switch(operation)
03109         {
03110         case 0:
03111             {
03112             cout << "Operation OR" << endl;
03113 
03114             bm::id_t predicted_count = bm::count_or(*bvect_full1, *bvect_full2);
03115             bm::id_t predicted_any = bm::any_or(*bvect_full1, *bvect_full2);
03116             if (predicted_any == 0 && predicted_count != 0)
03117             {
03118                 cout << "Predicted any error!" << endl;
03119                 exit(1);
03120             }
03121             
03122             bvect    bv_target_s;
03123             SerializationOperation2Test(&bv_target_s,
03124                                         *bvect_full1,
03125                                         *bvect_full2,
03126                                         predicted_count,
03127                                         set_COUNT_OR,
03128                                         set_OR);
03129 
03130             bvect_full1->bit_or(*bvect_full2);
03131             
03132             bm::id_t count = bvect_full1->count();
03133 
03134             if (count != predicted_count)
03135             {
03136                 cout << "Predicted count error!" << endl;
03137                 cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
03138                 exit(1);
03139             }
03140             int res = bvect_full1->compare(bv_target_s);
03141             if (res != 0)
03142             {
03143                 cout << "Serialization operation failed!" << endl;
03144                 exit(1);
03145             }
03146             
03147             }
03148             break;
03149 
03150         case 1:
03151             {
03152             cout << "Operation SUB" << endl;
03153             
03154             bm::id_t predicted_count = bm::count_sub(*bvect_full1, *bvect_full2);
03155             bm::id_t predicted_any = bm::any_sub(*bvect_full1, *bvect_full2);
03156             if (predicted_any == 0 && predicted_count != 0)
03157             {
03158                 cout << "Predicted any error!" << endl;
03159                 exit(1);
03160             }
03161             
03162             bvect    bv_target_s;
03163             SerializationOperation2Test(&bv_target_s,
03164                                         *bvect_full1,
03165                                         *bvect_full2,
03166                                         predicted_count,
03167                                         set_COUNT_SUB_AB,
03168                                         set_SUB);
03169 
03170             bvect_full1->bit_sub(*bvect_full2);
03171             
03172             bm::id_t count = bvect_full1->count();
03173 
03174             if (count != predicted_count)
03175             {
03176                 cout << "Predicted count error!" << endl;
03177                 cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
03178                 exit(1);
03179             }
03180             int res = bvect_full1->compare(bv_target_s);
03181             if (res != 0)
03182             {
03183                 cout << "Serialization operation failed!" << endl;
03184                 exit(1);
03185             }
03186             
03187             
03188             }
03189             break;
03190 
03191         case 2:
03192             {
03193             cout << "Operation XOR" << endl;
03194            
03195             bm::id_t predicted_count = bm::count_xor(*bvect_full1, *bvect_full2);
03196             bm::id_t predicted_any = bm::any_xor(*bvect_full1, *bvect_full2);
03197             if (predicted_any == 0 && predicted_count != 0)
03198             {
03199                 cout << "Predicted any error!" << endl;
03200                 exit(1);
03201             }
03202 
03203             bvect    bv_target_s;
03204             SerializationOperation2Test(&bv_target_s,
03205                                         *bvect_full1,
03206                                         *bvect_full2,
03207                                         predicted_count,
03208                                         set_COUNT_XOR,
03209                                         set_XOR);
03210             
03211             bvect_full1->bit_xor(*bvect_full2);
03212             
03213             bm::id_t count = bvect_full1->count();
03214 
03215             if (count != predicted_count)
03216             {
03217                 cout << "Predicted count error!" << endl;
03218                 cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
03219                 exit(1);
03220             }
03221             int res = bvect_full1->compare(bv_target_s);
03222             if (res != 0)
03223             {
03224                 cout << "Serialization operation failed!" << endl;
03225                 exit(1);
03226             }
03227             
03228             }
03229             
03230             break;
03231 
03232         default:
03233             {
03234             cout << "Operation AND" << endl;
03235 
03236             bm::id_t predicted_count = bm::count_and(*bvect_full1, *bvect_full2);
03237             bm::id_t predicted_any = bm::any_and(*bvect_full1, *bvect_full2);
03238             if (predicted_any == 0 && predicted_count != 0)
03239             {
03240                 cout << "Predicted any error!" << endl;
03241                 exit(1);
03242             }
03243 
03244             bvect    bv_target_s;
03245             SerializationOperation2Test(&bv_target_s,
03246                                         *bvect_full1,
03247                                         *bvect_full2,
03248                                         predicted_count,
03249                                         set_COUNT_AND,
03250                                         set_AND);
03251 
03252             bvect_full1->bit_and(*bvect_full2);
03253 
03254             bm::id_t count = bvect_full1->count();
03255 
03256             if (count != predicted_count)
03257             {
03258                 cout << "Predicted count error!" << endl;
03259                 cout << "Count = " << count << "Predicted count = " << predicted_count << endl;
03260                 exit(1);
03261             }
03262             int res = bvect_full1->compare(bv_target_s);
03263             if (res != 0)
03264             {
03265                 cout << "Serialization operation failed!" << endl;
03266                 exit(1);
03267             }
03268 
03269             }
03270             break;
03271         }
03272 
03273         cout << "Operation comparison" << endl;
03274         CheckVectors(*bvect_min1, *bvect_full1, size);
03275 
03276         int cres2 = bvect_full1->compare(*bvect_full2);
03277 
03278         CheckIntervals(*bvect_full1, BITVECT_SIZE);
03279 
03280         if (cres1 != cres2)
03281         {
03282             cout << cres1 << " " << cres2 << endl;
03283             cout << bvect_full1->get_first() << " " << bvect_full1->count() << endl;
03284             cout << bvect_full2->get_first() << " " << bvect_full2->count() << endl;
03285 
03286            // bvect_full1->stat(1000);
03287             cout << endl;
03288            // bvect_full2->stat(1000);
03289             printf("Bitset comparison operation failed.\n");
03290             exit(1);
03291         }
03292 
03293         {
03294             bvect bv1(*bvect_full1);
03295             unsigned idx = rand() % size;
03296             bool b = bv1[idx];
03297             bool changed;
03298             if (b) 
03299             {
03300                 changed = bv1.set_bit_conditional(idx, true, false);
03301                 if (changed)
03302                 {
03303                     cout << "Set bit conditional failed!" << endl;
03304                     exit(1);
03305                 }
03306                 b = bv1[idx];
03307                 if (!b)
03308                 {
03309                     cout << "Set bit conditional failed!" << endl;
03310                     exit(1);
03311                 }
03312 
03313                 changed = bv1.set_bit_conditional(idx, false, false);
03314                 if (changed)
03315                 {
03316                     cout << "Set bit conditional failed!" << endl;
03317                     exit(1);
03318                 }
03319                 changed = bv1.set_bit_conditional(idx, true, true);
03320                 if (changed)
03321                 {
03322                     cout << "Set bit conditional failed!" << endl;
03323                     exit(1);
03324                 }
03325                 changed = bv1.set_bit_conditional(idx, false, true);
03326                 if (!changed)
03327                 {
03328                     cout << "Set bit conditional failed!" << endl;
03329                     exit(1);
03330                 }
03331                 b = bv1[idx];
03332                 if (b)
03333                 {
03334                     cout << "Set bit conditional failed!" << endl;
03335                     exit(1);
03336                 }
03337             } 
03338             else 
03339             {
03340                 changed = bv1.set_bit_conditional(idx, false, true);
03341                 if (changed)
03342                 {
03343                     cout << "Set bit conditional failed!" << endl;
03344                     exit(1);
03345                 }
03346                 changed = bv1.set_bit_conditional(idx, true, false);
03347                 if (!changed)
03348                 {
03349                     cout << "Set bit conditional failed!" << endl;
03350                     exit(1);
03351                 }
03352                 b = bv1[idx];
03353                 if (!b)
03354                 {
03355                     cout << "Set bit conditional failed!" << endl;
03356                     exit(1);
03357                 }
03358             }
03359 
03360 
03361         }
03362 
03363         delete bvect_full2;
03364 
03365 
03366         struct bvect::statistics st1;
03367         bvect_full1->calc_stat(&st1);
03368         bvect_full1->optimize();
03369         bvect_full1->optimize_gap_size();
03370         struct bvect::statistics st2;
03371         bvect_full1->calc_stat(&st2);
03372 
03373         unsigned Ratio = (st2.memory_used * 100)/st1.memory_used;
03374         RatioSum+=Ratio;
03375         DeltaSum+=st1.memory_used - st2.memory_used;
03376 
03377         cout << "Optimization statistics: " << endl  
03378              << "   MemUsedBefore=" << st1.memory_used
03379              << "   MemUsed=" << st2.memory_used 
03380              << "   Ratio=" << Ratio << "%"
03381              << "   Delta=" << st1.memory_used - st2.memory_used
03382              << endl;
03383                 
03384         cout << "Optimization comparison" << endl;
03385 
03386         CheckVectors(*bvect_min1, *bvect_full1, size);
03387 
03388         bvect_full1->set_gap_levels(gap_len_table_min<true>::_len);
03389         CheckVectors(*bvect_min1, *bvect_full1, size);
03390         CheckIntervals(*bvect_full1, BITVECT_SIZE);
03391 
03392         //CheckCountRange(*bvect_full1, 0, size);
03393 
03394 
03395         // Serialization
03396         bvect_full1->calc_stat(&st2);
03397 
03398         cout << "Memory allocation: " << st2.max_serialize_mem << endl;
03399         unsigned char* sermem = new unsigned char[st2.max_serialize_mem];
03400 
03401 //    bvect_full1->stat();
03402 
03403         cout << "Serialization...";
03404         unsigned slen = 
03405             bm::serialize(*bvect_full1, sermem, 
03406                           BM_NO_GAP_LENGTH|BM_NO_BYTE_ORDER);
03407         cout << "Ok" << endl;
03408 
03409         delete bvect_full1;
03410 
03411         unsigned SRatio = (slen*100)/st2.memory_used;
03412         SRatioSum+=SRatio;
03413         SDeltaSum+=st2.memory_used - slen;
03414 
03415 
03416         cout << "Serialized mem_max = " << st2.max_serialize_mem 
03417              << " size= " << slen 
03418              << " Ratio=" << SRatio << "%"
03419              << " Delta=" << st2.memory_used - slen
03420              << endl;
03421 
03422         bvect*        bvect_full3= new bvect();
03423         unsigned char* new_sermem = new unsigned char[slen];
03424         memcpy(new_sermem, sermem, slen);
03425         delete [] sermem;
03426 
03427         cout << "Deserialization...";
03428 
03429         bm::deserialize(*bvect_full3, new_sermem);
03430 
03431         bm::deserialize(bvtotal, new_sermem);
03432 
03433         bvect* bv_target_s=new bvect();
03434         operation_deserializer<bvect>::deserialize(*bv_target_s,
03435                                             new_sermem,
03436                                             0,
03437                                             set_OR);
03438 
03439         cout << "Ok." << endl;
03440         delete [] new_sermem;
03441 
03442         cout << "Optimization...";
03443         bvtotal.optimize();
03444         cout << "Ok." << endl;
03445 
03446         ++clear_count;
03447 
03448         if (clear_count == 4)
03449         {
03450            bvtotal.clear();
03451            clear_count = 0;
03452         }
03453 
03454         cout << "Serialization comparison" << endl;
03455 
03456         CheckVectors(*bvect_min1, *bvect_full3, size, true);
03457         int res = bv_target_s->compare(*bvect_full3);
03458         if (res != 0)
03459         {
03460             CheckVectors(*bvect_min1, *bv_target_s, size, true);
03461         }
03462 
03463         delete bv_target_s;
03464         delete bvect_min1;
03465         delete bvect_full3;
03466 
03467     }
03468 
03469     --i;
03470     cout << "Repetitions:" << i <<
03471             " AVG optimization ratio:" << RatioSum/i 
03472          << " AVG Delta:" << DeltaSum/i
03473          << endl
03474          << " AVG serialization Ratio:"<< SRatioSum/i
03475          << " Delta:" << SDeltaSum/i
03476          << endl;
03477 }
03478 
03479 void GAPCheck()
03480 {
03481    cout << "-------------------------------------------GAPCheck" << endl;
03482 
03483     {
03484 
03485     gap_vector   gapv(0);
03486     bvect_mini  bvect_min(bm::gap_max_bits);
03487 
03488     unsigned i;
03489     for( i = 0; i < 454; ++i)
03490     {
03491         bvect_min.set_bit(i);
03492         gapv.set_bit(i);
03493     }
03494 
03495     for(i = 0; i < 254; ++i)
03496     {
03497         bvect_min.clear_bit(i);
03498         gapv.clear_bit(i);
03499     }
03500 
03501     for(i = 5; i < 10; ++i)
03502     {
03503         bvect_min.set_bit(i);
03504         gapv.set_bit(i);
03505     }
03506 
03507     for( i = 0; i < bm::gap_max_bits; ++i)
03508     {
03509         int bit1 = (gapv.is_bit_true(i) == 1);
03510         int bit2 = (bvect_min.is_bit_true(i) != 0);
03511         int bit3 = (gapv.test(i) == 1);
03512         if (bit1 != bit2)
03513         {
03514             cout << "problem with bit comparison " << i << endl;
03515             exit(1);
03516         }
03517         if (bit1 != bit3)
03518         {
03519             cout << "problem with bit test comparison " << i << endl;
03520             exit(1);
03521         }
03522 
03523     }
03524 
03525     }
03526 
03527 
03528    {
03529    gap_vector gapv(1);
03530    int bit = gapv.is_bit_true(65535);
03531 
03532    if (bit != 1)
03533    {
03534       cout << "Bit != 1" << endl;
03535       exit(1);
03536    }
03537    
03538    int i;
03539    for (i = 0; i < 65536; ++i)
03540    {
03541         bit = gapv.is_bit_true(i);
03542         if (bit != 1)
03543         {
03544             cout << "2.Bit != 1" << endl;
03545             exit(1);
03546         }
03547    }
03548    unsigned cnt = gapv.count_range(0, 65535);
03549    if (cnt != 65536)
03550    {
03551        cout << "count_range failed:" << cnt << endl;
03552        exit(1);
03553    }
03554    
03555    CheckCountRange(gapv, 10, 20);
03556    CheckCountRange(gapv, 0, 20);
03557 
03558    printf("gapv 1 check ok\n");
03559    }
03560 
03561    {
03562    gap_vector gapv(0);
03563 
03564 
03565    int bit = gapv.is_bit_true(65535);
03566    int bit1 = gapv.test(65535);
03567    if(bit != 0)
03568    {
03569       cout << "Bit != 0" << endl;
03570       exit(1);
03571    }
03572       
03573    int i;
03574    for (i = 0; i < 65536; ++i)
03575    {
03576         bit = gapv.is_bit_true(i);
03577         bit1 = gapv.test(i);
03578         if (bit != 0)
03579         {
03580             cout << "2.Bit != 0 bit =" << i << endl;
03581             exit(1);
03582         }
03583         if (bit1 != 0)
03584         {
03585             cout << "2.Bit test != 0 bit =" << i << endl;
03586             exit(1);
03587         }
03588    }
03589    unsigned cnt = gapv.count_range(0, 65535);
03590    if (cnt != 0)
03591    {
03592        cout << "count_range failed:" << cnt << endl;
03593        exit(1);
03594    }
03595    CheckCountRange(gapv, 10, 20);
03596    CheckCountRange(gapv, 0, 20);
03597 
03598    printf("gapv 2 check ok\n");
03599    }
03600 
03601    {
03602    gap_vector gapv(0);
03603 
03604    gapv.set_bit(1);
03605    gapv.set_bit(0);
03606 
03607    gapv.control();
03608    CheckCountRange(gapv, 0, 20);
03609 
03610    int bit = gapv.is_bit_true(0);
03611 
03612    if (bit != 1)
03613    {
03614       cout << "Trouble" << endl;
03615       exit(1);
03616    }
03617    
03618    bit = gapv.is_bit_true(1);
03619    if (bit != 1)
03620    {
03621       cout << "Trouble 2." << endl;
03622       exit(1);
03623    }
03624 
03625 
03626    bit = gapv.is_bit_true(2);
03627    if(bit != 0)
03628    {
03629       cout << "Trouble 3." << endl;
03630       exit(1);
03631    }
03632 
03633    }
03634 
03635    {
03636    gap_vector gapv(0);
03637 
03638    gapv.set_bit(0);
03639    gapv.control();
03640    gapv.set_bit(1);
03641    gapv.control();
03642 
03643    gapv.set_bit(4);
03644    gapv.control();
03645    gapv.set_bit(5);
03646    gapv.control();
03647    CheckCountRange(gapv, 4, 5);
03648    CheckCountRange(gapv, 3, 5);
03649 
03650    gapv.set_bit(3);
03651    CheckCountRange(gapv, 3, 3);
03652    CheckCountRange(gapv, 3, 5);
03653 
03654    gapv.control();
03655    
03656    int bit = gapv.is_bit_true(0);
03657    if(bit!=1)
03658    {
03659       cout << "Bug" << endl;
03660    }
03661    bit = gapv.is_bit_true(1);
03662    if(bit!=1)
03663    {
03664       cout << "Bug2" << endl;
03665    }
03666 
03667    gapv.control();
03668    gapv.set_bit(4);
03669    gapv.control();
03670 
03671 
03672    printf("gapv 3 check ok\n");
03673    }
03674 
03675    {
03676         gap_vector gapv(0);
03677         bvect_mini   bvect_min(bm::gap_max_bits);
03678 cout << "++++++1" << endl;
03679 print_gap(gapv, 10);
03680         gapv.set_bit(bm::gap_max_bits-1);
03681         gapv.control();
03682         print_gap(gapv, 10);
03683 
03684 //cout << "++++++2" << endl;
03685 //cout << "m_buf=" << bvect_min.m_buf << endl;
03686 
03687         bvect_min.set_bit(bm::gap_max_bits-1);
03688 cout << "++++++3" << endl;
03689         gapv.set_bit(5);
03690         print_gap(gapv,15);
03691         gapv.control();
03692         bvect_min.set_bit(5);
03693 cout << "++++++4" << endl;
03694 
03695         CheckCountRange(gapv, 13, 150);
03696         gapv.control();
03697 
03698         unsigned i;
03699         for (i = 0; i < bm::gap_max_bits; ++i)
03700         {
03701             if (i == 65535)
03702                 printf("%i\n", i);
03703             int bit1 = (gapv.is_bit_true(i) == 1);
03704             int bit2 = (bvect_min.is_bit_true(i) != 0);
03705             int bit3 = (gapv.test(i) == 1);
03706             if (bit1 != bit2)
03707             {
03708                 cout << "problem with bit comparison " << i << endl;
03709             }
03710             if (bit1 != bit3)
03711             {
03712                 cout << "problem with bit test comparison " << i << endl;
03713             }
03714 
03715         }
03716 
03717         gapv.clear_bit(5);
03718         bvect_min.clear_bit(5);
03719         gapv.control();
03720 
03721         for ( i = 0; i < bm::gap_max_bits; ++i)
03722         {
03723             if (i == 65535)
03724                 printf("%i\n", i);
03725             int bit1 = (gapv.is_bit_true(i) == 1);
03726             int bit2 = (bvect_min.is_bit_true(i) != 0);
03727             int bit3 = (gapv.test(i) == 1);
03728             if (bit1 != bit2)
03729             {
03730                 cout << "2.problem with bit comparison " << i << endl;
03731             }
03732             if (bit1 != bit3)
03733             {
03734                 cout << "2.problem with bit test comparison " << i << endl;
03735             }
03736         }
03737    printf("gapv check 4 ok.\n");
03738    }
03739 
03740    {
03741         gap_vector gapv(0);
03742         bvect_mini   bvect_min(65536);
03743         
03744         int i;
03745         for (i = 10; i > 0; i-=2)
03746         {
03747             bvect_min.set_bit(i);
03748             gapv.set_bit(i);
03749             gapv.control();
03750             CheckCountRange(gapv, 0, i);
03751 
03752             int bit1 = (gapv.is_bit_true(i) == 1);
03753             int bit2 = (bvect_min.is_bit_true(i) != 0);
03754             int bit3 = (gapv.test(i) != 0);
03755             if (bit1 != bit2)
03756             {
03757                 cout << "3.problem with bit comparison " << i << endl;
03758             }
03759             if (bit1 != bit3)
03760             {
03761                 cout << "3.problem with bit test comparison " << i << endl;
03762             }
03763 
03764         }
03765         for (i = 0; i < (int)bm::gap_max_bits; ++i)
03766         {
03767             int bit1 = (gapv.is_bit_true(i) == 1);
03768             int bit2 = (bvect_min.is_bit_true(i) != 0);
03769             int bit3 = (gapv.test(i) == 1);
03770 
03771             if (bit1 != bit2)
03772             {
03773                 cout << "3.problem with bit comparison " << i << endl;
03774             }
03775             if (bit1 != bit3)
03776             {
03777                 cout << "3.problem with bit test comparison " << i << endl;
03778             }
03779         }
03780    printf("gapv check 5 ok.\n");
03781    }
03782 
03783    {
03784         gap_vector gapv(0);
03785         bvect_mini   bvect_min(bm::gap_max_bits);
03786         
03787         int i;
03788         for (i = 0; i < 25; ++i)
03789         {
03790             unsigned id = random_minmax(0, bm::gap_max_bits);
03791             bvect_min.set_bit(id);
03792             gapv.set_bit(id);
03793             gapv.control();
03794             CheckCountRange(gapv, 0, id);
03795             CheckCountRange(gapv, id, 65535);
03796         }
03797 
03798         for (i = 0; i < (int)bm::gap_max_bits; ++i)
03799         {
03800             int bit1 = (gapv.is_bit_true(i) == 1);
03801             int bit2 = (bvect_min.is_bit_true(i) != 0);
03802             if (bit1 != bit2)
03803             {
03804                 cout << "4.problem with bit comparison " << i << endl;
03805             }
03806         }
03807 
03808         for (i = bm::gap_max_bits; i < 0; --i)
03809         {
03810             int bit1 = (gapv.is_bit_true(i) == 1);
03811             int bit2 = (bvect_min.is_bit_true(i) != 0);
03812             if (bit1 != bit2)
03813             {
03814                 cout << "5.problem with bit comparison " << i << endl;
03815             }
03816         }
03817    printf("gapv check 6 ok.\n");
03818 
03819    }
03820 
03821    printf("gapv random bit set check ok.\n");
03822 
03823 
03824    // conversion functions test
03825    
03826    {
03827    // aligned position test
03828    bvect        bvect;
03829 
03830    bvect.set_bit(1);
03831    bvect.clear();
03832 
03833 
03834    unsigned* buf = (unsigned*) bvect.get_block(0);
03835 
03836    bm::or_bit_block(buf, 0, 4);
03837    unsigned cnt = bm::bit_block_calc_count_range(buf, 0, 3);
03838    assert(cnt == 4);
03839    
03840    bool bit = bvect.get_bit(0);
03841    assert(bit);
03842    bit = bvect.get_bit(1);
03843    assert(bit);
03844    bit = bvect.get_bit(2);
03845    assert(bit);
03846    bit = bvect.get_bit(3);
03847    assert(bit);
03848    bit = bvect.get_bit(4);
03849    assert(bit==0);
03850 
03851    bm::or_bit_block(buf, 0, 36); 
03852    cnt = bm::bit_block_calc_count_range(buf, 0, 35);
03853    assert(cnt == 36);
03854 
03855    for (int i = 0; i < 36; ++i)
03856    {
03857         bit = (bvect.get_bit(i) != 0);
03858         assert(bit);
03859    }
03860    bit = (bvect.get_bit(36) != 0);
03861    assert(bit==0);
03862 
03863    unsigned count = bvect.recalc_count();
03864    assert(count == 36);   
03865    
03866    cout << "Aligned position test ok." << endl; 
03867 
03868    }
03869 
03870 
03871    {
03872    // unaligned position test
03873    bvect   bvect;
03874 
03875    bvect.set_bit(0);
03876    bvect.clear();
03877 
03878    unsigned* buf = (unsigned*) bvect.get_block(0);
03879 
03880    bm::or_bit_block(buf, 5, 32);
03881    bool bit = (bvect.get_bit(4) != 0);
03882    assert(bit==0);
03883    unsigned cnt = bm::bit_block_calc_count_range(buf, 5, 5+32-1);
03884    assert(cnt == 32);
03885    cnt = bm::bit_block_calc_count_range(buf, 5, 5+32);
03886    assert(cnt == 32);
03887 
03888    int i;
03889    for (i = 5; i < 4 + 32; ++i)
03890    {
03891         bit = bvect.get_bit(i);
03892         assert(bit);
03893    }
03894    int count = bvect.recalc_count();
03895    assert(count==32);
03896 
03897    cout << "Unaligned position ok." << endl;
03898 
03899    } 
03900 
03901    // random test
03902    {
03903    cout << "random test" << endl;
03904 
03905    bvect   bvect;
03906 
03907    bvect.set_bit(0);
03908    bvect.clear();
03909 
03910    unsigned* buf = (unsigned*) bvect.get_block(0);
03911    for (int i = 0; i < 5000; ++i)
03912    {
03913         unsigned start = rand() % 65535;
03914         unsigned end = rand() % 65535;
03915         if (start > end)
03916         {
03917             unsigned tmp = end;
03918             end = start;
03919             start = tmp;
03920         }
03921         unsigned len = end - start;
03922         if (len)
03923         {
03924            bm::or_bit_block(buf, start, len);
03925            unsigned cnt = bm::bit_block_calc_count_range(buf, start, end);
03926            if (cnt != len)
03927            {
03928             cout << "random test: count_range comparison failed. " 
03929                  << " LEN = " << len << " cnt = " << cnt
03930                  << endl;
03931                  exit(1);
03932            }
03933 
03934            unsigned count = bvect.recalc_count();
03935 
03936            if (count != len)
03937            {
03938             cout << "random test: count comparison failed. " 
03939                  << " LEN = " << len << " count = " << count
03940                  << endl;
03941                  exit(1);
03942            }            
03943 
03944            for (unsigned j = start; j < end; ++j)
03945            {
03946                 bool bit = bvect.get_bit(j);
03947                 if (!bit)
03948                 {
03949                     cout << "random test: bit comparison failed. bit#" 
03950                          << i << endl;
03951                     exit(1);
03952                 } 
03953            } // for j
03954 
03955         } 
03956         bvect.clear();
03957 
03958         if ((i % 100)==0)
03959         {
03960             cout << "*" << flush;
03961         }
03962    } // for i
03963 
03964    cout << endl << "Random test Ok." << endl;
03965 
03966    }
03967 
03968 
03969    // conversion test
03970  
03971    cout << "Conversion test" << endl;
03972     
03973    {
03974    
03975    gap_vector gapv(0);
03976    bvect   bvect;
03977 
03978    gapv.set_bit(0);
03979    gapv.set_bit(2);
03980    gapv.set_bit(10);
03981    gapv.set_bit(11);
03982    gapv.set_bit(12);
03983    
03984    CheckCountRange(gapv, 3, 15);
03985 
03986    print_gap(gapv, 100);
03987    bvect.set_bit(0);
03988    bvect.clear();
03989 
03990    unsigned* buf = (unsigned*) bvect.get_block(0);
03991 
03992 
03993    gapv.convert_to_bitset(buf);
03994 
03995 
03996    unsigned bitcount = bvect.recalc_count();
03997 
03998 
03999    if (bitcount != 5)
04000    {
04001       cout << "test failed: bitcout = " << bitcount << endl;
04002       exit(1);
04003    }
04004 
04005 
04006    gap_vector gapv1(0);
04007    gap_word_t* gap_buf = gapv1.get_buf();
04008    *gap_buf = 0;
04009    bit_convert_to_gap(gap_buf, buf, bm::gap_max_bits, bm::gap_max_buff_len);
04010    print_gap(gapv1, 100);
04011 
04012    bitcount = gapv1.bit_count();
04013    if(bitcount != 5)
04014    {
04015       cout << "2.test_failed: bitcout = " << bitcount << endl;
04016       exit(1);
04017    }
04018 
04019    printf("conversion test ok.\n");
04020     
04021    }
04022 
04023    // gap AND test
04024 
04025    {
04026    // special case 1: operand is all 1
04027    gap_vector gapv1(0);
04028    gapv1.set_bit(2);
04029    gap_vector gapv2(1); 
04030 
04031    gapv1.combine_and(gapv2.get_buf());
04032    gapv1.control();
04033    print_gap(gapv1, 0);
04034 
04035    int count = gapv1.bit_count();
04036    assert(count == 1);
04037    int bit = gapv1.is_bit_true(2);
04038    if(bit == 0)
04039    {
04040       cout << "Wrong bit" << endl;
04041       exit(1);
04042    }
04043    CheckCountRange(gapv1, 0, 17);
04044 
04045    }
04046 
04047    {
04048    // special case 2: src is all 1
04049    gap_vector gapv1(1);
04050    gap_vector gapv2(0); 
04051    gapv2.set_bit(2);
04052 
04053    gapv1.combine_and(gapv2.get_buf());
04054    gapv1.control();
04055    print_gap(gapv1, 0);
04056 
04057    int count = gapv1.bit_count();
04058    assert(count == 1);
04059    int bit = gapv1.is_bit_true(2);
04060    assert(bit);
04061 
04062    }
04063 
04064    {
04065    gap_vector gapv;
04066    gap_vector gapv1(0);
04067 
04068    gapv1.set_bit(3);
04069    gapv1.set_bit(4);
04070    print_gap(gapv1, 0);
04071 
04072    gap_vector gapv2(0); 
04073    gapv2.set_bit(2);
04074    gapv2.set_bit(3);
04075    print_gap(gapv2, 0);
04076 
04077    bm::gap_buff_op((gap_word_t*)gapv.get_buf(), 
04078                          gapv1.get_buf(), 0,
04079                          gapv2.get_buf(), 0, bm::and_op); 
04080    print_gap(gapv, 0);
04081    gapv.control();
04082 
04083 
04084     int bit1 = (gapv.is_bit_true(3) == 1);
04085     if(bit1 == 0)
04086     {
04087        cout << "Checking failed." << endl;
04088        exit(0);
04089     }
04090 
04091    gapv1.combine_or(gapv2);
04092    print_gap(gapv1, 0);
04093    gapv1.control();
04094 
04095    }
04096 
04097    {
04098         printf("gap AND test 1.\n");
04099         gap_vector gapv1(0);
04100         gap_vector gapv2(0);
04101         bvect_mini   bvect_min1(65536);
04102         bvect_mini   bvect_min2(65536);
04103 
04104         gapv1.set_bit(65535);
04105         bvect_min1.set_bit(65535);
04106         gapv1.set_bit(4);
04107         bvect_min1.set_bit(4);
04108 
04109         gapv2.set_bit(65535);
04110         bvect_min2.set_bit(65535);
04111         gapv2.set_bit(3);
04112         bvect_min2.set_bit(3);
04113         CheckCountRange(gapv2, 3, 65535);
04114 
04115         gapv2.control();
04116 
04117         printf("vect1:"); print_gap(gapv1, 0);
04118         printf("vect2:");print_gap(gapv2, 0);
04119 
04120         gapv1.combine_and(gapv2.get_buf());
04121         printf("vect1:");print_gap(gapv1, 0);
04122 
04123         gapv1.control();
04124         unsigned bit1 = gapv1.is_bit_true(65535);
04125         assert(bit1);
04126 
04127         bvect_min1.combine_and(bvect_min2);
04128         CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
04129    }
04130 
04131    {
04132         printf("gap random AND test.\n");
04133         gap_vector gapv1(0);
04134         gap_vector gapv2(0);
04135         bvect_mini   bvect_min1(65536);
04136         bvect_mini   bvect_min2(65536);
04137         
04138         int i;
04139         for (i = 0; i < 25; ++i)
04140         {
04141             unsigned id = random_minmax(0, 65535);
04142             bvect_min1.set_bit(id);
04143             gapv1.set_bit(id);
04144             gapv1.control();
04145             CheckCountRange(gapv1, 0, id);
04146             CheckCountRange(gapv1, id, 65535);
04147         }
04148         for (i = 0; i < 25; ++i)
04149         {
04150             unsigned id = random_minmax(0, 65535);
04151             bvect_min2.set_bit(id);
04152             gapv2.set_bit(id);
04153             gapv2.control();
04154         }
04155 
04156         gapv1.combine_and(gapv2.get_buf());
04157         gapv1.control();
04158         gapv2.control();
04159         bvect_min1.combine_and(bvect_min2);
04160 
04161         CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
04162 
04163         printf("gap random AND test ok.\n");
04164 
04165    }
04166 
04167    {
04168         printf("gap OR test.\n");
04169 
04170         gap_vector gapv1(0);
04171         gap_vector gapv2(0);
04172 
04173         gapv1.set_bit(2);
04174         gapv2.set_bit(3);
04175 
04176         gapv1.combine_or(gapv2);
04177         gapv1.control();
04178         print_gap(gapv1, 0);   
04179         int bit1 = (gapv1.is_bit_true(0) == 1);
04180         assert(bit1==0);
04181         bit1=(gapv1.is_bit_true(2) == 1);
04182         assert(bit1);
04183         bit1=(gapv1.is_bit_true(3) == 1);
04184         assert(bit1);
04185    }
04186 
04187    {
04188         printf("gap XOR test.\n");
04189 
04190         gap_vector gapv1(0);
04191         gap_vector gapv2(0);
04192 
04193         gapv1.set_bit(2);
04194         gapv2.set_bit(3);
04195         gapv1.set_bit(4);
04196         gapv2.set_bit(4);
04197         print_gap(gapv1, 0);   
04198         print_gap(gapv2, 0);   
04199 
04200         gapv1.combine_xor(gapv2);
04201         gapv1.control();
04202         print_gap(gapv1, 0);   
04203         int bit1 = (gapv1.is_bit_true(0) == 0);
04204         assert(bit1);
04205         bit1=(gapv1.is_bit_true(2) == 1);
04206         assert(bit1);
04207         bit1=(gapv1.is_bit_true(3) == 1);
04208         assert(bit1);
04209         bit1=(gapv1.is_bit_true(4) == 0);
04210         assert(bit1);
04211 
04212    }
04213 
04214 
04215    {
04216         int i;
04217         printf("gap random OR test.\n");
04218         gap_vector gapv1(0);
04219         gap_vector gapv2(0);
04220         bvect_mini   bvect_min1(bm::gap_max_bits);
04221         bvect_mini   bvect_min2(bm::gap_max_bits);
04222         
04223         for (i = 0; i < 10; ++i)
04224         {
04225             unsigned id = random_minmax(0, 100);
04226             bvect_min1.set_bit(id);
04227             gapv1.set_bit(id);
04228             gapv1.control();
04229         }
04230         for (i = 0; i < 10; ++i)
04231         {
04232             unsigned id = random_minmax(0, 100);
04233             bvect_min2.set_bit(id);
04234             gapv2.set_bit(id);
04235             gapv2.control();
04236         }
04237 
04238         print_mv(bvect_min1, 64);
04239         print_mv(bvect_min2, 64);
04240 
04241         gapv1.combine_or(gapv2);
04242         gapv1.control();
04243         gapv2.control();
04244         bvect_min1.combine_or(bvect_min2);
04245 
04246         print_mv(bvect_min1, 64);
04247 
04248         CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
04249 
04250         printf("gap random OR test ok.\n");
04251 
04252    }
04253 
04254 
04255    {
04256         int i;
04257         printf("gap random SUB test.\n");
04258         gap_vector gapv1(0);
04259         gap_vector gapv2(0);
04260         bvect_mini   bvect_min1(bm::gap_max_bits);
04261         bvect_mini   bvect_min2(bm::gap_max_bits);
04262         
04263         for (i = 0; i < 25; ++i)
04264         {
04265             unsigned id = random_minmax(0, 100);
04266             bvect_min1.set_bit(id);
04267             gapv1.set_bit(id);
04268             gapv1.control();
04269         }
04270         for (i = 0; i < 25; ++i)
04271         {
04272             unsigned id = random_minmax(0, 100);
04273             bvect_min2.set_bit(id);
04274             gapv2.set_bit(id);
04275             gapv2.control();
04276         }
04277 
04278         print_mv(bvect_min1, 64);
04279         print_mv(bvect_min2, 64);
04280 
04281         gapv1.combine_sub(gapv2);
04282         gapv1.control();
04283         gapv2.control();
04284         bvect_min1.combine_sub(bvect_min2);
04285 
04286         print_mv(bvect_min1, 64);
04287 
04288         CheckGAPMin(gapv1, bvect_min1, bm::gap_max_bits);
04289 
04290         printf("gap random SUB test ok.\n");
04291    }
04292 
04293    {
04294        printf("GAP comparison test.\n");
04295 
04296        gap_vector gapv1(0);
04297        gap_vector gapv2(0);
04298 
04299        gapv1.set_bit(3);
04300        gapv2.set_bit(3);
04301 
04302        int res = gapv1.compare(gapv2);
04303        if (res != 0)
04304        {
04305            printf("GAP comparison failed!");
04306            exit(1);
04307        }
04308 
04309        gapv1.set_bit(4);
04310        gapv2.set_bit(4);
04311 
04312        res = gapv1.compare(gapv2);
04313        if (res != 0)
04314        {
04315            printf("GAP comparison failed!");
04316            exit(1);
04317        }
04318 
04319        gapv1.set_bit(0);
04320        gapv1.set_bit(1);
04321 
04322        res = gapv1.compare(gapv2);
04323        if (res != 1)
04324        {
04325            printf("GAP comparison failed!");
04326            exit(1);
04327        }
04328 
04329        gapv2.set_bit(0);
04330        gapv2.set_bit(1);
04331        res = gapv1.compare(gapv2);
04332        if (res != 0)
04333        {
04334            printf("GAP comparison failed!");
04335            exit(1);
04336        }
04337 
04338        gapv1.clear_bit(1);
04339 
04340        res = gapv1.compare(gapv2);
04341        if (res != -1)
04342        {
04343            printf("GAP comparison failed!");
04344            exit(1);
04345        }
04346 
04347 
04348    }
04349 
04350 
04351 }
04352 
04353 // -----------------------------------------------------------------------------
04354 
04355 void MutationTest()
04356 {
04357 
04358     cout << "--------------------------------- MutationTest" << endl;
04359 
04360     bvect_mini     bvect_min(BITVECT_SIZE);
04361     bvect          bvect_full;
04362 
04363     printf("\nMutation test.\n");
04364 
04365     bvect_full.set_new_blocks_strat(bm::BM_GAP);
04366 
04367     bvect_full.set_bit(5);
04368     bvect_full.set_bit(5);
04369 
04370     bvect_min.set_bit(5);
04371 
04372     bvect_full.set_bit(65535);
04373     bvect_full.set_bit(65537);
04374     bvect_min.set_bit(65535);
04375     bvect_min.set_bit(65537);
04376 
04377     bvect_min.set_bit(100000);
04378     bvect_full.set_bit(100000);
04379 
04380 bvect_full.stat();
04381     // detailed vectors verification
04382     ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
04383 
04384     int i;
04385     for (i = 5; i < 20000; i+=3)
04386     {
04387         bvect_min.set_bit(i);
04388         bvect_full.set_bit(i);
04389     }
04390 bvect_full.stat();
04391     ::CheckVectors(bvect_min, bvect_full, ITERATIONS, false);
04392 
04393     for (i = 100000; i < 200000; i+=3)
04394     {
04395         bvect_min.set_bit(i);
04396         bvect_full.set_bit(i);
04397     }
04398 
04399     ::CheckVectors(bvect_min, bvect_full, 300000);
04400 
04401     // set-clear functionality
04402 
04403     {
04404         printf("Set-clear functionality test.");
04405 
04406         bvect_mini     bvect_min(BITVECT_SIZE);
04407         bvect          bvect_full;
04408         bvect_full.set_new_blocks_strat(bm::BM_GAP);
04409 
04410         int i;
04411         for (i = 100000; i < 100010; ++i)
04412         {
04413             bvect_min.set_bit(i);
04414             bvect_full.set_bit(i);            
04415         }
04416         ::CheckVectors(bvect_min, bvect_full, 300000);
04417 
04418         for (i = 100000; i < 100010; ++i)
04419         {
04420             bvect_min.clear_bit(i);
04421             bvect_full.clear_bit(i);            
04422         }
04423         ::CheckVectors(bvect_min, bvect_full, 300000);
04424         
04425         bvect_full.optimize();
04426         CheckVectors(bvect_min, bvect_full, 65536);//max+10);
04427     }
04428 
04429 }
04430 
04431 void MutationOperationsTest()
04432 {
04433 
04434    cout << "------------------------------------ MutationOperationsTest" << endl;
04435 
04436    printf("Mutation operations test 1.\n");
04437    {
04438     bvect_mini   bvect_min1(BITVECT_SIZE);
04439     bvect_mini   bvect_min2(BITVECT_SIZE);
04440     bvect        bvect_full1;
04441     bvect        bvect_full2;
04442 
04443     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
04444     bvect_full2.set_new_blocks_strat(bm::BM_BIT);
04445 
04446     bvect_full1.set_bit(100);
04447     bvect_min1.set_bit(100);
04448 
04449     int i;
04450     for(i = 0; i < 10000; i+=2)
04451     {
04452        bvect_full2.set_bit(i);
04453        bvect_min2.set_bit(i);
04454     }
04455     bvect_full2.stat();
04456     CheckVectors(bvect_min2, bvect_full2, 65536, true);
04457     
04458     bvect_min1.combine_and(bvect_min2);
04459     bvect_full1.bit_and(bvect_full2);
04460 
04461     CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
04462 
04463    }
04464 
04465    printf("Mutation operations test 2.\n");
04466    {
04467     unsigned delta = 65536;
04468     bvect_mini   bvect_min1(BITVECT_SIZE);
04469     bvect_mini   bvect_min2(BITVECT_SIZE);
04470     bvect        bvect_full1;
04471     bvect        bvect_full2;
04472 
04473     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
04474     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
04475 
04476     int i;
04477     for(i = 0; i < 1000; i+=1)
04478     {
04479        bvect_full1.set_bit(delta+i);
04480        bvect_min1.set_bit(delta+i);
04481     }
04482 
04483     for(i = 0; i < 100; i+=2)
04484     {
04485        bvect_full2.set_bit(delta+i);
04486        bvect_min2.set_bit(delta+i);
04487     }
04488 //    CheckVectors(bvect_min2, bvect_full2, 65536);
04489     
04490     bvect_min1.combine_and(bvect_min2);
04491     bvect_full1.bit_and(bvect_full2);
04492 
04493     CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
04494     bvect_full1.optimize();
04495     CheckVectors(bvect_min1, bvect_full1, 65536);//max+10);
04496 
04497    }
04498 
04499    {
04500     bvect_mini   bvect_min1(BITVECT_SIZE);
04501     bvect        bvect_full1;
04502 
04503     bvect_full1.set_bit(3);
04504     bvect_min1.set_bit(3);
04505 
04506     struct bvect::statistics st;
04507     bvect_full1.calc_stat(&st);
04508 
04509     // serialization
04510 
04511     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04512     unsigned slen = bm::serialize(bvect_full1, sermem);
04513     cout << "BVECTOR SERMEM=" << slen << endl;
04514 
04515 
04516     bvect        bvect_full3;
04517     bm::deserialize(bvect_full3, sermem);
04518     bvect_full3.stat();
04519     CheckVectors(bvect_min1, bvect_full3, 100, true);
04520    }
04521 
04522 
04523    printf("Mutation operations test 3.\n");
04524    {
04525     bvect_mini   bvect_min1(BITVECT_SIZE);
04526     bvect_mini   bvect_min2(BITVECT_SIZE);
04527     bvect        bvect_full1;
04528     bvect        bvect_full2;
04529 
04530     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
04531     bvect_full2.set_new_blocks_strat(bm::BM_GAP);
04532 
04533    
04534     unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
04535     unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
04536     if (max > BITVECT_SIZE) 
04537         max = BITVECT_SIZE - 1;
04538 
04539     unsigned len = max - min;
04540 
04541     FillSets(&bvect_min1, &bvect_full1, min, max, 0);
04542     FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
04543     printf("Bvect_FULL 1 STAT\n");
04544     bvect_full1.stat();
04545 //    CheckVectors(bvect_min1, bvect_full1, max+10, false);
04546     FillSets(&bvect_min2, &bvect_full2, min, max, 0);
04547     FillSets(&bvect_min2, &bvect_full2, 0, len, 0);
04548     printf("Bvect_FULL 2 STAT\n");
04549     bvect_full2.stat();
04550 //    CheckVectors(bvect_min2, bvect_full2, max+10);
04551     
04552 
04553     bvect_min1.combine_and(bvect_min2);
04554     bvect_full1.bit_and(bvect_full2);
04555     printf("Bvect_FULL 1 STAT after AND\n");
04556     bvect_full1.stat();
04557 
04558     CheckVectors(bvect_min1, bvect_full1, max+10, false);
04559 
04560     struct bvect::statistics st;
04561     bvect_full1.calc_stat(&st);
04562     cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
04563          << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
04564          << endl;
04565     cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
04566 
04567     bvect_full1.optimize();
04568     bvect_full1.stat();
04569 
04570     CheckVectors(bvect_min1, bvect_full1, max+10, false);
04571 
04572     bvect_full1.calc_stat(&st);
04573     cout << "BVECTOR: GAP=" << st.gap_blocks << " BIT=" << st.bit_blocks 
04574          << " MEM=" << st.memory_used << " SERMAX=" << st.max_serialize_mem
04575          << endl;
04576     cout << "MINIVECT: " << bvect_min1.mem_used() << endl;
04577 
04578 
04579 
04580     // serialization
04581 
04582     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04583     unsigned slen = bm::serialize(bvect_full1, sermem);
04584     cout << "BVECTOR SERMEM=" << slen << endl;
04585 
04586 
04587     
04588     bvect        bvect_full3;
04589     bm::deserialize(bvect_full3, sermem);
04590     bvect_full3.stat();
04591     CheckVectors(bvect_min1, bvect_full3, max+10, true);
04592     
04593     delete [] sermem;
04594     
04595 
04596     cout << "Copy constructor check." << endl;
04597 
04598 
04599     {
04600     bvect       bvect_full4(bvect_full3);
04601     bvect_full3.stat();
04602     CheckVectors(bvect_min1, bvect_full4, max+10, true);
04603     }
04604     
04605 
04606    }
04607 
04608 }
04609 
04610 
04611 void SerializationTest()
04612 {
04613 
04614    cout << " ----------------------------------- SerializationTest" << endl;
04615 
04616    cout << "Serialization STEP 0" << endl;
04617 
04618    {
04619     unsigned size = BITVECT_SIZE/6000;
04620 
04621 
04622     bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
04623     bvect*        bvect_full1= new bvect();
04624     bvect*        bvect_full2= new bvect();
04625     bvect*        bv_target_s= new bvect();
04626 
04627     bvect_full1->set_new_blocks_strat(bm::BM_BIT);
04628     bvect_full2->set_new_blocks_strat(bm::BM_BIT);
04629 
04630     for(unsigned i = 0; i < size; ++i)
04631     {
04632         bvect_full1->set_bit(i);
04633         bvect_min1->set_bit(i);
04634     }
04635 
04636     bvect_full1->optimize();
04637     CheckVectors(*bvect_min1, *bvect_full1, size, true);
04638 
04639 
04640 
04641     bvect::statistics st;
04642     bvect_full1->calc_stat(&st);
04643     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04644     unsigned slen = bm::serialize(*bvect_full1, sermem);
04645     cout << "Serialized mem_max = " << st.max_serialize_mem 
04646          << " size= " << slen 
04647          << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
04648          << endl;
04649 
04650     bm::deserialize(*bvect_full2, sermem);
04651     operation_deserializer<bvect>::deserialize(*bv_target_s,
04652                                                sermem,
04653                                                0,
04654                                                set_OR);
04655     delete [] sermem;
04656 
04657 
04658     CheckVectors(*bvect_min1, *bvect_full2, size, true);
04659     CheckVectors(*bvect_min1, *bv_target_s, size, true);
04660 
04661 
04662     delete bvect_full2;
04663     delete bvect_min1;
04664     delete bvect_full1;
04665     delete bv_target_s;
04666 
04667     }
04668 
04669 
04670    {
04671     unsigned size = BITVECT_SIZE/6000;
04672 
04673 
04674     bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
04675     bvect*        bvect_full1= new bvect();
04676     bvect*        bvect_full2= new bvect();
04677     bvect*        bv_target_s= new bvect();
04678 
04679     bvect_full1->set_new_blocks_strat(bm::BM_BIT);
04680     bvect_full2->set_new_blocks_strat(bm::BM_BIT);
04681 
04682         bvect_full1->set_bit(131072);
04683         bvect_min1->set_bit(131072);
04684     
04685 
04686     bvect_full1->optimize();
04687 
04688     bvect::statistics st;
04689     bvect_full1->calc_stat(&st);
04690     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04691     unsigned slen = bm::serialize(*bvect_full1, sermem);
04692     cout << "Serialized mem_max = " << st.max_serialize_mem 
04693          << " size= " << slen 
04694          << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
04695          << endl;
04696 
04697     bm::deserialize(*bvect_full2, sermem);
04698     operation_deserializer<bvect>::deserialize(*bv_target_s,
04699                                                sermem,
04700                                                0,
04701                                                set_OR);
04702 
04703     delete [] sermem;
04704 
04705     CheckVectors(*bvect_min1, *bvect_full2, size, true);
04706     CheckVectors(*bvect_min1, *bv_target_s, size, true);
04707 
04708     delete bvect_full2;
04709     delete bvect_min1;
04710     delete bvect_full1;
04711     delete bv_target_s;
04712 
04713     }
04714 
04715 
04716     cout << "Serialization STEP 1." << endl;
04717 
04718     {
04719     bvect_mini   bvect_min1(BITVECT_SIZE);
04720     bvect        bvect_full1;
04721 
04722     bvect_full1.set_new_blocks_strat(bm::BM_GAP);
04723    
04724     unsigned min = BITVECT_SIZE / 2 - ITERATIONS;
04725     unsigned max = BITVECT_SIZE / 2 + ITERATIONS;
04726     if (max > BITVECT_SIZE) 
04727         max = BITVECT_SIZE - 1;
04728 
04729     unsigned len = max - min;
04730 
04731     FillSets(&bvect_min1, &bvect_full1, min, max, 0);
04732     FillSets(&bvect_min1, &bvect_full1, 0, len, 5);
04733 
04734     // shot some random bits
04735 
04736     int i;
04737     for (i = 0; i < 10000; ++i)
04738     {
04739         unsigned bit = rand() % BITVECT_SIZE;
04740         bvect_full1.set_bit(bit);
04741         bvect_min1.set_bit(bit);
04742     }
04743 
04744     bvect::statistics st;
04745     bvect_full1.calc_stat(&st);
04746 
04747     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04748     bvect_full1.stat();
04749     
04750     unsigned slen = bm::serialize(bvect_full1, sermem);
04751 
04752     cout << "Serialized len = " << slen << endl;
04753 
04754     bvect        bvect_full3;
04755     bm::deserialize(bvect_full3, sermem);
04756     bvect*  bv_target_s = new bvect();
04757     operation_deserializer<bvect>::deserialize(*bv_target_s,
04758                                                sermem,
04759                                                0,
04760                                                set_OR);
04761 
04762     CheckVectors(bvect_min1, bvect_full3, max+10, true);
04763     CheckVectors(bvect_min1, *bv_target_s, max+10, true);
04764 
04765     delete [] sermem;
04766     delete bv_target_s;
04767 
04768     }
04769 
04770 
04771    cout << "Stage 2" << endl;
04772 
04773    {
04774 
04775     bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
04776 //    bm::bvect_mini*   bvect_min2= new bm::bvect_mini(BITVECT_SIZE);
04777     bvect*        bvect_full1= new bvect();
04778     bvect*        bvect_full2= new bvect();
04779 
04780     bvect_full1->set_new_blocks_strat(bm::BM_GAP);
04781     bvect_full2->set_new_blocks_strat(bm::BM_GAP);
04782 
04783     FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE-10, 1);
04784 //    FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE-10, 1);
04785 
04786 //bvect_full1->stat();
04787 cout << "Filling. OK." << endl;
04788     bvect::statistics st;
04789     bvect_full1->calc_stat(&st);
04790 cout << st.max_serialize_mem << endl;
04791     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04792 cout << "Serialization" << endl;
04793     unsigned slen = bm::serialize(*bvect_full1, sermem);
04794 
04795     cout << "Serialized mem_max = " << st.max_serialize_mem 
04796          << " size= " << slen 
04797          << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
04798          << endl;
04799 cout << "Deserialization" << endl;
04800     bm::deserialize(*bvect_full2, sermem);
04801 cout << "Deserialization ok" << endl;
04802     bvect*  bv_target_s=new bvect();
04803     operation_deserializer<bvect>::deserialize(*bv_target_s,
04804                                                sermem,
04805                                                0,
04806                                                set_OR);
04807 
04808     CheckVectors(*bvect_min1, *bvect_full2, BITVECT_SIZE, true);
04809     CheckVectors(*bvect_min1, *bv_target_s, BITVECT_SIZE, true);
04810 
04811     delete [] sermem;
04812 
04813     delete bv_target_s;
04814     delete bvect_full2;
04815     delete bvect_min1;
04816     delete bvect_full1;
04817 
04818     }
04819 
04820 
04821 
04822    cout << "Stage 3" << endl;
04823 
04824    {
04825 
04826     bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
04827     bvect_mini*   bvect_min2= new bvect_mini(BITVECT_SIZE);
04828     bvect*        bvect_full1= new bvect();
04829     bvect*        bvect_full2= new bvect();
04830 
04831     bvect_full1->set_new_blocks_strat(bm::BM_GAP);
04832     bvect_full2->set_new_blocks_strat(bm::BM_GAP);
04833 
04834 
04835     FillSetsRandomMethod(bvect_min1, bvect_full1, 1, BITVECT_SIZE, 1);
04836     FillSetsRandomMethod(bvect_min2, bvect_full2, 1, BITVECT_SIZE, 1);
04837 
04838     bvect::statistics st;
04839     bvect_full1->calc_stat(&st);
04840     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04841     unsigned slen = bm::serialize(*bvect_full1, sermem);
04842     delete bvect_full1;
04843 
04844     cout << "Serialized mem_max = " << st.max_serialize_mem 
04845          << " size= " << slen 
04846          << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
04847          << endl;
04848 
04849     bvect*  bv_target_s=new bvect(*bvect_full2);
04850     bv_target_s->stat();
04851 
04852     bm::deserialize(*bvect_full2, sermem);
04853 
04854     operation_deserializer<bvect>::deserialize(*bv_target_s,
04855                                                sermem,
04856                                                0,
04857                                                set_OR);
04858     delete [] sermem;
04859     
04860     bvect_min2->combine_or(*bvect_min1);
04861     delete bvect_min1;
04862 
04863     CheckVectors(*bvect_min2, *bvect_full2, BITVECT_SIZE, true);
04864     CheckVectors(*bvect_min2, *bv_target_s, BITVECT_SIZE, true);
04865 
04866     delete bv_target_s;
04867     delete bvect_full2;
04868     delete bvect_min2;    
04869 
04870 
04871     }
04872 
04873    cout << "Stage 4. " << endl;
04874 
04875    {
04876     unsigned size = BITVECT_SIZE/3;
04877 
04878 
04879     bvect_mini*   bvect_min1= new bvect_mini(BITVECT_SIZE);
04880     bvect*        bvect_full1= new bvect();
04881     bvect*        bvect_full2= new bvect();
04882 
04883     bvect_full1->set_new_blocks_strat(bm::BM_BIT);
04884     bvect_full2->set_new_blocks_strat(bm::BM_BIT);
04885 
04886     unsigned i;
04887     for(i = 0; i < 65000; ++i)
04888     {
04889         bvect_full1->set_bit(i);
04890         bvect_min1->set_bit(i);
04891     }
04892 
04893     for(i = 65536; i < 65536+65000; ++i)
04894     {
04895         bvect_full1->set_bit(i);
04896         bvect_min1->set_bit(i);
04897     }
04898 
04899     for (i = 65536*2; i < size/6; ++i)
04900     {
04901         bvect_full1->set_bit(i);
04902         bvect_min1->set_bit(i);
04903     }
04904 
04905 
04906     bvect_full1->optimize();
04907 
04908     bvect_full1->stat();
04909 
04910 
04911     bvect::statistics st;
04912     bvect_full1->calc_stat(&st);
04913     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
04914     unsigned slen = bm::serialize(*bvect_full1, sermem);
04915     cout << "Serialized mem_max = " << st.max_serialize_mem 
04916          << " size= " << slen 
04917          << " Ratio=" << (slen*100)/st.max_serialize_mem << "%"
04918          << endl;
04919     
04920     unsigned char* new_sermem = new unsigned char[st.max_serialize_mem];
04921     ::memcpy(new_sermem, sermem, slen);
04922 
04923     bvect  bv_target_s(*bvect_full2);
04924 
04925     bm::deserialize(*bvect_full2, new_sermem);
04926     operation_deserializer<bvect>::deserialize(bv_target_s,
04927                                                new_sermem,
04928                                                0,
04929                                                set_OR);
04930 
04931     delete [] sermem;
04932     delete [] new_sermem;
04933 
04934     CheckVectors(*bvect_min1, *bvect_full2, size, true);
04935     CheckVectors(*bvect_min1, bv_target_s, size, true);
04936 
04937 
04938     delete bvect_full2;
04939     delete bvect_min1;
04940     delete bvect_full1;
04941 
04942     }
04943 
04944 
04945 }
04946 
04947 void GetNextTest()
04948 {
04949    cout << "-------------------------------------------- GetNextTest" << endl;
04950 
04951    int i;
04952    for(i = 0; i < 2; ++i)
04953    {
04954       cout << "Strategy " << i << endl;
04955 
04956    {
04957       bvect       bvect_full1;
04958       bvect_mini  bvect_min1(BITVECT_SIZE);
04959 
04960       bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
04961 
04962       bvect_full1.set_bit(0);
04963       bvect_min1.set_bit(0);
04964 
04965 
04966       bvect_full1.set_bit(65536);
04967       bvect_min1.set_bit(65536);
04968 
04969       unsigned nbit1 = bvect_full1.get_first();
04970       unsigned nbit2 = bvect_min1.get_first();
04971 
04972       if (nbit1 != nbit2)
04973       {
04974          cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
04975          exit(1);
04976       }
04977       nbit1 = bvect_full1.get_next(nbit1);
04978       nbit2 = bvect_min1.get_next(nbit2);
04979       if ((nbit1 != nbit2) || (nbit1 != 65536))
04980       {
04981          cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
04982          exit(1);
04983       }
04984    }
04985 
04986 
04987 
04988    {
04989       bvect       bvect_full1;
04990       bvect_mini  bvect_min1(BITVECT_SIZE);
04991       bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
04992 
04993       bvect_full1.set_bit(65535);
04994       bvect_min1.set_bit(65535);
04995 
04996       unsigned nbit1 = bvect_full1.get_first();
04997       unsigned nbit2 = bvect_min1.get_first();
04998 
04999       if ((nbit1 != nbit2) || (nbit1 != 65535))
05000       {
05001          cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
05002          exit(1);
05003       }
05004       nbit1 = bvect_full1.get_next(nbit1);
05005       nbit2 = bvect_min1.get_next(nbit2);
05006       if (nbit1 != nbit2 )
05007       {
05008          cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
05009          exit(1);
05010       }
05011    }
05012 
05013    {
05014       cout << "--------------" << endl;
05015       bvect       bvect_full1;
05016       bvect_mini  bvect_min1(BITVECT_SIZE);
05017       bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
05018 
05019       bvect_full1.set_bit(655350);
05020       bvect_min1.set_bit(655350);
05021 
05022       unsigned nbit1 = bvect_full1.get_first();
05023       unsigned nbit2 = bvect_min1.get_first();
05024 
05025       if (nbit1 != nbit2 || nbit1 != 655350)
05026       {
05027          cout << "1. get_first failed() " <<  nbit1 << " " << nbit2 << endl;
05028          exit(1);
05029       }
05030 
05031       nbit1 = bvect_full1.get_next(nbit1);
05032       nbit2 = bvect_min1.get_next(nbit2);
05033       if (nbit1 != nbit2)
05034       {
05035          cout << "1. get_next failed() " <<  nbit1 << " " << nbit2 << endl;
05036          exit(1);
05037       }
05038    }
05039 
05040 
05041    {
05042    bvect       bvect_full1;
05043    bvect_mini  bvect_min1(BITVECT_SIZE);
05044 
05045    bvect_full1.set_new_blocks_strat(i ? bm::BM_GAP : bm::BM_BIT);
05046 
05047    bvect_full1.set_bit(256);
05048    bvect_min1.set_bit(256);
05049 
05050 //   bvect_full1.clear_bit(256);
05051    bvect_full1.set_bit(65536);
05052    bvect_min1.set_bit(65536);
05053 
05054    unsigned nbit1 = bvect_full1.get_first();
05055    unsigned nbit2 = bvect_min1.get_first();
05056 
05057    if (nbit1 != nbit2)
05058    {
05059       cout << "get_first failed " <<  nbit1 << " " << nbit2 << endl;
05060       exit(1);
05061    }
05062 
05063    while (nbit1)
05064    {
05065       cout << nbit1 << endl;
05066       nbit1 = bvect_full1.get_next(nbit1);
05067       nbit2 = bvect_min1.get_next(nbit2);
05068       if (nbit1 != nbit2)
05069       {
05070          cout << "get_next failed " <<  nbit1 << " " << nbit2 << endl;
05071          exit(1);
05072       }
05073 
05074    } // while
05075 
05076    }
05077 
05078    
05079    }// for
05080 
05081 }
05082 
05083 // Test contributed by Maxim Shemanarev.
05084 
05085 void MaxSTest()
05086 {
05087    bvect vec;
05088 
05089    int i, j;
05090    unsigned id;
05091    for(i = 0; i < 100; i++)
05092    {
05093       int n = rand() % 2000 + 1;
05094       id = 1;
05095       for(j = 0; j < n; j++)
05096       {
05097          id += rand() % 10 + 1;
05098          vec.set_bit(id);
05099 
05100       }
05101       vec.optimize();
05102       vec.clear();
05103       fprintf(stderr, ".");
05104    }
05105 }
05106 
05107 
05108 void CalcBeginMask()
05109 {
05110     printf("BeginMask:\n");
05111 
05112     int i;
05113     for (i = 0; i < 32; ++i)
05114     {
05115     unsigned mask = 0;
05116 
05117         for(int j = i; j < 32; ++j)
05118         {
05119             unsigned nbit  = j; 
05120             nbit &= bm::set_word_mask;
05121             bm::word_t  mask1 = (((bm::word_t)1) << j);
05122 
05123             mask |= mask1;
05124         }
05125 
05126         printf("0x%x, ", mask);
05127         
05128     } 
05129     printf("\n");
05130 }
05131 
05132 void CalcEndMask()
05133 {
05134     printf("EndMask:\n");
05135 
05136     int i;
05137     for (i = 0; i < 32; ++i)
05138     {
05139     unsigned mask = 1;
05140 
05141         for(int j = i; j > 0; --j)
05142         {
05143             unsigned nbit  = j; 
05144             nbit &= bm::set_word_mask;
05145             bm::word_t  mask1 = (((bm::word_t)1) << j);
05146 
05147             mask |= mask1;
05148         }
05149 
05150         printf("0x%x,", mask);
05151         
05152     } 
05153     printf("\n");
05154 }
05155 
05156 
05157 void EnumeratorTest()
05158 {
05159     cout << "-------------------------------------------- EnumeratorTest" << endl;
05160 
05161     {
05162     bvect bvect1;
05163 
05164     bvect1.set_bit(100);
05165 
05166     bvect::enumerator en = bvect1.first();
05167 
05168     if (*en != 100)
05169     {
05170         cout << "Enumerator error !" << endl;
05171         exit(1);
05172     }
05173 
05174     bvect1.clear_bit(100);
05175 
05176     bvect1.set_bit(2000000000);
05177     en.go_first();
05178 
05179     if (*en != 2000000000)
05180     {
05181         cout << "Enumerator error !" << endl;
05182         exit(1);
05183     }
05184     }
05185 
05186     {
05187         bvect bvect1;
05188         bvect1.set_bit(0);
05189         bvect1.set_bit(10);
05190         bvect1.set_bit(35);
05191         bvect1.set_bit(1000);
05192         bvect1.set_bit(2016519);
05193         bvect1.set_bit(2034779);
05194         bvect1.set_bit(bm::id_max-1);
05195 
05196         bvect::enumerator en = bvect1.first();
05197 
05198         unsigned num = bvect1.get_first();
05199 
05200         bvect::enumerator end = bvect1.end();
05201         while (en < end)
05202         {
05203             if (*en != num)
05204             {
05205                 cout << "Enumeration comparison failed !" << 
05206                         " enumerator = " << *en <<
05207                         " get_next() = " << num << endl; 
05208                 exit(1);
05209             }
05210             ++en;
05211             num = bvect1.get_next(num);
05212         }
05213         if (num != 0)
05214         {
05215             cout << "Enumeration error!" << endl;
05216             exit(1);
05217         }
05218     }
05219 /*
05220     {
05221         bvect bvect1;
05222         bvect1.set();
05223 
05224         bvect::enumerator en = bvect1.first();
05225 
05226         unsigned num = bvect1.get_first();
05227 
05228         while (en < bvect1.end())
05229         {
05230             if (*en != num)
05231             {
05232                 cout << "Enumeration comparison failed !" << 
05233                         " enumerator = " << *en <<
05234                         " get_next() = " << num << endl; 
05235                 exit(1);
05236             }
05237 
05238             ++en;
05239             num = bvect1.get_next(num);
05240         }
05241         if (num != 0)
05242         {
05243             cout << "Enumeration error!" << endl;
05244             exit(1);
05245         }
05246     }
05247 */
05248 
05249     {
05250         bvect bvect1;
05251 
05252         int i;
05253         for(i = 0; i < 65536; ++i)
05254         {
05255             bvect1.set_bit(i);
05256         }
05257 /*
05258         for(i = 65536*10; i < 65536*20; i+=3)
05259         {
05260             bvect1.set_bit(i);
05261         }
05262 */
05263 
05264         bvect::enumerator en = bvect1.first();
05265 
05266         unsigned num = bvect1.get_first();
05267 
05268         while (en < bvect1.end())
05269         {
05270             if (*en != num)
05271             {
05272                 cout << "Enumeration comparison failed !" << 
05273                         " enumerator = " << *en <<
05274                         " get_next() = " << num << endl; 
05275                 exit(1);
05276             }
05277             ++en;
05278             num = bvect1.get_next(num);
05279             if (num == 31)
05280             {
05281                 num = num + 0;
05282             }
05283         }
05284         if (num != 0)
05285         {
05286             cout << "Enumeration error!" << endl;
05287             exit(1);
05288         }
05289     }
05290 
05291 
05292     {
05293     bvect bvect1;
05294     bvect1.set_new_blocks_strat(bm::BM_GAP);
05295     bvect1.set_bit(100);
05296 
05297     bvect::enumerator en = bvect1.first();
05298 
05299     if (*en != 100)
05300     {
05301         cout << "Enumerator error !" << endl;
05302         exit(1);
05303     }
05304 
05305     bvect1.clear_bit(100);
05306 
05307     bvect1.set_bit(2000000);
05308     en.go_first();
05309 
05310     if (*en != 2000000)
05311     {
05312         cout << "Enumerator error !" << endl;
05313         exit(1);
05314     }
05315     bvect1.stat();
05316     }
05317 
05318     {
05319         bvect bvect1;
05320         bvect1.set_new_blocks_strat(bm::BM_GAP);
05321         bvect1.set_bit(0);
05322         bvect1.set_bit(1);
05323         bvect1.set_bit(10);
05324         bvect1.set_bit(100);
05325         bvect1.set_bit(1000);
05326 
05327         bvect::enumerator en = bvect1.first();
05328 
05329         unsigned num = bvect1.get_first();
05330 
05331         while (en < bvect1.end())
05332         {
05333             if (*en != num)
05334             {
05335                 cout << "Enumeration comparison failed !" << 
05336                         " enumerator = " << *en <<
05337                         " get_next() = " << num << endl; 
05338                 exit(1);
05339             }
05340             ++en;
05341             num = bvect1.get_next(num);
05342         }
05343         if (num != 0)
05344         {
05345             cout << "Enumeration error!" << endl;
05346             exit(1);
05347         }
05348     }
05349 
05350 
05351 }
05352 
05353 
05354 
05355 void BlockLevelTest()
05356 {
05357     bvect  bv;
05358     bvect  bv2;
05359 
05360     bv.set_new_blocks_strat(bm::BM_GAP);
05361     bv2.set_new_blocks_strat(bm::BM_GAP);
05362 
05363     int i;
05364     for (i = 0; i < 500; i+=1)
05365     {
05366         bv.set_bit(i);
05367     }
05368     bv.stat();
05369 
05370     for (i = 0; i < 1000; i+=2)
05371     {
05372         bv2.set_bit(i);
05373     }
05374     bv2.stat();
05375 
05376     struct bvect::statistics st;
05377     bv2.calc_stat(&st);
05378 
05379     unsigned char* sermem = new unsigned char[st.max_serialize_mem];
05380 
05381     unsigned slen = bm::serialize(bv2, sermem);
05382     assert(slen);
05383     slen = 0;
05384     
05385     bm::deserialize(bv, sermem);
05386 //    bv.optimize();
05387 
05388     bv.stat();
05389 
05390 }
05391 
05392 /*
05393 __int64 CalcBitCount64(__int64 b)
05394 {
05395     b = (b & 0x5555555555555555) + (b >> 1 & 0x5555555555555555);
05396     b = (b & 0x3333333333333333) + (b >> 2 & 0x3333333333333333);
05397     b = b + (b >> 4) & 0x0F0F0F0F0F0F0F0F;
05398     b = b + (b >> 8);
05399     b = b + (b >> 16);
05400     b = b + (b >> 32) & 0x0000007F;
05401     return b;
05402 }
05403 
05404 unsigned CalcBitCount32(unsigned b)
05405 {
05406     b = (b & 0x55555555) + (b >> 1 & 0x55555555);
05407     b = (b & 0x33333333) + (b >> 2 & 0x33333333);
05408     b = b + (b >> 4) & 0x0F0F0F0F;
05409     b = b + (b >> 8);
05410     b = b + (b >> 16) & 0x0000003F;
05411     return b;
05412 }
05413 */
05414 
05415 
05416 void SyntaxTest()
05417 {
05418     cout << "----------------------------- Syntax test." << endl;
05419     bvect bv1;
05420     
05421     bvect::allocator_type a = bv1.get_allocator();
05422 
05423     bvect bv2(bv1);
05424     bvect bv3;
05425     bv3.swap(bv1);
05426      
05427     bv1[100] = true;
05428     bool v = bv1[100];
05429     assert(v);
05430     v = false;
05431 
05432     bv1[100] = false;
05433 
05434     bv2 |= bv1;
05435     bv2 &= bv1;
05436     bv2 ^= bv1;
05437     bv2 -= bv1;
05438 
05439     bv3 = bv1 | bv2;
05440 
05441     if (bv1 < bv2)
05442     {
05443     }
05444 
05445     bvect::reference ref = bv1[10];
05446     bool bn = !ref;
05447     bool bn2 = ~ref;
05448 
05449     bn = bn2 = false;
05450 
05451     ref.flip();
05452 
05453     bvect bvn = ~bv1;
05454 
05455     cout << "----------------------------- Syntax test ok." << endl;
05456 }
05457 
05458 
05459 void SetTest()
05460 {
05461     unsigned cnt;
05462     bvect bv;
05463 
05464     bv.set();
05465 
05466     cnt = bv.count();
05467     if (cnt != bm::id_max)
05468     {
05469         cout << "Set test failed!." << endl;
05470         exit(1);
05471     }
05472 
05473     bv.invert();
05474     cnt = bv.count();
05475     if (cnt != 0)
05476     {
05477         cout << "Set invert test failed!." << endl;
05478         exit(1);
05479     }
05480 
05481     bv.set(0);
05482     bv.set(bm::id_max-1);
05483     cnt = bv.count();
05484 
05485     assert(cnt == 2);
05486 
05487     bv.invert();
05488     bv.stat();
05489     cnt = bv.count();
05490 
05491     if (cnt != bm::id_max-2)
05492     {
05493         cout << "Set invert test failed!." << endl;
05494         exit(1);
05495     }
05496 
05497     bv.clear();
05498     bv[1] &= true;
05499     bool v = bv[1];
05500     if (v)
05501     {
05502         cout << "Set &= test failed!" << endl;
05503         exit(1);
05504     }
05505 
05506     bv[1] = true;
05507     bv[1] &= true;
05508     v = bv[1];
05509     if (!v)
05510     {
05511         cout << "Set &= test failed (2)!" << endl;
05512         exit(1);
05513     }
05514     bv.clear(true);
05515     bv.invert();
05516     bv[1] &= true;
05517     v = bv[1];
05518     if (!v)
05519     {
05520         cout << "Set &= test failed (2)!" << endl;
05521         exit(1);
05522     }
05523 
05524     bvect bv2;
05525     bv2[1] = true;
05526     bv2[1] = false;
05527     bvect::statistics stat1;
05528     bv2.calc_stat(&stat1);
05529     
05530     bv2.optimize();
05531 
05532     bvect::statistics stat2;
05533     bv2.calc_stat(&stat2);
05534 
05535     if (stat2.bit_blocks != 0 || 
05536         stat2.gap_blocks != 0 ||
05537         stat1.memory_used <= stat2.memory_used)
05538     {
05539         cout << "Optimization memory free test failed (2)!" << endl;
05540         exit(1);
05541     }
05542 
05543     {
05544         bvect bv3;
05545         bool changed;
05546         changed = bv3.set_bit_conditional(10, true, true);
05547         v = bv3[10];
05548         if (v || changed) {
05549             cout << "Conditional bit set failed." << endl;
05550             exit(1);
05551         }
05552         changed = bv3.set_bit_conditional(10, true, false);
05553         v = bv3[10];
05554         if (!v || !changed) {
05555             cout << "Conditional bit set failed." << endl;
05556             exit(1);
05557         }
05558         changed = bv3.set_bit_conditional(10, false, false);
05559         v = bv3[10];
05560         if (!v || changed) {
05561             cout << "Conditional bit set failed." << endl;
05562             exit(1);
05563         }
05564         changed = bv3.set_bit_conditional(10, false, true);
05565         v = bv3[10];
05566         if (v || !changed) {
05567             cout << "Conditional bit set failed." << endl;
05568             exit(1);
05569         }
05570     }
05571     {
05572         bvect bv3(bm::BM_GAP);
05573         bool changed;
05574         changed = bv3.set_bit_conditional(10, true, true);
05575         v = bv3[10];
05576         if (v || changed) {
05577             cout << "Conditional bit set failed." << endl;
05578             exit(1);
05579         }
05580         changed = bv3.set_bit_conditional(10, true, false);
05581         v = bv3[10];
05582         if (!v || !changed) {
05583             cout << "Conditional bit set failed." << endl;
05584             exit(1);
05585         }
05586         changed = bv3.set_bit_conditional(10, false, false);
05587         v = bv3[10];
05588         if (!v || changed) {
05589             cout << "Conditional bit set failed." << endl;
05590             exit(1);
05591         }
05592         changed = bv3.set_bit_conditional(10, false, true);
05593         v = bv3[10];
05594         if (v || !changed) {
05595             cout << "Conditional bit set failed." << endl;
05596             exit(1);
05597         }
05598     }
05599     {
05600         bvect bv3(bm::BM_GAP);
05601         bv3.invert();
05602         bv3.optimize();
05603         bool changed;
05604         changed = bv3.set_bit_conditional(10, true, true);
05605         v = bv3[10];
05606         if (!v || changed) {
05607             cout << "Conditional bit set failed." << endl;
05608             exit(1);
05609         }
05610         changed = bv3.set_bit_conditional(10, true, false);
05611         v = bv3[10];
05612         if (!v || changed) {
05613             cout << "Conditional bit set failed." << endl;
05614             exit(1);
05615         }
05616         changed = bv3.set_bit_conditional(10, false, false);
05617         v = bv3[10];
05618         if (!v || changed) {
05619             cout << "Conditional bit set failed." << endl;
05620             exit(1);
05621         }
05622         changed = bv3.set_bit_conditional(10, false, true);
05623         v = bv3[10];
05624         if (v || !changed) {
05625             cout << "Conditional bit set failed." << endl;
05626             exit(1);
05627         }
05628         changed = bv3.set_bit_conditional(10, true, false);
05629         v = bv3[10];
05630         if (!v || !changed) {
05631             cout << "Conditional bit set failed." << endl;
05632             exit(1);
05633         }
05634     }
05635 
05636 }
05637 
05638 
05639 template<class A, class B> void CompareMiniSet(const A& ms,
05640                                           const B& bvm)
05641 {
05642     for (unsigned i = 0; i < bm::set_total_blocks; ++i)
05643     {
05644         bool ms_val = ms.test(i)!=0;
05645         bool bvm_val = bvm.is_bit_true(i)!=0;
05646         if (ms_val != bvm_val)
05647         {
05648             printf("MiniSet comparison error: %u\n",i);
05649             exit(1);
05650         }
05651     }
05652 }
05653 
05654 void MiniSetTest()
05655 {
05656     cout << "----------------------- MiniSetTest" << endl;
05657     {
05658     bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
05659     bvect_mini bvm(bm::set_total_blocks);
05660 
05661 
05662     CompareMiniSet(ms, bvm);
05663 
05664 
05665     ms.set(1);
05666     bvm.set_bit(1);
05667 
05668     CompareMiniSet(ms, bvm);
05669 
05670     unsigned i;
05671 
05672     for (i = 1; i < 10; i++)
05673     {
05674         ms.set(i);
05675         bvm.set_bit(i);
05676     }
05677     CompareMiniSet(ms, bvm);
05678 
05679     for (i = 1; i < 10; i++)
05680     {
05681         ms.set(i, false);
05682         bvm.clear_bit(i);
05683     }
05684     CompareMiniSet(ms, bvm);
05685 
05686 
05687     for (i = 1; i < 10; i+=3)
05688     {
05689         ms.set(i);
05690         bvm.set_bit(i);
05691     }
05692     CompareMiniSet(ms, bvm);
05693 
05694     for (i = 1; i < 5; i+=3)
05695     {
05696         ms.set(i, false);
05697         bvm.clear_bit(i);
05698     }
05699     CompareMiniSet(ms, bvm);
05700     }
05701 
05702 
05703     {
05704     bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
05705     bvect_mini bvm(bm::set_total_blocks);
05706 
05707 
05708     ms.set(1);
05709     bvm.set_bit(1);
05710 
05711     CompareMiniSet(ms, bvm);
05712 
05713     unsigned i;
05714     for (i = 1; i < bm::set_total_blocks; i+=3)
05715     {
05716         ms.set(i);
05717         bvm.set_bit(i);
05718     }
05719     CompareMiniSet(ms, bvm);
05720 
05721     for (i = 1; i < bm::set_total_blocks/2; i+=3)
05722     {
05723         ms.set(i, false);
05724         bvm.clear_bit(i);
05725     }
05726     CompareMiniSet(ms, bvm);
05727     }
05728 
05729 
05730     {
05731     bm::bvmini<bm::set_total_blocks> ms(0);
05732     bvect_mini bvm(bm::set_total_blocks);
05733 
05734 
05735     CompareMiniSet(ms, bvm);
05736 
05737 
05738     ms.set(1);
05739     bvm.set_bit(1);
05740 
05741     CompareMiniSet(ms, bvm);
05742 
05743     unsigned i;
05744 
05745     for (i = 1; i < 10; i++)
05746     {
05747         ms.set(i);
05748         bvm.set_bit(i);
05749     }
05750     CompareMiniSet(ms, bvm);
05751 
05752     for (i = 1; i < 10; i++)
05753     {
05754         ms.set(i, false);
05755         bvm.clear_bit(i);
05756     }
05757     CompareMiniSet(ms, bvm);
05758 
05759 
05760     for (i = 1; i < bm::set_total_blocks; i+=3)
05761     {
05762         ms.set(i);
05763         bvm.set_bit(i);
05764     }
05765     CompareMiniSet(ms, bvm);
05766 
05767     for (i = 1; i < bm::set_total_blocks/2; i+=3)
05768     {
05769         ms.set(i, false);
05770         bvm.clear_bit(i);
05771     }
05772     CompareMiniSet(ms, bvm);
05773     }
05774 
05775 
05776     {
05777     bm::miniset<bm::block_allocator, bm::set_total_blocks> ms;
05778     bvect_mini bvm(bm::set_total_blocks);
05779 
05780 
05781     ms.set(1);
05782     bvm.set_bit(1);
05783 
05784     CompareMiniSet(ms, bvm);
05785 
05786     unsigned i;
05787     for (i = 1; i < 15; i+=3)
05788     {
05789         ms.set(i);
05790         bvm.set_bit(i);
05791     }
05792     CompareMiniSet(ms, bvm);
05793 
05794     for (i = 1; i < 7; i+=3)
05795     {
05796         ms.set(i, false);
05797         bvm.clear_bit(i);
05798     }
05799     CompareMiniSet(ms, bvm);
05800     }
05801 
05802 
05803     cout << "----------------------- MiniSetTest ok" << endl;
05804 }
05805 
05806 
05807 unsigned CalcBitCount32(unsigned b)
05808 {
05809     b = (b & 0x55555555) + (b >> 1 & 0x55555555);
05810     b = (b & 0x33333333) + (b >> 2 & 0x33333333);
05811     b = b + (b >> 4) & 0x0F0F0F0F;
05812     b = b + (b >> 8);
05813     b = b + (b >> 16) & 0x0000003F;
05814     return b;
05815 }
05816 
05817 
05818 void PrintGapLevels(const gap_word_t* glevel)
05819 {
05820     cout << "Gap levels:" << endl;
05821     unsigned i;
05822     for (i = 0; i < bm::gap_levels; ++i)
05823     {
05824         cout << glevel[i] << ",";
05825     }
05826     cout << endl;
05827 }
05828 
05829 void OptimGAPTest()
05830 {
05831     gap_word_t    glevel[bm::gap_levels];
05832     ::memcpy(glevel, gap_len_table<true>::_len, bm::gap_levels * sizeof(gap_word_t));
05833 
05834     {
05835     gap_word_t  length[] = { 2, 2, 5, 5, 10, 11, 12 };
05836     unsigned lsize = sizeof(length) / sizeof(gap_word_t);
05837 
05838     bm::improve_gap_levels(length, length + lsize, glevel);
05839 
05840     PrintGapLevels(glevel);
05841     }
05842 
05843     {
05844     gap_word_t  length[] = { 3, 5, 15, 15, 100, 110, 120 };
05845     unsigned lsize = sizeof(length) / sizeof(gap_word_t);
05846 
05847     bm::improve_gap_levels(length, length + lsize, glevel);
05848     PrintGapLevels(glevel);
05849     }
05850 
05851     {
05852     gap_word_t  length[] = { 15, 80, 5, 3, 100, 110, 95 };
05853     unsigned lsize = sizeof(length) / sizeof(gap_word_t);
05854 
05855     bm::improve_gap_levels(length, length + lsize, glevel);
05856     PrintGapLevels(glevel);
05857     }
05858 
05859     {
05860     gap_word_t  length[] = 
05861     { 16,30,14,24,14,30,18,14,12,16,8,38,28,4,20,18,28,22,32,14,12,16,10,8,14,18,14,8,
05862       16,30,8,8,58,28,18,4,26,14,52,12,18,10,14,18,22,18,20,70,12,6,26,6,8,22,12,4,8,8,
05863       8,54,18,6,8,4,4,10,4,4,4,4,4,6,22,14,38,40,56,50,6,10,8,18,82,16,6,18,20,12,12,
05864       16,8,14,14,10,16,12,10,16,14,12,18,14,18,34,14,12,18,18,10,20,10,18,8,14,14,22,16,
05865       10,10,18,8,20,14,10,14,12,12,14,16,16,6,10,14,6,10,10,10,10,12,4,8,8,8,10,10,8,
05866       8,12,10,10,14,14,14,8,4,4,10,10,4,10,4,8,6,52,104,584,218
05867     };
05868     unsigned lsize = sizeof(length) / sizeof(gap_word_t);
05869 
05870     bm::improve_gap_levels(length, length + lsize, glevel);
05871     PrintGapLevels(glevel);
05872     }
05873 
05874     {
05875     gap_word_t  length[] = {
05876      30,46,26,4,4,68,72,6,10,4,6,14,6,42,198,22,12,4,6,24,12,8,18,4,6,10,6,4,6,6,12,6
05877     ,6,4,4,78,38,8,52,4,8,10,6,8,8,6,10,4,6,6,4,10,6,8,16,22,28,14,10,10,16,10,20,10
05878     ,14,12,8,18,4,8,10,6,10,4,6,12,16,12,6,4,8,4,14,14,6,8,4,10,10,8,8,6,8,6,8,4,8,4
05879     ,8,10,6,4,6 
05880     };
05881     unsigned lsize = sizeof(length) / sizeof(gap_word_t);
05882 
05883     bm::improve_gap_levels(length, length + lsize, glevel);
05884     PrintGapLevels(glevel);
05885 
05886     }
05887 
05888 }
05889 
05890 void BitCountChangeTest()
05891 {
05892     cout << "---------------------------- BitCountChangeTest " << endl;
05893 
05894     unsigned i;
05895     for(i = 0xFFFFFFFF; i; i <<= 1) 
05896     { 
05897         unsigned a0 = bm::bit_count_change(i);
05898         unsigned a1 = BitCountChange(i);
05899         
05900         if (a0 != a1)
05901         {
05902             cout << hex 
05903                  << "Bit count change test failed!" 
05904                  << " i = " << i << " return = " 
05905                  << a0 << " check = " << a1
05906                  << endl;
05907             exit(1);
05908         }
05909     }
05910 
05911     cout << "---------------------------- STEP 2 " << endl;
05912 
05913     for(i = 0xFFFFFFFF; i; i >>= 1) 
05914     { 
05915         unsigned a0 = bm::bit_count_change(i);
05916         unsigned a1 = BitCountChange(i);
05917         
05918         if (a0 != a1)
05919         {
05920             cout << "Bit count change test failed!" 
05921                  << " i = " << i << " return = " 
05922                  << a0 << " check = " << a1
05923                  << endl;
05924             exit(1);
05925         }
05926     }
05927 
05928     cout << "---------------------------- STEP 3 " << endl;
05929 
05930     for (i = 0; i < 0xFFFFFFF; ++i)
05931     {
05932         unsigned a0 = bm::bit_count_change(i);
05933         unsigned a1 = BitCountChange(i);
05934         
05935         if (a0 != a1)
05936         {
05937             cout << "Bit count change test failed!" 
05938                  << " i = " << i << " return = " 
05939                  << a0 << " check = " << a1
05940                  << endl;
05941             exit(1);
05942         }
05943     }
05944    
05945 
05946     bm::word_t   arr[16] = {0,};
05947     arr[0] = (bm::word_t)(1 << 31);
05948     arr[1] = 1; //(bm::word_t)(1 << 31);
05949     
05950     bm::id_t cnt;
05951     
05952     cnt = bm::bit_count_change(arr[1]);
05953     cout << cnt << endl;
05954     if (cnt != 2)
05955     {
05956         cout << "0.count_change() failed " << cnt << endl;
05957         exit(1);
05958     }
05959     
05960     cnt = bm::bit_block_calc_count_change(arr, arr+4);
05961     
05962     if (cnt != 3)
05963     {
05964         cout << "1.count_intervals() failed " << cnt << endl;
05965         exit(1);
05966     }
05967 
05968     arr[0] = arr[1] = arr[2] = 0xFFFFFFFF;
05969     arr[3] = (bm::word_t)(0xFFFFFFFF >> 1);
05970     
05971     cnt = bm::bit_block_calc_count_change(arr, arr+4);
05972     cout << cnt << endl;
05973     
05974     if (cnt != 2)
05975     {
05976         cout << "1.1 count_intervals() failed " << cnt << endl;
05977         exit(1);
05978     }
05979     
05980  
05981     cout << "---------------------------- STEP 4 " << endl;
05982 
05983     bvect   bv1;
05984     cnt = bm::count_intervals(bv1);
05985     
05986     if (cnt != 1)
05987     {
05988         cout << "1.count_intervals() failed " << cnt << endl;
05989         exit(1);
05990     }
05991     CheckIntervals(bv1, 65536);
05992     
05993     bv1.invert();
05994 
05995     cnt = count_intervals(bv1);
05996     cout << "Inverted cnt=" << cnt << endl;
05997     
05998     if (cnt != 2)
05999     {
06000         cout << "2.inverted count_intervals() failed " << cnt << endl;
06001         exit(1);
06002     }
06003     
06004     bv1.invert();
06005         
06006     for (i = 10; i < 100000; ++i)
06007     {
06008         bv1.set(i);
06009     }
06010     
06011     cnt = count_intervals(bv1);
06012     
06013     if (cnt != 3)
06014     {
06015         cout << "3.count_intervals() failed " << cnt << endl;
06016         exit(1);
06017     }
06018     cout << "-----" << endl;
06019     CheckIntervals(bv1, 65536*2);
06020     cout << "Optmization..." << endl; 
06021     bv1.optimize();
06022     cnt = count_intervals(bv1);
06023     
06024     if (cnt != 3)
06025     {
06026         cout << "4.count_intervals() failed " << cnt << endl;
06027         exit(1);
06028     }
06029     
06030     CheckIntervals(bv1, 65536*2);
06031 
06032     cout << "---------------------------- BitCountChangeTest Ok." << endl;
06033 }
06034 
06035 /*
06036 #define POWER_CHECK(w, mask) \
06037     (bm::bit_count_table<true>::_count[(w&mask) ^ ((w&mask)-1)])
06038 
06039 void BitListTest()
06040 {
06041     unsigned bits[64] = {0,};
06042 
06043     unsigned w = 0;
06044 
06045     w = (1 << 3) | 1;
06046 
06047 
06048     int bn3 = POWER_CHECK(w, 1 << 3) - 1;
06049     int bn2 = POWER_CHECK(w, 1 << 2) - 1;
06050     int bn0 = POWER_CHECK(w, 1 << 0) - 1;
06051 
06052     bit_list(w, bits+1);
06053   
06054 }
06055 */
06056 
06057 void ResizeTest()
06058 {
06059     {{
06060     bvect bv(0);
06061     assert(bv.any() == false);
06062     assert(bv.count() == 0);
06063     }}
06064 
06065     {{
06066     bvect bv1(10);
06067     bvect bv2(bv1);
06068     assert(bv1.size() == bv2.size());
06069     }}
06070 
06071     {{
06072     bvect bv(10);
06073     assert(bv.any() == false);
06074     assert(bv.count() == 0);
06075     bv.invert();
06076     unsigned cnt = bv.count();
06077     assert(cnt == 10);
06078     }}
06079 
06080     {{
06081     bvect bv1(10);
06082     bv1.set(1);
06083     bvect bv2(0);
06084 
06085     assert(bv1.size() == 10);
06086     assert(bv2.size() == 0);
06087     assert(bv1.count() == 1);
06088     assert(bv2.count() == 0);
06089     
06090     bv1.swap(bv2);
06091 
06092     assert(bv2.size() == 10);
06093     assert(bv2.count() == 1);
06094     assert(bv1.size() == 0);
06095     assert(bv1.count() == 0);
06096 
06097     }}
06098 
06099     {{
06100     bvect bv1;
06101     bv1.set(65536);
06102     bv1.set(100);
06103     assert(bv1.size() == bm::id_max);
06104     assert(bv1.count() == 2);
06105     bv1.resize(101);
06106     assert(bv1.size() == 101);
06107     assert(bv1.count() == 1);
06108     {{
06109     bm::id_t f = bv1.get_first();
06110     assert(f == 100);
06111     f = bv1.get_next(f);
06112     assert(f == 0);
06113     }}
06114 
06115     bv1.resize(10);
06116     assert(bv1.size() == 10);
06117     assert(bv1.count() == 0);
06118     bm::id_t f = bv1.get_first();
06119     assert(f == 0);
06120     }}
06121 
06122     {{
06123     bvect bv;
06124     bv.stat();
06125     bv.set(100);
06126     bv.set(65536 + 10);
06127     bv.stat();
06128     bv.set_range(0, 65536*10, false);
06129     bv.stat();
06130     }}
06131 
06132     // test logical operations
06133 
06134     {{
06135     bvect bv1(65536 * 10);
06136     bvect bv2(65536 * 100);
06137     bv1.set(5);
06138     bv2.set(5);
06139     bv2.set(65536 * 2);
06140     bv2 &= bv1;
06141     assert(bv2.size() == 65536 * 100);
06142     assert(bv2.count() == 1);
06143     assert(bv2.get_first() == 5);
06144     }}
06145 
06146     {{
06147     bvect bv1(10);
06148     bvect bv2;
06149     bv1.set(5);
06150     bv2.set(5);
06151     bv2.set(65536 * 2);
06152     bv1 &= bv2;
06153     assert(bv1.size() == bv2.size());
06154     assert(bv1.count() == 1);
06155     assert(bv1.get_first() == 5);
06156     }}
06157 
06158     {{
06159     bvect bv1(10);
06160     bvect bv2;
06161     bv1.set(5);
06162     bv2.set(6);
06163     bv2.set(65536 * 2);
06164     bv1 |= bv2;
06165     assert(bv1.size() == bv2.size());
06166     assert(bv1.count() == 3);
06167     }}
06168 
06169     // comparison test
06170 
06171     {{
06172     int cmp;
06173     bvect bv1(10);
06174     bvect bv2;
06175     bv2.set(65536 * 2);
06176 
06177     cmp = bv1.compare(bv2);
06178     assert(cmp < 0);
06179 
06180     bv1.set(5);
06181     assert(cmp < 0);
06182     cmp = bv1.compare(bv2);
06183     assert(cmp > 0);
06184     cmp = bv2.compare(bv1);
06185     assert(cmp < 0);
06186 
06187     }}
06188 
06189     // inserter
06190 
06191     {{
06192     bvect bv1(10);
06193     {
06194         bvect::insert_iterator it(bv1);
06195         *it = 100 * 65536;
06196     }
06197     assert(bv1.size() ==  100 * 65536 + 1);
06198     }}
06199 
06200     // serialization
06201 
06202     {{
06203     bvect bv1(10);
06204     bv1.set(5);
06205     struct bvect::statistics st1;
06206     bv1.calc_stat(&st1);
06207 
06208     unsigned char* sermem = new unsigned char[st1.max_serialize_mem];
06209     unsigned slen2 = bm::serialize(bv1, sermem);
06210 
06211     bvect bv2(0);
06212     bm::deserialize(bv2, sermem);
06213     delete [] sermem;
06214 
06215     assert(bv2.size() == 10);
06216     assert(bv2.count() == 1);
06217     assert(bv2.get_first() == 5);
06218     
06219     }}
06220 
06221     {{
06222     bvect bv1(10);
06223     bv1.set(5);
06224     unsigned int arg[] = { 10, 65536, 65537, 65538 * 10000 };
06225     unsigned* it1 = arg;
06226     unsigned* it2 = arg + 4;
06227     combine_or(bv1, it1, it2);
06228     assert(bv1.size() == 65538 * 10000 + 1);
06229     bvect::enumerator en = bv1.first();
06230     while (en.valid())
06231     {
06232         cout << *en << " ";
06233         ++en;
06234     }
06235     }}
06236 
06237 }
06238 
06239 void ExportTest()
06240 {
06241     cout << "---------------------------- ExportTest..." << endl;
06242 
06243     {
06244     char buf[20] = {0,};
06245 
06246     buf[0] = 1;
06247     buf[1] = 1;
06248     buf[2]= (char)(1 << 1);
06249 
06250     bvect bv1;
06251     export_array(bv1, buf + 0, buf + 20);
06252 
06253     assert(bv1.count() == 3);
06254     assert(bv1.test(0));
06255     assert(bv1.test(8));
06256     assert(bv1.test(17));
06257     }
06258 
06259     {
06260     char buf[65536*10] = {0,};
06261 
06262     buf[0] = 1;
06263     buf[1] = 1;
06264     buf[2]= (char)(1 << 1);
06265 
06266     bvect bv1;
06267     export_array(bv1, buf + 0, buf + 65536*10);
06268 
06269     assert(bv1.count() == 3);
06270     assert(bv1.test(0));
06271     assert(bv1.test(8));
06272     assert(bv1.test(17));
06273     }
06274 
06275     {
06276     short buf[20] = {0,};
06277 
06278     buf[0] = 1;
06279     buf[1] = 1;
06280     buf[2]= (char)(1 << 1);
06281 
06282     bvect bv1;
06283     export_array(bv1, buf + 0, buf + 20);
06284 
06285     assert(bv1.count() == 3);
06286     assert(bv1.test(0));
06287     assert(bv1.test(16));
06288     assert(bv1.test(33));
06289     }
06290 
06291     {
06292     int buf[20] = {0,};
06293 
06294     buf[0] = 1;
06295     buf[1] = 1;
06296     buf[2]= (char)(1 << 1);
06297 
06298     bvect bv1;
06299     export_array(bv1, buf + 0, buf + 20);
06300 
06301     assert(bv1.count() == 3);
06302     assert(bv1.test(0));
06303     assert(bv1.test(32));
06304     assert(bv1.test(65));
06305     }
06306 
06307 
06308     cout << "---------------------------- ExportTest Ok." << endl;
06309 }
06310 
06311 
06312 void TestRecomb()
06313 {
06314     bm::word_t b1[bm::set_block_size]= {0,};
06315     bm::word_t b2[bm::set_block_size]= {0,};
06316     bm::word_t br[bm::set_block_size]= {0,};
06317  
06318     b1[0] = 1;
06319     b1[1] = 1;
06320     b2[0] = 1;
06321 
06322     bitblock_get_adapter bga1(b1);
06323     bitblock_get_adapter bga2(b2);
06324     bitblock_store_adapter bbsa(br);
06325     bm::bit_AND<bm::word_t> and_func;
06326     bit_recomb<bitblock_get_adapter,
06327                bitblock_get_adapter,
06328                bm::bit_AND<bm::word_t>,
06329                bitblock_store_adapter>
06330            (bga1, bga2,and_func, bbsa);
06331 /*
06332     bit_recomb(bitblock_get_adapter(b1),
06333                bitblock_get_adapter(b2),
06334                bit_AND<bm::word_t>(),
06335                bitblock_store_adapter(br)
06336                );
06337 
06338     assert(br[0] == 1);
06339     for (int i = 1; i < bm::set_block_size; ++i)
06340     {
06341         assert(br[i] == 0);
06342     }
06343 
06344     bitblock_sum_adapter sa;
06345     bit_recomb(bitblock_get_adapter(b1),
06346                bitblock_get_adapter(b2),
06347                bit_COUNT_AND<bm::word_t>(),
06348                sa
06349                );
06350     assert(sa.sum() == 1);
06351 */
06352 }
06353 
06354 
06355 int main(void)
06356 {
06357     time_t      start_time = time(0);
06358     time_t      finish_time;
06359 
06360     TestRecomb();
06361 
06362     OptimGAPTest();
06363 
06364     CalcBeginMask();
06365     CalcEndMask();
06366 /*
06367     unsigned a = bm::id_max / 65536;
06368 
06369     bvect bv(BM_BIT, bm::gap_len_table<true>::_len, 65536*10);
06370     bm::id_t c = bv.capacity();
06371     bv.set(bm::id_max-1);
06372 */
06373 
06374 //   cout << sizeof(__int64) << endl;
06375 
06376 //   ::srand((unsigned)::time(NULL));
06377 
06378 /*
06379     bvect bv;
06380 
06381     bv.set(786694);
06382     bv.set(895785); 
06383     bv.set(977200); 
06384     bv.set(1038827); 
06385     bv.set(1110924); 
06386     bv.set(1119532); 
06387     bv.set(1123158); 
06388     bv.set(1203562); 
06389     bv.set(1209502); 
06390     bv.set(1247904); 
06391     bv.set(1276296); 
06392     bv.set(1384903);
06393 
06394     cout << bv.count() << endl;
06395 
06396     bvect::statistics st;
06397     bv.calc_stat(&st);
06398     unsigned char buf[2048];
06399     unsigned slen = bm::serialize(bv, buf);
06400     cout << "slen=" << slen << endl;
06401 
06402     bvect bv2;
06403     operation_deserializer<bvect>::deserialize(bv2,
06404                                                 buf,
06405                                                 0,
06406                                                 set_ASSIGN);
06407 
06408     cout << "count2=" << bv2.count() << endl;
06409     PrintSet(cout, bv2);
06410     return 0;
06411 */
06412      ExportTest();
06413      ResizeTest();
06414 
06415      MiniSetTest();
06416 
06417      SyntaxTest();
06418 
06419      SetTest();
06420 
06421      BitCountChangeTest();
06422 
06423      EnumeratorTest();
06424 
06425      BasicFunctionalityTest();
06426 
06427      ClearAllTest();
06428 
06429      GAPCheck();
06430 
06431      MaxSTest();
06432 
06433      GetNextTest();
06434 
06435      SimpleRandomFillTest();
06436      
06437      RangeRandomFillTest();
06438 
06439      AndOperationsTest();   
06440            
06441      OrOperationsTest();
06442 
06443      XorOperationsTest();
06444 
06445      SubOperationsTest();
06446 
06447      WordCmpTest();
06448 
06449      ComparisonTest();
06450 
06451      MutationTest();
06452 
06453      MutationOperationsTest();
06454 
06455      SerializationTest();
06456 
06457      DesrializationTest2();
06458 
06459      BlockLevelTest();
06460 
06461      StressTest(800);
06462 
06463     finish_time = time(0);
06464 
06465 
06466     cout << "Test execution time = " << finish_time - start_time << endl;
06467 
06468 #ifdef MEM_DEBUG
06469     cout << "Number of BLOCK allocations = " <<  dbg_block_allocator::na_ << endl;
06470     cout << "Number of PTR allocations = " <<  dbg_ptr_allocator::na_ << endl << endl;
06471 
06472     assert(dbg_block_allocator::balance() == 0);
06473     assert(dbg_ptr_allocator::balance() == 0);
06474 #endif
06475 
06476     return 0;
06477 }
06478 
06479 
06480 

Generated on Sun Dec 6 22:43:54 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:21:14 2009 by modify_doxy.py rev. 173732