Classes | Macros | Functions
span_test.cc File Reference

Unit test for util::span class. More...

#include <boost/test/unit_test.hpp>
#include <boost/iterator/indirect_iterator.hpp>
#include "larcorealg/CoreUtils/span.h"
#include "larcorealg/CoreUtils/enumerate.h"
#include "larcorealg/CoreUtils/zip.h"
#include "larcorealg/CoreUtils/counter.h"
#include "larcorealg/CoreUtils/operations.h"
#include <iostream>
#include <vector>
#include <memory>
#include <numeric>
#include <type_traits>

Go to the source code of this file.

Classes

struct  SpanDocumentation1TestClass
 
struct  makeAdaptedSpanDocumentation1TestClass
 
struct  makeTransformedSpanDocumentation1TestClass
 

Macros

#define BOOST_TEST_MODULE   ( RealComparisons_test )
 

Functions

template<typename Iter , typename Cont >
void test_span (Cont &v)
 
template<typename Iter , typename Cont >
void test_const_span (Cont &v)
 
template<typename Iter , typename Cont >
void test_adapted_span (Cont &v)
 
template<typename Iter , typename Cont >
void test_transformed_span (Cont &v)
 
template<typename Iter , typename Cont >
void test_transformed_span_with_unmoveable_values (Cont &v)
 
 BOOST_AUTO_TEST_CASE (span_testcase)
 
 BOOST_AUTO_TEST_CASE (adapted_span_testcase)
 
 BOOST_AUTO_TEST_CASE (transformed_span_testcase)
 
 BOOST_AUTO_TEST_CASE (span_documentation_testcase)
 
 BOOST_AUTO_TEST_CASE (adapted_span_documentation_testcase)
 
 BOOST_AUTO_TEST_CASE (transformed_span_documentation_testcase)
 

Detailed Description

Unit test for util::span class.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.slac..nosp@m.stan.nosp@m.ford..nosp@m.edu)
Date
January 29, 2019
See also
larcorealg/CoreUtils/span.h

Definition in file span_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( RealComparisons_test )

Definition at line 10 of file span_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( span_testcase  )

Definition at line 372 of file span_test.cc.

372  {
373 
374  using TestVector_t = std::vector<int>;
375 
376  TestVector_t ev;
377  test_span<TestVector_t::iterator>(ev);
378  test_const_span<TestVector_t::const_iterator>(ev);
379 
380  TestVector_t v3 { 1, 2, 3 };
381  test_span<TestVector_t::iterator>(v3);
382  test_const_span<TestVector_t::const_iterator>(v3);
383 
384  TestVector_t const cv4 { 1, 2, 3, 4 };
385  test_span<TestVector_t::const_iterator>(cv4);
386  test_const_span<TestVector_t::const_iterator>(cv4);
387 
388 
389 } // BOOST_AUTO_TEST_CASE(span_testcase)
BOOST_AUTO_TEST_CASE ( adapted_span_testcase  )

Definition at line 393 of file span_test.cc.

393  {
394 
395  using TestVector_t = std::vector<int>;
396 
397  TestVector_t ev;
398  test_adapted_span<TestVector_t::iterator>(ev);
399 
400  TestVector_t v3 { 1, 2, 3 };
401  test_adapted_span<TestVector_t::iterator>(v3);
402 
403  TestVector_t const cv4 { 1, 2, 3, 4 };
404  test_adapted_span<TestVector_t::const_iterator>(cv4);
405 
406 } // BOOST_AUTO_TEST_CASE(adapted_span_testcase)
BOOST_AUTO_TEST_CASE ( transformed_span_testcase  )

Definition at line 410 of file span_test.cc.

410  {
411 
412  using TestVector_t = std::vector<int>;
413 
414  TestVector_t ev;
415  test_transformed_span<TestVector_t::iterator>(ev);
416  test_transformed_span_with_unmoveable_values<TestVector_t::iterator>(ev);
417 
418  TestVector_t v3 { 1, 2, 3 };
419  test_transformed_span<TestVector_t::iterator>(v3);
420 
421  TestVector_t const cv4 { 1, 2, 3, 4 };
422  test_transformed_span<TestVector_t::const_iterator>(cv4);
423 
424 } // BOOST_AUTO_TEST_CASE(transformed_span_testcase)
BOOST_AUTO_TEST_CASE ( span_documentation_testcase  )

