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