Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestIntSeq.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sun Jan  2 18:26:01 EST 2005 by ken@xorian.net
00020 //      modified on Mon May  3 16:04:41 PDT 1999 by heydon
00021 //      modified on Sat Feb 17 11:29:26 PST 1996 by mcjones
00022 
00023 /* Based on /proj/m3/pkg/sequence/tests/src/Test.m3 as
00024    Last modified on Tue Nov  1 09:10:02 PST 1994 by kalsow */
00025 
00026 #include <limits.h>
00027 #include <stdlib.h>
00028 #include <Basics.H>
00029 #include "Sequence.H"
00030 
00031 #include <algorithm> // STL algorithms package
00032 
00033 // int -> int sequence
00034 typedef Sequence<int> T;
00035 
00036 using std::cout;
00037 using std::endl;
00038 
00039 bool Eq(const T& s, const T& t)
00040 {
00041     const int n = s.size();
00042     if( n != t.size() ) return false;
00043     for( int i=0; i<n; i++ )
00044       if( s.get(i) != t.get(i) ) return false;
00045     return true;
00046 }
00047 
00048 T Iota(int n)
00049 {
00050     T res(n);
00051     for( int i=0; i<n; i++ )
00052       res.addhi(i);
00053     return res;
00054 }
00055 
00056 T Rev(const T& s)
00057 {
00058     const int n = s.size();
00059     T res(n);
00060     for( int i=0; i<n; i++ )
00061       res.addlo(s.get(i));
00062     return res;
00063 }
00064 
00065 T Sort(const T &s)
00066 {
00067   T res(s);
00068   std::sort(res.begin(), res.end());
00069   return res;
00070 }
00071 
00072 T Rev_Iter(T &s)
00073 {
00074   T res(s.size());
00075   for(T::Iterator it = s.begin(); it != s.end(); ++it)
00076     res.addlo(*it);
00077   return res;
00078 }
00079 
00080 int main() {
00081 
00082     typedef Sequence<int> T;
00083 
00084     T empty(0);
00085     int i;
00086 
00087     T iota10 = Iota(10);
00088     T revIota10 = Rev(iota10);
00089     T revRevIota10 = Rev(revIota10);
00090     T iota10a = Iota(10);
00091     assert ( Eq( revRevIota10, iota10a) );
00092     /* assert ( Eq(Rev(Rev(Iota(10))), Iota(10)) ); */
00093     assert (! Eq(Iota(5), Iota(0)));
00094     assert (! Eq(Iota(5), Rev(Iota(5))));
00095 
00096     T s1 = Iota(5);
00097     for( i=4; 0<=i; i-- ) assert( s1.remhi() == i );
00098     assert( Eq( s1, empty ) );
00099 
00100     T s2 = Iota(100);
00101     assert( s2.size() == 100 );
00102     for( i=0; i<100; i++ ) assert( s2.remlo() == i );
00103     assert( Eq( s2, empty ) );
00104 
00105     assert( Iota(12).gethi() == 11 );
00106 
00107     assert( Iota(12).getlo() == 0 );
00108 
00109     T s3 = Iota(10);
00110     for( i=0; i<10; i++ ) s3.put(i, i);
00111     assert( Eq( s3, Iota(10) ) );
00112 
00113     int *arNone = 0;
00114 
00115     T s4(arNone, 0);
00116     s4.addhi(0); s4.addhi(1); s4.addhi(2);
00117     assert( Eq( Iota(3), s4 ) );
00118 
00119     int arIota5[] = {0, 1, 2, 3, 4};
00120     T sIota5(arIota5, 5);
00121     assert( Eq( Iota(5), sIota5 ) );
00122 
00123     T s5 = empty.cat(empty);
00124     s5.addhi(0); s5.addhi(1); s5.addhi(2);
00125     assert( Eq( Iota(3), s5 ) );
00126 
00127 
00128     assert( Eq( Iota(5), Iota(5).cat( Iota(0) ) ) );
00129     assert( Eq( Iota(5), Iota(0).cat( Iota(5) ) ) );
00130 
00131     T s6 = Iota(5).cat( Rev(Iota(5)) );
00132     for( i=0; i<5; i++ )
00133     {
00134         assert( s6.get(i) == i );
00135         assert( s6.get(i + 5) == 4 - i );
00136     }
00137 
00138     int ar34[] = {3, 4};
00139     T s34(ar34, 2);
00140     assert( Eq( Iota(5).sub(3), s34 ) );
00141 
00142     assert( Eq( Iota(5).sub(0), Iota(5) ) );
00143     assert( Eq( Iota(5).sub(5, 5), Iota(0) ) );
00144     assert( Eq( Iota(5).sub(0, 4), Iota(4) ) );
00145     assert( Eq( Rev(Iota(5)).sub(1, 4), Rev(Iota(4)) ) );
00146 
00147     T s7(5);
00148     s7.addhi(0);
00149     s7.addhi(0);
00150     (void)s7.remlo();
00151     (void)s7.remlo();
00152     for( i=0; i<5; i++) s7.addhi(i);
00153     assert( Eq( s7, Iota(5) ) );
00154 
00155     // Try simple use of Sequence::Iterator
00156     assert( Eq( Rev_Iter(s7), Rev(s7)));
00157 
00158     // Try sorting a Sequence
00159     assert( Eq(Sort(Rev(Iota(20))), Iota(20)));
00160 
00161  /*   
00162     s7.~T();
00163     s34.~T();
00164     s6.~T();
00165     s5.~T();
00166     sIota5.~T();
00167     s4.~T();
00168     s3.~T();
00169     s2.~T();
00170     s1.~T();
00171     iota10a.~T();
00172     revRevIota10.~T();
00173     revIota10.~T();
00174     iota10.~T();
00175     empty.~T();
00176     
00177     abort();
00178 */
00179 
00180     cout << "All tests passed!" << endl;
00181     return 0;
00182 }

Generated on Mon May 8 00:48:30 2006 for Vesta by  doxygen 1.4.2