Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestBitVector.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu Aug  5 10:08:21 EDT 2004 by ken@xorian.net  
00020 //      modified on Sat Feb 12 17:29:24 PST 2000 by mann  
00021 //      modified on Thu Dec 12 16:04:56 PST 1996 by heydon
00022 
00023 // A test of the BitVector module
00024 
00025 #include <Basics.H>
00026 #include "BitVector.H"
00027 
00028 using std::cout;
00029 using std::endl;
00030 
00031 static bool SetResetTest() throw ()
00032 {
00033     BitVector bv;
00034     int i, j;
00035     const int step = 3, lo = 20, hi = lo + (2 * step * 20);
00036 
00037     cout << "*** Set/Reset Bit Test ***\n\n";
00038     cout << "Testing upward...\n";
00039     cout.flush();
00040     for (i = lo; i <= hi; i += step) {
00041         bool set = bv.Set(i); assert(!set);
00042         for (j = 0; j < bv.Size(); j++) {
00043             if (bv.Read(j) != (lo <= j && j <= i && (j-lo) % step == 0)) {
00044                 cout << "  Error: failed at bit " << i << "!\n\n";
00045                 return false;
00046             }
00047         }
00048     }
00049     cout << "  Passed!\n\n";
00050     cout << "Testing downward...\n";
00051     cout.flush();
00052     for (i -= step; i >= lo; i -= 2 * step) {
00053         bool set = bv.Reset(i); assert(set);
00054         for (j = 0; j < bv.Size(); j++) {
00055             if (bv.Read(j) !=
00056                 ((lo <= j && j < i && (j-lo) % step == 0) ||
00057                  (i <= j && (step+j-lo) % (2 * step) == 0))) {
00058                 cout << "  Error: failed at bit " << i << "!\n\n";
00059                 return false;
00060             }
00061         }
00062     }
00063     cout << "  Passed!\n\n";
00064     cout.flush();
00065     return true;
00066 }
00067 
00068 static bool IteratorTest() throw ()
00069 {
00070     BitVector bv;
00071     const int lo = 190, hi = 210;
00072     int i;
00073 
00074     cout << "*** Iterator Test ***\n";
00075     cout.flush();
00076     for (i = lo; i < hi; i++) {
00077         bool set = bv.Set(i * i); assert(!set);
00078     }
00079     BVIter iter(bv);
00080     int ix;
00081     for (i=lo; iter.Next(ix); i++) {
00082         if (ix != (i * i)) {
00083             cout << "  Error: failed at bit " << ix << "!\n\n";
00084             return false;
00085         }
00086     }
00087     cout << "  Passed!\n\n";
00088     cout.flush();
00089     return true;
00090 }
00091 
00092 static bool NextAvailTest() throw ()
00093 {
00094     BitVector bv;
00095     const int MaxSqrt = 100;
00096     int i;
00097 
00098     cout << "*** NextAvail() Test ***\n\n";
00099     cout << "Testing upward...\n";
00100     cout.flush();
00101     for (i = 0; i < MaxSqrt * MaxSqrt; i++) {
00102         if (bv.NextAvail() != i) {
00103             cout << "  Error: failed at bit " << i << "!\n\n";
00104             return false;
00105         }
00106     }
00107     cout << "  Passed!\n\n";
00108     cout << "Testing downward by squares...\n";
00109     cout.flush();
00110     int sqr;
00111     for (i = MaxSqrt - 1; i >= 0; i--) {
00112         sqr = i * i;
00113         bool set = bv.Reset(sqr); assert(set);
00114         if (bv.NextAvail() != sqr) {
00115             cout << "  Error: failed at bit " << sqr << "!\n\n";
00116             return false;
00117         }
00118     }
00119     cout << "  Passed!\n\n";
00120     cout.flush();
00121     return true;
00122 }
00123 
00124 static bool NextAvailExceptTest() throw ()
00125 {
00126     BitVector bv1, bv2;
00127     const int Max = 1000;
00128     int i;
00129 
00130     cout << "*** NextAvailExcept() Test ***\n\n";
00131     cout << "Test 1...\n";
00132     cout.flush();
00133     bv2.SetInterval(0, Max - 1);
00134     if (bv2.NextAvail() != Max) {
00135         cout << "  Error: failed at bit " << Max << "!\n\n";
00136         return false;
00137     }
00138     for (i = 1; i < Max; i++) {
00139         if (bv1.NextAvailExcept(&bv2) != (Max + i)) {
00140             cout << "  Error: failed at bit " << (Max + i) << "!\n\n";
00141             return false;
00142         }
00143     }
00144     bool set = bv2.Reset(Max / 2); assert(set);
00145     if (bv1.NextAvailExcept(&bv2) != (Max / 2)) {
00146         cout << "  Error: failed at bit " << (Max / 2) << "!\n\n";
00147         return false;
00148     }
00149     cout << "  Passed!\n\n";
00150 
00151     cout << "Test 2...\n";
00152     cout.flush();
00153     bv2.ResetAll();
00154     // set "bv2" so only unset bits are multiple of 7
00155     for (i = 0; i < Max; i += 7) {
00156         for (int j = 1; j < 7; j++) {
00157             bool set = bv2.Set(i+j); assert(!set);
00158         }
00159     }
00160     for (i = 0; i < Max / 7; i++) {
00161         if (bv1.NextAvailExcept(&bv2) != (i * 7)) {
00162             cout << "  Error: failed at bit " << (i*7) << "!\n\n";
00163             return false;
00164         }
00165     }
00166     cout << "  Passed!\n\n";
00167     cout.flush();
00168     return true;
00169 }
00170 
00171 static int MyRand(int lower, int upper) throw ()
00172 {
00173     int res;
00174     res = lower + (rand() % (upper-lower+1));
00175     return res;
00176 }
00177 
00178 static bool RdWrIntvlTest() throw ()
00179 {
00180     BitVector bv;
00181     const int WdSize = 64;
00182 
00183     cout << "*** Read/Write Interval Test ***\n\n";
00184 
00185     // test writing/reading within word boundaries
00186     (cout << "Testing intra-words...\n").flush();
00187     Word mask = ~((Word) 0);
00188     int i;
00189     for (i = 0; i < WdSize; i++) {
00190         bv.WriteWord(i * WdSize, WdSize, mask);
00191     }
00192     mask = 1UL;
00193     for (i = 0; i < WdSize; i++) {
00194         bv.WriteWord(i * WdSize, WdSize, mask);
00195         mask <<= 1;
00196     }
00197     int len = WdSize;
00198     for (i = 0; i < WdSize; i++) {
00199         Word res = bv.ReadWord(i * WdSize + i, len);
00200         if (res != 1UL) {
00201             cout << "  Error: failed on word " << i << "!\n\n";
00202             cout.flush();
00203             return false;
00204         }
00205         len--;
00206     }
00207     (cout << "  Passed!\n\n").flush();
00208 
00209     // test writing/reading across word boundaries
00210     (cout << "Testing inter-words...\n").flush();
00211     len = WdSize - 1;
00212     Word val = (((Word) 1) << len) | ((Word) 1);
00213     for (i = 0; i < WdSize * WdSize; i++) {
00214         bv.WriteWord(i * len, len, val);
00215         val++;
00216     }
00217     val = 1UL;
00218     for (i = 0; i < WdSize * WdSize; i++) {
00219         Word res = bv.ReadWord(i * len, len);
00220         if (res != val) {
00221             cout << "  Error: failed on sub-word " << i << "!\n\n";
00222             cout.flush();
00223             return false;
00224         }
00225         val++;
00226     }
00227     val = bv.ReadWord(i * WdSize + WdSize / 2, WdSize);
00228     if (val != 0UL) {
00229         cout << "  Error: failed to read 0 past end of bit vector!\n\n";
00230         cout.flush();
00231         return false;
00232     }
00233     (cout << "  Passed!\n\n").flush();
00234     return true;
00235 }
00236 
00237 static bool SetIntvlTest() throw ()
00238 {
00239     BitVector bv;
00240     const int WdSize = 64;
00241     int i, j, k, lo, hi;
00242 
00243     cout << "*** Set Interval Test ***\n\n";
00244     cout << "All intra-word intervals:\n";
00245     cout.flush();
00246     for (i = 1; i <= WdSize; i++) {
00247         for (j = 0; j < WdSize - i; j ++) {
00248             lo = WdSize +j;
00249             hi = lo + i - 1;
00250             bv.ResetAll();
00251             bv.SetInterval(lo, hi);
00252             for (k = 0; k < bv.Size() + WdSize; k++) {
00253                 if (bv.Read(k) != (lo <= k && k <= hi)) {
00254                     cout << "  Error: failed on interval [" 
00255                          << lo << ", " << hi << "]!\n\n";
00256                     cout.flush();
00257                     return false;
00258                 }
00259             }
00260         }
00261     }
00262     cout << "  Passed!\n\n";
00263     cout << "Inter-word intervals:\n";
00264     cout.flush();
00265     for (i = 0; i < 1000; i++) {
00266         lo = MyRand(0, 1000);
00267         hi = lo + MyRand(0, 4 * WdSize);
00268         bv.ResetAll();
00269         bv.SetInterval(lo, hi);
00270         for (k = 0; k < bv.Size(); k++) {
00271             if (bv.Read(k) != (lo <= k && k <= hi)) {
00272                 cout << "  Error: failed on interval [" 
00273                      << lo << ", " << hi << "]!\n\n";
00274                 cout.flush();
00275                 return false;
00276             }
00277         }
00278     }
00279     cout << "  Passed!\n\n";
00280     cout.flush();
00281     return true;
00282 }
00283 
00284 static bool ResetIntvlTest() throw ()
00285 {
00286     BitVector bv;
00287     const int WdSize = 64;
00288     int i, j, k, lo, hi;
00289 
00290     cout << "*** Reset Interval Test ***\n\n";
00291     cout << "All intra-word intervals:\n";
00292     cout.flush();
00293     for (i = 1; i <= WdSize; i++) {
00294         for (j = 0; j < WdSize - i; j ++) {
00295             lo = WdSize + j;
00296             hi = lo + i - 1;
00297             bv.ResetAll();
00298             bv.SetInterval(0, 3 * WdSize - 1);
00299             bv.ResetInterval(lo, hi);
00300             for (k = 0; k < bv.Size(); k++) {
00301                 if (bv.Read(k) == (lo <= k && k <= hi)) {
00302                     cout << "  Error: failed on interval [" 
00303                          << lo << ", " << hi << "]!\n\n";
00304                     cout.flush();
00305                     return false;
00306                 }
00307             }
00308         }
00309     }
00310     cout << "  Passed!\n\n";
00311     cout << "Inter-word intervals:\n";
00312     cout.flush();
00313     for (i = 0; i < 1000; i++) {
00314         lo = MyRand(0, 1000);
00315         hi = lo + MyRand(0, 4 * WdSize);
00316         bv.ResetAll();
00317         bv.SetInterval(0, hi + WdSize);
00318         bv.ResetInterval(lo, hi);
00319         for (k = 0; k < bv.Size(); k++) {
00320             if (bv.Read(k) == (lo <= k && k <= hi)) {
00321                 cout << "  Error: failed on interval [" 
00322                      << lo << ", " << hi << "]!\n\n";
00323                 cout.flush();
00324                 return false;
00325             }
00326         }
00327     }
00328     cout << "  Passed!\n\n";
00329     cout.flush();
00330     return true;
00331 }
00332 
00333 static bool CopyTest() throw ()
00334 {
00335     cout << "*** Copy Test ***\n";
00336 
00337     // initialize bv0
00338     // (set bits with indexes that are perfect squares in [0,100))
00339     BitVector bv0(30);
00340     int i;
00341     for (i = 0; i < 10; i++) {
00342         bool set = bv0.Set(i*i); assert(!set);
00343     }
00344 
00345     // copy bv0 -> bv1
00346     BitVector bv1(&bv0);
00347 
00348     // test that they are identical
00349     for (i = 0; i < 10; i++) {
00350         int j = i * i;
00351         if (!bv1.Read(j)) {
00352             cout << "  Error: bit " << j << " was not set!\n\n";
00353             return false;
00354         }
00355         bool set = bv1.Reset(j); assert(set);
00356     }
00357 
00358     // bv1 should now be empty
00359     BVIter iter(bv1);
00360     if (iter.Next(i)) {
00361         cout << "  Error: bit " << i << " should not be set!\n\n";
00362         return false;
00363     }
00364     cout << "  Passed!\n\n";
00365     cout.flush();
00366     return true;
00367 }
00368 
00369 static bool PackTest() throw ()
00370 {
00371     cout << "*** Pack Test ***\n\n";
00372     BitVector bv, packMask;
00373 
00374     // test "bv" empty case
00375     cout << "Empty bit-vector case:\n"; cout.flush();
00376     int wordSize = 8 * sizeof(Word);
00377     packMask.SetInterval(0, wordSize - 1);
00378     bv.Pack(packMask);
00379     if (!bv.IsEmpty()) {
00380         cout << "  Error: some bits were set on an empty bit vector!\n\n";
00381         return false;
00382     }
00383     cout << "  Passed!!\n\n";
00384 
00385     // test "bv == packMask" case
00386     cout << "Identical bit-vector case:\n"; cout.flush();
00387     bv = packMask;
00388     bv.Pack(packMask);
00389     if (bv != packMask) {
00390         cout << "  Error: the bit vector was changed!\n\n";
00391         return false;
00392     }
00393     cout << "  Passed!!\n\n";
00394 
00395     // test random cases
00396     cout << "Random packing tests:\n"; cout.flush();
00397     cout.flush();
00398     for (int cnt = 0; cnt < 500; cnt++) {
00399         bv.ResetAll(); packMask.ResetAll();
00400         int numBits = MyRand(50, 250);
00401         for (int j = 0, k = 0; k < numBits; k++) {
00402             int skip = MyRand(1, 10);
00403             if (skip == 10) skip = MyRand(50, 100);
00404             j += skip;
00405             bool set = bv.Set(j); assert(!set);
00406             set = packMask.Set(j); assert(!set);
00407         }
00408         bv.Pack(packMask);
00409         BitVector expected;
00410         expected.SetInterval(0, numBits - 1);
00411         if (bv != expected) {
00412             cout << "  Error: packing result different from expected!\n\n";
00413             return false;
00414         }
00415     }
00416     cout << "  Passed!!\n\n";
00417     cout.flush();
00418     return true;
00419 }
00420 
00421 static bool MSBTest() throw ()
00422 {
00423     cout << "*** MSB Test ***\n\n";
00424     BitVector bv; int i;
00425     
00426     cout << "Testing upward...\n"; cout.flush();
00427     for (i = 0; i < 1000; i++) {
00428         bool set = bv.Set(i); assert(!set);
00429         if (bv.MSB() != i) {
00430             cout << "  Failed on bit " << i << "\n";
00431             return false;
00432         }
00433     }
00434     cout << "  Passed!\n\n";
00435     cout.flush();
00436 
00437     cout << "Testing downward...\n"; cout.flush();
00438     for (i--; i >= 0; i--) {
00439         bool set = bv.Reset(i); assert(set);
00440         if (bv.MSB() != (i-1)) {
00441             cout << "  Failed on bit " << i-1 << "\n";
00442             return false;
00443         }
00444     }
00445     cout << "  Passed!\n\n";
00446     cout.flush();
00447     return true;
00448 }
00449 
00450 static bool TestLTBug() throw()
00451 {
00452     cout << "*** '<' comparator bug Test ***\n\n";
00453 
00454     // commonNames = { 0-135 }
00455     BitVector commonNames;
00456     commonNames.SetInterval(0, 135);
00457     cout << "commonNames = ";
00458     commonNames.PrintAll(cout);
00459 
00460     // newCommonNames = { 0-33, 136-301 }
00461     BitVector newCommonNames;
00462     newCommonNames.SetInterval(0, 33);
00463     newCommonNames.SetInterval(136, 301);
00464     cout << "newCommonNames = ";
00465     newCommonNames.PrintAll(cout);
00466 
00467     // bothCommon = (commonNames & newCommonNames)
00468     //            = { 0-33 }
00469     BitVector bothCommon(&(commonNames));
00470     bothCommon &= newCommonNames;
00471     cout << "bothCommon = ";
00472     bothCommon.PrintAll(cout);
00473     cout << endl;
00474 
00475     bool l_compare = (bothCommon < newCommonNames);
00476 
00477     cout << "(bothCommon < newCommonNames) == "
00478          << (l_compare?"true":"false") << endl;
00479 
00480     return l_compare;
00481 }
00482 
00483 int main(int argc, char *argv[]) 
00484 {
00485     if (SetResetTest() && IteratorTest() && NextAvailTest()
00486         && NextAvailExceptTest() && RdWrIntvlTest() && SetIntvlTest()
00487         && ResetIntvlTest() && CopyTest() && PackTest() && MSBTest()
00488         && TestLTBug()) {
00489         cout << "All tests passed!\n";
00490     } else {
00491         cout << "Test failed!\n";
00492     }
00493     cout.flush();
00494     return 0;
00495 }

Generated on Mon May 8 00:48:32 2006 for Vesta by  doxygen 1.4.2