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
00026 #include <Basics.H>
00027 #include "IntKey.H"
00028 #include "SharedTable.H"
00029
00030 const Word Factor = 2;
00031 const Word MinEntries = 10;
00032 const Word MaxEntries = 170000;
00033
00034 typedef SharedTable<IntKey,int>::KVPair IntIntPair;
00035 typedef SharedTable<IntKey,int>::T IntIntSTbl;
00036 typedef SharedTable<IntKey,int>::Iterator IntIntSIter;
00037
00038 void RunTest(IntIntSTbl &tbl, Word numEntries) throw ()
00039 {
00040 Word i;
00041 bool inTable;
00042
00043
00044 for (i = 0; i < numEntries; i++) {
00045 IntKey k(i);
00046 IntIntPair *pr = NEW_CONSTR(IntIntPair, (k, i));
00047 (void)tbl.Put(pr);
00048 }
00049
00050
00051 assert(tbl.Size() == numEntries);
00052
00053
00054 for (i = 0; i < numEntries; i++) {
00055 IntKey k(i); IntIntPair *pr;
00056 inTable = tbl.Get(k, pr);
00057 assert(inTable && (i == pr->val));
00058 }
00059
00060
00061 IntIntSIter it(&tbl);
00062 IntIntPair *pr;
00063 bool *eltInTbl = NEW_PTRFREE_ARRAY(bool, numEntries);
00064 for (i = 0; i < numEntries; i++) eltInTbl[i] = false;
00065 while (it.Next( pr)) {
00066 i = pr->key.Val();
00067 assert(i < numEntries); // Note: i >= 0 because it's unsigned
00068 assert((i == pr->val) && !eltInTbl[i]);
00069 eltInTbl[i] = true;
00070 }
00071 for (i = 0; i < numEntries; i++) assert(eltInTbl[i]);
00072 delete[] eltInTbl;
00073
00074
00075 it.Reset();
00076 for (i = 0; it.Next( pr); i++) ;
00077 assert(i == tbl.Size());
00078
00079
00080 for (i = 0; i < numEntries; i++) {
00081 IntKey k(i);
00082 inTable = tbl.Delete(k, pr);
00083 assert(inTable && (pr->val == i));
00084 inTable = tbl.Get(k, pr);
00085 assert(!inTable);
00086 }
00087 assert(tbl.Size() == 0);
00088 }
00089
00090 using std::cout;
00091 using std::endl;
00092
00093 int main()
00094 {
00095 IntIntSTbl tbl(MinEntries);
00096 Word numEntries = MinEntries;
00097
00098 while (true) {
00099 (cout << "Testing table of size " << numEntries << "...\n").flush();
00100 RunTest(tbl, numEntries);
00101 numEntries *= Factor;
00102 if (numEntries > MaxEntries) break;
00103 tbl.Init();
00104 }
00105 cout << "Passed all tests!" << endl;
00106
00107 return 0;
00108 }