Macros | Functions
counter_test.cc File Reference

Test of util::counter() and support utilities. More...

#include "larcorealg/CoreUtils/counter.h"
#include <boost/test/unit_test.hpp>
#include <vector>
#include <cstddef>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( counter_test )
 

Functions

void test_count_iterator_documentation ()
 
void test_counter_documentation ()
 
void test_infinite_counter_documentation ()
 
 BOOST_AUTO_TEST_CASE (counter_testcase)
 

Detailed Description

Test of util::counter() and support utilities.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.slac..nosp@m.stan.nosp@m.ford..nosp@m.edu)
Date
April 14, 2019

Definition in file counter_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( counter_test )

Definition at line 13 of file counter_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( counter_testcase  )

Definition at line 131 of file counter_test.cc.

131  {
132 
136 
137 } // BOOST_AUTO_TEST_CASE(counter_testcase)
void test_count_iterator_documentation()
Definition: counter_test.cc:22
void test_counter_documentation()
Definition: counter_test.cc:58
void test_infinite_counter_documentation()
Definition: counter_test.cc:96
void test_count_iterator_documentation ( )

Definition at line 22 of file counter_test.cc.

22  {
23 
24  // the promise:
25  /*
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  * std::vector<int> data;
28  * for (util::count_iterator it;; ++it) {
29  * if (*it >= 10) break;
30  * data.push_back(*it); // implicit conversion `std::size_t` to `int`
31  * }
32  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33  * this infinite loop will print `0` on the first iteration, `1` on the second
34  * and so forth, until at the eleventh iteration, just before it can print
35  * `10`, the loop is forcibly broken.
36  *
37  */
38  std::vector<int> data;
39  // BUG Clang 5.0 can't correctly apply explicit (or implicit) deduction rules; Clang 7.0 fixes it
40  // for (util::count_iterator it;; ++it) {
41  for (util::count_iterator<> it;; ++it) {
42  if (*it >= 10) break;
43  data.push_back(*it); // implicit conversion `std::size_t` to `int`
44  }
45 
46  // the test:
47  BOOST_TEST(data.size() == 10U);
48  for (std::size_t i = 0; i < data.size(); ++i) {
49 
50  BOOST_TEST(data[i] == (int) i);
51 
52  } // for
53 
54 } // test_count_iterator_documentation()
An iterator dereferencing to a counter value.
Definition: counter.h:54
void test_counter_documentation ( )

Definition at line 58 of file counter_test.cc.

58  {
59 
60  // the promise:
61  /*
62  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
63  * std::vector<std::size_t> data;
64  * for (auto i: util::counter(4, 8)) {
65  * data.push_back(i);
66  * }
67  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68  * will insert in `data` the numbers from `4` to `7`, just like:
69  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
70  * for (std::size_t i = 4; i < 8; ++i) {
71  * data.push_back(i);
72  * }
73  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74  * would.
75  */
76  std::vector<std::size_t> data;
77  for (auto i: util::counter(4, 8)) {
78  data.push_back(i);
79  }
80 
81  // the test:
82  std::vector<std::size_t> control_data;
83  for (std::size_t i = 4; i < 8; ++i) {
84  control_data.push_back(i);
85  }
86 
87  BOOST_TEST(data.size() == control_data.size());
88  for (std::size_t i = 0; i < data.size(); ++i) {
89  BOOST_TEST(data[i] == control_data[i]);
90  }
91 
92 } // test_counter_documentation()
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
void test_infinite_counter_documentation ( )

Definition at line 96 of file counter_test.cc.

96  {
97 
98  // the promise:
99  /*
100  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
101  * std::vector<unsigned char> data;
102  * for (auto ch: util::infinite_counter<unsigned char>()) {
103  * if (data.size() >= 512U) break;
104  * data.push_back(ch);
105  * }
106  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107  */
108  std::vector<unsigned char> data;
109  for (auto ch: util::infinite_counter<unsigned char>()) {
110  if (data.size() >= 512U) break;
111  data.push_back(ch);
112  }
113 
114  // the test:
115  constexpr std::size_t N = 1U << 8 * sizeof(unsigned char);
116  static_assert(N == 256U); // just in case...
117 
118  BOOST_TEST(data.size() == N * 2);
119  for (std::size_t i = 0; i < data.size(); ++i) {
120 
121  BOOST_TEST(data[i] == static_cast<unsigned char>(i % N));
122 
123  } // for
124 
125 } // test_infinite_counter_documentation()