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