Polaris: KeyIterator_test.cc Source File

KeyIterator_test.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// KeyIterator test driver
00004 ///
00005 #include <stdio.h>
00006 #include <math.h>
00007 
00008 #include "../Listable.h"
00009 #include "../String.h"
00010 #include "../HeapStats.h"
00011 #include "../p-setjmp.h"
00012 
00013 #include "List.h"
00014 #include "RefMap.h"
00015 #include "KeyIterator.h"
00016 
00017 
00018 
00019 class MapData : public Listable {
00020  public:
00021     String       *map_strptr;
00022 
00023     MapData() { map_strptr = 0; }
00024 
00025     MapData(int bar) {
00026         char            cusp[10];
00027         sprintf(cusp, "%lu", bar);
00028         map_strptr = new String(cusp, 10);
00029     }
00030 
00031     void print(ostream & o) const {
00032         o << (*map_strptr);
00033     }
00034 
00035     operator const char * () const { return *map_strptr; }
00036 
00037     Listable       *listable_clone() const {
00038         MapData     *bar = new MapData;
00039         bar->map_strptr = new String(*map_strptr, 10);
00040         return bar;
00041     }
00042 
00043     int structures_OK() const { return 1; }
00044 
00045     ~MapData() { delete map_strptr; }
00046 };
00047 
00048 void
00049 report(int tag)
00050 {
00051     cout << "TAG = " << tag << endl;
00052     HeapStats::report( cout );
00053     cout << endl;
00054 }
00055 
00056 int 
00057 process()
00058 {
00059     MapData     *map_test;
00060 
00061     int             count = 10;
00062     int             seedval = 456;
00063     int             passed = 1;
00064     int             tree_num;
00065 
00066     report(1);
00067 
00068     P_ASSERT_HANDLER(0);
00069 
00070     RefMap<String, MapData> tree;
00071 
00072     report(2);
00073 
00074     List<MapData> map_list;
00075 
00076     report(3);
00077 
00078     srand48(seedval);
00079 
00080     for (int i = 0; i < count; i++) {
00081         tree_num = (int) lrand48();
00082 #ifdef DEBUG
00083         cout << "Insert node " << tree_num << ".\n";
00084 #endif
00085         map_test = new MapData(tree_num);
00086 
00087         while (tree.member( *map_test->map_strptr )) {
00088             delete map_test;
00089 #ifdef DEBUG
00090             cout << "Duplicate insert attempt ignored.\n";
00091 #endif
00092             tree_num = (int) lrand48();
00093 #ifdef DEBUG
00094             cout << "Insert node " << tree_num << ".\n";
00095 #endif
00096             map_test = new MapData(tree_num);
00097         }
00098 
00099         map_list.ins_first( map_test );
00100         tree.ins(*map_test->map_strptr, *map_test);
00101 
00102         if (!tree.structures_OK()) {
00103             passed = 0;
00104             cout << "Tree is broken after Insert\n";
00105             tree.print( cout, ": ", ", " );
00106         }
00107     }
00108 
00109     report(4);
00110 
00111     for (KeyIterator<String,MapData> k_iter = tree; k_iter.valid(); ++k_iter) 
00112         cout << k_iter.current_key() << ": " << k_iter.current_data() << endl;
00113 
00114     report(5);
00115 
00116     if (passed)
00117         cout << "Map test passed.\n\n";
00118     else
00119         cout << "Map test failed.\n\n";
00120 
00121     report(6);
00122 
00123     return 0;
00124 }
00125 
00126 int
00127 main(int argc, char *argv[])
00128 {
00129     cout << "STARTING\n";
00130     HeapStats::reset();
00131     process();
00132     HeapStats::report();
00133     cout << "DONE\n";
00134 }
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:05:57 2005