Polaris: KeyMap_test.cc Source File

KeyMap_test.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// KeyMap 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 #include "Element.h"
00027 #include "RefElement.h"
00028 
00029 #include "KeySet.h"
00030 #include "Set.h"
00031 
00032 class KeyMapData : public Listable {
00033  public:
00034     String       *map_strptr;
00035 
00036     KeyMapData() { map_strptr = 0; }
00037 
00038     KeyMapData(int  bar) {
00039         char            cusp[10];
00040         sprintf(cusp, "%lu", bar);
00041         map_strptr = new String(cusp, 10);
00042     }
00043 
00044     operator String * () const { return map_strptr; }
00045 
00046     void print(ostream & o) const {
00047         o << (*map_strptr);
00048     }
00049 
00050         operator const char * () const { return *map_strptr; }
00051 
00052     Listable       *listable_clone() const {
00053         KeyMapData     *bar = new KeyMapData;
00054         bar->map_strptr = new String(*map_strptr, 10);
00055         return bar;
00056     }
00057 
00058     int structures_OK() const { return 1; }
00059 
00060     ~KeyMapData() { cout << "DELETE " << map_strptr << endl; delete map_strptr; }
00061 };
00062 
00063 int 
00064 main(int argc, char *argv[])
00065 {
00066   KeyMapData     *map_test;
00067   
00068   int             count = 15;
00069   int             seedval = 456;
00070   int             passed = 1;
00071   int             tree_num;
00072   
00073   String*         used[500];
00074   
00075   P_ASSERT_HANDLER(0);
00076 
00077   Database<String,KeyMapData> dat_0;
00078   KeySet<KeyMapData>          dat_1;
00079   Set<KeyMapData>             dat_2;
00080 
00081   KeyMap<String, KeyMapData> tree;
00082   
00083   RefKeyMap<String, KeyMapData> tree4;
00084 
00085   Element<KeyMapData>    elt_0;
00086   RefElement<KeyMapData> elt_1;
00087 
00088   
00089   srand48(seedval);
00090 
00091   tree_num = (int) lrand48();
00092 
00093   elt_0 = new KeyMapData(tree_num);
00094   elt_1 = *elt_0;
00095   
00096   for (int i = 0; i < count; i++) {
00097     tree_num = (int) lrand48();
00098     
00099 #ifdef DEBUG
00100     cout << "Insert node " << tree_num << ".\n";
00101 #endif
00102     
00103     map_test = new KeyMapData(tree_num);
00104 
00105     String *key = new String(*map_test->map_strptr);
00106     tree.ins(key, map_test);
00107     
00108 #ifdef REFTEST
00109     tree4.ins(new String(*map_test->map_strptr), *map_test);
00110 #endif
00111     
00112     used[i] = key;
00113     
00114     if (!tree.structures_OK()) {
00115       passed = 0;
00116       cout << "Tree is broken after Insert\n";
00117       cout << tree;
00118     }
00119     
00120 #ifdef REFTEST
00121     if (!tree4.structures_OK()) {
00122       passed = 0;
00123       cout << "RefTree is broken after Insert\n";
00124       cout << tree;
00125     }
00126 #endif
00127   }
00128   
00129   {
00130     KeyIterator<String, KeyMapData> iter = tree;
00131     
00132 #ifdef REFTEST
00133     KeyIterator<String, KeyMapData> refiter = tree4;
00134 #endif
00135     
00136     while (iter.valid()) {
00137       cout << "[" << iter.current_key() << ", " <<
00138     iter.current_data() << "]";
00139       
00140 #ifdef REFTEST
00141       if (refiter.valid()) {
00142     cout << "  {" << refiter.current_key() << ", " <<
00143       refiter.current_data() << "}";
00144     refiter++;
00145       }
00146 #endif
00147       
00148       cout << endl;
00149       iter++;
00150     }
00151   }
00152   
00153   for (i=0; i<count ; i++) {
00154     String  *tree_data  = used[i];
00155     KeyMapData  *md;
00156     
00157 #ifdef DEBUG
00158     cout << "Delete node " << (*tree_data) << ".\n";
00159 #endif
00160     
00161 #ifdef REFTEST 
00162 #if 0
00163     tree4.del(*tree_data);
00164 #endif
00165 #endif
00166     
00167     cout << "DELETE\n";
00168     tree.del(*tree_data);
00169     
00170     if (!tree.structures_OK()) {
00171       passed = 0;
00172       cout << "Tree is broken after Delete\n";
00173       cout << tree;
00174     }
00175     
00176 #ifdef REFTEST
00177     if (!tree4.structures_OK()) {
00178       passed = 0;
00179       cout << "RefTree is broken after Delete\n";
00180       cout << tree;
00181     }
00182 #endif
00183     
00184   }
00185   
00186   {
00187     KeyIterator<String, KeyMapData> iter = tree;
00188     
00189 #ifdef REFTEST
00190     KeyIterator<String, KeyMapData> refiter = tree4;
00191 #endif
00192     
00193     while (iter.valid()) {
00194       cout << "[" << iter.current_key() << ", " <<
00195     iter.current_data() << "]";
00196       
00197 #ifdef REFTEST
00198       if (refiter.valid()) {
00199     cout << "  {" << refiter.current_key() << ", " <<
00200       refiter.current_data() << "}";
00201     refiter++;
00202       }
00203 #endif
00204       
00205       cout << endl;
00206       iter++;
00207     }
00208   }
00209   
00210   if (passed)
00211     cout << "KeyMap test passed.\n\n";
00212   else
00213     cout << "KeyMap test failed.\n\n";
00214   
00215 }
00216 
00217 
00218 
00219 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:57 2005