All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
counter_test.cc
Go to the documentation of this file.
1 /**
2  * @file counter_test.cc
3  * @brief Test of `util::counter()` and support utilities.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date April 14, 2019
6  *
7  */
8 
9 // testing library
11 
12 // Boost libraries
13 #define BOOST_TEST_MODULE ( counter_test )
14 #include <boost/test/unit_test.hpp>
15 
16 // C/C++ libraries
17 #include <vector>
18 #include <cstddef> // std::size_t
19 
20 
21 //------------------------------------------------------------------------------
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()
55 
56 
57 //------------------------------------------------------------------------------
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()
93 
94 
95 // -----------------------------------------------------------------------------
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()
126 
127 
128 // -----------------------------------------------------------------------------
129 // BEGIN Test cases -----------------------------------------------------------
130 // -----------------------------------------------------------------------------
131 BOOST_AUTO_TEST_CASE(counter_testcase) {
132 
136 
137 } // BOOST_AUTO_TEST_CASE(counter_testcase)
138 
139 
140 // -----------------------------------------------------------------------------
141 // END Test cases -------------------------------------------------------------
142 // -----------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE(AllTests)
An iterator dereferencing to a counter value.
Definition: counter.h:54
void test_count_iterator_documentation()
Definition: counter_test.cc:22
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_counter_documentation()
Definition: counter_test.cc:58
Test of util::counter and support utilities.
void test_infinite_counter_documentation()
Definition: counter_test.cc:96
process_name largeant stream1 can override from command line with o or output physics producers generator N