list.test.h
Go to the documentation of this file.
// -*- C++ -*-
#include <wibble/list.h>
#include <wibble/test.h>
using namespace wibble;
struct TestList {
struct My {
typedef int Type;
int i, max;
int head() const { return i; }
My tail() const {
My t = *this;
if ( i < max )
t.i ++;
if ( i > max )
t.i --;
return t;
}
bool empty() const { return i == max; }
My( int j = 0, int m = 0 ) : i( j ), max( m ) {}
};
struct My2 {
typedef int Type;
int i, max, rep, repmax;
int head() const { return i; }
My2 tail() const {
My2 t = *this;
if ( rep > 0 )
t.rep --;
else {
t.rep = repmax;
if ( i < max )
t.i ++;
if ( i > max )
t.i --;
}
return t;
}
bool empty() const { return i == max; }
My2( int j = 0, int m = 0, int r = 0 ) : i( j ), max( m ),
rep( r ), repmax( r ) {}
};
static bool odd( int i ) {
return i % 2 == 1;
}
template< typename List >
void checkOddList( List l ) {
int i = 0;
while ( !l.empty() ) {
assert( odd( l.head() ) );
l = l.tail();
++ i;
}
assert_eq( i, 512 );
}
template< typename List >
void checkListSorted( List l )
{
if ( l.empty() )
return;
typename List::Type last = l.head();
while ( !l.empty() ) {
assert( last <= l.head() );
last = l.head();
l = l.tail();
}
}
Test count() {
My list( 512, 1024 );
assert_eq( list::count( list ), 512u );
list = My( 0, 1024 );
assert_eq( list::count( list ), 1024u );
}
Test filtered() {
My list( 1, 1024 );
checkOddList( list::filter( list, odd ) );
assert_eq( list::count( list::filter( list, odd ) ), 512 );
}
Test sorted() {
My list( 1, 1024 );
assert_eq( list::count( list ), list::count( list::sort( list ) ) );
checkListSorted( list );
checkListSorted( list::sort( list ) );
{
ExpectFailure fail;
checkListSorted( My( 100, 0 ) );
}
checkListSorted( list::sort( My( 100, 0 ) ) );
}
#if 0
#warning Disabled until mornfall fixes it
T-est take() {
My list( 0, 1024 );
assert_eq( list::count( list ), 1024 );
assert_eq( list::count( list::take( 50, list ) ), 50 );
}
#endif
Test unique() {
My2 list( 0, 20, 3 );
assert_eq( list::count( list ), 80 );
assert_eq( list::count( list::unique( list ) ), 20 );
assert_eq( list::unique( list ).head(), 0 );
assert_eq( list::unique( list ).tail().head(), 1 );
}
Test stl() {
My list( 0, 1024 );
std::vector< int > vec;
std::copy( list::begin( list ), list::end( list ),
std::back_inserter( vec ) );
for ( int i = 0; i < 1024; ++i )
assert_eq( vec[i], i );
}
static int mul2add1( int a ) {
return a * 2 + 1;
}
#if 0
#warning Disabled until mornfall fixes it
T-est map() {
My list( 0, 512 );
checkOddList(
list::map( list, std::ptr_fun( mul2add1 ) ) );
}
#endif
Test empty() {
assert( list::Empty< int >().empty() );
}
Test single() {
assert_eq( list::singular( 0 ).head(), 0 );
assert( list::singular( 0 ).tail().empty() );
}
Test append() {
assert_eq( list::append( list::singular( 0 ),
list::singular( 1 ) ).head(), 0 );
assert_eq( list::append( list::singular( 0 ),
list::singular( 1 ) ).tail().head(), 1 );
assert( list::append( list::singular( 0 ),
list::singular( 1 ) ).tail().tail().empty() );
}
Test appendCount() {
assert_eq( list::count( list::append( My( 0, 10 ),
My2( 0, 5, 1 ) ) ), 20 );
}
};
TestList::mul2add1
static int mul2add1(int a)
Definition: list.test.h:132
wibble::list::take
Take< List > take(int t, List l)
Definition: list.h:399
TestList
Definition: list.test.h:8
TestList::single
Test single()
Definition: list.test.h:149
Test
void Test
Definition: test.h:178
TestList::My2::head
int head() const
Definition: list.test.h:31
TestList::filtered
Test filtered()
Definition: list.test.h:88
TestList::unique
Test unique()
Definition: list.test.h:115
test.h
TestList::My2::empty
bool empty() const
Definition: list.test.h:47
TestList::My2
Definition: list.test.h:28
wibble::ExpectFailure
Definition: test.h:171
TestList::My::max
int max
Definition: list.test.h:11
TestList::stl
Test stl()
Definition: list.test.h:123
TestList::My::Type
int Type
Definition: list.test.h:10
wibble::list::begin
ListIterator< List > begin(List l)
Definition: list.h:420
TestList::My2::repmax
int repmax
Definition: list.test.h:30
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30
wibble::list::append
Append< X, Y > append(const X &x, const Y &y)
Definition: list.h:336
TestList::My
Definition: list.test.h:9
wibble::list::filter
Filtered< List, Pred > filter(List l, Pred p)
Definition: list.h:369
wibble::list::count
size_t count(List l)
Definition: list.h:341
TestList::count
Test count()
Definition: list.test.h:81
TestList::append
Test append()
Definition: list.test.h:154
wibble::list::unique
Unique< List > unique(List l)
Definition: list.h:393
list.h
TestList::My::My
My(int j=0, int m=0)
Definition: list.test.h:25
TestList::My2::i
int i
Definition: list.test.h:30
TestList::My::i
int i
Definition: list.test.h:11
TestList::empty
Test empty()
Definition: list.test.h:145
TestList::odd
static bool odd(int i)
Definition: list.test.h:53
TestList::My2::Type
int Type
Definition: list.test.h:29
TestList::My2::My2
My2(int j=0, int m=0, int r=0)
Definition: list.test.h:49
TestList::My::empty
bool empty() const
Definition: list.test.h:23
TestList::My2::rep
int rep
Definition: list.test.h:30
TestList::sorted
Test sorted()
Definition: list.test.h:94
wibble::list::singular
Singular< X > singular(const X &x)
Definition: list.h:331
wibble::list::Empty
Definition: list.h:284
wibble
Definition: amorph.h:17
TestList::appendCount
Test appendCount()
Definition: list.test.h:163
TestList::My::head
int head() const
Definition: list.test.h:12
TestList::My2::tail
My2 tail() const
Definition: list.test.h:33
wibble::list::map
Map< List, F > map(const List &l, const F &f)
Definition: list.h:381
TestList::checkOddList
void checkOddList(List l)
Definition: list.test.h:58
TestList::My::tail
My tail() const
Definition: list.test.h:14
wibble::list::sort
Sorted< List > sort(List l)
Definition: list.h:387
wibble::list::end
ListIterator< List > end(List)
Definition: list.h:425
TestList::checkListSorted
void checkListSorted(List l)
Definition: list.test.h:69