bitvectortest.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00078 Bits.Set(i,j-i);
00079 unsigned k;
00080 for(k=i; k<j; k++)
00081 TEST(Bits.Test(k));
00082 if(i)
00083 {
00084
00085 TEST(!Bits.Test(i-1));
00086 Bits.Set(i-1);
00087 }
00088 if(j+1<TestSize)
00089 {
00090
00091 TEST(!Bits.Test(j));
00092 Bits.Set(j);
00093 }
00094
00095
00096 if(j-i)
00097 TEST(!Bits.TestClear(i,j-i));
00098 if(j-i>1)
00099 for(k=i; k<j; k++)
00100 {
00101
00102 Bits.Clear(k);
00103 TEST(!Bits.TestClear(i,j-i));
00104 Bits.Set(k);
00105 }
00106
00107 if(j-i)
00108 TEST(Bits.TestSet(i,j-i));
00109 if(j-i>1)
00110 for(k=i; k<j; k++)
00111 {
00112
00113 Bits.Clear(k);
00114 TEST(!Bits.TestSet(i,j-i));
00115 Bits.Set(k);
00116 }
00117
00118
00119 Bits.Clear(i,j-i);
00120 for(k=i; k<j; k++)
00121 TEST(!Bits.Test(k));
00122 if(i)
00123 {
00124
00125 TEST(Bits.Test(i-1));
00126 Bits.Clear(i-1);
00127 }
00128 if(j+1<TestSize)
00129 {
00130
00131 TEST(Bits.Test(j));
00132 Bits.Clear(j);
00133 }
00134
00135
00136 if(j-i)
00137 TEST(Bits.TestClear(i,j-i));
00138 if(j-i>1)
00139 for(k=i; k<j; k++)
00140 {
00141
00142 Bits.Set(k);
00143 TEST(!Bits.TestClear(i,j-i));
00144 Bits.Clear(k);
00145 }
00146
00147 if(j-i)
00148 TEST(!Bits.TestSet(i,j-i));
00149 if(j-i>1)
00150 for(k=i; k<j; k++)
00151 {
00152
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 }