Polaris: Database_test.cc Source File

Database_test.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// Database test driver
00004 ///
00005 #include <assert.h>
00006 #include <stdio.h>
00007 #include <math.h>
00008 
00009 #include "../Listable.h"
00010 #include "../String.h"
00011 #include "../p-setjmp.h"
00012 
00013 #define DEBUG       1
00014 #define REFTEST     1
00015 
00016 #include "Map.h"
00017 #include "Database.h"
00018 #include "KeyMap.h"
00019 #include "KeyDatabase.h"
00020 #include "RefMap.h"
00021 #include "RefDatabase.h"
00022 #include "RefKeyMap.h"
00023 #include "RefKeyDatabase.h"
00024 #include "KeyIterator.h"
00025 
00026 
00027 class DatabaseData : public Listable {
00028  public:
00029     String       *map_strptr;
00030 
00031     DatabaseData() { map_strptr = 0; }
00032 
00033     DatabaseData(int  bar) {
00034         char            cusp[10];
00035         sprintf(cusp, "%lu", bar);
00036         map_strptr = new String(cusp, 10);
00037     }
00038 
00039     void print(ostream & o) const {
00040         o << (*map_strptr);
00041     }
00042 
00043         operator const char * () const { return *map_strptr; }
00044 
00045     Listable       *listable_clone() const {
00046         DatabaseData     *bar = new DatabaseData;
00047         bar->map_strptr = new String(*map_strptr, 10);
00048         return bar;
00049     }
00050 
00051     int structures_OK() const { return 1; }
00052 
00053     ~DatabaseData() { cout << "DELETE " << map_strptr << endl; delete map_strptr; }
00054 };
00055 
00056 int 
00057 main(int argc, char *argv[])
00058 {
00059   DatabaseData     *map_test;
00060   
00061   int             count = 15;
00062   int             seedval = 456;
00063   int             passed = 1;
00064   int             tree_num;
00065   
00066   String*         used[500];
00067   
00068   P_ASSERT_HANDLER(0);
00069   
00070   Database<String, DatabaseData> tree;
00071   
00072   RefDatabase<String, DatabaseData> tree4;
00073   
00074   srand48(seedval);
00075   
00076   for (int i = 0; i < count; i++) {
00077     tree_num = (int) lrand48();
00078     
00079 #ifdef DEBUG
00080     cout << "Insert node " << tree_num << ".\n";
00081 #endif
00082     
00083     map_test = new DatabaseData(tree_num);
00084     
00085     tree.ins(*map_test->map_strptr, map_test);
00086     
00087 #ifdef REFTEST
00088     tree4.ins(*map_test->map_strptr, *map_test);
00089 #endif
00090     
00091     used[i] = map_test->map_strptr;
00092     
00093     if (!tree.structures_OK()) {
00094       passed = 0;
00095       cout << "Tree is broken after Insert\n";
00096       cout << tree;
00097     }
00098     
00099 #ifdef REFTEST
00100     if (!tree4.structures_OK()) {
00101       passed = 0;
00102       cout << "RefTree is broken after Insert\n";
00103       cout << tree;
00104     }
00105 #endif
00106   }
00107   
00108   {
00109     KeyIterator<String, DatabaseData> iter = tree;
00110     
00111 #ifdef REFTEST
00112     KeyIterator<String, DatabaseData> refiter = tree4;
00113 #endif
00114     
00115     while (iter.valid()) {
00116       cout << "[" << iter.current_key() << ", " <<
00117     iter.current_data() << "]";
00118       
00119 #ifdef REFTEST
00120       if (refiter.valid()) {
00121     cout << "  {" << refiter.current_key() << ", " <<
00122       refiter.current_data() << "}";
00123     refiter++;
00124       }
00125 #endif
00126       
00127       cout << endl;
00128       iter++;
00129     }
00130   }
00131   
00132 /// Check duplicate insertion (should delete the old one first)
00133   {
00134     tree_num = atoi(*used[count/2]);
00135     
00136 #ifdef DEBUG
00137     cout << "Insert DUPLICATE node " << tree_num << ".\n";
00138 #endif
00139     
00140     map_test = new DatabaseData(tree_num);
00141     
00142     tree.ins(*map_test->map_strptr, map_test);
00143     
00144 #ifdef REFTEST
00145     tree4.ins(*map_test->map_strptr, *map_test);
00146 #endif
00147     
00148     used[count/2] = map_test->map_strptr;
00149     
00150     if (!tree.structures_OK()) {
00151       passed = 0;
00152       cout << "Tree is broken after duplicate Insert\n";
00153       cout << tree;
00154     }
00155     
00156 #ifdef REFTEST
00157     if (!tree4.structures_OK()) {
00158       passed = 0;
00159       cout << "RefTree is broken after duplicate Insert\n";
00160       cout << tree;
00161     }
00162 #endif
00163   }
00164   
00165   {
00166     KeyIterator<String, DatabaseData> iter = tree;
00167     
00168 #ifdef REFTEST
00169     KeyIterator<String, DatabaseData> refiter = tree4;
00170 #endif
00171     
00172     while (iter.valid()) {
00173       cout << "[" << iter.current_key() << ", " <<
00174     iter.current_data() << "]";
00175       
00176 #ifdef REFTEST
00177       if (refiter.valid()) {
00178     cout << "  {" << refiter.current_key() << ", " <<
00179       refiter.current_data() << "}";
00180     refiter++;
00181       }
00182 #endif
00183       
00184       cout << endl;
00185       iter++;
00186     }
00187     
00188   }
00189   for (i=0; i<count ; i++) {
00190     String  *tree_data  = used[i];
00191     DatabaseData    *md;
00192     
00193 #ifdef DEBUG
00194     cout << "Delete node " << (*tree_data) << ".\n";
00195 #endif
00196     
00197 #ifdef REFTEST 
00198 #if 0
00199     tree4.del(*tree_data);
00200 #endif
00201 #endif
00202     
00203     cout << "GRAB\n";
00204     md = tree.grab( *tree_data );
00205     tree.ins( *md->map_strptr, md );
00206     
00207     cout << "DELETE\n";
00208     tree.del(*tree_data);
00209     
00210     if (!tree.structures_OK()) {
00211       passed = 0;
00212       cout << "Tree is broken after Delete\n";
00213       cout << tree;
00214     }
00215     
00216 #ifdef REFTEST
00217     if (!tree4.structures_OK()) {
00218       passed = 0;
00219       cout << "RefTree is broken after Delete\n";
00220       cout << tree;
00221     }
00222 #endif
00223     
00224   }
00225   
00226   {
00227     KeyIterator<String, DatabaseData> iter = tree;
00228     
00229 #ifdef REFTEST
00230     KeyIterator<String, DatabaseData> refiter = tree4;
00231 #endif
00232     
00233     while (iter.valid()) {
00234       cout << "[" << iter.current_key() << ", " <<
00235     iter.current_data() << "]";
00236       
00237 #ifdef REFTEST
00238       if (refiter.valid()) {
00239     cout << "  {" << refiter.current_key() << ", " <<
00240       refiter.current_data() << "}";
00241     refiter++;
00242       }
00243 #endif
00244       
00245       cout << endl;
00246       iter++;
00247     }
00248   }
00249   
00250   if (passed)
00251     cout << "Database test passed.\n\n";
00252   else
00253     cout << "Database test failed.\n\n";
00254   
00255 }
00256 
00257 
00258 
00259 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:43 2005