Lumiera  0.pre.03
»edit your freedom«
iter-index-test.cpp
Go to the documentation of this file.
1 /*
2  IterIndex(Test) - verify index access packaged as iterator handle
3 
4  Copyright (C) Lumiera.org
5  2024, 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/iter-index.hpp"
31 #include "lib/test/test-helper.hpp"
32 #include "lib/iter-explorer.hpp"
33 #include "lib/format-util.hpp"
34 #include "lib/util.hpp"
35 
36 #include <vector>
37 #include <memory>
38 
39 
40 
41 namespace lib {
42 namespace test{
43 
44  using ::Test;
45  using util::join;
46  using util::isnil;
47  using std::vector;
48  using std::shared_ptr;
49  using std::make_shared;
50 
51  using LERR_(ITER_EXHAUST);
52  using LERR_(INDEX_BOUNDS);
53 
54 
55  namespace { // test fixture
56 
57  const uint NUM_ELMS = 10;
58 
59  using Numz = vector<uint>;
60  using Iter = IterIndex<Numz>;
63 
64  inline Numz
65  makeNumz()
66  {
67  Numz numz;
68  for (uint i=0; i<NUM_ELMS; ++i)
69  numz.push_back(i);
70  return numz;
71  }
72 
73  } // (END)fixture
74 
75 
76 
77 
78 
79 
80 
81  /*****************************************************************/
89  class IterIndex_test : public Test
90  {
91 
92  virtual void
93  run (Arg)
94  {
98  }
99 
100 
102  void
104  {
105  Numz numz{makeNumz()};
106  Iter i1{numz};
107 
108  CHECK (not isnil(i1));
109  CHECK (0 == *i1);
110  ++++++i1;
111  CHECK (3 == *i1);
112  for (uint i=*i1 ; i1; ++i1, ++i)
113  CHECK (i == *i1);
114 
115  CHECK (isnil(i1));
116 
117  auto sum = explore(Iter{numz}).resultSum();
118  uint n = numz.size() - 1;
119  CHECK (sum == n*(n+1)/2);
120 
121  for (auto & i : Iter{numz})
122  ++i; // note: manipulate the contents...
123  CHECK (join(numz,"◇") == "1◇2◇3◇4◇5◇6◇7◇8◇9◇10"_expect);
124 
125  verifyComparisons (Iter{numz});
126  }
127 
128 
129 
137  void
139  {
140  Numz numz{makeNumz()};
141  Iter iter{numz};
142 
143  CHECK (0 == *iter);
144  ++++++++iter;
145  CHECK (4 == *iter);
146  CHECK (not isnil(iter));
147  CHECK (join(iter) == "4, 5, 6, 7, 8, 9"_expect);
149 
150  CHECK (4 == *iter);
151  CHECK (4 == iter.getIDX());
152  iter.setIDX(7);
153  CHECK (7 == iter.getIDX());
154  CHECK (not isnil(iter));
155  CHECK (7 == *iter);
156  ++iter;
157  CHECK (8 == *iter);
158  iter.setIDX(6);
159  CHECK (join(iter) == "6, 7, 8, 9"_expect);
161 
162  ++++++++iter;
163  CHECK (isnil(iter));
164  VERIFY_ERROR (ITER_EXHAUST, *iter);
165  VERIFY_ERROR (ITER_EXHAUST, ++iter);
166  VERIFY_ERROR (ITER_EXHAUST, iter.getIDX());
167 
168  iter.setIDX(9);
169  CHECK (not isnil(iter));
170  CHECK (9 == *iter);
171 
172  VERIFY_ERROR (INDEX_BOUNDS, iter.setIDX(10));
173  CHECK (9 == iter.getIDX());
174  VERIFY_ERROR (INDEX_BOUNDS, iter.setIDX(-1));
175  CHECK (9 == iter.getIDX());
176  }
177 
178 
179 
183  void
185  {
186  auto smartNumz = make_shared<Numz> (makeNumz());
187  Numz & numz{*smartNumz};
188  Numz const& const_numz{numz};
189 
190  uint i = 0;
191  for (Iter iter{numz};
192  iter; ++iter, ++i
193  )
194  {
195  CHECK (iter);
196  CHECK (iter != Iter());
197  CHECK (*iter == i);
198  --(*iter);
199  CHECK (*iter == i-1);
200  }
201 
202  i = 0;
203  for (CIter iter{const_numz};
204  iter; ++iter, ++i
205  )
206  {
207  CHECK (iter);
208  CHECK (iter != CIter());
209  CHECK (*iter == i-1);
210  // Note: the preceding loop has indeed modified the contents
211 // ++(*iter); // but this doesn't compile, because the CIter yields a _const_
212  }
213 
214  verifyComparisons (CIter{numz});
215 
216  CHECK (1 == smartNumz.use_count());
217  {
218  SMIter smIter{smartNumz};
219  CIter cIter{*smartNumz};
220  CHECK (*cIter == uint(-1));
221  for (i=0; smIter; ++smIter, ++i)
222  {
223  CHECK (smIter);
224  CHECK (smIter != SMIter());
225  CHECK (*smIter == i-1);
226  ++(*smIter);
227  CHECK (*smIter == i);
228  }
229  CHECK (isnil (smIter));
230  CHECK (smIter == SMIter());
231  cIter.setIDX(5);
232  smIter.setIDX(5);
233  CHECK (*smIter == *cIter);
234 
235  verifyComparisons (smIter);
236 
237  CHECK (5 == *cIter); // shared data modified
238  CHECK (2 == smartNumz.use_count());
239  }
240  CHECK (1 == smartNumz.use_count());
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 (IterIndex_test, "unit common");
289 
290 
291 }} // namespace lib::test
292 
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Definition: run.hpp:49
Iterator-style access handle to a referred container with subscript index.
void verifyComparisons(IT const &ii)
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Subscript-index based access to a container, packaged as iterator.
Definition: iter-index.hpp:121
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.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
Building tree expanding and backtracking evaluations within hierarchical scopes.