Macros | Functions
values_test.cc File Reference
#include "larcorealg/CoreUtils/values.h"
#include <boost/test/unit_test.hpp>
#include <numeric>
#include <vector>
#include <map>
#include <utility>
#include <cstddef>

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   ( values_test )
 

Functions

void test_values ()
 
void test_const_values ()
 
template<template< typename Key, typename Value, typename...Args > typename Map>
void test_values_map ()
 
template<template< typename Key, typename Value, typename...Args > typename Map>
void test_const_values_map ()
 
void test_values_documentation ()
 
 BOOST_AUTO_TEST_CASE (values_testcase)
 

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( values_test )

Definition at line 14 of file values_test.cc.

Function Documentation

BOOST_AUTO_TEST_CASE ( values_testcase  )

Definition at line 191 of file values_test.cc.

191  {
192 
193  test_values();
195  test_values_map<std::map>();
196  test_const_values_map<std::map>();
197 
199 
200 } // BOOST_AUTO_TEST_CASE(values_testcase)
void test_values()
Definition: values_test.cc:26
void test_const_values()
Definition: values_test.cc:74
void test_values_documentation()
Definition: values_test.cc:159
void test_const_values ( )

Definition at line 74 of file values_test.cc.

74  {
75 
76  // prepare the data set
77  constexpr std::size_t N = 7U;
78 
79  std::vector<float> data(N);
80  std::iota(data.begin(), data.end(), 0.0F);
81 
82  //
83  // static checks
84  //
85  // for a vector, we expect exactly the same vector object as return value:
86  static_assert(std::is_same_v<
87  decltype(util::const_values(data)),
88  std::add_lvalue_reference_t<std::vector<float> const>
89  >
90  );
91  BOOST_TEST(&util::const_values(data) == &data);
92 
93  //
94  // read-only access
95  //
96  std::size_t i = 0U;
97  for (auto&& value: util::const_values(data)) {
98  static_assert(std::is_const_v<std::remove_reference_t<decltype(value)>>);
99  BOOST_TEST(value == data[i]);
100  BOOST_TEST(std::addressof(value) == std::addressof(data[i]));
101  ++i;
102  } // for
103  BOOST_TEST(i == data.size());
104 
105 } // test_const_values()
decltype(auto) const_values(Coll &&coll)
Range-for loop helper iterating across the constant values of the specified collection.
template<template< typename Key, typename Value, typename...Args > typename Map>
void test_const_values_map ( )

Definition at line 135 of file values_test.cc.

135  {
136 
137  // prepare the data set
138  constexpr std::size_t N = 7U;
139  constexpr double factor = 5.0;
140 
141  std::vector<int> keys(N);
142  std::iota(keys.begin(), keys.end(), 0U);
143  Map<int, float> data;
144  for (int key: keys) data.emplace(key, key * factor);
145 
146  std::size_t i = 0U;
147  for (auto&& value: util::const_values(data)) {
148  static_assert(std::is_same_v<decltype(value), float const&>);
149  BOOST_TEST(value == data[i]);
150  BOOST_TEST(std::addressof(value) == std::addressof(data[i]));
151  ++i;
152  } // for
153  BOOST_TEST(i == data.size());
154 
155 } // test_const_values_map()
decltype(auto) const_values(Coll &&coll)
Range-for loop helper iterating across the constant values of the specified collection.
def key(type, name=None)
Definition: graph.py:13
void test_values ( )

Definition at line 26 of file values_test.cc.

26  {
27 
28  // prepare the data set
29  constexpr std::size_t N = 7U;
30 
31  std::vector<float> data(N);
32  std::iota(data.begin(), data.end(), 0.0F);
33 
34  //
35  // static checks
36  //
37  // for a vector, we expect exactly the same vector object as return value:
38  static_assert(std::is_same_v<
39  decltype(util::values(data)),
40  std::add_lvalue_reference_t<std::vector<float>>
41  >
42  );
43  BOOST_TEST(&util::values(data) == &data);
44 
45  //
46  // read-only access
47  //
48  {
49  std::size_t i = 0U;
50  for (auto&& value: util::values(std::as_const(data))) {
51  BOOST_TEST(value == data[i]);
52  BOOST_TEST(std::addressof(value) == std::addressof(data[i]));
53  ++i;
54  } // for
55  BOOST_TEST(i == data.size());
56  }
57 
58  //
59  // read/write access
60  //
61  {
62  std::size_t i = 0U;
63  for (auto&& value: util::values(data)) {
64  BOOST_TEST(value == data[i]);
65  BOOST_TEST(std::addressof(value) == std::addressof(data[i]));
66  ++i;
67  } // for
68  BOOST_TEST(i == data.size());
69  }
70 
71 } // test_values()
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
void test_values_documentation ( )

Definition at line 159 of file values_test.cc.

159  {
160  /*
161  * The promise:
162  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
163  * std::map<int, float> data { { 1, 4.0F }, { 3, 12.0F }, { 2, 8.0F } };
164  * std::vector<float> values;
165  *
166  * for (float value: util::values(data))
167  * values.push_back(value);
168  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169  * will result in `values` vector being of size `3` and with values
170  * `{ 4.0F, 8.0F, 12.0F }`
171  */
172 
173  std::map<int, float> data { { 1, 4.0F }, { 3, 12.0F }, { 2, 8.0F } };
174  std::vector<float> values;
175 
176  for (float value: util::values(data))
177  values.push_back(value);
178 
179  // the test
180  BOOST_TEST(values.size() == 3U);
181  BOOST_TEST(values[0] == 4.0F);
182  BOOST_TEST(values[1] == 8.0F);
183  BOOST_TEST(values[2] == 12.0F);
184 
185 } // test_values_documentation()
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
Q_UINT16 values[128]
template<template< typename Key, typename Value, typename...Args > typename Map>
void test_values_map ( )

Definition at line 110 of file values_test.cc.

110  {
111 
112  // prepare the data set
113  constexpr std::size_t N = 7U;
114  constexpr double factor = 5.0;
115 
116  std::vector<int> keys(N);
117  std::iota(keys.begin(), keys.end(), 0U);
118  Map<int, float> data;
119  for (int key: keys) data.emplace(key, key * factor);
120 
121  std::size_t i = 0U;
122  for (auto&& value: util::values(data)) {
123  static_assert(std::is_same_v<decltype(value), float&>);
124  BOOST_TEST(value == data[i]);
125  BOOST_TEST(std::addressof(value) == std::addressof(data[i]));
126  ++i;
127  } // for
128  BOOST_TEST(i == data.size());
129 
130 } // test_values_map()
def key(type, name=None)
Definition: graph.py:13
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.