set.test.cc

Go to the documentation of this file.
00001 /*
00002    The lestes compiler suite
00003    Copyright (C) 2002, 2003, 2004, 2005 Miroslav Tichy
00004    Copyright (C) 2002, 2003, 2004, 2005 Petr Zika
00005    Copyright (C) 2002, 2003, 2004, 2005 Vojtech Hala
00006    Copyright (C) 2002, 2003, 2004, 2005 Jiri Kosina
00007    Copyright (C) 2002, 2003, 2004, 2005 Pavel Sanda
00008    Copyright (C) 2002, 2003, 2004, 2005 Jan Zouhar
00009    Copyright (C) 2002, 2003, 2004, 2005 Rudolf Thomas
00010 
00011    This program is free software; you can redistribute it and/or modify
00012    it under the terms of the GNU General Public License as published by
00013    the Free Software Foundation; version 2 of the License.
00014 
00015    This program is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018    GNU General Public License for more details.
00019 
00020    See the full text of the GNU General Public License version 2, and
00021    the limitations in the file doc/LICENSE.
00022 
00023    By accepting the license the licensee waives any and all claims
00024    against the copyright holder(s) related in whole or in part to the
00025    work, its use, and/or the inability to use it.
00026  
00027  */
00028 #include <lestes/common.hh>
00029 #include <lestes/std/set.hh>
00030 
00031 #include <algorithm>
00032 
00033 package(lestes);
00034 package(std);
00035 
00036 struct integer : public object {
00037         int value;
00038         static ptr<integer> create( int i )
00039         {
00040                 integer * result = new integer();
00041                 result->value = i;
00042                 return result;
00043         }
00044 };
00045 struct smart_int : public object {
00046         int value;
00047         bool operator < ( const smart_int &si )
00048         {
00049                 return value < si.value;
00050         }
00051         static ptr<smart_int> create( int i )
00052         {
00053                 smart_int * result = new smart_int();
00054                 result->value = i;
00055                 return result;
00056         }
00057 };
00058 
00059 template<typename T>
00060 struct test_less {
00061         bool operator()( T x, T y )
00062         {
00063                 return x < y;
00064         }
00065 };
00066 
00067 template<>
00068 struct test_less< srp<integer> > {
00069         bool operator()( srp<integer> x, srp<integer> y )
00070         {
00071                 return x->value > y->value;
00072         }
00073 };
00074 
00075 template<typename T>
00076 struct deref_less {
00077         bool operator()( ptr<T> x, ptr<T> y )
00078         {
00079                 return *x < *y;
00080         }
00081 };
00082 
00083 template<typename T>
00084 struct deref_eqeq {
00085         bool operator()( ptr<T> x, ptr<T> y )
00086         {
00087                 return x->value == y->value;
00088         }
00089 };
00090 
00091 template class set< int >;
00092 template class set< int, test_less<int> >;
00093 template class set< srp<integer> >;
00094 template class set< srp<integer>, deref_less<integer> >;
00095 
00096 typedef set< int >                                      set_i;
00097 typedef set< int,		test_less<int> >     set_ic;
00098 typedef set< srp<integer> >                             set_p;
00099 typedef set< srp<smart_int>,    deref_less<smart_int> > set_pc;
00100 
00101 int set_test()
00102 {
00103         ptr<set_i> s1 = set_i::create();
00104         ptr<set_i> s2 = set_i::create();
00105         s1->insert(0);
00106         s2->insert(0);
00107         lassert( *s1 == *s2 );
00108         s2->insert(1);
00109         lassert( *s1 != *s2 );
00110 
00111         ptr<set_pc> x1 = set_pc::create();
00112         ptr<set_pc> x2 = set_pc::create();
00113 
00114         ptr<smart_int> p = smart_int::create(1);
00115 
00116         x1->insert( p );
00117         x2->insert( p );
00118         lassert( *x1 == *x2 );
00119 
00120         x1->insert( smart_int::create(2) );
00121         x2->insert( smart_int::create(2) );
00122         lassert( x1->size() == x2->size() );
00123         deref_eqeq<smart_int> comparator;
00124         lassert( equal( x1->begin(), x1->end(), x2->begin(), comparator ) );
00125 
00126         x1->insert( smart_int::create(3) );
00127         x2->insert( smart_int::create(4) );
00128         lassert( x1->size() == x2->size() );
00129         lassert( !equal( x1->begin(), x1->end(), x2->begin(), comparator ) );
00130 
00131         ptr<set_pc> x3 = set_pc::create(x2);
00132         lassert( equal( x2->begin(), x2->end(), x3->begin(), comparator ) );
00133 
00134         return 0;
00135 }
00136 
00137 end_package(std);
00138 end_package(lestes);
00139 
00140 extern "C"
00141 int main()
00142 {
00143         return lestes::std::set_test();
00144 }

Generated on Mon Feb 12 18:23:15 2007 for lestes by doxygen 1.5.1-20070107