Lumiera  0.pre.03
»edit your freedom«
iter-cursor-test.cpp
Go to the documentation of this file.
1 /*
2  IterCursor(Test) - verify operation of a iterator based navigation cursor
3 
4  Copyright (C)
5  2015, 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/format-util.hpp"
23 #include "lib/iter-cursor.hpp"
24 #include "lib/util.hpp"
25 
26 #include <vector>
27 
28 
29 
30 namespace lib {
31 namespace test{
32 
33  using ::Test;
34  using util::join;
35  using util::isnil;
36  using std::vector;
37 
38  using LERR_(ITER_EXHAUST);
39 
40 
41  namespace { // test fixture
42 
43  const uint NUM_ELMS = 10;
44 
45  using Numz = vector<uint>;
48 
49  inline Numz
50  makeNumz()
51  {
52  Numz numz;
53  for (uint i=0; i<NUM_ELMS; ++i)
54  numz.push_back(i);
55  return numz;
56  }
57 
58  } // (END)fixture
59 
60 
61 
62 
63 
64 
65 
66  /*****************************************************************/
74  class IterCursor_test : public Test
75  {
76 
77  virtual void
78  run (Arg)
79  {
83  }
84 
85 
87  void
89  {
90  Numz numz{makeNumz()};
91  Iter i1{numz.begin(), numz.end()};
92 
93  CHECK (!isnil(i1));
94  CHECK (0 == *i1);
95  ++++++i1;
96  CHECK (3 == *i1);
97  for (uint i=*i1 ; i1; ++i1, ++i)
98  CHECK (i == *i1);
99 
100  CHECK (isnil(i1));
101 
102  Iter i2{numz};
103  uint sum =0;
104  while (++i2)
105  sum += *i2;
106  uint n = numz.size() - 1;
107  CHECK (sum == n*(n+1) / 2);
108 
109  Iter i3{numz};
110  for (auto & i : i3)
111  ++i; // note: manipulate the contents...
112 
113  Iter i4{numz};
114  CHECK (join(i4,"-+-") == "1-+-2-+-3-+-4-+-5-+-6-+-7-+-8-+-9-+-10");
115  verifyComparisons (i4);
116  }
117 
118 
119 
127  void
129  {
130  Numz numz{makeNumz()};
131  Iter iter{numz};
132 
133  CHECK (0 == *iter);
134  ++++++++iter;
135  CHECK (4 == *iter);
136  CHECK (!isnil(iter));
137  CHECK (join(iter) == "4, 5, 6, 7, 8, 9");
139 
140  iter.switchDir();
141  CHECK (4 == *iter);
142  CHECK (!isnil(iter));
143  ++iter;
144  CHECK (3 == *iter);
145  CHECK (!isnil(iter));
146  CHECK (join(iter) == "3, 2, 1, 0");
148 
149  ++iter;
150  CHECK (2 == *iter);
151  ++++iter;
152  CHECK (0 == *iter);
153  CHECK (!isnil(iter));
154 
155  iter.switchDir();
156  CHECK (0 == *iter);
157  CHECK (!isnil(iter));
158  ++iter;
159  CHECK (1 == *iter);
160 
161  iter.switchDir();
162  ++iter;
163  CHECK (0 == *iter);
164  CHECK (!isnil(iter));
165 
166  ++iter;
167  CHECK (isnil(iter));
168  VERIFY_ERROR (ITER_EXHAUST, *iter);
169  VERIFY_ERROR (ITER_EXHAUST, ++iter);
170 
171  iter.switchDir();
172  CHECK (!isnil(iter));
173  CHECK (0 == *iter);
174 
175  while (++iter);
176  CHECK (isnil(iter));
177  VERIFY_ERROR (ITER_EXHAUST, *iter);
178  VERIFY_ERROR (ITER_EXHAUST, ++iter);
179 
180  iter.switchDir();
181  CHECK (!isnil(iter));
182  CHECK (9 == *iter);
183 
184  Iter nil;
185  CHECK (isnil (nil));
186  iter.switchDir();
187  CHECK (isnil (nil));
188  VERIFY_ERROR (ITER_EXHAUST, *nil);
189  VERIFY_ERROR (ITER_EXHAUST, ++nil);
190  }
191 
192 
193 
197  void
199  {
200  Numz numz{makeNumz()};
201  Numz const& const_numz{numz};
202 
203  uint i = 0;
204  for (Iter iter{numz};
205  iter; ++iter, ++i
206  )
207  {
208  CHECK (iter);
209  CHECK (iter != Iter());
210  CHECK (*iter == i);
211  --(*iter);
212  CHECK (*iter == i-1);
213  }
214 
215  i = 0;
216  for (CIter iter{const_numz};
217  iter; ++iter, ++i
218  )
219  {
220  CHECK (iter);
221  CHECK (iter != CIter());
222  CHECK (*iter == i-1);
223 
224  // note: the previous run indeed modified
225  // the elements within the container.
226 
227  // ++(*iter); // doesn't compile, because it yields a "* const"
228  }
229 
230  verifyComparisons (CIter{numz});
231  }
232 
233 
234 
235 
236 
237 
243  template<class IT>
244  void
245  verifyComparisons (IT const& ii)
246  {
247  IT i1(ii);
248  IT i2(ii);
249  IT iN;
250  CHECK ( isnil (iN));
251  CHECK (!isnil (i1));
252  CHECK (!isnil (i2));
253 
254  CHECK (i1 == i2); CHECK (i2 == i1);
255  CHECK (i1 != iN); CHECK (iN != i1);
256  CHECK (i2 != iN); CHECK (iN != i2);
257 
258  ++i1;
259  CHECK (i1 != i2);
260  CHECK (i1 != iN);
261 
262  ++i2;
263  CHECK (i1 == i2);
264  CHECK (i1 != iN);
265  CHECK (i2 != iN);
266 
267  while (++i1) { }
268  CHECK (isnil(i1));
269  CHECK (i1 != i2);
270  CHECK (i1 == iN);
271 
272  while (++i2) { }
273  CHECK (isnil(i2));
274  CHECK (i2 == i1);
275  CHECK (i2 == iN);
276  }
277  };
278 
279  LAUNCHER (IterCursor_test, "unit common");
280 
281 
282 }} // namespace lib::test
283 
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
An iterator with the ability to switch direction.
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...
A collection of frequently used helper functions to support unit testing.
A cursor-like iterator with the ability to switch iteration direction.
void verifyComparisons(IT const &ii)
Collection of small helpers and convenience shortcuts for diagnostics & formatting.