Lumiera  0.pre.03
»edit your freedom«
itertools-test.cpp
Go to the documentation of this file.
1 /*
2  IterTools(Test) - building combined and filtering iterators based on the Iterator tools
3 
4  Copyright (C)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
20 #include "lib/test/run.hpp"
21 #include "lib/test/test-helper.hpp"
22 #include "lib/util-foreach.hpp"
23 #include "lib/format-cout.hpp"
24 #include "lib/util.hpp"
25 
26 #include "lib/itertools.hpp"
27 
28 #include <boost/lexical_cast.hpp>
29 #include <vector>
30 
31 
32 
33 namespace lib {
34 namespace test{
35 
36  using ::Test;
37  using boost::lexical_cast;
38  using util::for_each;
39  using util::isnil;
40  using std::vector;
41 
42  using LERR_(ITER_EXHAUST);
43 
44 
45 
46  namespace { // Test data
47 
48  struct TestSource
49  {
50  vector<int> data_;
51 
52  TestSource(uint num)
53  {
54  while (num)
55  data_.push_back(num--);
56  }
57 
58  typedef vector<int>::iterator sourceIter;
60 
61  iterator begin() { return iterator(data_.begin(),data_.end()); }
62  iterator end() { return iterator(); }
63 
64  };
65 
66  } // (END) Test data
67 
68 
69 
70 
71 
72 
73 
74  /***************************************************************************/
81  class IterTools_test : public Test
82  {
83 
84  typedef TestSource::iterator Iter;
85 
86  uint NUM_ELMS{0};
87 
88 
89  virtual void
90  run (Arg arg)
91  {
92  NUM_ELMS = firstVal (arg, 10);
93 
94  TestSource source(NUM_ELMS);
95 
96  pullOut (source.begin());
97  verifyComparisons (source.begin());
98 
99 
100  buildFilterIterator (source.begin());
101  Iter ii (source.begin());
102  ++++++ii;
103  buildFilterIterator (ii);
104  verify_filterExtension();
105  verify_filterRepetitions();
106 
107  buildWrappedSingleElement();
108 
109  buildTransformingIterator (source.begin());
110 
111  verifyPullLast(source.begin());
112  }
113 
114 
115 
116  template<class IT>
117  void
118  pullOut (IT const& ii)
119  {
120  for (IT iter(ii) ; iter; ++iter )
121  cout << "::" << *iter;
122  cout << endl;
123  }
124 
125 
126 
127  static bool takeAll (int) { return true; }
128  static bool takeOdd (int i) { return 0 != i % 2; }
129  static bool takeEve (int i) { return 0 == i % 2; }
130  static bool takeTrd (int i) { return 0 == i % 3; }
131 
132  void
133  buildFilterIterator (Iter const& ii)
134  {
135  pullOut (filterIterator (ii, takeAll)); // note: using the convenient builder function
136  pullOut (filterIterator (ii, takeEve));
137  pullOut (filterIterator (ii, takeOdd));
138 
139  FilterIter<Iter> all (ii, takeAll);
140  FilterIter<Iter> odd (ii, takeOdd);
141  verifyComparisons (all);
142  verifyComparisons (odd);
143 
144  while (++all && ++odd)
145  CHECK (all != odd);
146 
147  while (++all) { }
148  CHECK (isnil (odd));
149  CHECK (all == odd);
150  }
151 
152 
164  void
166  {
167  typedef vector<uint64_t> Src;
168  typedef Src::iterator SrcIter;
169  typedef RangeIter<SrcIter> SeqIter;
170  typedef ExtensibleFilterIter<SeqIter> FilteredSeq;
171 
172  Src src;
173  for (uint i=0; i < 3*NUM_ELMS; ++i)
174  src.push_back(i);
175 
176  SeqIter completeSequence (src.begin(), src.end());
177  FilteredSeq filterIter (completeSequence, takeAll);
178 
179  CHECK (!isnil (filterIter));
180  CHECK (0 == *filterIter);
181  ++filterIter;
182  CHECK (1 == *filterIter);
183 
184  filterIter.andFilter(takeEve);
185  CHECK (!isnil (filterIter));
186  CHECK (2 == *filterIter);
187  ++filterIter;
188  CHECK (4 == *filterIter);
189 
190  // sharpen the condition...
191  filterIter.andFilter(takeTrd);
192  CHECK (!isnil (filterIter));
193  CHECK (6 == *filterIter); // divisible by two and by three
194  ++filterIter;
195  CHECK (12 == *filterIter);
196 
197  verifyComparisons (filterIter);
198  pullOut (filterIter);
199 
200  // adding a disjunctive clause actually weakens the filter...
201  filterIter = {completeSequence, takeTrd};
202  CHECK (!isnil (filterIter));
203  CHECK (0 == *filterIter);
204  ++filterIter;
205  CHECK (3 == *filterIter);
206 
207  filterIter.orFilter(takeEve);
208  CHECK (3 == *filterIter);
209  ++filterIter;
210  CHECK (4 == *filterIter);
211  ++filterIter;
212  CHECK (6 == *filterIter);
213  verifyComparisons (filterIter);
214 
215  // flip filter logic
216  filterIter.flipFilter();
217  CHECK (7 == *filterIter); // not even and not divisible by three
218  ++filterIter;
219  CHECK (11 == *filterIter);
220  ++filterIter;
221  CHECK (13 == *filterIter);
222 
223  verifyComparisons (filterIter);
224  pullOut (filterIter);
225  }
226 
227 
232  void
234  {
235  vector<uint> numberz;
236  for (uint i=0; i<NUM_ELMS; ++i)
237  {
238  uint n = 1 + rani (100);
239  do numberz.push_back(i);
240  while (--n);
241  }
242  CHECK (NUM_ELMS < numberz.size(), "no repetition in test data??");
243 
244  typedef vector<uint>::iterator SrcIter;
245  typedef RangeIter<SrcIter> SeqIter;
246  typedef FilterIter<SeqIter> FilteredSeq;
247 
248  SeqIter completeSequence (numberz.begin(), numberz.end());
249  FilteredSeq filtered = filterRepetitions (completeSequence);
250 
251  uint num=0;
252  for (; num<NUM_ELMS && !isnil(filtered);
253  ++num,
254  ++filtered
255  )
256  CHECK (num == *filtered);
257 
258  CHECK (num == NUM_ELMS && isnil(filtered));
259  }
260 
261 
262 
264  void
266  {
267  uint i{12};
268 
269  auto i1 = singleValIterator(12);
270  auto i2 = singleValIterator( i);
271  auto i3 = singleValIterator(&i);
272 
273  CHECK (not isnil(i1));
274  CHECK (not isnil(i2));
275  CHECK (not isnil(i3));
276  CHECK (12 == *i1);
277  CHECK (12 == *i2);
278  CHECK (12 == **i3);
279 
280  i = 23;
281  CHECK (12 == *i1);
282  CHECK (23 == *i2);
283  CHECK (23 == **i3);
284 
285  ++i1;
286  ++i2;
287  ++i3;
288  CHECK (isnil(i1));
289  CHECK (isnil(i2));
290  CHECK (isnil(i3));
291  VERIFY_ERROR (ITER_EXHAUST, *i1 );
292  VERIFY_ERROR (ITER_EXHAUST, *i2 );
293  VERIFY_ERROR (ITER_EXHAUST, *i3 );
294 
295  // assignable as any iterator..
296  i1 = singleValIterator(13);
297  CHECK (13 == *i1);
298 
299  i1 = SingleValIter<int>{};
300  CHECK (isnil(i1));
301  }
302 
303 
304 
305  static ulong addTwo (int i) { return i+2; }
306  static int negate (int i) { return -i; }
307  static int idFunc (int i) { return i; }
308 
309  void
310  buildTransformingIterator (Iter const& ii)
311  {
312  pullOut (transformIterator(ii, idFunc));
313  pullOut (transformIterator(ii, negate));
314  pullOut (transformIterator(ii, addTwo)); // note: changing output type to unsigned
315 
316  TransformIter<Iter, int> idi (ii, idFunc);
317  TransformIter<Iter, int> neg (ii, negate);
318  verifyComparisons (idi);
319  verifyComparisons (neg);
320 
321  CHECK (idi);
322  CHECK (neg);
323  for ( ;idi&&neg;
324  ++idi,++neg)
325  CHECK (idi != neg);
326 
327  CHECK (!idi && !neg);
328  CHECK (idi == neg);
329  }
330 
331 
332 
333 
334 
337  template<class IT>
338  void
339  verifyComparisons (IT const& ii)
340  {
341  IT i1(ii);
342  IT i2(ii);
343  IT iN;
344  CHECK ( isnil (iN));
345  CHECK (!isnil (i1));
346  CHECK (!isnil (i2));
347 
348  CHECK (i1 == i2); CHECK (i2 == i1);
349  CHECK (i1 != iN); CHECK (iN != i1);
350  CHECK (i2 != iN); CHECK (iN != i2);
351 
352  ++i1;
353  CHECK (i1 != i2);
354  CHECK (i1 != iN);
355 
356  ++i2;
357  CHECK (i1 == i2);
358  CHECK (i1 != iN);
359  CHECK (i2 != iN);
360 
361  while (++i1) { }
362  CHECK (isnil(i1));
363  CHECK (i1 != i2);
364  CHECK (i1 == iN);
365 
366  while (++i2) { }
367  CHECK (isnil(i2));
368  CHECK (i2 == i1);
369  CHECK (i2 == iN);
370  }
371 
372 
373  void
374  verifyPullLast(Iter const& ii)
375  {
376  Iter::value_type lastElm = pull_last (ii);
377  CHECK (1 == lastElm); // TestSource holds a decreasing sequence of numbers ending with 1
378 
379  Iter emptyIterator;
380  CHECK (isnil (emptyIterator));
381 
382  VERIFY_ERROR (ITER_EXHAUST, pull_last(emptyIterator) );
383  }
384  };
385 
386  LAUNCHER (IterTools_test, "unit common");
387 
388 
389 }} // namespace lib::test
390 
Iterator tool filtering pulled data according to a predicate.
Definition: itertools.hpp:337
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Additional capabilities for FilterIter, allowing to extend the filter condition underway.
Definition: itertools.hpp:410
Implementation namespace for support and library code.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Iterator tool treating pulled data by a custom transformation (function)
Definition: itertools.hpp:754
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
A collection of frequently used helper functions to support unit testing.
auto singleValIterator(VAL &&something)
Build a SingleValIter: convenience free function shortcut, to pick up just any value and wrap it as L...
Definition: itertools.hpp:655
void verifyComparisons(IT const &ii)
Helpers for working with iterators based on the pipeline model.
Pseudo-Iterator to yield just a single value.
Definition: itertools.hpp:626
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
auto filterIterator(IT const &src, PRED filterPredicate)
Build a FilterIter: convenience free function shortcut, picking up the involved types automatically...
Definition: itertools.hpp:372
auto transformIterator(IT const &src, FUN processingFunc)
Build a TransformIter: convenience free function shortcut, picking up the involved types automaticall...
Definition: itertools.hpp:788
Perform operations "for each element" of a collection.
auto filterRepetitions(IT const &source)
filters away repeated values emitted by source iterator
Definition: itertools.hpp:846