Polaris: RefList_test.cc Source File

RefList_test.cc

Go to the documentation of this file.
00001 ///
00002 /// .NAME RefList_test -- Routines to test RefList
00003 /// .LIBRARY Polaris
00004 /// .HEADER C++ VDL
00005 /// .FILE RefList_test.cc
00006 ///
00007 /// .SECTION Overview
00008 /// Contains a main routine to test the functionality of the
00009 /// RefList class.  Defines an IntElem class to be used
00010 /// by the RefList.
00011 ///
00012 /// .SECTION See Also
00013 /// RefList
00014 ///
00015 #include <stdlib.h>
00016 #include <stream.h>
00017 
00018 #include "../Listable.h"
00019 #include "../p-assert.h"
00020 #include "../p-setjmp.h"
00021 
00022 #include "List.h"
00023 #include "RefList.h"
00024 #include "Iterator.h"
00025 
00026 class IntElem   : public Listable {
00027  public:
00028     int             val;
00029 
00030     IntElem(int n) { val = n; }
00031 
00032     virtual         ~IntElem() { cout << "[" << val << " deleted]"; }
00033 
00034     virtual Listable *listable_clone() const {
00035         IntElem        *o = new IntElem(val);
00036         return o;
00037     }
00038 
00039     int             value() { return val; }
00040 
00041     virtual void    print(ostream & o) const { o << val; }
00042 };
00043 
00044 ostream & 
00045 operator << (ostream & out, const IntElem & ie) 
00046 {
00047     out << ie.val;
00048     return out;
00049 }
00050 
00051 
00052 #define EXPECT(l, what) {                                                     \
00053   cout << "Expect " #what " => " << (l);                                      \
00054   /* Also check the structure */                                              \
00055   cout << " ...";                                                             \
00056   if (!l.structures_OK()) {                                                   \
00057     cerr << "\n**Error found.\n\n";                                           \
00058     p_abort("\nError detected.\n");                                       \
00059   }                                                                           \
00060   cout << endl;                                                               \
00061 }
00062 
00063 int 
00064 elok(Listable *e, void *f)
00065 {
00066     return (((IntElem *) e)->val != 2);
00067 }
00068 
00069 main()
00070 {
00071     P_ASSERT_HANDLER(0);
00072 
00073     List<IntElem> intl;
00074 
00075     intl.ins_first(new IntElem(1));
00076     intl.ins_first(new IntElem(2));
00077     intl.ins_first(new IntElem(3));
00078     intl.ins_last(new IntElem(4));      /// ...  3 2 1 4
00079 
00080     intl.ins_first(new IntElem(2));
00081 
00082     cout << "expect 3 1 4 ==> ";
00083 
00084     Iterator<IntElem> rit(intl, elok, NULL);
00085 
00086     while (rit.valid()) {
00087         cout << rit.current() << " ";
00088         rit++;
00089     }
00090 
00091     cout << endl;
00092 
00093     intl.del(0);
00094 
00095     RefList<IntElem>rlist;
00096 
00097     rlist.ins(intl[2], 0);
00098     rlist.ins(intl[1], 1);
00099     rlist.ins_last(intl[3]);
00100     rlist.ins_first(intl[2]);   /// ...  1 1 2 4
00101     IntElem & foo = rlist.grab(1); /// ...  1 2 4
00102 
00103     EXPECT(rlist, "[1,2,4]");
00104 
00105     /// ...  Put reference back
00106     rlist.ins(foo, 1);   /// ...  1 1 2 4
00107     rlist.ins_before(intl[0], intl[3]); /// ...  1 1 2 3 4
00108 
00109     rlist.ins_after(intl[1], intl[0]);  /// ...  1 1 2 3 2 4
00110 
00111     EXPECT(rlist, "[1,1,2,3,2,4]");
00112 
00113     rlist.del(rlist[0]);
00114     rlist.del(rlist[4]);        /// ...  1 2 3 2
00115 
00116     EXPECT(rlist, "[1, 2, 3, 2]");
00117 
00118     rlist.del(rlist[1], 2);     /// ...  1 2 3
00119 
00120     EXPECT(rlist, "[1, 2, 3]");
00121 
00122     rlist.del(2);
00123     rlist.del(0);
00124     rlist.del(0);
00125 
00126     EXPECT(rlist, "[]");
00127 
00128     rlist.ins(intl[2], 0);
00129     rlist.ins(intl[1], 1);
00130     rlist.ins_last(intl[3]);
00131     rlist.ins_first(intl[2]);   /// ...  1 1 2 4
00132 
00133     rlist.ins_before(intl[0], intl[3]); /// ...  1 1 2 3 4
00134 
00135     rlist.ins_after(intl[1], intl[0]);  /// ...  1 1 2 3 2 4
00136 
00137     EXPECT(rlist, "[1,1,2,3,2,4]");
00138 
00139     rlist.modify(4, rlist[3]);  /// ...  1 1 2 3 3 4
00140 
00141     EXPECT(rlist, "[1,1,2,3,3,4]");
00142 
00143     IntElem         x(1);
00144 
00145     rit = rlist;
00146     while (rit.valid()) {
00147         cout << rit.current() << " is in loc " << rlist.index(rit.current()) << endl;
00148         rit++;
00149     }
00150 
00151     cout << "x is in loc " << rlist.index(x) << endl;
00152 
00153     rlist.del(2);
00154 
00155     RefList<IntElem>refs = rlist;
00156 
00157     rlist.clear();
00158 
00159     EXPECT(refs, "[1,1,3,3,4]");
00160     EXPECT(rlist, "[]");
00161 
00162     IntElem        *old = intl.grab(0);
00163 
00164     /// ...  EXPECT(refs, "[1,1,4]"); -- structures_OK objects to non_valid els
00165 
00166     cout << "expect [1,1,*,*,4] => [";
00167 
00168     Iterator<IntElem> rghost(refs);
00169 
00170     while (rghost.valid()) {
00171         if (rghost.current_valid())
00172             cout << rghost.current() << ",";
00173         else
00174             cout << "*,";
00175         rghost++;
00176     }
00177     cout << "]" << endl;
00178 
00179     refs.ins(intl[2], 3);
00180 
00181     rghost.reset();
00182 
00183     cout << "expect 1,1,*,4,*,4 => ";
00184 
00185     while (rghost.valid()) {
00186         if (rghost.current_valid())
00187             cout << rghost.current() << ",";
00188         else
00189             cout << "*,";
00190         rghost++;
00191     }
00192 
00193     cout << endl;
00194 
00195     cout << "Expect *,4,*,4 => ";
00196 
00197     Iterator<IntElem> sub(refs, old);
00198 
00199     while (sub.valid()) {
00200         if (sub.current_valid())
00201             cout << sub.current() << ",";
00202         else
00203             cout << "*,";
00204         sub++;
00205     }
00206 
00207     cout << endl;
00208 
00209     refs.del(*old);
00210 
00211     rghost.reset();
00212 
00213     cout << "expect 1,1,4,*,4 => ";
00214 
00215     while (rghost.valid()) {
00216         if (rghost.current_valid())
00217             cout << rghost.current() << ",";
00218         else
00219             cout << "*,";
00220         rghost++;
00221     }
00222 
00223     cout << endl;
00224 
00225     cout << "ghost at loc " << refs.index(*old) << endl;
00226 
00227     refs.modify(3, intl[0]);
00228 
00229     rghost.reset();
00230 
00231     cout << "expect 1,1,4,2,4 => ";
00232 
00233     while (rghost.valid()) {
00234         if (rghost.current_valid())
00235             cout << rghost.current() << ",";
00236         else
00237             cout << "*,";
00238         rghost++;
00239     }
00240 
00241     cout << endl;
00242 
00243     rghost = intl;
00244 
00245     intl.clear();
00246 
00247     cout << "expect *,*,* => ";
00248 
00249     while (rghost.valid()) {
00250         if (rghost.current_valid())
00251             cout << rghost.current() << ",";
00252         else
00253             cout << "*,";
00254         rghost++;
00255     }
00256 
00257     cout << endl;
00258 
00259     rghost = refs;
00260 
00261     cout << "expect *,*,*,*,* => ";
00262 
00263     while (rghost.valid()) {
00264         if (rghost.current_valid())
00265             cout << rghost.current() << ",";
00266         else
00267             cout << "*,";
00268         rghost++;
00269     }
00270     cout << endl;
00271 
00272     refs.clear();
00273 
00274     cout << "expect an error...\n";
00275 
00276     refs.ins(x, 3);
00277 }
 © 1995-2005 University of Illinois, Urbana-Champaign. All rights reserved.  Fri Mar 25 23:06:05 2005