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) Lumiera.org
5  2015, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/test/run.hpp"
30 #include "lib/test/test-helper.hpp"
31 #include "lib/format-util.hpp"
32 #include "lib/iter-cursor.hpp"
33 #include "lib/util.hpp"
34 
35 #include <vector>
36 
37 
38 
39 namespace lib {
40 namespace test{
41 
42  using ::Test;
43  using util::join;
44  using util::isnil;
45  using std::vector;
46 
47  using LERR_(ITER_EXHAUST);
48 
49 
50  namespace { // test fixture
51 
52  const uint NUM_ELMS = 10;
53 
54  using Numz = vector<uint>;
57 
58  inline Numz
59  makeNumz()
60  {
61  Numz numz;
62  for (uint i=0; i<NUM_ELMS; ++i)
63  numz.push_back(i);
64  return numz;
65  }
66 
67  } // (END)fixture
68 
69 
70 
71 
72 
73 
74 
75  /*****************************************************************/
83  class IterCursor_test : public Test
84  {
85 
86  virtual void
87  run (Arg)
88  {
92  }
93 
94 
96  void
98  {
99  Numz numz{makeNumz()};
100  Iter i1{numz.begin(), numz.end()};
101 
102  CHECK (!isnil(i1));
103  CHECK (0 == *i1);
104  ++++++i1;
105  CHECK (3 == *i1);
106  for (uint i=*i1 ; i1; ++i1, ++i)
107  CHECK (i == *i1);
108 
109  CHECK (isnil(i1));
110 
111  Iter i2{numz};
112  uint sum =0;
113  while (++i2)
114  sum += *i2;
115  uint n = numz.size() - 1;
116  CHECK (sum == n*(n+1) / 2);
117 
118  Iter i3{numz};
119  for (auto & i : i3)
120  ++i; // note: manipulate the contents...
121 
122  Iter i4{numz};
123  CHECK (join(i4,"-+-") == "1-+-2-+-3-+-4-+-5-+-6-+-7-+-8-+-9-+-10");
124  verifyComparisons (i4);
125  }
126 
127 
128 
136  void
138  {
139  Numz numz{makeNumz()};
140  Iter iter{numz};
141 
142  CHECK (0 == *iter);
143  ++++++++iter;
144  CHECK (4 == *iter);
145  CHECK (!isnil(iter));
146  CHECK (join(iter) == "4, 5, 6, 7, 8, 9");
148 
149  iter.switchDir();
150  CHECK (4 == *iter);
151  CHECK (!isnil(iter));
152  ++iter;
153  CHECK (3 == *iter);
154  CHECK (!isnil(iter));
155  CHECK (join(iter) == "3, 2, 1, 0");
157 
158  ++iter;
159  CHECK (2 == *iter);
160  ++++iter;
161  CHECK (0 == *iter);
162  CHECK (!isnil(iter));
163 
164  iter.switchDir();
165  CHECK (0 == *iter);
166  CHECK (!isnil(iter));
167  ++iter;
168  CHECK (1 == *iter);
169 
170  iter.switchDir();
171  ++iter;
172  CHECK (0 == *iter);
173  CHECK (!isnil(iter));
174 
175  ++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 (0 == *iter);
183 
184  while (++iter);
185  CHECK (isnil(iter));
186  VERIFY_ERROR (ITER_EXHAUST, *iter);
187  VERIFY_ERROR (ITER_EXHAUST, ++iter);
188 
189  iter.switchDir();
190  CHECK (!isnil(iter));
191  CHECK (9 == *iter);
192 
193  Iter nil;
194  CHECK (isnil (nil));
195  iter.switchDir();
196  CHECK (isnil (nil));
197  VERIFY_ERROR (ITER_EXHAUST, *nil);
198  VERIFY_ERROR (ITER_EXHAUST, ++nil);
199  }
200 
201 
202 
206  void
208  {
209  Numz numz{makeNumz()};
210  Numz const& const_numz{numz};
211 
212  uint i = 0;
213  for (Iter iter{numz};
214  iter; ++iter, ++i
215  )
216  {
217  CHECK (iter);
218  CHECK (iter != Iter());
219  CHECK (*iter == i);
220  --(*iter);
221  CHECK (*iter == i-1);
222  }
223 
224  i = 0;
225  for (CIter iter{const_numz};
226  iter; ++iter, ++i
227  )
228  {
229  CHECK (iter);
230  CHECK (iter != CIter());
231  CHECK (*iter == i-1);
232 
233  // note: the previous run indeed modified
234  // the elements within the container.
235 
236  // ++(*iter); // doesn't compile, because it yields a "* const"
237  }
238 
239  verifyComparisons (CIter{numz});
240  }
241 
242 
243 
244 
245 
246 
252  template<class IT>
253  void
254  verifyComparisons (IT const& ii)
255  {
256  IT i1(ii);
257  IT i2(ii);
258  IT iN;
259  CHECK ( isnil (iN));
260  CHECK (!isnil (i1));
261  CHECK (!isnil (i2));
262 
263  CHECK (i1 == i2); CHECK (i2 == i1);
264  CHECK (i1 != iN); CHECK (iN != i1);
265  CHECK (i2 != iN); CHECK (iN != i2);
266 
267  ++i1;
268  CHECK (i1 != i2);
269  CHECK (i1 != iN);
270 
271  ++i2;
272  CHECK (i1 == i2);
273  CHECK (i1 != iN);
274  CHECK (i2 != iN);
275 
276  while (++i1) { }
277  CHECK (isnil(i1));
278  CHECK (i1 != i2);
279  CHECK (i1 == iN);
280 
281  while (++i2) { }
282  CHECK (isnil(i2));
283  CHECK (i2 == i1);
284  CHECK (i2 == iN);
285  }
286  };
287 
288  LAUNCHER (IterCursor_test, "unit common");
289 
290 
291 }} // namespace lib::test
292 
Definition: run.hpp:49
#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.
Simple 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.