Lumiera  0.pre.03
»edit your freedom«
index-iter-test.cpp
1 /*
2  IndexIter(Test) - verify index access packaged as iterator handle
3 
4  Copyright (C)
5  2024, 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/index-iter.hpp"
22 #include "lib/test/test-helper.hpp"
23 #include "lib/iter-explorer.hpp"
24 #include "lib/format-util.hpp"
25 #include "lib/util.hpp"
26 
27 #include <vector>
28 #include <memory>
29 
30 
31 
32 namespace lib {
33 namespace test{
34 
35  using ::Test;
36  using util::join;
37  using util::isnil;
38  using std::vector;
39  using std::shared_ptr;
40  using std::make_shared;
41 
42  using LERR_(ITER_EXHAUST);
43  using LERR_(INDEX_BOUNDS);
44 
45 
46  namespace { // test fixture
47 
48  const uint NUM_ELMS = 10;
49 
50  using Numz = vector<uint>;
51  using Iter = IndexIter<Numz>;
54 
55  inline Numz
56  makeNumz()
57  {
58  Numz numz;
59  for (uint i=0; i<NUM_ELMS; ++i)
60  numz.push_back(i);
61  return numz;
62  }
63 
64  } // (END)fixture
65 
66 
67 
68 
69 
70 
71 
72  /************************************************************************/
80  class IndexIter_test : public Test
81  {
82 
83  virtual void
84  run (Arg)
85  {
89  }
90 
91 
93  void
95  {
96  Numz numz{makeNumz()};
97  Iter i1{numz};
98 
99  CHECK (not isnil(i1));
100  CHECK (0 == *i1);
101  ++++++i1;
102  CHECK (3 == *i1);
103  for (uint i=*i1 ; i1; ++i1, ++i)
104  CHECK (i == *i1);
105 
106  CHECK (isnil(i1));
107 
108  auto sum = explore(Iter{numz}).resultSum();
109  uint n = numz.size() - 1;
110  CHECK (sum == n*(n+1)/2);
111 
112  for (auto & i : Iter{numz})
113  ++i; // note: manipulate the contents...
114  CHECK (join(numz,"◇") == "1◇2◇3◇4◇5◇6◇7◇8◇9◇10"_expect);
115 
116  verifyComparisons (Iter{numz});
117  }
118 
119 
120 
127  void
129  {
130  Numz numz{makeNumz()};
131  Iter iter{numz};
132 
133  CHECK (0 == *iter);
134  ++++++++iter;
135  CHECK (4 == *iter);
136  CHECK (not isnil(iter));
137  CHECK (join(iter) == "4, 5, 6, 7, 8, 9"_expect);
139 
140  CHECK (4 == *iter);
141  CHECK (4 == iter.getIDX());
142  iter.setIDX(7);
143  CHECK (7 == iter.getIDX());
144  CHECK (not isnil(iter));
145  CHECK (7 == *iter);
146  ++iter;
147  CHECK (8 == *iter);
148  iter.setIDX(6);
149  CHECK (join(iter) == "6, 7, 8, 9"_expect);
151 
152  ++++++++iter;
153  CHECK (isnil(iter));
154  VERIFY_ERROR (ITER_EXHAUST, *iter);
155  VERIFY_ERROR (ITER_EXHAUST, ++iter);
156  VERIFY_ERROR (ITER_EXHAUST, iter.getIDX());
157 
158  iter.setIDX(9);
159  CHECK (not isnil(iter));
160  CHECK (9 == *iter);
161 
162  VERIFY_ERROR (INDEX_BOUNDS, iter.setIDX(10));
163  CHECK (9 == iter.getIDX());
164  VERIFY_ERROR (INDEX_BOUNDS, iter.setIDX(-1));
165  CHECK (9 == iter.getIDX());
166 
167  Iter empty;
168  CHECK (isnil (empty));
169  VERIFY_ERROR (INDEX_BOUNDS, empty.setIDX(0));
170  VERIFY_ERROR (ITER_EXHAUST, empty.getIDX());
171  }
172 
173 
174 
178  void
180  {
181  auto smartNumz = make_shared<Numz> (makeNumz());
182  Numz & numz{*smartNumz};
183  Numz const& const_numz{numz};
184 
185  uint i = 0;
186  for (Iter iter{numz};
187  iter; ++iter, ++i
188  )
189  {
190  CHECK (iter);
191  CHECK (iter != Iter());
192  CHECK (*iter == i);
193  --(*iter);
194  CHECK (*iter == i-1);
195  }
196 
197  i = 0;
198  for (CIter iter{const_numz};
199  iter; ++iter, ++i
200  )
201  {
202  CHECK (iter);
203  CHECK (iter != CIter());
204  CHECK (*iter == i-1);
205  // Note: the preceding loop has indeed modified the contents
206 // ++(*iter); // but this doesn't compile, because the CIter yields a _const_
207  }
208 
209  verifyComparisons (CIter{numz});
210 
211  CHECK (1 == smartNumz.use_count());
212  {
213  SMIter smIter{smartNumz};
214  CIter cIter{*smartNumz};
215  CHECK (*cIter == uint(-1));
216  for (i=0; smIter; ++smIter, ++i)
217  {
218  CHECK (smIter);
219  CHECK (smIter != SMIter());
220  CHECK (*smIter == i-1);
221  ++(*smIter);
222  CHECK (*smIter == i);
223  }
224  CHECK (isnil (smIter));
225  CHECK (smIter == SMIter());
226  cIter.setIDX(5);
227  smIter.setIDX(5);
228  CHECK (*smIter == *cIter);
229 
230  verifyComparisons (smIter);
231 
232  CHECK (5 == *cIter); // shared data modified
233  CHECK (2 == smartNumz.use_count());
234  }
235  CHECK (1 == smartNumz.use_count());
236  }
237 
238 
239 
240 
241 
247  template<class IT>
248  void
249  verifyComparisons (IT const& ii)
250  {
251  IT i1(ii);
252  IT i2(ii);
253  IT iN;
254  CHECK ( isnil (iN));
255  CHECK (!isnil (i1));
256  CHECK (!isnil (i2));
257 
258  CHECK (i1 == i2); CHECK (i2 == i1);
259  CHECK (i1 != iN); CHECK (iN != i1);
260  CHECK (i2 != iN); CHECK (iN != i2);
261 
262  ++i1;
263  CHECK (i1 != i2);
264  CHECK (i1 != iN);
265 
266  ++i2;
267  CHECK (i1 == i2);
268  CHECK (i1 != iN);
269  CHECK (i2 != iN);
270 
271  while (++i1) { }
272  CHECK (isnil(i1));
273  CHECK (i1 != i2);
274  CHECK (i1 == iN);
275 
276  while (++i2) { }
277  CHECK (isnil(i2));
278  CHECK (i2 == i1);
279  CHECK (i2 == iN);
280  }
281  };
282 
283  LAUNCHER (IndexIter_test, "unit common");
284 
285 
286 }} // namespace lib::test
287 
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Implementation namespace for support and library code.
Simplistic test class runner.
Iterator-style access handle to a referred container with subscript index.
Subscript-index based access to a container, packaged as iterator.
Definition: index-iter.hpp:112
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.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Building tree expanding and backtracking evaluations within hierarchical scopes.
void verifyComparisons(IT const &ii)