NestedIterator_test.cc
Go to the documentation of this file.
1 /**
2  * @file NestedIterator_test.cc
3  * @brief Tests nested iterators
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date 20140822
6  * @version 1.0
7  *
8  * See http://www.boost.org/libs/test for the Boost test library home page.
9  *
10  * Timing:
11  * version 1.0 takes negligible time on a 3 GHz machine
12  */
13 
14 // C/C++ standard libraries
15 #include <map>
16 #include <random>
17 #include <iostream>
18 
19 // Boost libraries
20 /*
21  * Boost Magic: define the name of the module;
22  * and do that before the inclusion of Boost unit test headers
23  * because it will change what they provide.
24  * Among the those, there is a main() function and some wrapping catching
25  * unhandled exceptions and considering them test failures, and probably more.
26  * This also makes fairly complicate to receive parameters from the command line
27  * (for example, a random seed).
28  */
29 #define BOOST_TEST_MODULE ( NestedIterator_test )
30 #include "boost/test/unit_test.hpp"
31 
32 // LArSoft libraries
34 
35 
36 /// The seed for the default random engine
37 constexpr unsigned int RandomSeed = 12345;
38 
39 
40 //------------------------------------------------------------------------------
41 //--- Test code
42 //
43 
44 /**
45  * @brief Tests bulk allocator with a vector of vectors
46  *
47  * The test consists in filling a sequence of integers in a two-level structure,
48  * and then iterating to recover the sequence.
49  *
50  * The test fails if the extracted sequence is not correct.
51  */
53 
54  // fill the double tier structure
55  using DoubleVectorI_t = std::vector<std::vector<int>>;
56  DoubleVectorI_t data(1); // get the first vector started
57  constexpr size_t NElements = 10000;
58  constexpr float SwitchProbability = 0.1; // expect about 1000 containers
59 
60  static std::default_random_engine random_engine(RandomSeed);
61  std::uniform_real_distribution<float> uniform(0., 1.);
62 
63  unsigned int nEmpty = 0;
64  for (size_t i = 0; i < NElements; ++i) {
65  // add a new vector (some times)
66  if (uniform(random_engine) < SwitchProbability) {
67  if (data.back().empty()) ++nEmpty;
68  data.emplace_back();
69  }
70  // add the element i to the last vector
71  data.back().push_back(i);
72  } // for
73  std::cout << "Working with " << NElements << " elements in " << data.size()
74  << " vectors (" << nEmpty << " empty) in a vector" << std::endl;
75 
76  unsigned int nMismatches = 0;
77 
78  int expected = 0;
82  while (iElem != eend) {
83  int elem = *iElem;
84  if (elem != expected) ++nMismatches;
85  ++expected;
86  ++iElem;
87  } // while
88 
89  BOOST_TEST((unsigned int) expected == NElements);
90  BOOST_TEST(nMismatches == 0U);
91 } // RunVectorVectorTest()
92 
93 
94 /**
95  * @brief Tests bulk allocator with a map of vectors
96  *
97  * The test consists in filling a sequence of integers in a two-level structure,
98  * and then iterating to recover the sequence.
99  *
100  * The test fails if the extracted sequence is not correct.
101  */
103 
104  // fill the double tier structure
105  using VectorMapI_t = std::map<int, std::vector<int>>;
106  VectorMapI_t data; // get the first vector started
107  //data.emplace(0, std::vector<int>{});
108  data[0] = {};
109  constexpr size_t NElements = 10000;
110  constexpr float SwitchProbability = 0.1; // expect about 1000 containers
111 
112  static std::default_random_engine random_engine(RandomSeed);
113  std::uniform_real_distribution<float> uniform(0., 1.);
114 
115  unsigned int nEmpty = 0;
116  for (size_t i = 0; i < NElements; ++i) {
117  // add a new map (some times)
118  if (uniform(random_engine) < SwitchProbability) {
119  if (data.rbegin()->second.empty()) ++nEmpty;
120  data.insert({ data.size(), {} });
121  }
122  // add the element i to the last vector
123  data.rbegin()->second.push_back(i);
124  } // for
125  std::cout << "Working with " << NElements << " elements in " << data.size()
126  << " vectors (" << nEmpty << " empty) in a map" << std::endl;
127 
128  unsigned int nMismatches = 0;
129 
130  int expected = 0;
131  using ConstIterator_t = lar::double_fwd_const_iterator
133  ConstIterator_t iElem(data, ConstIterator_t::begin),
134  eend(data, ConstIterator_t::end);
135  while (iElem != eend) {
136  int elem = *iElem;
137  if (elem != expected) ++nMismatches;
138  ++expected;
139  ++iElem;
140  } // while
141 
142  BOOST_TEST((unsigned int) expected == NElements);
143  BOOST_TEST(nMismatches == 0U);
144 } // RunVectorMapTest()
145 
146 
147 //------------------------------------------------------------------------------
148 //--- registration of tests
149 //
150 // Boost needs now to know which tests we want to run.
151 // Tests are "automatically" registered, hence the BOOST_AUTO_TEST_CASE()
152 // macro name. The argument is a name for the test; each test will have a
153 // number of checks and it will fail if any of them does.
154 //
155 
156 BOOST_AUTO_TEST_CASE(RunVectorVector) {
158 }
159 
160 BOOST_AUTO_TEST_CASE(RunVectorMap) {
162 }
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
BOOST_AUTO_TEST_CASE(RunVectorVector)
const char expected[]
Definition: Exception_t.cc:22
intermediate_table::const_iterator const_iterator
constexpr unsigned int RandomSeed
The seed for the default random engine.
void RunVectorVectorTest()
Tests bulk allocator with a vector of vectors.
Internal helper class: actual implementation of nested iterator.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
Iterators recursing though nested collections.
QTextStream & endl(QTextStream &s)
void RunVectorMapTest()
Tests bulk allocator with a map of vectors.