Polaris: Map_test.cc Source File

Map_test.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// Map test driver
00004 ///
00005 #include <stdio.h>
00006 #include <math.h>
00007 
00008 #include "../Listable.h"
00009 #include "../String.h"
00010 #include "../p-setjmp.h"
00011 
00012 #define DEBUG       1
00013 #define REFTEST     1
00014 
00015 #include "Map.h"
00016 #include "Database.h"
00017 #include "KeyMap.h"
00018 #include "KeyDatabase.h"
00019 #include "RefMap.h"
00020 #include "RefDatabase.h"
00021 #include "RefKeyMap.h"
00022 #include "RefKeyDatabase.h"
00023 #include "KeyIterator.h"
00024 
00025 
00026 class MapData : public Listable {
00027  public:
00028     String       *map_strptr;
00029 
00030     MapData() { map_strptr = 0; }
00031 
00032     MapData(int  bar) {
00033         char            cusp[10];
00034         sprintf(cusp, "%lu", bar);
00035         map_strptr = new String(cusp, 10);
00036     }
00037 
00038     void print(ostream & o) const {
00039         o << (*map_strptr);
00040     }
00041 
00042         operator const char * () const { return *map_strptr; }
00043 
00044     Listable       *listable_clone() const {
00045         MapData     *bar = new MapData;
00046         bar->map_strptr = new String(*map_strptr, 10);
00047         return bar;
00048     }
00049 
00050     int structures_OK() const { return 1; }
00051 
00052     ~MapData() { cout << "DELETE " << map_strptr << endl; delete map_strptr; }
00053 };
00054 
00055 int 
00056 main(int argc, char *argv[])
00057 {
00058     MapData     *map_test;
00059 
00060     int             count = 15;
00061     int             seedval = 456;
00062     int             passed = 1;
00063     int             tree_num;
00064 
00065     String*         used[500];
00066 
00067     P_ASSERT_HANDLER(0);
00068 
00069     Map<String, MapData> tree;
00070     Database<String, MapData> tree1;
00071     KeyMap<String, MapData> tree2;
00072     KeyDatabase<String, MapData> tree3;
00073 
00074     RefMap<String, MapData> tree4;
00075     RefDatabase<String, MapData> tree5;
00076     RefKeyMap<String, MapData> tree6;
00077     RefKeyDatabase<String, MapData> tree7;
00078 
00079     srand48(seedval);
00080 
00081     for (int i = 0; i < count; i++) {
00082         tree_num = (int) lrand48();
00083 
00084 #ifdef DEBUG
00085         cout << "Insert node " << tree_num << ".\n";
00086 #endif
00087 
00088         map_test = new MapData(tree_num);
00089 
00090         tree.ins(*map_test->map_strptr, map_test);
00091 
00092 #ifdef REFTEST
00093         tree4.ins(*map_test->map_strptr, *map_test);
00094 #endif
00095 
00096         used[i] = map_test->map_strptr;
00097 
00098         if (!tree.structures_OK()) {
00099             passed = 0;
00100             cout << "Tree is broken after Insert\n";
00101             cout << tree;
00102         }
00103 
00104 #ifdef REFTEST
00105         if (!tree4.structures_OK()) {
00106             passed = 0;
00107             cout << "RefTree is broken after Insert\n";
00108             cout << tree;
00109         }
00110 #endif
00111     }
00112 
00113     KeyIterator<String, MapData> iter = tree;
00114 
00115 #ifdef REFTEST
00116     KeyIterator<String, MapData> refiter = tree4;
00117 #endif
00118 
00119     while (iter.valid()) {
00120         cout << "[" << iter.current_key() << ", " <<
00121             iter.current_data() << "]";
00122 
00123 #ifdef REFTEST
00124         if (refiter.valid()) {
00125             cout << "  {" << refiter.current_key() << ", " <<
00126                 refiter.current_data() << "}";
00127             refiter++;
00128         }
00129 #endif
00130 
00131         cout << endl;
00132         iter++;
00133     }
00134     
00135     for (i=0; i<count ; i++) {
00136         String  *tree_data  = used[i];
00137         MapData *md;
00138 
00139 #ifdef DEBUG
00140         cout << "Delete node " << (*tree_data) << ".\n";
00141 #endif
00142 
00143 #ifdef REFTEST 
00144 #if 0
00145         tree4.del(*tree_data);
00146 #endif
00147 #endif
00148 
00149         cout << "GRAB\n";
00150         md = tree.grab( *tree_data );
00151         tree.ins( *md->map_strptr, md );
00152 
00153         cout << "DELETE\n";
00154         tree.del(*tree_data);
00155 
00156         if (!tree.structures_OK()) {
00157             passed = 0;
00158             cout << "Tree is broken after Delete\n";
00159             cout << tree;
00160         }
00161 
00162 #ifdef REFTEST
00163         if (!tree4.structures_OK()) {
00164             passed = 0;
00165             cout << "RefTree is broken after Delete\n";
00166             cout << tree;
00167         }
00168 #endif
00169 
00170     }
00171 
00172     if (passed)
00173         cout << "Map test passed.\n\n";
00174     else
00175         cout << "Map test failed.\n\n";
00176 
00177 }
00178 
00179 
00180 
00181 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:59 2005