00001
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 Test take() {
00107 My list( 0, 1024 );
00108 assert_eq( list::count( list ), 1024 );
00109 assert_eq( list::count( list::take( 50, list ) ), 50 );
00110 }
00111
00112 Test unique() {
00113 My2 list( 0, 20, 3 );
00114 assert_eq( list::count( list ), 80 );
00115 assert_eq( list::count( list::unique( list ) ), 20 );
00116 assert_eq( list::unique( list ).head(), 0 );
00117 assert_eq( list::unique( list ).tail().head(), 1 );
00118 }
00119
00120 Test stl() {
00121 My list( 0, 1024 );
00122 std::vector< int > vec;
00123 std::copy( list::begin( list ), list::end( list ),
00124 std::back_inserter( vec ) );
00125 for ( int i = 0; i < 1024; ++i )
00126 assert_eq( vec[i], i );
00127 }
00128
00129 static int mul2add1( int a ) {
00130 return a * 2 + 1;
00131 }
00132
00133 Test map() {
00134 My list( 0, 512 );
00135 checkOddList(
00136 list::map( list, std::ptr_fun( mul2add1 ) ) );
00137 }
00138
00139 Test empty() {
00140 assert( list::Empty< int >().empty() );
00141 }
00142
00143 Test single() {
00144 assert_eq( list::singular( 0 ).head(), 0 );
00145 assert( list::singular( 0 ).tail().empty() );
00146 }
00147
00148 Test append() {
00149 assert_eq( list::append( list::singular( 0 ),
00150 list::singular( 1 ) ).head(), 0 );
00151 assert_eq( list::append( list::singular( 0 ),
00152 list::singular( 1 ) ).tail().head(), 1 );
00153 assert( list::append( list::singular( 0 ),
00154 list::singular( 1 ) ).tail().tail().empty() );
00155 }
00156
00157 Test appendCount() {
00158 assert_eq( list::count( list::append( My( 0, 10 ),
00159 My2( 0, 5, 1 ) ) ), 20 );
00160 }
00161 };