Lumiera  0.pre.03
»edit your freedom«
iter-core-adapter-test.cpp
Go to the documentation of this file.
1 /*
2  IterCoreAdapter(Test) - iterating over a »state core«
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/test/test-helper.hpp"
22 #include "lib/util.hpp"
23 
24 #include "lib/iter-adapter.hpp"
25 
26 #include <string>
27 
28 
29 
30 namespace lib {
31 namespace test{
32 
33  using LERR_(ITER_EXHAUST);
34  using util::isSameObject;
35  using std::string;
36 
37  namespace { // Test fixture
38 
47  struct StepDown
48  {
49  uint n;
50 
51  StepDown(uint start =0)
52  : n(start)
53  { }
54 
55  bool
56  checkPoint () const
57  {
58  return n != 0;
59  }
60 
61  uint&
62  yield () const
63  {
64  return util::unConst(n);
65  }
66 
67  void
68  iterNext ()
69  {
70  --n;
71  }
72 
73  bool
74  operator== (StepDown const& o) const
75  {
76  return n == o.n;
77  }
78  };
79  }// (END) test dummy
80 
81 
82 
83 
84 
85 
86 
87  /*****************************************************************/
94  class IterCoreAdapter_test : public Test
95  {
96  virtual void
97  run (Arg)
98  {
99  simpleUsage();
100  stateManipulation();
101  checked_and_protected();
102  value_and_reference_yield();
103  verify_TypeReconciliation();
104  }
105 
106 
107 
108 
112  void
114  {
115  auto it = IterableDecorator<StepDown>{3};
116  CHECK (it);
117  CHECK (*it == 3);
118  ++it;
119  CHECK (it);
120  CHECK (*it == 2);
121  ++it;
122  CHECK (it);
123  CHECK (*it == 1);
124  ++it;
125  CHECK (not it);
126  }
127 
128 
130  void
132  {
133  auto it = IterableDecorator<StepDown>{};
134  CHECK (not it);
135  CHECK (*it == 0);
136  ++it;
137  CHECK (*it == uint(-1));
138  CHECK (it);
139  *it = 5;
140  CHECK (*it == 5);
141  ++it;
142  CHECK (*it == 4);
143  it.n = 1;
144  CHECK (*it == 1);
145  CHECK (it);
146  ++it;
147  CHECK (not it);
148  CHECK (it.n == 0);
149  CHECK (isSameObject(*it, it.n));
150  }
151 
152 
155  void
157  {
158  auto cc = CheckedCore<StepDown>{2};
159  CHECK (cc.checkPoint());
160  CHECK (cc.yield() == 2u);
161  cc.n = 1;
162  CHECK (cc.yield() == 1u);
163  cc.iterNext();
164  CHECK (not cc.checkPoint());
165  CHECK (cc.n == 0u);
166  VERIFY_ERROR (ITER_EXHAUST, cc.yield() );
167  VERIFY_ERROR (ITER_EXHAUST, cc.iterNext() );
168 
169  auto it = IterStateWrapper{StepDown{2}};
170  CHECK (it);
171  CHECK (*it == 2u);
172  ++it;
173  CHECK (*it == 1u);
174  ++it;
175  CHECK (not it);
176  VERIFY_ERROR (ITER_EXHAUST, *it );
177  VERIFY_ERROR (ITER_EXHAUST, ++it );
178  }
179 
180 
181 
199  void
201  {
202  struct ValueStep
203  : StepDown
204  {
205  using StepDown::StepDown;
206  int yield() const { return StepDown::yield(); }
207  };
208 
210  CHECK (it);
211  CHECK (*it == 2);
212  CHECK (it.n == 2);
213  CHECK (not isSameObject(*it, it.n));
214  CHECK (showType<decltype(*it)>() == "int"_expect);
215 
216  StepDown& ix{it};
217  CHECK (ix.yield() == 2u);
218  CHECK ( isSameObject(ix.yield(), ix.n));
219  CHECK ( isSameObject(ix.yield(), it.n));
220  CHECK (showType<decltype(ix.yield())>() == "uint&"_expect);
221 
222  ++it;
223  CHECK (*it == 1);
224  ++it;
225  VERIFY_ERROR (ITER_EXHAUST, *it );
226  VERIFY_ERROR (ITER_EXHAUST, ++it );
227  }
228 
229 
236  void
238  {
239  using C1 = Common<int,string>;
240  CHECK (not C1());
241  CHECK (not C1::value);
242 
243  using C2 = Common<int,long*>;
244  CHECK (not C2()); // can not be reconciled
245  CHECK (not C2::value);
246 // using X = C2::ResType; // does not (and should not) compile
247 
248  using C3 = Common<string,string>;
249  CHECK (C3());
250  CHECK (showType<C3::ResType>() == "string"_expect );
251 
252  using C4 = Common<string&,string>;
253  CHECK (showType<C4::ResType>() == "string"_expect );
254 
255  using C5 = Common<string&,string&>;
256  CHECK (showType<C5::ResType>() == "string&"_expect ); // ref access to both is possible
257 
258  using C6 = Common<string&,string&&>;
259  CHECK (showType<C6::ResType>() == "string"_expect ); // caution, RValue might be a temporary
260 
261  using C7 = Common<string&&,string&&>;
262  CHECK (showType<C7::ResType>() == "string"_expect );
263 
265  CHECK (showType<C8::ResType>() == "string const&"_expect );
266 
268  CHECK (showType<C9::ResType>() == "string const&"_expect ); // reconcile to const&
269 
270  using C10 = Common<string const&, string>;
271  CHECK (showType<C10::ResType>() == "const string"_expect );
272 
274  CHECK (showType<C11::ResType>() == "const string"_expect ); // reconciled to value type
275 
276  using C12 = Common<long const&, int>;
277  CHECK (showType<C12::ResType>() == "const long"_expect ); // reconciled to the larger number type
278 
279  using C13 = Common<double&, long const&>;
280  CHECK (showType<C13::ResType>() == "double const&"_expect ); // usual in C++ (loss of precision possible)
281  }
282 
283  template<typename T1, typename T2>
285  };
286 
287  LAUNCHER (IterCoreAdapter_test, "unit common");
288 
289 
290 }} // namespace lib::test
291 
A test »*State Core*« which steps down a number to zero.
Definition: run.hpp:40
Helper template(s) for creating Lumiera Forward Iterators.
Adapter to add sanity checks to a »state core«.
bool operator==(PtrDerefIter< I1 > const &il, PtrDerefIter< I2 > const &ir)
Supporting equality comparisons...
#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.
Another Lumiera Forward Iterator building block, based on incorporating a state type as »*State Core*...
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.
string showType()
diagnostic type output, including const and similar adornments
Decorator-Adapter to make a »*State Core*« iterable as Lumiera Forward Iterator.
Decision helper to select between returning results by value or reference.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421