Definition at line 428 of file span_test.cc.

428  {
429 
430  // this only checks that the example compiles and runs
432  doc1.analyse();
433 
434 } // BOOST_AUTO_TEST_CASE(span_documentation_testcase)
BOOST_AUTO_TEST_CASE ( adapted_span_documentation_testcase  )

Definition at line 438 of file span_test.cc.

438  {
439 
441  doc1.analyse();
442 
443 } // BOOST_AUTO_TEST_CASE(span_documentation_testcase)
BOOST_AUTO_TEST_CASE ( transformed_span_documentation_testcase  )

Definition at line 447 of file span_test.cc.

447  {
448 
450  doc1.analyse();
451 
452 } // BOOST_AUTO_TEST_CASE(span_documentation_testcase)
template<typename Iter , typename Cont >
void test_adapted_span ( Cont &  v)

Definition at line 99 of file span_test.cc.

99  {
100 
101  /*
102  * In this test we create a container of pointers to `v` elements and iterate
103  * `v` values through that one. The iteration is supposed to be transparent,
104  * with the iteration code not expressing that we are passing via pointers.
105  */
106 
107  std::vector<typename Iter::pointer> ptrs;
108  std::transform
109  (v.begin(), v.end(), std::back_inserter(ptrs), [](auto& v){ return &v; });
110 
112  (ptrs, [](auto&& iter){ return boost::make_indirect_iterator(iter); });
113 
114  // check the type of the object
115  static_assert(
116  std::is_same<typename decltype(r)::value_type, typename Iter::value_type>()
117  );
118 
119  BOOST_TEST(r.empty() == v.empty());
120  BOOST_TEST(r.size() == v.size());
121 
122  unsigned int n = 0;
123  for (auto&& [ rangeValue, value ]: util::zip(r, v)) {
124  BOOST_TEST(&rangeValue == &value);
125  BOOST_TEST(rangeValue == value);
126  ++n;
127  } // for
128  BOOST_TEST(n == v.size());
129 
130 } // test_adapted_span()
auto make_adapted_span(BIter begin, EIter end, Adaptor &&adaptor)
Definition: span.h:223
std::void_t< T > n
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
template<typename Iter , typename Cont >
void test_const_span ( Cont &  v)

Definition at line 65 of file span_test.cc.

65  {
66 
67  auto r = util::make_const_span(v);
68 
69  // check the type of the object
70  static_assert
71  (std::is_same<decltype(r), util::span<Iter>>());
72  static_assert(std::is_same<decltype(r), util::span<Iter, Iter>>());
73  static_assert(
74  std::is_same<typename decltype(r)::value_type, typename Iter::value_type>()
75  );
76  static_assert
77  (std::is_same<typename decltype(r)::reference, typename Iter::reference>());
78 
79  BOOST_TEST((r.begin() == v.cbegin()));
80  BOOST_TEST((r.end() == v.cend()));
81 
82  BOOST_TEST(r.empty() == v.empty());
83  BOOST_TEST(r.size() == v.size());
84 
85  auto iV = v.cbegin();
86  unsigned int n = 0;
87  for (auto i: r) {
88  BOOST_TEST(i == *iV);
89  ++n;
90  ++iV;
91  } // for
92  BOOST_TEST(n == v.size());
93 
94 } // test_const_span()
std::void_t< T > n
auto make_const_span(Cont &cont)
Creates a span with constant iterator access from a container type.
Definition: span.h:206
template<typename Iter , typename Cont >
void test_span ( Cont &  v)

Definition at line 31 of file span_test.cc.

