Polaris: Element_test.cc Source File

Element_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   srand48(seedval);
00089 
00090   tree_num = (int) lrand48();
00091 
00092   elt_0 = new KeyMapData(tree_num);
00093   elt_1 = *elt_0;
00094 
00095     cout << elt_0 << endl;
00096     cout << elt_1 << endl;
00097 
00098     cout << *elt_0 << endl;
00099     cout << *elt_1 << endl;
00100   
00101   for (int i = 0; i < count; i++) {
00102     tree_num = (int) lrand48();
00103     
00104 #ifdef DEBUG
00105     cout << "Insert node " << tree_num << ".\n";
00106 #endif
00107     
00108     map_test = new KeyMapData(tree_num);
00109 
00110     String *key = new String(*map_test->map_strptr);
00111     tree.ins(key, map_test);
00112     
00113 #ifdef REFTEST
00114     tree4.ins(new String(*map_test->map_strptr), *map_test);
00115 #endif
00116     
00117     used[i] = key;
00118     
00119     if (!tree.structures_OK()) {
00120       passed = 0;
00121       cout << "Tree is broken after Insert\n";
00122       cout << tree;
00123     }
00124     
00125 #ifdef REFTEST
00126     if (!tree4.structures_OK()) {
00127       passed = 0;
00128       cout << "RefTree is broken after Insert\n";
00129       cout << tree;
00130     }
00131 #endif
00132   }
00133   
00134   {
00135     KeyIterator<String, KeyMapData> iter = tree;
00136     
00137 #ifdef REFTEST
00138     KeyIterator<String, KeyMapData> refiter = tree4;
00139 #endif
00140     
00141     while (iter.valid()) {
00142       cout << "[" << iter.current_key() << ", " <<
00143     iter.current_data() << "]";
00144       
00145 #ifdef REFTEST
00146       if (refiter.valid()) {
00147     cout << "  {" << refiter.current_key() << ", " <<
00148       refiter.current_data() << "}";
00149     refiter++;
00150       }
00151 #endif
00152       
00153       cout << endl;
00154       iter++;
00155     }
00156   }
00157   
00158   for (i=0; i<count ; i++) {
00159     String  *tree_data  = used[i];
00160     KeyMapData  *md;
00161     
00162 #ifdef DEBUG
00163     cout << "Delete node " << (*tree_data) << ".\n";
00164 #endif
00165     
00166 #ifdef REFTEST 
00167 #if 0
00168     tree4.del(*tree_data);
00169 #endif
00170 #endif
00171     
00172     cout << "DELETE\n";
00173     tree.del(*tree_data);
00174     
00175     if (!tree.structures_OK()) {
00176       passed = 0;
00177       cout << "Tree is broken after Delete\n";
00178       cout << tree;
00179     }
00180     
00181 #ifdef REFTEST
00182     if (!tree4.structures_OK()) {
00183       passed = 0;
00184       cout << "RefTree is broken after Delete\n";
00185       cout << tree;
00186     }
00187 #endif
00188     
00189   }
00190   
00191   {
00192     KeyIterator<String, KeyMapData> iter = tree;
00193     
00194 #ifdef REFTEST
00195     KeyIterator<String, KeyMapData> refiter = tree4;
00196 #endif
00197     
00198     while (iter.valid()) {
00199       cout << "[" << iter.current_key() << ", " <<
00200     iter.current_data() << "]";
00201       
00202 #ifdef REFTEST
00203       if (refiter.valid()) {
00204     cout << "  {" << refiter.current_key() << ", " <<
00205       refiter.current_data() << "}";
00206     refiter++;
00207       }
00208 #endif
00209       
00210       cout << endl;
00211       iter++;
00212     }
00213   }
00214   
00215   if (passed)
00216     cout << "KeyMap test passed.\n\n";
00217   else
00218     cout << "KeyMap test failed.\n\n";
00219   
00220 }
00221 
00222 
00223 
00224 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:46 2005