wibble  1.1
list.test.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #include <wibble/list.h>
4 #include <wibble/test.h>
5 
6 using namespace wibble;
7 
8 struct TestList {
9  struct My {
10  typedef int Type;
11  int i, max;
12  int head() const { return i; }
13 
14  My tail() const {
15  My t = *this;
16  if ( i < max )
17  t.i ++;
18  if ( i > max )
19  t.i --;
20  return t;
21  }
22 
23  bool empty() const { return i == max; }
24 
25  My( int j = 0, int m = 0 ) : i( j ), max( m ) {}
26  };
27 
28  struct My2 {
29  typedef int Type;
30  int i, max, rep, repmax;
31  int head() const { return i; }
32 
33  My2 tail() const {
34  My2 t = *this;
35  if ( rep > 0 )
36  t.rep --;
37  else {
38  t.rep = repmax;
39  if ( i < max )
40  t.i ++;
41  if ( i > max )
42  t.i --;
43  }
44  return t;
45  }
46 
47  bool empty() const { return i == max; }
48 
49  My2( int j = 0, int m = 0, int r = 0 ) : i( j ), max( m ),
50  rep( r ), repmax( r ) {}
51  };
52 
53  static bool odd( int i ) {
54  return i % 2 == 1;
55  }
56 
57  template< typename List >
58  void checkOddList( List l ) {
59  int i = 0;
60  while ( !l.empty() ) {
61  assert( odd( l.head() ) );
62  l = l.tail();
63  ++ i;
64  }
65  assert_eq( i, 512 );
66  }
67 
68  template< typename List >
69  void checkListSorted( List l )
70  {
71  if ( l.empty() )
72  return;
73  typename List::Type last = l.head();
74  while ( !l.empty() ) {
75  assert( last <= l.head() );
76  last = l.head();
77  l = l.tail();
78  }
79  }
80 
81  Test count() {
82  My list( 512, 1024 );
83  assert_eq( list::count( list ), 512u );
84  list = My( 0, 1024 );
85  assert_eq( list::count( list ), 1024u );
86  }
87 
89  My list( 1, 1024 );
90  checkOddList( list::filter( list, odd ) );
91  assert_eq( list::count( list::filter( list, odd ) ), 512 );
92  }
93 
95  My list( 1, 1024 );
96  assert_eq( list::count( list ), list::count( list::sort( list ) ) );
97  checkListSorted( list );
98  checkListSorted( list::sort( list ) );
99  {
100  ExpectFailure fail;
101  checkListSorted( My( 100, 0 ) );
102  }
103  checkListSorted( list::sort( My( 100, 0 ) ) );
104  }
105 
106 #if 0
107 #warning Disabled until mornfall fixes it
108  T-est take() {
109  My list( 0, 1024 );
110  assert_eq( list::count( list ), 1024 );
111  assert_eq( list::count( list::take( 50, list ) ), 50 );
112  }
113 #endif
114 
116  My2 list( 0, 20, 3 );
117  assert_eq( list::count( list ), 80 );
118  assert_eq( list::count( list::unique( list ) ), 20 );
119  assert_eq( list::unique( list ).head(), 0 );
120  assert_eq( list::unique( list ).tail().head(), 1 );
121  }
122 
123  Test stl() {
124  My list( 0, 1024 );
125  std::vector< int > vec;
126  std::copy( list::begin( list ), list::end( list ),
127  std::back_inserter( vec ) );
128  for ( int i = 0; i < 1024; ++i )
129  assert_eq( vec[i], i );
130  }
131 
132  static int mul2add1( int a ) {
133  return a * 2 + 1;
134  }
135 
136 #if 0
137 #warning Disabled until mornfall fixes it
138  T-est map() {
139  My list( 0, 512 );
140  checkOddList(
141  list::map( list, std::ptr_fun( mul2add1 ) ) );
142  }
143 #endif
144 
146  assert( list::Empty< int >().empty() );
147  }
148 
150  assert_eq( list::singular( 0 ).head(), 0 );
151  assert( list::singular( 0 ).tail().empty() );
152  }
153 
156  list::singular( 1 ) ).head(), 0 );
158  list::singular( 1 ) ).tail().head(), 1 );
160  list::singular( 1 ) ).tail().tail().empty() );
161  }
162 
164  assert_eq( list::count( list::append( My( 0, 10 ),
165  My2( 0, 5, 1 ) ) ), 20 );
166  }
167 };