Polaris: RefMap_test.cc Source File

RefMap_test.cc

Go to the documentation of this file.
00001 ///
00002 ///
00003 /// RefMap 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 #include "List.h"
00013 #include "RefMap.h"
00014 #include "KeyIterator.h"
00015 
00016 class RefMapData : public Listable {
00017  public:
00018     String       *map_strptr;
00019 
00020     RefMapData() { map_strptr = 0; }
00021 
00022     RefMapData(int  bar) {
00023         char            cusp[10];
00024         sprintf(cusp, "%lu", bar);
00025         map_strptr = new String(cusp, 10);
00026     }
00027 
00028     void print(ostream & o) const {
00029         o << (*map_strptr);
00030     }
00031 
00032     operator const char * () const { return *map_strptr; }
00033 
00034     Listable       *listable_clone() const {
00035         RefMapData     *bar = new RefMapData;
00036         bar->map_strptr = new String(*map_strptr, 10);
00037         return bar;
00038     }
00039 
00040     int structures_OK() const { return 1; }
00041 
00042     ~RefMapData() { delete map_strptr; }
00043 };
00044 
00045 int 
00046 main(int argc, char *argv[])
00047 {
00048     RefMapData     *map_test;
00049 
00050     int             count = 500;
00051     int             seedval = 456;
00052     int             passed = 1;
00053     int             tree_num;
00054 
00055     P_ASSERT_HANDLER(0);
00056 
00057     RefMap<String, RefMapData> tree;
00058     List<RefMapData> map_list;
00059 
00060     srand48(seedval);
00061 
00062     for (int i = 0; i < count; i++) {
00063         tree_num = (int) lrand48();
00064 #ifdef DEBUG
00065         cout << "Insert node " << tree_num << ".\n";
00066 #endif
00067         map_test = new RefMapData(tree_num);
00068         map_list.ins_first(map_test);
00069 
00070         while (tree.member( *map_test->map_strptr )) {
00071             map_list.del(map_test);
00072 #ifdef DEBUG
00073             cout << "Duplicate insert attempt ignored.\n";
00074 #endif
00075             tree_num = (int) lrand48();
00076 #ifdef DEBUG
00077             cout << "Insert node " << tree_num << ".\n";
00078 #endif
00079             map_test = new RefMapData(tree_num);
00080             map_list.ins_first(map_test);
00081         }
00082 
00083         tree.ins(*map_test->map_strptr, *map_test);
00084 
00085         if (!tree.structures_OK()) {
00086             passed = 0;
00087             cout << "Tree is broken after Insert\n";
00088             cout << tree;
00089         }
00090     }
00091 
00092     while (map_list.entries() > 0) {
00093         int     tree_index = (int) (floor(drand48()*(map_list.entries())));
00094         String *tree_data  = map_list[tree_index].map_strptr;
00095 
00096 #ifdef DEBUG
00097         cout << "Delete node " << (*tree_data) << ".\n";
00098 #endif
00099         tree.del(*tree_data);
00100         map_list.del( &map_list[tree_index] );
00101 
00102         if (!tree.structures_OK()) {
00103             passed = 0;
00104             cout << "Tree is broken after Delete\n";
00105             cout << tree;
00106         }
00107     }
00108 
00109     if (passed)
00110         cout << "RefMap test passed.\n\n";
00111     else
00112         cout << "RefMap test failed.\n\n";
00113 
00114     return 0;
00115 }
00116 
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:05 2005