bitvectortest.cpp

Go to the documentation of this file.
00001 /*
00002 This program is distributed under the terms of the 'MIT license'. The text
00003 of this licence follows...
00004 
00005 Copyright (c) 2007-2009 J.D.Medhurst (a.k.a. Tixy)
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00020 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023 THE SOFTWARE.
00024 */
00025 
00032 #include "common.h"
00033 #include "test/test.h"
00034 #include "../bitvector.h"
00035 
00036 
00037 
00038 static const size_t TestSize = 160;
00039 static BitVector<TestSize> Bits;
00040 static BitVector<0> ZeroBits;
00041 
00042 
00043 
00044 //
00045 // Top level test function
00046 //
00047 
00048 void TestBitVector()
00049     {
00050 #ifdef TEST_NOISY
00051     printf("TestBitVector Set(i) Clear(i) Test(i)\n");
00052 #endif
00053     TEST((unsigned*)ZeroBits==0);
00054     unsigned i;
00055     for(i=0; i<TestSize; i++)
00056         {
00057         TEST(!Bits.Test(i));
00058         Bits.Set(i);
00059         TEST(Bits.Test(i));
00060         Bits.Clear(i);
00061         TEST(!Bits.Test(i));
00062         }
00063 
00064 #ifdef TEST_NOISY
00065     printf("TestBitVector Set(i,n) Clear(i,n) TestSet(i,n) TestClear(i,n)\n");
00066 #endif
00067     for(i=0; i<TestSize; i++)
00068         {
00069 #ifdef TEST_NOISY
00070         printf("%d\n",i);
00071 #endif
00072         for(unsigned j=i; j<=TestSize; j++)
00073             {
00074             if(j-i)
00075                 TEST(Bits.TestClear(i,j-i));
00076 
00077             // test Set()
00078             Bits.Set(i,j-i);
00079             unsigned k;
00080             for(k=i; k<j; k++)
00081                 TEST(Bits.Test(k)); // test each bit is set
00082             if(i)
00083                 {
00084                 // bit before range should still be clear...
00085                 TEST(!Bits.Test(i-1));
00086                 Bits.Set(i-1);
00087                 }
00088             if(j+1<TestSize)
00089                 {
00090                 // bit after range should still be clear...
00091                 TEST(!Bits.Test(j));
00092                 Bits.Set(j);
00093                 }
00094 
00095             // test TestClear()
00096             if(j-i)
00097                 TEST(!Bits.TestClear(i,j-i)); // test on fully set range
00098             if(j-i>1)
00099                 for(k=i; k<j; k++)
00100                     {
00101                     // test single bit in range clear...
00102                     Bits.Clear(k);
00103                     TEST(!Bits.TestClear(i,j-i));
00104                     Bits.Set(k);
00105                     }
00106             // test TestSet()
00107             if(j-i)
00108                 TEST(Bits.TestSet(i,j-i)); // test on fully set range
00109             if(j-i>1)
00110                 for(k=i; k<j; k++)
00111                     {
00112                     // test single bit in range clear...
00113                     Bits.Clear(k);
00114                     TEST(!Bits.TestSet(i,j-i));
00115                     Bits.Set(k);
00116                     }
00117 
00118             // test Clear()
00119             Bits.Clear(i,j-i);
00120             for(k=i; k<j; k++)
00121                 TEST(!Bits.Test(k)); // test each bit is clear
00122             if(i)
00123                 {
00124                 // bit before range should still be set...
00125                 TEST(Bits.Test(i-1));
00126                 Bits.Clear(i-1);
00127                 }
00128             if(j+1<TestSize)
00129                 {
00130                 // bit after range should still be set...
00131                 TEST(Bits.Test(j));
00132                 Bits.Clear(j);
00133                 }
00134 
00135             // test TestClear()
00136             if(j-i)
00137                 TEST(Bits.TestClear(i,j-i)); // test on cleared range
00138             if(j-i>1)
00139                 for(k=i; k<j; k++)
00140                     {
00141                     // test single bit in range set...
00142                     Bits.Set(k);
00143                     TEST(!Bits.TestClear(i,j-i));
00144                     Bits.Clear(k);
00145                     }
00146             // test TestSet()
00147             if(j-i)
00148                 TEST(!Bits.TestSet(i,j-i)); // test on cleared range
00149             if(j-i>1)
00150                 for(k=i; k<j; k++)
00151                     {
00152                     // test single bit in range set...
00153                     Bits.Set(k);
00154                     TEST(!Bits.TestSet(i,j-i));
00155                     Bits.Clear(k);
00156                     }
00157 
00158             }
00159         }
00160 
00161 #ifdef TEST_NOISY
00162     printf("TestBitVector Find(n,0)\n");
00163 #endif
00164     for(i=0; i<=TestSize/2; i++)
00165         for(unsigned ss=0; ss<=i; ss++)
00166             {
00167 #ifdef TEST_NOISY
00168             printf("%d\n",i);
00169 #endif
00170             for(unsigned se=ss+1; se<=ss+64; se++)
00171                 for(unsigned b=se+1; b<=se+i && b<TestSize; b++)
00172                     {
00173                     Bits.Reset();
00174                     Bits.Set(ss,se-ss);
00175                     Bits.Set(b);
00176                     int a = Bits.Find(i,0);
00177                     if(i<=ss)
00178                         TEST(a==0);
00179                     else if(i<=b-se)
00180                         TEST(a==(int)se);
00181                     else if(i<=TestSize-(b+1))
00182                         TEST(a==(int)b+1);
00183                     else
00184                         TEST(a==-1);
00185                     }
00186             }
00187     }

Generated by  doxygen 1.6.1