Dictionary_test.ccGo to the documentation of this file.00001
00002
00003 #include <stdio.h>
00004 #include <math.h>
00005
00006 #include "../Definition.h"
00007 #include "../Dictionary.h"
00008 #include "../Collection/KeyIterator.h"
00009 #include "../SimpleString.h"
00010
00011 #include "../p-setjmp.h"
00012
00013
00014
00015
00016 class MapData : public Definition {
00017 public:
00018 String *map_strptr;
00019
00020 MapData() : Definition("") { map_strptr = 0; }
00021
00022 MapData(int bar) : Definition("") {
00023 char cusp[10];
00024 sprintf(cusp, "%lu", bar);
00025 map_strptr = new String(cusp, 10);
00026 rename( *map_strptr );
00027 }
00028
00029 virtual void print(ostream & o) const {
00030 o << (*map_strptr);
00031 }
00032
00033 operator const char * () const { return *map_strptr; }
00034
00035 virtual Definition *definition_clone() const {
00036 MapData *bar = new MapData;
00037 bar->map_strptr = new String(*map_strptr, 10);
00038 return bar;
00039 }
00040
00041 virtual int structures_OK() const { return 1; }
00042
00043 virtual ~MapData() { delete map_strptr; }
00044 };
00045
00046 int
00047 main(int argc, char *argv[])
00048 {
00049 MapData *dict_test;
00050
00051 int count = 15;
00052 int seedval = 456;
00053 int passed = 1;
00054 int dict_num;
00055
00056 String* used[500];
00057
00058 P_ASSERT_HANDLER(0);
00059
00060 Dictionary<MapData> dict;
00061
00062 srand48(seedval);
00063
00064 for (int i = 0; i < count; i++) {
00065 dict_num = lrand48();
00066
00067 dict_test = new MapData(dict_num);
00068 dict.ins(dict_test);
00069
00070 used[i] = dict_test->map_strptr;
00071
00072 if (!dict.structures_OK()) {
00073 passed = 0;
00074 cout << "Dict is broken after Insert\n";
00075 cout << dict;
00076 }
00077 }
00078
00079 DictionaryIter<MapData> iter = dict;
00080
00081 while (iter.valid()) {
00082 cout << "[" << iter.current_key() << ", " <<
00083 iter.current_data() << "]";
00084 cout << endl;
00085 iter++;
00086 }
00087
00088 for (i=0; i<count ; i++) {
00089 int dict_index = (int) (floor(drand48()*(dict.entries())));
00090 String *dict_data = used[dict_index];
00091
00092 dict.del(*dict_data);
00093
00094 if (!dict.structures_OK()) {
00095 passed = 0;
00096 cout << "Dict is broken after Delete\n";
00097 cout << dict;
00098 }
00099 }
00100
00101 for (i=0; i<count ; i++) {
00102 String *dict_data = used[i];
00103
00104 dict.del(*dict_data);
00105
00106 if (!dict.structures_OK()) {
00107 passed = 0;
00108 cout << "Dict is broken after Delete\n";
00109 cout << dict;
00110 }
00111 }
00112
00113 if (passed)
00114 cout << "Map test passed.\n\n";
00115 else
00116 cout << "Map test failed.\n\n";
00117
00118 }
00119
00120
00121
00122
|