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

TestIntSharedTbl.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 Apr 21 09:32:54 EDT 2005 by irina.furman@intel.com 
00020 //      modified on Wed Jun  2 18:21:54 EDT 2004 by ken@xorian.net        
00021 //      modified on Mon Jul 29 14:55:23 EDT 2002 by kcschalk@shr.intel.com
00022 //      modified on Mon May  3 16:04:41 PDT 1999 by heydon
00023 
00024 // Test of the "SharedTable" implementation
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     // fill table
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     // print size
00051     assert(tbl.Size() == numEntries);
00052 
00053     // print expected entries
00054     for (i = 0; i < numEntries; i++) {
00055         IntKey k(i); IntIntPair *pr;
00056         inTable = tbl.Get(k, /*OUT*/ pr);
00057         assert(inTable && (i == pr->val));
00058     }
00059 
00060     // iterate over table
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(/*OUT*/ 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     // iterate again (test "Reset")
00075     it.Reset();
00076     for (i = 0; it.Next(/*OUT*/ pr); i++) /*SKIP*/;
00077     assert(i == tbl.Size());
00078 
00079     // delete entries
00080     for (i = 0; i < numEntries; i++) {
00081         IntKey k(i);
00082         inTable = tbl.Delete(k, /*OUT*/ pr);
00083         assert(inTable && (pr->val == i));
00084         inTable = tbl.Get(k, /*OUT*/ 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 }

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