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

TestIntTbl.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:33:32 EDT 2005 by irina.furman@intel.com 
00020 //      modified on Thu May 27 00:47:48 EDT 2004 by ken@xorian.net        
00021 //      modified on Mon Jul 29 14:51:55 EDT 2002 by kcschalk@shr.intel.com
00022 //      modified on Mon May  3 16:04:41 PDT 1999 by heydon
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     // fill table
00039     for (i = 0; i < numEntries; i++) {
00040         IntKey k(i);
00041         (void)tbl.Put(k, i);
00042     }
00043 
00044     // print size
00045     assert(tbl.Size() == numEntries);
00046 
00047     // print expected entries
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     // iterate over table
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     // iterate again (test "Reset")
00069     it.Reset();
00070     for (i = 0; it.Next(k, v); i++) /*SKIP*/;
00071     assert(i == tbl.Size());
00072 
00073     // delete entries
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 }

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