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

Tests nested iterators. More...

#include <map>
#include <random>
#include <iostream>
#include "boost/test/unit_test.hpp"
#include "lardata/Utilities/NestedIterator.h"

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( NestedIterator_test )
 

Functions

void RunVectorVectorTest ()
 Tests bulk allocator with a vector of vectors. More...
 
void RunVectorMapTest ()
 Tests bulk allocator with a map of vectors. More...
 
 BOOST_AUTO_TEST_CASE (RunVectorVector)
 
 BOOST_AUTO_TEST_CASE (RunVectorMap)
 

Variables

constexpr unsigned int RandomSeed = 12345
 The seed for the default random engine. More...
 

Detailed Description

Tests nested iterators.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
20140822
Version
1.0

See http://www.boost.org/libs/test for the Boost test library home page.

Timing: version 1.0 takes negligible time on a 3 GHz machine

Definition in file NestedIterator_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( NestedIterator_test )

Definition at line 29 of file NestedIterator_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( RunVectorVector  )

Definition at line 156 of file NestedIterator_test.cc.

156  {
158 }
void RunVectorVectorTest()
Tests bulk allocator with a vector of vectors.
BOOST_AUTO_TEST_CASE ( RunVectorMap  )

Definition at line 160 of file NestedIterator_test.cc.

160  {
162 }
void RunVectorMapTest()
Tests bulk allocator with a map of vectors.
void RunVectorMapTest ( )

Tests bulk allocator with a map of vectors.

The test consists in filling a sequence of integers in a two-level structure, and then iterating to recover the sequence.

The test fails if the extracted sequence is not correct.

Definition at line 102 of file NestedIterator_test.cc.

102  {
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
132  <VectorMapI_t::const_iterator, lar::PairSecond<VectorMapI_t::value_type>>;
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()
constexpr unsigned int RandomSeed
The seed for the default random engine.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
Internal helper class: actual implementation of nested iterator.
BEGIN_PROLOG could also be cout
void RunVectorVectorTest ( )

Tests bulk allocator with a vector of vectors.

The test consists in filling a sequence of integers in a two-level structure, and then iterating to recover the sequence.

The test fails if the extracted sequence is not correct.

Definition at line 52 of file NestedIterator_test.cc.

52  {
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()
constexpr unsigned int RandomSeed
The seed for the default random engine.
Internal helper class: actual implementation of nested iterator.
BEGIN_PROLOG could also be cout

Variable Documentation

constexpr unsigned int RandomSeed = 12345

The seed for the default random engine.

Definition at line 37 of file NestedIterator_test.cc.