All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DereferenceIterator_test.cc
Go to the documentation of this file.
1 /**
2  * @file DereferenceIterator_test.cc
3  * @brief Unit test for DereferenceIterator class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 18th, 2016
6  * @see DereferenceIterator.h
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE ( DereferenceIterator_test )
11 #include <boost/test/unit_test.hpp>
12 
13 // LArSoft libraries
15 
16 // C/C++ standard libraries
17 #include <iterator> // std::back_inserter()
18 #include <memory> // std::make_unique()
19 #include <utility> // std::move()
20 #include <vector>
21 #include <list>
22 
23 template <template <typename T, typename...> class SeqCont>
25 
26  using Container_t = SeqCont<std::unique_ptr<size_t>>;
27 
28  //
29  // create the dummy structure (not a very smart one)
30  //
31  Container_t v;
32 
33  auto inserter = std::back_inserter(v);
34  for (size_t i = 0; i < 10; ++i)
35  *(inserter++) = std::make_unique<size_t>(i);
36 
37  //
38  // test that we can loop and find the correct value in a forward loop
39  //
42  auto it = begin;
43  BOOST_TEST((it == begin));
44  for (size_t i = 0; i < 10; ++i, ++it) {
45 
46  BOOST_TEST(*it == i);
47 
48  } // for i
49  BOOST_TEST((it == end));
50 
51  //
52  // test that we can loop and find the correct value in a reversed loop
53  //
54  auto rbegin = lar::util::rbeginDereferenceIterator(v);
56  auto rit = rbegin;
57  BOOST_TEST((rit == rbegin));
58  for (size_t i = 0; i < 10; ++i, ++rit) {
59 
60  BOOST_TEST(*rit == v.size() - i - 1);
61 
62  } // for i
63  BOOST_TEST((rit == rend));
64 
65  //
66  // test that we can do a ranged-for loop
67  //
68  size_t index = 0;
69  for (size_t& i: lar::util::dereferenceIteratorLoop(v)) {
70 
71  BOOST_TEST(i == index);
72 
73  ++index;
74  } // for i
75 
76  //
77  // test that we can do a constant ranged-for loop
78  //
79  index = 0; // reset
80  auto const& cv = v; // constant version
81  for (auto& i: lar::util::dereferenceConstIteratorLoop(cv)) {
82 
83  static_assert(std::is_const<std::remove_reference_t<decltype(i)>>::value,
84  "Dereferenced value from constant vector is not constant");
85 
86  BOOST_TEST(i == index);
87 
88  ++index;
89  } // for i
90 
91  //
92  // test that we can do a constant ranged-for loop also on a non-const coll.
93  //
94  index = 0; // reset
95  for (auto& i: lar::util::dereferenceConstIteratorLoop(v)) {
96 
97  static_assert(std::is_const<std::remove_reference_t<decltype(i)>>::value,
98  "Dereferenced value from constant vector is not constant");
99 
100  BOOST_TEST(i == index);
101 
102  ++index;
103 
104  } // for i
105 
106  //
107  // check that we can write in a normal loop
108  //
109  for (auto& i: lar::util::dereferenceIteratorLoop(v)) i = 10;
110 
112  BOOST_TEST(i == 10);
113 
114 
115 } // test_DereferenceIterator<>()
116 
117 
118 BOOST_AUTO_TEST_CASE(DereferenceIterator_testcase) {
119 
120  test_DereferenceIterator<std::vector>();
121 
122  test_DereferenceIterator<std::list>();
123 
124 } // BOOST_AUTO_TEST_CASE(DereferenceIterator_testcase)
BOOST_AUTO_TEST_CASE(AllTests)
auto dereferenceConstIteratorLoop(Cont &cont)
Returns an object enabling a dereferencing range-for loop.
auto dereferenceIteratorLoop(Cont &cont)
Returns an object enabling a dereferencing range-for loop.
auto rendDereferenceIterator(Cont &cont)
Returns a dereference reverse iterator to the end of container.
auto beginDereferenceIterator(Cont &cont)
Returns a dereference iterator to the begin of specified container.
void test_DereferenceIterator()
Offer iterators automatically dereferencing their values.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
auto rbeginDereferenceIterator(Cont &cont)
Returns a dereference reverse iterator to the begin of container.
temporary value
auto endDereferenceIterator(Cont &cont)
Returns a dereference iterator to the end of specified container.