00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <Basics.H>
00025 #include "Table.H"
00026 #include "IntKey.H"
00027 #include "Generics.H"
00028
00029 const Word Factor = 2;
00030 const Word MinEntries = 10;
00031 const Word MaxEntries = 10000;
00032
00033 void RunTest(IntIntTbl &tbl, Word numEntries) throw ()
00034 {
00035 Word i;
00036 bool inTable;
00037
00038
00039 for (i = 0; i < numEntries; i++) {
00040 IntKey k(i);
00041 (void)tbl.Put(k, i);
00042 }
00043
00044
00045 assert(tbl.Size() == numEntries);
00046
00047
00048 for (i = 0; i < numEntries; i++) {
00049 IntKey k(i); int v;
00050 inTable = tbl.Get(k, v);
00051 assert(inTable && (i == v));
00052 }
00053
00054
00055 IntIntIter it(&tbl);
00056 IntKey k; int v;
00057 bool *eltInTbl = NEW_PTRFREE_ARRAY(bool, numEntries);
00058 for (i = 0; i < numEntries; i++) eltInTbl[i] = false;
00059 while (it.Next(k, v)) {
00060 i = k.Val();
00061 assert(i < numEntries); // Note: i >= 0 because it's unsigned
00062 assert((i == v) && !eltInTbl[i]);
00063 eltInTbl[i] = true;
00064 }
00065 for (i = 0; i < numEntries; i++) assert(eltInTbl[i]);
00066 delete[] eltInTbl;
00067
00068
00069 it.Reset();
00070 for (i = 0; it.Next(k, v); i++) ;
00071 assert(i == tbl.Size());
00072
00073
00074 for (i = 0; i < numEntries; i++) {
00075 IntKey k(i); int v;
00076 inTable = tbl.Delete(k, v);
00077 assert(inTable && (v == i));
00078 inTable = tbl.Get(k, v);
00079 assert(!inTable);
00080 }
00081 assert(tbl.Size() == 0);
00082 }
00083
00084 using std::cout;
00085 using std::endl;
00086
00087 int main()
00088 {
00089 IntIntTbl tbl(MinEntries);
00090 Word numEntries = MinEntries;
00091
00092 while (1) {
00093 cout << "Testing table of size " << numEntries << "..." << endl;
00094 RunTest(tbl, numEntries);
00095 numEntries *= Factor;
00096 if (numEntries > MaxEntries) break;
00097 tbl.Init();
00098 }
00099 cout << "Passed all tests!" << endl;
00100 return 0;
00101 }