All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Macros | Functions
CollectionView_test.cc File Reference

Unit test for CollectionView class. More...

#include "lardata/Utilities/CollectionView.h"
#include <boost/test/unit_test.hpp>
#include <deque>
#include <list>
#include <forward_list>
#include <numeric>
#include <sstream>
#include <iostream>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( CollectionView_test )
 

Functions

 BOOST_AUTO_TEST_CASE (VectorTestCase)
 
 BOOST_AUTO_TEST_CASE (DequeTestCase)
 
 BOOST_AUTO_TEST_CASE (ListTestCase)
 
 BOOST_AUTO_TEST_CASE (ForwardListTestCase)
 
 BOOST_AUTO_TEST_CASE (DocumentationTestCase)
 

Detailed Description

Unit test for CollectionView class.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
August 3rd, 2017

This is a Boost unit test with no specific configuration.

Definition in file CollectionView_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( CollectionView_test )

Definition at line 14 of file CollectionView_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( VectorTestCase  )

Definition at line 27 of file CollectionView_test.cc.

27  {
28  //
29  // test on contiguous access collection
30  //
31  std::vector<int> c{ 3, 4, 5 };
32  auto const cbegin = c.cbegin();
33  auto const cend = c.cend();
34 
36 
37  BOOST_TEST(cv.empty() == c.empty());
38  BOOST_TEST(cv.size() == c.size());
39 
40  //
41  // iterators
42  //
43  BOOST_TEST((cv.cbegin() == c.cbegin()));
44  BOOST_TEST((cv.cend() == c.cend()));
45  BOOST_TEST((cv.crbegin() == c.crbegin()));
46  BOOST_TEST((cv.crend() == c.crend()));
47 
48  //
49  // elements
50  //
51  BOOST_TEST(&(cv.front()) == &(c.front()));
52  BOOST_TEST(cv.front() == c.front());
53  BOOST_TEST(&(cv.back()) == &(c.back()));
54  BOOST_TEST(cv.back() == c.back());
55 
56  //
57  // data
58  //
59  BOOST_TEST(cv.data() == c.data());
60 
61  //
62  // range-for iteration
63  //
64  auto ic = cbegin;
65  size_t i = 0;
66  for (auto const& d: cv) {
67  BOOST_TEST(d == *ic);
68  BOOST_TEST(cv[i] == d);
69  BOOST_TEST(&(cv[i]) == &(c[i]));
70  BOOST_TEST(cv.at(i) == d);
71  BOOST_TEST(&(cv.at(i)) == &(c.at(i)));
72 
73  ++i;
74  ++ic;
75  } // for
76  BOOST_TEST((ic == cend));
77 
78 } // BOOST_AUTO_TEST_CASE(VectorTestCase)
auto cbegin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:567
auto cend(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:579
auto makeCollectionView(BeginIter const &b, EndIter const &e)
Creates a CollectionView from the specified iterators.
BOOST_AUTO_TEST_CASE ( DequeTestCase  )

Definition at line 82 of file CollectionView_test.cc.

82  {
83  //
84  // test on random access collection
85  //
86  std::deque<int> c{ 3, 4, 5 };
87  auto const cbegin = c.cbegin();
88  auto const cend = c.cend();
89 
91 
92  BOOST_TEST(cv.empty() == c.empty());
93  BOOST_TEST(cv.size() == c.size());
94 
95  //
96  // iterators
97  //
98  BOOST_TEST((cv.cbegin() == c.cbegin()));
99  BOOST_TEST((cv.cend() == c.cend()));
100  BOOST_TEST((cv.crbegin() == c.crbegin()));
101  BOOST_TEST((cv.crend() == c.crend()));
102 
103  //
104  // elements
105  //
106  BOOST_TEST(&(cv.front()) == &(c.front()));
107  BOOST_TEST(cv.front() == c.front());
108  BOOST_TEST(&(cv.back()) == &(c.back()));
109  BOOST_TEST(cv.back() == c.back());
110 
111  //
112  // range-for iteration
113  //
114  auto ic = cbegin;
115  size_t i = 0;
116  for (auto const& d: cv) {
117  BOOST_TEST(d == *ic);
118  BOOST_TEST(cv[i] == d);
119  BOOST_TEST(&(cv[i]) == &(c[i]));
120  BOOST_TEST(cv.at(i) == d);
121  BOOST_TEST(&(cv.at(i)) == &(c.at(i)));
122 
123  ++i;
124  ++ic;
125  } // for
126  BOOST_TEST((ic == cend));
127 
128 } // BOOST_AUTO_TEST_CASE(DequeTestCase)
auto cbegin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:567
auto cend(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:579
auto makeCollectionView(BeginIter const &b, EndIter const &e)
Creates a CollectionView from the specified iterators.
BOOST_AUTO_TEST_CASE ( ListTestCase  )

Definition at line 132 of file CollectionView_test.cc.

132  {
133  //
134  // test on bidirectional access collection
135  //
136  std::list<int> c{ 3, 4, 5 };
137  auto const cbegin = c.cbegin();
138  auto const cend = c.cend();
139 
140  auto cv = lar::makeCollectionView(cbegin, cend);
141 
142  BOOST_TEST(cv.empty() == c.empty());
143  BOOST_TEST(cv.size() == c.size());
144 
145  //
146  // iterators
147  //
148  BOOST_TEST((cv.cbegin() == c.cbegin()));
149  BOOST_TEST((cv.cend() == c.cend()));
150  BOOST_TEST((cv.crbegin() == c.crbegin()));
151  BOOST_TEST((cv.crend() == c.crend()));
152 
153  //
154  // elements
155  //
156  BOOST_TEST(&(cv.front()) == &(c.front()));
157  BOOST_TEST(cv.front() == c.front());
158  BOOST_TEST(&(cv.back()) == &(c.back()));
159  BOOST_TEST(cv.back() == c.back());
160 
161  //
162  // range-for iteration
163  //
164  auto ic = cbegin;
165  for (auto const& d: cv) {
166  BOOST_TEST(d == *ic);
167 
168  ++ic;
169  } // for
170  BOOST_TEST((ic == cend));
171 
172 } // BOOST_AUTO_TEST_CASE(ListTestCase)
auto cbegin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:567
auto cend(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:579
auto makeCollectionView(BeginIter const &b, EndIter const &e)
Creates a CollectionView from the specified iterators.
BOOST_AUTO_TEST_CASE ( ForwardListTestCase  )

Definition at line 176 of file CollectionView_test.cc.

176  {
177  //
178  // test on forward access collection
179  //
180  std::forward_list<int> c{ 3, 4, 5 };
181  auto const cbegin = c.cbegin();
182  auto const cend = c.cend();
183 
184  auto cv = lar::makeCollectionView(cbegin, cend);
185 
186  BOOST_TEST(cv.empty() == c.empty());
187 
188  //
189  // iterators
190  //
191  BOOST_TEST((cv.cbegin() == c.cbegin()));
192  BOOST_TEST((cv.cend() == c.cend()));
193 
194  //
195  // elements
196  //
197  BOOST_TEST(&(cv.front()) == &(c.front()));
198  BOOST_TEST(cv.front() == c.front());
199 
200  //
201  // range-for iteration
202  //
203  auto ic = cbegin;
204  for (auto const& d: cv) {
205  BOOST_TEST(d == *ic);
206 
207  ++ic;
208  } // for
209  BOOST_TEST((ic == cend));
210 
211 } // BOOST_AUTO_TEST_CASE(ForwardListTestCase)
auto cbegin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:567
auto cend(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:579
auto makeCollectionView(BeginIter const &b, EndIter const &e)
Creates a CollectionView from the specified iterators.
BOOST_AUTO_TEST_CASE ( DocumentationTestCase  )

Definition at line 215 of file CollectionView_test.cc.

215  {
216 
217  std::ostringstream out;
218 
219  /* The promises:
220  *
221  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222  * std::vector<int> range(5);
223  * std::iota(range.begin(), range.end(), 1); // { 1, 2, 3, 4, 5 }
224  *
225  * for (int d: lar::wrapCollectionIntoView(range)) {
226  * std::cout << d << " ";
227  * }
228  * std::cout << std::endl;
229  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
230  * which will print "1 2 3 4 5 ".
231  */
232  out.str("");
233 
234  std::vector<int> range(5);
235  std::iota(range.begin(), range.end(), 1); // { 1, 2, 3, 4, 5 }
236 
237  for (int d: lar::wrapCollectionIntoView(range)) {
238  out << d << " ";
239  }
240  std::cout << out.str() << std::endl;
241 
242  BOOST_TEST(out.str() == "1 2 3 4 5 ");
243 
244  /*
245  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
246  * decltype(auto) view = lar::wrapCollectionIntoView(range);
247  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248  */
249  {
250  out.str("");
251  decltype(auto) view = lar::wrapCollectionIntoView(range);
252 
253  for (int d: view) {
254  out << d << " ";
255  }
256 
257  BOOST_TEST(out.str() == "1 2 3 4 5 ");
258  }
259 
260  /*
261  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
262  * auto const& view = lar::wrapCollectionIntoView(range);
263  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264  */
265  {
266  out.str("");
267  auto const& view = lar::wrapCollectionIntoView(range);
268 
269  for (int d: view) {
270  out << d << " ";
271  }
272 
273  BOOST_TEST(out.str() == "1 2 3 4 5 ");
274  }
275 
276  /* The promise:
277  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
278  * std::vector<int> v(10);
279  * std::iota(v.begin(), v.end(), 0); // { 0, 1, ..., 9 }
280  *
281  * for (int d: lar::makeCollectionView(v.begin() + 4, v.begin() + 7)) {
282  * std::cout << d << " ";
283  * }
284  * std::cout << std::endl;
285  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286  * will print "4 5 6 ".
287  */
288  out.str("");
289  std::vector<int> v(10);
290  std::iota(v.begin(), v.end(), 0); // { 0, 1, ..., 9 }
291 
292  for (int d: lar::makeCollectionView(v.begin() + 4, v.begin() + 7)) {
293  out << d << " ";
294  }
295  std::cout << out.str() << std::endl;
296 
297  BOOST_TEST(out.str() == "4 5 6 ");
298 
299  /* The promise:
300  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
301  * class IntVector {
302  * using vector_t = std::vector<int>;
303  *
304  * vector_t data;
305  *
306  * public:
307  * IntVector(vector_t&& data): data(std::move(data)) {}
308  *
309  * auto begin() const -> decltype(auto) { return data.cbegin(); }
310  * auto end() const -> decltype(auto) { return data.cend(); }
311  *
312  * }; // struct IntVector
313  *
314  * using IntViewBase_t = lar::CollectionView<IntVector>;
315  *
316  * struct MyCollection: public IntViewBase_t {
317  * MyCollection(std::vector<int>&& data) : IntViewBase_t(std::move(data)) {}
318  * }; // class MyCollection
319  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320  */
321  out.str("");
322  {
323  std::vector<int> v_data(10);
324  std::iota(v_data.begin(), v_data.end(), 0); // { 0, 1, ..., 9 }
325 
326  class IntVector {
327  using vector_t = std::vector<int>;
328 
329  vector_t data;
330 
331  public:
332  IntVector(vector_t&& data): data(std::move(data)) {}
333 
334  auto begin() const -> decltype(auto) { return data.cbegin(); }
335  auto end() const -> decltype(auto) { return data.cend(); }
336 
337  }; // struct IntVector
338 
339  using IntViewBase_t = lar::CollectionView<IntVector>;
340 
341  struct MyCollection: public IntViewBase_t {
342  MyCollection(std::vector<int>&& data) : IntViewBase_t(std::move(data)) {}
343  }; // class MyCollection
344 
345  MyCollection v(std::move(v_data));
346 
347  for (int d: v) {
348  out << d << " ";
349  }
350  std::cout << out.str() << std::endl;
351 
352  BOOST_TEST(out.str() == "0 1 2 3 4 5 6 7 8 9 ");
353  }
354 
355 
356 } // BOOST_AUTO_TEST_CASE(DocumentationTestCase)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
Provides features of a collections, from begin and end iterators.
std::vector< int > IntVector
for($it=0;$it< $RaceTrack_number;$it++)
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
auto makeCollectionView(BeginIter const &b, EndIter const &e)
Creates a CollectionView from the specified iterators.
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
CollectionView< Range > const & wrapCollectionIntoView(Range const &c)
Returns the specified container, wrapped in the view.
BEGIN_PROLOG could also be cout