range.test.h
Go to the documentation of this file.
// -*- C++ -*- (c) 2005, 2006, 2007 Petr Rockai <me@mornfall.net>
#include <wibble/range.h>
#include <wibble/operators.h>
#include <list>
#include <functional>
namespace {
using namespace wibble;
using namespace operators;
struct TestRange {
Test iteratorRange() {
std::list<int> a;
a.push_back( 10 );
a.push_back( 20 );
Range< int > r = range( a.begin(), a.end() );
Range< int >::iterator i = r.begin();
assert_eq( *i, 10 );
assert_eq( *(i + 1), 20 );
assert( i + 2 == r.end() );
}
Test copy() {
std::list<int> a;
a.push_back( 10 );
a.push_back( 20 );
Range< int > r = range( a.begin(), a.end() );
std::list<int> b;
assert( a != b );
std::copy( r.begin(), r.end(), back_inserter( b ) );
assert( a == b );
}
Test copyToConsumer() {
// std::vector< int > &vec = *new (GC) std::vector< int >;
std::vector< int > vec;
std::list< int > a;
a.push_back( 10 );
a.push_back( 20 );
Range< int > r = range( a.begin(), a.end() );
std::copy( r.begin(), r.end(), consumer( vec ) );
Range< int > r1 = range( vec );
assert_eq( *r1.begin(), 10 );
assert_eq( *(r1.begin() + 1), 20 );
assert( r1.begin() + 2 == r1.end() );
while ( !r.empty() ) {
assert_eq( r.head(), r1.head() );
r = r.tail();
r1 = r1.tail();
}
assert( r1.empty() );
}
Test _filteredRange() {
std::vector< int > v;
std::list<int> a;
a.push_back( 10 );
a.push_back( 20 );
Range< int > r = range( a.begin(), a.end() );
r.output( consumer( v ) );
Range<int> fr =
filteredRange( range( v ),
std::bind1st( std::equal_to< int >(), 10 ) );
assert_eq( fr.head(), 10 );
fr = fr.tail();
assert( fr.empty() );
}
Test sort() {
std::vector< int > v;
std::list<int> a;
a.push_back( 20 );
a.push_back( 10 );
a.push_back( 30 );
Range< int > r = range( a.begin(), a.end() );
r.output( consumer( v ) );
std::sort( v.begin(), v.end() );
assert_eq( *(v.begin()), 10 );
assert_eq( *(v.begin() + 1), 20 );
assert_eq( *(v.begin() + 2), 30 );
assert( v.begin() + 3 == v.end() );
}
Test assignment() {
std::vector< int > vec;
Range< int > a;
a = range( vec );
assert( a.empty() );
vec.push_back( 4 );
Range< int > b = range( vec );
assert_eq( b.head(), 4 );
a = b;
assert( !a.empty() );
assert_eq( a.head(), 4 );
}
Test _transformedRange() {
Range< int > a;
std::vector< int > xv;
Consumer< int > x = consumer( xv );
x.consume( 4 );
x.consume( 8 );
a = transformedRange( range( xv ),
std::bind1st( std::plus< int >(), 2 ) );
assert_eq( a.head(), 6 );
a.removeFirst();
assert_eq( a.head(), 10 );
a.removeFirst();
assert( a.empty() );
}
Test _transformedRange2() {
Range< int > a;
std::vector< unsigned > xv;
Consumer< unsigned > x = consumer( xv );
x.consume( 4 );
x.consume( 8 );
a = transformedRange(
range( xv ), std::bind1st( std::plus< int >(), 2 ) );
assert_eq( a.head(), 6 );
a.removeFirst();
assert_eq( a.head(), 10 );
a.removeFirst();
assert( a.empty() );
}
Test tailOfIteratorRange() {
std::vector<int> a;
a.insert( a.begin(), 30 );
a.insert( a.begin(), 10 );
a.insert( a.begin(), 20 );
Range< int > r = range( a.begin(), a.end() );
assert_eq( r.head(), 20 );
r = r.tail();
assert_eq( r.head(), 10 );
r = r.tail();
assert_eq( r.head(), 30 );
r = r.tail();
assert( r.empty() );
}
Test _castedRange()
{
std::vector<int> a;
a.insert( a.begin(), 30 );
a.insert( a.begin(), 10 );
a.insert( a.begin(), 20 );
Range< unsigned > r = castedRange< unsigned >(
range( a.begin(), a.end() ) );
assert_eq( r.head(), 20u );
r = r.tail();
assert_eq( r.head(), 10u );
r = r.tail();
assert_eq( r.head(), 30u );
r = r.tail();
assert( r.empty() );
}
static void removeFirst( int &i ) {
++i;
}
static bool isEnd( const int &i ) {
return i >= 5;
}
Test _generatedRange() {
Range< int > r = generatedRange( 0, removeFirst, isEnd );
assert( !r.empty() );
assert_eq( *(r.begin() + 0), 0 );
assert_eq( *(r.begin() + 1), 1 );
assert_eq( *(r.begin() + 2), 2 );
assert_eq( *(r.begin() + 3), 3 );
assert_eq( *(r.begin() + 4), 4 );
assert( (r.begin() + 5) == r.end() );
}
};
}
wibble::list::sort
Sorted< List > sort(List l)
Definition: list.h:387
wibble
Definition: amorph.h:17
wibble::range
Range< typename In::value_type > range(In b, In e)
Definition: range.h:264
wibble::generatedRange
GeneratedRange< T, A, E > generatedRange(T t, A a, E e)
Definition: range.h:494
wibble::transformedRange
TransformedRange< Trans > transformedRange(Range< typename Trans::argument_type > r, Trans t)
Definition: range.h:451
wibble::filteredRange
FilteredRange< R, Pred > filteredRange(R r, Pred p)
Definition: range.h:377
wibble::consumer
Consumer< typename Out::container_type::value_type > consumer(Out out)
Definition: consumer.h:108
operators.h
range.h
-*- C++ -*-
wibble::Consumer
Definition: consumer.h:67
wibble::Consumer::consume
void consume(const T &a)
Definition: consumer.h:78
wibble::RangeMixin::empty
bool empty() const
Definition: range.h:82
wibble::RangeMixin::output
void output(Consumer< T > t) const
Definition: range.h:78
wibble::RangeMixin::begin
iterator begin() const
Definition: range.h:70
wibble::RangeMixin::end
iterator end() const
Definition: range.h:71
wibble::RangeMixin::tail
Self tail() const
Definition: range.h:75
wibble::Range
Definition: range.h:155
wibble::Range::removeFirst
void removeFirst()
Definition: range.h:164
wibble::Range::head
T head() const
Definition: range.h:163
Test
void Test
Definition: test.h:178
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30