Lumiera  0.pre.03
»edit your freedom«
iter-adapter-stl-test.cpp
Go to the documentation of this file.
1 /*
2  IterAdapterSTL(Test) - building various custom iterators for a given container
3 
4  Copyright (C)
5  2010, 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-coll.hpp"
22 #include "lib/format-cout.hpp"
23 #include "lib/util.hpp"
24 
25 #include "lib/iter-adapter-stl.hpp"
26 
27 #include <boost/lexical_cast.hpp>
28 #include <vector>
29 
30 
31 
32 namespace lib {
33 namespace test{
34 
35  using ::Test;
36  using boost::lexical_cast;
37  using util::isnil;
38 
39  namespace iter = lib::iter_stl;
40 
41 
44 #define TEST_ITER(_CTOR_, _ARG_) \
45  cout << STRINGIFY(_CTOR_) ;\
46  pullOut (_CTOR_ _ARG_) ;\
47  cout << endl;
48 
50 #define PRINT_FUNC(_F_NAME_, _F_TYPE_) \
51  cout << "-----"<<STRINGIFY(_F_NAME_)<<"---" << util::typeStr<_F_TYPE_>() << endl;
52 
53 
54 
55 
56 
57 
58 
59 
60  /********************************************************************/
77  class IterAdapterSTL_test : public Test
78  {
79  uint NUM_ELMS{0};
80 
81  virtual void
82  run (Arg arg)
83  {
84  NUM_ELMS = firstVal (arg, 10);
85 
86  checkDistinctValIter();
87 
88  iterateMapKeyVal (getTestMap_int<MapII> (NUM_ELMS));
89  iterateMapKeyVal (getTestMap_int<HMapII> (NUM_ELMS));
90 
91  iterateMapKeyVal (getTestMultiMap_int<MMapII> (NUM_ELMS));
92  iterateMapKeyVal (getTestMultiMap_int<HMMapII> (NUM_ELMS));
93 
94  iterateValues4Key (getTestMultiMap_int<MMapII> (NUM_ELMS));
95  iterateValues4Key (getTestMultiMap_int<HMMapII> (NUM_ELMS));
96 
97  checkIteratorSnapshot();
98  }
99 
100 
101  template<class MAP>
102  void
103  iterateMapKeyVal (MAP const& map)
104  {
105  PRINT_FUNC (iterateMapKeyVal, MAP);
106 
107  TEST_ITER (iter::eachKey, (map));
108  TEST_ITER (iter::eachKey, (map.begin(), map.end()));
109  TEST_ITER (iter::eachVal, (map));
110  TEST_ITER (iter::eachVal, (map.begin(), map.end()));
111  TEST_ITER (iter::eachDistinctKey, (map));
112  }
113 
114 
115  template<class MMAP>
116  void
117  iterateValues4Key (MMAP const& mumap)
118  {
119  PRINT_FUNC (iterateValues4Key, MMAP);
120 
121  TEST_ITER (iter::eachValForKey, (mumap, 0));
122 
123  // non-existing key should yield empty iterator
124  CHECK (! iter::eachValForKey (mumap, NUM_ELMS));
125  }
126 
127 
128  void
129  checkDistinctValIter()
130  {
131  PRINT_FUNC (filter-distinct-values, VecI);
132 
133  VecI vec;
134  TEST_ITER(iter::eachDistinct, (vec));
135 
136  vec.push_back (1);
137  vec.push_back (1);
138  vec.push_back (1);
139  TEST_ITER(iter::eachDistinct, (vec));
140 
141  vec.push_back (2);
142  vec.push_back (3);
143  vec.push_back (3);
144  vec.push_back (1);
145  TEST_ITER(iter::eachDistinct, (vec));
146 
147  vec.push_back (1);
148  vec.push_back (1);
149  vec.push_back (1);
150  vec.push_back (1);
151  vec.push_back (1);
152  TEST_ITER(iter::eachDistinct, (vec));
153  }
154 
155 
156  void
157  checkIteratorSnapshot()
158  {
159  typedef VecI::iterator Iter;
160  typedef RangeIter<Iter> Range;
161  typedef iter::IterSnapshot<int> Snapshot;
162 
163  VecI vec = getTestSeq_int<VecI> (NUM_ELMS);
164  Snapshot capture1 (vec.begin(), vec.end());
165 
166  Range range_of_all (vec.begin(), vec.end());
167  Snapshot capture2 = iter::snapshot(range_of_all); // NOTE: when specifically taken this way,
168  CHECK (range_of_all); // snapshot doesn't affect given source iterator pos
169  CHECK (capture2); // (but WARNING, the IterSnapshot ctor itself is destructive)
170 
171  CHECK (vec.begin() == range_of_all.getPos());
172  CHECK (vec.end() == range_of_all.getEnd());
173  CHECK (!isnil (vec));
174 
175  // concurrent or intermittent modification
176  vec.clear();
177  CHECK (isnil (vec));
178  CHECK (vec.end() != range_of_all.getEnd()); // range_of_all is now corrupted
179 
180  CHECK (capture1); // ...but the snapshots remain unaffected
181  CHECK (capture2);
182  CHECK (capture1 == capture2); // can compare snapshots, based on actual contents
183 
184  vec.push_back(22);
185  vec.push_back(44);
186  Snapshot capture3 (vec.begin(), vec.end()); // take another snapshot from current contents
187  CHECK (capture3);
188  CHECK (capture3 != capture1);
189  CHECK (capture3 != capture2);
190 
191 
192  uint sum_should_be = (NUM_ELMS-1)*NUM_ELMS/2;
193 
194  CHECK (sum_should_be == sumAll (capture1));
195  CHECK (!capture1); // this one is exhausted now
196  CHECK ( capture2); // ...but the others are really independent
197  CHECK ( capture3);
198  CHECK (capture1 != capture2); // comparison includes the current position
199 
200  CHECK (sum_should_be == sumAll (capture2));
201  CHECK (!capture1);
202  CHECK (!capture2);
203  CHECK ( capture3);
204  CHECK (capture1 == capture2); // now again equal (both exhausted and equal contents)
205 
206  CHECK (22+44 == sumAll (capture3));
207  CHECK (!capture1);
208  CHECK (!capture2);
209  CHECK (!capture3);
210  CHECK (capture1 == capture2); // all exhausted iterators count as "equal"
211  CHECK (capture3 == capture1); // this ensures the idiom while(pos != end) works
212  CHECK (capture3 == capture2);
213  }
214 
215  template<class IT>
216  uint
217  sumAll (IT& it)
218  {
219  uint sum(0);
220  cout << "snapshot";
221  while (it)
222  {
223  cout << "-" << *it;
224  sum += *it;
225  ++it;
226  }
227  cout << endl;
228  return sum;
229  }
230  };
231 
232  LAUNCHER (IterAdapterSTL_test, "unit common");
233 
234 
235 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
bool filter(Placement< DummyMO > const &candidate)
a filter predicate to pick some objects from a resultset.
Definition: run.hpp:40
#define PRINT_FUNC(_F_NAME_, _F_TYPE_)
print descriptive separator to STDOUT
Implementation namespace for support and library code.
Simplistic test class runner.
some bits of unit test helper code to fabricate collections with test data
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
#define TEST_ITER(_CTOR_, _ARG_)
test an iterator: create it by calling a constructor function and then pull out all contents and prin...
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
materialised iterator contents.
Preconfigured adapters for some STL container standard usage situations.