KeySet_test.ccGo to the documentation of this file.00001
00002 #include <stdlib.h>
00003 #include <stream.h>
00004
00005 #include "../Listable.h"
00006 #include "../Collection/KeySet.h"
00007 #include "../Collection/KeyIterator.h"
00008
00009 #include "../p-assert.h"
00010 #include "../p-setjmp.h"
00011
00012 class IntElem : public Listable {
00013 public:
00014 int val;
00015
00016 IntElem(int n) { val = n; }
00017
00018 virtual ~IntElem() { cout << "[" << val << " deleted]"; }
00019
00020 virtual Listable *listable_clone() const {
00021 IntElem *o = new IntElem(val);
00022 return o;
00023 }
00024
00025 int operator == (IntElem &el) const
00026 { return el.value() == val; };
00027
00028 int operator < (IntElem &el) const
00029 { return el.value() < val; };
00030
00031 int value() { return val; }
00032 virtual void print(ostream & o) const { o << val; }
00033 };
00034
00035 ostream &
00036 operator << (ostream &out, const IntElem &ie)
00037 {
00038 out << ie.val;
00039 return out;
00040 }
00041
00042
00043 #define EXPECT(l, what) { \
00044 cout << "Expect " #what " => " << (l); \
00045 \
00046 cout << " ..."; \
00047 if (!l.structures_OK()) { \
00048 cerr << "\n**Error found.\n\n"; \
00049 p_assert(0, "\nError detected.\n"); \
00050 } \
00051 cout << endl; \
00052 }
00053
00054 void
00055 increase(Listable * &e, void *f)
00056 {
00057 IntElem **el = (IntElem **) & e;
00058
00059
00060 *el = new IntElem((int) f);
00061 }
00062
00063 int
00064 main()
00065 {
00066 P_ASSERT_HANDLER(0);
00067
00068 int el;
00069 int i;
00070
00071 KeySet<IntElem> aset;
00072
00073 IntElem *a1 = (new IntElem(1));
00074 IntElem *a2 = (new IntElem(2));
00075 IntElem *a3 = (new IntElem(3));
00076 IntElem *a4 = (new IntElem(4));
00077
00078 aset.ins(a1);
00079 aset.ins(a2);
00080 aset.ins(a3);
00081 aset.ins(a4);
00082
00083 KeyIterator<IntElem,IntElem> intiter = aset;
00084
00085 cout << "Expect 1 2 3 4 => ";
00086
00087 while (intiter.valid()) {
00088 cout << intiter.current_data() << " ";
00089 ++intiter;
00090 }
00091 cout << endl;
00092
00093 aset.ins(a1);
00094 aset.ins(a2);
00095 aset.ins(a3);
00096 aset.ins(a4);
00097
00098 EXPECT(aset, "{1,2,3,4}");
00099
00100 cout << "expect 1 2 3 4 =>";
00101
00102 #if 0
00103 for (int i = 0; i<aset.entries(); i++)
00104 cout << aset._element(i) << " ";
00105 cout << endl;
00106
00107 cout << "expect shrinkage =>";
00108
00109 for (i = 0; !aset.empty();) {
00110 aset._del(i);
00111 cout << aset << " -> ";
00112 }
00113 cout << endl;
00114 #else
00115 aset.clear();
00116 #endif
00117
00118 a1 = (new IntElem(1));
00119 a2 = (new IntElem(2));
00120 a3 = (new IntElem(3));
00121 a4 = (new IntElem(4));
00122
00123 aset.ins(a1);
00124 aset.ins(a2);
00125 aset.ins(a3);
00126 aset.ins(a4);
00127
00128 EXPECT(aset, "{1,2,3,4}");
00129
00130 aset.del(a1);
00131 aset.del(a4);
00132 aset.del(a3);
00133
00134 EXPECT(aset, "{2}");
00135
00136 a1 = (new IntElem(1));
00137 a3 = (new IntElem(3));
00138 a4 = (new IntElem(4));
00139
00140 aset.ins(a1);
00141 aset.ins(a2);
00142 aset.ins(a3);
00143 aset.ins(a4);
00144
00145 for (i = 0; i<4; i++)
00146 cout << "We got " << *aset.grab() << "!\n";
00147
00148 aset.ins(a1);
00149 aset.ins(a2);
00150 aset.ins(a3);
00151 aset.ins(a4);
00152
00153 if (aset.member(*a1))
00154 cout << *a1 << " in da set.\n";
00155
00156 if (aset.member(*a2))
00157 cout << *a2 << " in da set.\n";
00158
00159 if (aset.member(*a3))
00160 cout << *a3 << " in da set.\n";
00161
00162 if (aset.member(*a4))
00163 cout << *a4 << " in da set.\n";
00164
00165 IntElem *a5 = (new IntElem(5));
00166
00167 #if 0
00168 aset._modify(a5, aset._member(*a2));
00169 aset._modify(a5, aset._member(*a3));
00170 EXPECT(aset, "{1, 5, 4}");
00171 #endif
00172
00173 KeySet<IntElem> bset;
00174
00175 bset = aset;
00176
00177 aset.clear();
00178
00179 EXPECT(aset, "{}");
00180 EXPECT(bset, "{1, 5, 4}");
00181
00182 cout << "Expect an error\n";
00183
00184 bset.clear();
00185
00186 a1 = aset.grab();
00187 }
|