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

Unit test for DereferenceIterator class. More...

#include <boost/test/unit_test.hpp>
#include "larcorealg/CoreUtils/DereferenceIterator.h"
#include <iterator>
#include <memory>
#include <utility>
#include <vector>
#include <list>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( DereferenceIterator_test )
 

Functions

template<template< typename T, typename...> class SeqCont>
void test_DereferenceIterator ()
 
 BOOST_AUTO_TEST_CASE (DereferenceIterator_testcase)
 

Detailed Description

Unit test for DereferenceIterator class.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
November 18th, 2016
See Also
DereferenceIterator.h

Definition in file DereferenceIterator_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( DereferenceIterator_test )

Definition at line 10 of file DereferenceIterator_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( DereferenceIterator_testcase  )

Definition at line 118 of file DereferenceIterator_test.cc.

118  {
119 
120  test_DereferenceIterator<std::vector>();
121 
122  test_DereferenceIterator<std::list>();
123 
124 } // BOOST_AUTO_TEST_CASE(DereferenceIterator_testcase)
template<template< typename T, typename...> class SeqCont>
void test_DereferenceIterator ( )

Definition at line 24 of file DereferenceIterator_test.cc.

24  {
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<>()
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.
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.