31  {
32 
33  auto r = util::make_span(v);
34 
35  // check the type of the object
36  static_assert
37  (std::is_same<decltype(r), util::span<Iter>>());
38  static_assert(std::is_same<decltype(r), util::span<Iter, Iter>>());
39  static_assert(
40  std::is_same<typename decltype(r)::value_type, typename Iter::value_type>()
41  );
42  static_assert
43  (std::is_same<typename decltype(r)::reference, typename Iter::reference>());
44 
45  BOOST_TEST((r.begin() == v.begin()));
46  BOOST_TEST((r.end() == v.end()));
47 
48  BOOST_TEST(r.empty() == v.empty());
49  BOOST_TEST(r.size() == v.size());
50 
51  auto iV = v.cbegin();
52  unsigned int n = 0;
53  for (auto i: r) {
54  BOOST_TEST(i == *iV);
55  ++n;
56  ++iV;
57  } // for
58  BOOST_TEST(n == v.size());
59 
60 } // test_span()
auto make_span(BIter begin, EIter end)
Creates a span from specified iterators (can use constructor instead).
Definition: span.h:197
std::void_t< T > n
template<typename Iter , typename Cont >
void test_transformed_span ( Cont &  v)

Definition at line 135 of file span_test.cc.

135  {
136 
137  /*
138  * In this test we create a container of pointers to `v` elements and iterate
139  * `v` values through that one. The iteration is supposed to be transparent,
140  * with the iteration code not expressing that we are passing via pointers,
141  * because of our transformation dereferencing the pointers.
142  */
143 
144  using pointer_t = typename std::iterator_traits<Iter>::pointer;
145  using reference_t = typename std::iterator_traits<Iter>::reference;
146 
147  std::vector<pointer_t> ptrs;
148  std::transform
149  (v.begin(), v.end(), std::back_inserter(ptrs), [](auto& v){ return &v; });
150 
152  (ptrs, [](auto* ptr) -> reference_t { return *ptr; });
153 
154  // check the type of the object
155  static_assert(
156  std::is_same<typename decltype(r)::value_type, typename Iter::value_type>()
157  );
158 
159  BOOST_TEST(r.empty() == v.empty());
160  BOOST_TEST(r.size() == v.size());
161 
162  unsigned int n = 0;
163  for (auto&& [ rangeValue, value ]: util::zip(r, v)) {
164  BOOST_TEST(&rangeValue == &value);
165  BOOST_TEST(rangeValue == value);
166  ++n;
167  } // for
168  BOOST_TEST(n == v.size());
169 
170 } // test_transformed_span()
auto make_transformed_span(BIter begin, EIter end, Op &&op)
Definition: span.h:294
std::void_t< T > n
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
template<typename Iter , typename Cont >
void test_transformed_span_with_unmoveable_values ( Cont &  v)

Definition at line 175 of file span_test.cc.

175  {
176 
177  /*
178  * In this test we create a container of unique_ptr to `v` elements and
179  * iterate `v` values through it. The iteration is supposed to be transparent,
180  * with the iteration code not expressing that we are passing via pointers,
181  * because of our transformation dereferencing the pointers.
182  */
183 
184  using value_t = typename std::iterator_traits<Iter>::value_type;
185  using pointer_t = std::unique_ptr<value_t>;
186 
187  std::vector<pointer_t> ptrs;
188  std::transform(
189  v.begin(), v.end(), std::back_inserter(ptrs),
190  [](auto& v){ return std::make_unique<value_t>(v); }
191  );
192 
194 
195  // check the type of the object
196  static_assert(std::is_same<typename decltype(r)::value_type, value_t>());
197 
198  BOOST_TEST(r.empty() == v.empty());
199  BOOST_TEST(r.size() == v.size());
200 
201  unsigned int n = 0;
202  for (auto&& [ rangeValue, value ]: util::zip(r, v)) {
203  BOOST_TEST(&rangeValue == &value);
204  BOOST_TEST(rangeValue == value);
205  ++n;
206  } // for
207  BOOST_TEST(n == v.size());
208 
209 } // test_transformed_span_with_unmoveable_values()
auto make_transformed_span(BIter begin, EIter end, Op &&op)
Definition: span.h:294
std::void_t< T > n
decltype(auto) dereference()
Returns a functor that returns *ptr of its argument ptr.
Definition: operations.h:125
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295