wibble 0.1.28
list.test.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 #include <wibble/list.h>
00004 #include <wibble/test.h>
00005 
00006 using namespace wibble;
00007 
00008 struct TestList {
00009     struct My {
00010         typedef int Type;
00011         int i, max;
00012         int head() const { return i; }
00013 
00014         My tail() const {
00015             My t = *this;
00016             if ( i < max )
00017                 t.i ++;
00018             if ( i > max )
00019                 t.i --;
00020             return t;
00021         }
00022 
00023         bool empty() const { return i == max; }
00024 
00025         My( int j = 0, int m = 0 ) : i( j ), max( m ) {}
00026     };
00027 
00028     struct My2 {
00029         typedef int Type;
00030         int i, max, rep, repmax;
00031         int head() const { return i; }
00032 
00033         My2 tail() const {
00034             My2 t = *this;
00035             if ( rep > 0 )
00036                 t.rep --;
00037             else {
00038                 t.rep = repmax;
00039                 if ( i < max )
00040                     t.i ++;
00041                 if ( i > max )
00042                     t.i --;
00043             }
00044             return t;
00045         }
00046 
00047         bool empty() const { return i == max; }
00048 
00049         My2( int j = 0, int m = 0, int r = 0 ) : i( j ), max( m ),
00050                                                  rep( r ), repmax( r ) {}
00051     };
00052 
00053     static bool odd( int i ) {
00054         return i % 2 == 1;
00055     }
00056 
00057     template< typename List >
00058     void checkOddList( List l ) {
00059         int i = 0;
00060         while ( !l.empty() ) {
00061             assert( odd( l.head() ) );
00062             l = l.tail();
00063             ++ i;
00064         }
00065         assert_eq( i, 512 );
00066     }
00067 
00068     template< typename List >
00069     void checkListSorted( List l )
00070     {
00071         if ( l.empty() )
00072             return;
00073         typename List::Type last = l.head();
00074         while ( !l.empty() ) {
00075             assert( last <= l.head() );
00076             last = l.head();
00077             l = l.tail();
00078         }
00079     }
00080 
00081     Test count() {
00082         My list( 512, 1024 );
00083         assert_eq( list::count( list ), 512u );
00084         list = My( 0, 1024 );
00085         assert_eq( list::count( list ), 1024u );
00086     }
00087 
00088     Test filtered() {
00089         My list( 1, 1024 );
00090         checkOddList( list::filter( list, odd ) );
00091         assert_eq( list::count( list::filter( list, odd ) ), 512 );
00092     }
00093 
00094     Test sorted() {
00095         My list( 1, 1024 );
00096         assert_eq( list::count( list ), list::count( list::sort( list ) ) );
00097         checkListSorted( list );
00098         checkListSorted( list::sort( list ) );
00099         {
00100             ExpectFailure fail;
00101             checkListSorted( My( 100, 0 ) );
00102         }
00103         checkListSorted( list::sort( My( 100, 0 ) ) );
00104     }
00105 
00106 #if 0
00107 #warning Disabled until mornfall fixes it
00108     T-est take() {
00109         My list( 0, 1024 );
00110         assert_eq( list::count( list ), 1024 );
00111         assert_eq( list::count( list::take( 50, list ) ), 50 );
00112     }
00113 #endif
00114 
00115     Test unique() {
00116         My2 list( 0, 20, 3 );
00117         assert_eq( list::count( list ), 80 );
00118         assert_eq( list::count( list::unique( list ) ), 20 );
00119         assert_eq( list::unique( list ).head(), 0 );
00120         assert_eq( list::unique( list ).tail().head(), 1 );
00121     }
00122 
00123     Test stl() {
00124         My list( 0, 1024 );
00125         std::vector< int > vec;
00126         std::copy( list::begin( list ), list::end( list ),
00127                    std::back_inserter( vec ) ); 
00128         for ( int i = 0; i < 1024; ++i )
00129             assert_eq( vec[i], i );
00130     }
00131 
00132     static int mul2add1( int a ) {
00133         return a * 2 + 1;
00134     }
00135 
00136 #if 0
00137 #warning Disabled until mornfall fixes it
00138     T-est map() {
00139         My list( 0, 512 );
00140         checkOddList( 
00141             list::map( list, std::ptr_fun( mul2add1 ) ) );
00142     }
00143 #endif
00144 
00145     Test empty() {
00146         assert( list::Empty< int >().empty() );
00147     }
00148 
00149     Test single() {
00150         assert_eq( list::singular( 0 ).head(), 0 );
00151         assert( list::singular( 0 ).tail().empty() );
00152     }
00153 
00154     Test append() {
00155         assert_eq( list::append( list::singular( 0 ),
00156                                  list::singular( 1 ) ).head(), 0 );
00157         assert_eq( list::append( list::singular( 0 ),
00158                                  list::singular( 1 ) ).tail().head(), 1 );
00159         assert( list::append( list::singular( 0 ),
00160                               list::singular( 1 ) ).tail().tail().empty() );
00161     }
00162 
00163     Test appendCount() {
00164         assert_eq( list::count( list::append( My( 0, 10 ),
00165                                               My2( 0, 5, 1 ) ) ), 20 );
00166     }
00167 };