Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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"
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
33namespace lib {
34namespace 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
49 {
50 vector<int> data_;
51
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
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;
106
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
134 {
135 pullOut (filterIterator (ii, takeAll)); // note: using the convenient builder function
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 and not isnil(filtered);
253 ++num,
254 ++filtered
255 )
256 CHECK (num == *filtered);
257
258 CHECK (num == NUM_ELMS and 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
311 {
314 pullOut (transformIterator(ii, addTwo)); // note: changing output type to unsigned
315
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 and !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
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
Additional capabilities for FilterIter, allowing to extend the filter condition underway.
Iterator tool filtering pulled data according to a predicate.
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Pseudo-Iterator to yield just a single value.
Iterator tool treating pulled data by a custom transformation (function)
void buildTransformingIterator(Iter const &ii)
virtual void run(Arg arg)
void buildFilterIterator(Iter const &ii)
static ulong addTwo(int i)
TestSource::iterator Iter
void verifyComparisons(IT const &ii)
void verifyPullLast(Iter const &ii)
#define LERR_(_NAME_)
Definition error.hpp:45
Automatically use custom string conversion in C++ stream output.
unsigned int uint
Definition integral.hpp:29
unsigned long int ulong
Definition integral.hpp:31
Helpers for working with iterators based on the pipeline model.
Implementation namespace for support and library code.
auto filterRepetitions(IT const &source)
filters away repeated values emitted by source iterator
int rani(uint bound=_iBOUND())
Definition random.hpp:135
IT::value_type pull_last(IT iter)
auto transformIterator(IT const &src, FUN processingFunc)
Build a TransformIter: convenience free function shortcut, picking up the involved types automaticall...
auto filterIterator(IT const &src, PRED filterPredicate)
Build a FilterIter: convenience free function shortcut, picking up the involved types automatically.
auto singleValIterator(VAL &&something)
Build a SingleValIter: convenience free function shortcut, to pick up just any value and wrap it as L...
Test runner and basic definitions for tests.
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
bool isnil(lib::time::Duration const &dur)
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Perform operations "for each element" of a collection.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...