Lumiera  0.pre.03
»edit your freedom«
util-collection-test.cpp
Go to the documentation of this file.
1 /*
2  UtilCollection(Test) - helpers and shortcuts for working with collections
3 
4  Copyright (C) Lumiera.org
5  2012, 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 
28 #include "lib/test/run.hpp"
29 #include "lib/itertools.hpp"
30 #include "lib/util-coll.hpp"
31 #include "lib/iter-adapter.hpp"
32 #include "lib/meta/trait.hpp"
33 
34 
35 #include <boost/lexical_cast.hpp>
36 #include <vector>
37 
38 
39 using ::Test;
40 
41 using util::first;
42 using util::last;
43 
47 
48 using boost::lexical_cast;
49 
50 
51 
52 namespace util {
53 namespace test {
54 
55  typedef std::vector<uint> VecI;
56  typedef lib::RangeIter<VecI::iterator> RangeI;
57 
58 
59 
60  namespace{ // Test data and operations
61 
62  uint NUM_ELMS = 20;
63 
64  VecI
65  someNumberz (uint count)
66  {
67  VecI numbers;
68  numbers.reserve(count);
69  while (count)
70  numbers.push_back(count--);
71 
72  return numbers;
73  }
74 
75  } // (End) test data and operations
76 
77 
78 
79  /*****************************************************************/
86  class UtilCollection_test : public Test
87  {
88 
89  void
90  run (Arg arg)
91  {
92  verify_typeDetectors();
93 
94  if (0 < arg.size()) NUM_ELMS = lexical_cast<uint> (arg[0]);
95 
96  VecI container = someNumberz (NUM_ELMS);
97  RangeI iterator(container.begin(), container.end());
98 
99  verify_accessFirstLast (container, NUM_ELMS);
100  verify_accessFirstLast (iterator, NUM_ELMS);
101 
102  verify_Min_Max (container, NUM_ELMS);
103  verify_Min_Max (iterator, NUM_ELMS);
104  }
105 
106 
107  template<class COL>
108  void
109  verify_accessFirstLast (COL const& col, uint lim)
110  {
111  uint theFirst = lim;
112  uint theLast = 1;
113 
114  CHECK (first(col) == theFirst);
115  CHECK (last(col) == theLast);
116  }
117 
118 
119  template<class COL>
120  void
121  verify_Min_Max (COL const& col, uint lim)
122  {
123  uint expectedMax = lim;
124  uint expectedMin = 1;
125 
126  CHECK (max (col) == expectedMax);
127  CHECK (min (col) == expectedMin);
128 
129  COL empty;
130 
131  using Val = typename COL::value_type;
132 
133  CHECK (max (empty) == std::numeric_limits<Val>::min());
134  CHECK (min (empty) == std::numeric_limits<Val>::max());
135  }
136 
137 
138  void
139  verify_typeDetectors()
140  {
143 
146 
149  }
150  };
151 
152 
153 
154 
155  LAUNCHER (UtilCollection_test, "unit common");
156 
157 
158 }} // namespace util::test
159 
Trait template to detect a type usable with the STL for-each loop.
Definition: trait.hpp:550
enable_if< treat_as_LumieraIterator< IT >, typename IT::reference > first(IT ii)
extract the first element yielded by an Lumiera Forward Iterator.
Definition: util-coll.hpp:144
Trait template to detect a type also supporting STL-style backwards iteration.
Definition: trait.hpp:614
Definition: run.hpp:49
Helper template(s) for creating Lumiera Forward Iterators.
enable_if< treat_as_STL_Container< COLL >, typename COLL::reference > first(COLL const &coll)
access the first element of a STL-like container.
Definition: util-coll.hpp:112
Simple test class runner.
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition: trait.hpp:505
Helpers for type detection, type rewriting and metaprogramming.
enable_if< can_direct_access_Last< COLL >, typename COLL::reference > last(COLL const &coll)
access the last element of a STL-like container.
Definition: util-coll.hpp:128
Helpers for working with iterators based on the pipeline model.
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Some small helpers and convenience shortcuts to ease working with collections and sequences (given by...