Classes | Macros | Functions
MappedContainer_test.cc File Reference

Tests the classes in MappedContainer.h. More...

#include <boost/test/unit_test.hpp>
#include "lardataalg/Utilities/MappedContainer.h"
#include "larcorealg/CoreUtils/ContainerMeta.h"
#include <iostream>
#include <array>
#include <functional>
#include <algorithm>
#include <iterator>
#include <type_traits>
#include <cstddef>
#include <limits>

Go to the source code of this file.

Classes

struct  TestDataMakerBase
 
struct  TestDataMakerClass< Cont >
 
struct  TestDataMakerClass< std::array< T, N > >
 
struct  TestDataMakerClass< std::unique_ptr< T[]> >
 

Macros

#define BOOST_TEST_MODULE   ( MappedContainer_test )
 

Functions

template<typename Cont >
auto makeTestData ()
 
template<typename Cont >
void copyTest ()
 
template<typename Cont >
void referenceTest ()
 
void defaultConstructorTest ()
 
void autosizeTest ()
 
void classDoc1Test ()
 
 BOOST_AUTO_TEST_CASE (TestCase)
 
 BOOST_AUTO_TEST_CASE (DocumentationTestCase)
 

Detailed Description

Tests the classes in MappedContainer.h.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
March 22, 2019
Version
1.0
See also
lardataalg/Utilities/MappedContainer.h

Definition in file MappedContainer_test.cc.

Macro Definition Documentation

#define BOOST_TEST_MODULE   ( MappedContainer_test )

Definition at line 11 of file MappedContainer_test.cc.

Function Documentation

void autosizeTest ( )

Definition at line 336 of file MappedContainer_test.cc.

336  {
337  /*
338  * Tests that the size is correctly evinced from the mapping.
339  */
341 
342  // BUG the double brace syntax is required to work around clang bug 21629
343  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
344  std::array<double, 4U> const data {{ 0.0, -1.0, -2.0, -3.0 }};
345 
346  std::array<std::size_t, 6U> const mapping = {{
347  1U, 0U, InvalidIndex,
348  3U, 2U, InvalidIndex,
349  }};
350 
351  util::MappedContainer const mappedData(std::ref(data), mapping);
352 
353  BOOST_TEST(mappedData.size() == mapping.size());
354  BOOST_TEST(mappedData.defaultValue() == double{});
355 
356 } // autosizeTest()
A meta-container providing transparent mapping on top of another.
static constexpr T invalidIndex()
const Index InvalidIndex
BOOST_AUTO_TEST_CASE ( TestCase  )

Definition at line 410 of file MappedContainer_test.cc.

410  {
411  copyTest<std::array<double, 10U>>();
412  referenceTest<std::array<double, 10U>>();
413  referenceTest<std::unique_ptr<double[]>>();
415  autosizeTest();
416 } // TestCase
void autosizeTest()
void defaultConstructorTest()
BOOST_AUTO_TEST_CASE ( DocumentationTestCase  )

Definition at line 418 of file MappedContainer_test.cc.

418  {
419  classDoc1Test();
420 } // DocumentationTestCase
void classDoc1Test()
void classDoc1Test ( )

Definition at line 360 of file MappedContainer_test.cc.

360  {
361  /*
362  * constexpr auto InvalidIndex = util::MappedContainerBase::invalidIndex();
363  *
364  * std::array<double, 4U> const data { 0.0, -1.0, -2.0, -3.0 };
365  *
366  * std::array<std::size_t, 6U> const mapping = {
367  * 1U, 0U, InvalidIndex,
368  * 3U, 2U, InvalidIndex,
369  * };
370  *
371  * util::MappedContainer const mappedData
372  * (std::ref(data), mapping, 6U, std::numeric_limits<double>::quiet_NaN());
373  *
374  * for (std::size_t i = 0; i < 6U; ++i) {
375  * std::cout
376  * << "Mapped element #" << i << ": " << mappedData[i] << std::endl;
377  * }
378  */
379  // BEGIN example -------------------------------------------------------------
381 
382  // BUG the double brace syntax is required to work around clang bug 21629
383  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
384  std::array<double, 4U> const data {{ 0.0, -1.0, -2.0, -3.0 }};
385 
386  std::array<std::size_t, 6U> const mapping = {{
387  1U, 0U, InvalidIndex,
388  3U, 2U, InvalidIndex,
389  }};
390 
391  util::MappedContainer const mappedData
392  (std::ref(data), mapping, 6U, std::numeric_limits<double>::quiet_NaN());
393 
394  for (std::size_t i = 0; i < 6U; ++i) {
395  std::cout
396  << "Mapped element #" << i << ": " << mappedData[i] << std::endl;
397  }
398  // END example ---------------------------------------------------------------
399 
400 } // classDoc1Test()
A meta-container providing transparent mapping on top of another.
static constexpr T invalidIndex()
const Index InvalidIndex
QTextStream & endl(QTextStream &s)
template<typename Cont >
void copyTest ( )

Definition at line 85 of file MappedContainer_test.cc.

85  {
86 
87  using Container_t = Cont;
88  using Literal_t = util::collection_value_t<Container_t>;
89  using Data_t = Literal_t;
90 
91  constexpr Literal_t defaultValue { 42 };
92 
94 
95  auto const data = makeTestData<Cont>();
96 
97  static_assert(std::is_copy_assignable_v<std::decay_t<decltype(data)>>);
98  static_assert(std::is_copy_constructible_v<std::decay_t<decltype(data)>>);
99  static_assert(std::is_move_assignable_v<std::decay_t<decltype(data)>>);
100  static_assert(std::is_move_constructible_v<std::decay_t<decltype(data)>>);
101 
102  // BUG the double brace syntax is required to work around clang bug 21629
103  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
104  std::array<std::size_t, 12U> const mapping1 = {{
105  4U, 3U, 2U, 1U, 0U, InvalidIndex,
106  9U, 8U, 7U, 6U, 5U, InvalidIndex,
107  }};
108 
109  std::array<Data_t, 12U> const expectedMappedData1 = {{
110  -4, -3, -2, -1, 0, defaultValue,
111  -9, -8, -7, -6, -5, defaultValue
112  }};
113 
114  util::MappedContainer const mappedData1
115  (data, mapping1, expectedMappedData1.size(), defaultValue);
116 
117  BOOST_TEST(mappedData1.size() == expectedMappedData1.size());
118  BOOST_TEST(mappedData1.minimal_size() == expectedMappedData1.size());
119 
120  BOOST_TEST(mappedData1.front() == expectedMappedData1.front());
121  BOOST_TEST(mappedData1.back() == expectedMappedData1.back());
122 
123  std::size_t i = 0;
124  auto const beginIterator = mappedData1.begin();
125  auto const secondIterator = beginIterator + 1;
126  auto mappedIterator = beginIterator;
127  for (auto&& mappedValue: mappedData1) {
128  BOOST_TEST_CHECKPOINT("mapped item: " << i);
129 
130  Data_t const expectedValue = expectedMappedData1[i];
131  Data_t const& expectedRef = (mapping1[i] == InvalidIndex)
132  ? mappedData1.defaultValue()
133  : data[mapping1[i]]
134  ;
135  BOOST_TEST(expectedRef == expectedValue); // test the test
136 
137  BOOST_TEST(mappedValue == expectedMappedData1[i]);
138  BOOST_TEST(mappedData1[i] == expectedMappedData1[i]);
139  BOOST_TEST(mappedData1.at(i) == expectedMappedData1[i]);
140  BOOST_TEST(beginIterator[i] == expectedMappedData1[i]);
141  BOOST_TEST(*(beginIterator + i) == expectedMappedData1[i]);
142  BOOST_TEST(*mappedIterator == expectedMappedData1[i]);
143  if (i >= 1) {
144  BOOST_TEST(secondIterator[i-1] == expectedMappedData1[i]);
145  BOOST_TEST(*(secondIterator + i - 1) == expectedMappedData1[i]);
146  BOOST_TEST( (mappedIterator != beginIterator));
147  BOOST_TEST(!(mappedIterator == beginIterator));
148  BOOST_TEST( (mappedIterator > beginIterator));
149  BOOST_TEST( (mappedIterator >= beginIterator));
150  BOOST_TEST(!(mappedIterator < beginIterator));
151  BOOST_TEST(!(mappedIterator <= beginIterator));
152  BOOST_TEST( (beginIterator != mappedIterator));
153  BOOST_TEST(!(beginIterator == mappedIterator));
154  BOOST_TEST(!(beginIterator > mappedIterator));
155  BOOST_TEST(!(beginIterator >= mappedIterator));
156  BOOST_TEST( (beginIterator < mappedIterator));
157  BOOST_TEST( (beginIterator <= mappedIterator));
158  }
159  else {
160  BOOST_TEST(!(mappedIterator != beginIterator));
161  BOOST_TEST( (mappedIterator == beginIterator));
162  BOOST_TEST(!(mappedIterator > beginIterator));
163  BOOST_TEST( (mappedIterator >= beginIterator));
164  BOOST_TEST(!(mappedIterator < beginIterator));
165  BOOST_TEST( (mappedIterator <= beginIterator));
166  BOOST_TEST(!(beginIterator != mappedIterator));
167  BOOST_TEST( (beginIterator == mappedIterator));
168  BOOST_TEST(!(beginIterator > mappedIterator));
169  BOOST_TEST( (beginIterator >= mappedIterator));
170  BOOST_TEST(!(beginIterator < mappedIterator));
171  BOOST_TEST( (beginIterator <= mappedIterator));
172  }
173 
174  BOOST_TEST(&mappedData1[i] != &expectedMappedData1[i]);
175  ++i;
176  ++mappedIterator;
177  } // for
178  BOOST_TEST(i == expectedMappedData1.size());
179 
180  // let's check a bit of overflow
181  while (i < expectedMappedData1.size() + 3U) {
182  BOOST_TEST_CHECKPOINT("mapped item: " << i << " (overflow)");
183  BOOST_CHECK_THROW(mappedData1.at(i), std::out_of_range);
184  ++i;
185  } // for
186 
187 
188 } // copyTest()
A meta-container providing transparent mapping on top of another.
const_iterator begin() const
Returns a constant iterator to the first mapped element.
static constexpr T invalidIndex()
const Index InvalidIndex
typename collection_value_type< Coll >::type collection_value_t
Type contained in the collection Coll.
Definition: ContainerMeta.h:65
void defaultConstructorTest ( )

Definition at line 321 of file MappedContainer_test.cc.

321  {
322 
323  using Mapping_t = std::array<std::size_t, 6U>;
324 
325  (void) util::MappedContainer<std::array<float, 4U> , Mapping_t>();
326  (void) util::MappedContainer<float[4U] , Mapping_t>();
329  (void) util::MappedContainer<std::unique_ptr<float> , Mapping_t>();
330  (void) util::MappedContainer<std::reference_wrapper<std::array<float, 4U>>, Mapping_t>();
331 
332 } // defaultConstructorTest()
A meta-container providing transparent mapping on top of another.
template<typename Cont >
auto makeTestData ( )

Definition at line 80 of file MappedContainer_test.cc.

unique_ptr< InputSource > make(ParameterSet const &conf, InputSourceDescription &desc)
template<typename Cont >
void referenceTest ( )

Definition at line 193 of file MappedContainer_test.cc.

193  {
194 
195  using Container_t = Cont;
196  using Literal_t = util::collection_value_t<Container_t>;
197  using Data_t = Literal_t;
198 
199  constexpr Literal_t defaultValue { 42 };
200 
202 
203  auto const data = makeTestData<Cont>();
204 
205  // BUG the double brace syntax is required to work around clang bug 21629
206  // (https://bugs.llvm.org/show_bug.cgi?id=21629)
207  std::array<std::size_t, 12U> const mapping1 = {{
208  4U, 3U, 2U, 1U, 0U, InvalidIndex,
209  9U, 8U, 7U, 6U, 5U, InvalidIndex,
210  }};
211 
212  std::array<Literal_t, 12U> const expectedMappedData1 = {{
213  -4, -3, -2, -1, 0, defaultValue,
214  -9, -8, -7, -6, -5, defaultValue
215  }};
216 
217  util::MappedContainer const mappedData1(
218  std::ref(data), mapping1, expectedMappedData1.size(),
219  defaultValue
220  );
221 
222  // a similar mapping, but the mapping itself is not copied
223  util::MappedContainer const mappedData2(std::ref(data), std::ref(mapping1));
224 
225  static_assert(
226  std::is_same_v<
227  typename decltype(mappedData1)::const_iterator::reference,
228  Data_t const&
229  >
230  );
231 
232  BOOST_TEST(mappedData1.size() == expectedMappedData1.size());
233  BOOST_TEST(mappedData1.minimal_size() == expectedMappedData1.size());
234 
235  BOOST_TEST(mappedData1.front() == expectedMappedData1.front());
236  BOOST_TEST(mappedData1.back() == expectedMappedData1.back());
237 
238  std::size_t i = 0;
239  auto const beginIterator = mappedData1.begin();
240  auto const secondIterator = beginIterator + 1;
241  auto mappedIterator = beginIterator;
242  for (auto&& mappedValue: mappedData1) {
243  BOOST_TEST_CHECKPOINT("mapped item: " << i);
244 
245  std::size_t const& expectedMappedIndex = mapping1[i];
246  Data_t const expectedValue = expectedMappedData1[i];
247  Data_t const& expectedRef = (expectedMappedIndex == InvalidIndex)
248  ? mappedData1.defaultValue()
249  : data[expectedMappedIndex]
250  ;
251  BOOST_TEST(expectedRef == expectedValue); // test the test
252 
253  decltype(auto) mappedDataRef = mappedData1[i];
254  static_assert(std::is_reference_v<decltype(mappedDataRef)>);
255 
256  BOOST_TEST(mappedData1.map_index(i) == expectedMappedIndex);
257  BOOST_TEST(mappedValue == expectedValue);
258  BOOST_TEST(mappedDataRef == expectedValue);
259  BOOST_TEST(mappedData1.at(i) == expectedValue);
260  BOOST_TEST(beginIterator[i] == expectedValue);
261  BOOST_TEST(*(beginIterator + i) == expectedValue);
262  BOOST_TEST(*mappedIterator == expectedValue);
263  if (i >= 1) {
264  BOOST_TEST(secondIterator[i-1] == expectedValue);
265  BOOST_TEST(*(secondIterator + i - 1) == expectedValue);
266  BOOST_TEST( (mappedIterator != beginIterator));
267  BOOST_TEST(!(mappedIterator == beginIterator));
268  BOOST_TEST( (mappedIterator > beginIterator));
269  BOOST_TEST( (mappedIterator >= beginIterator));
270  BOOST_TEST(!(mappedIterator < beginIterator));
271  BOOST_TEST(!(mappedIterator <= beginIterator));
272  BOOST_TEST( (beginIterator != mappedIterator));
273  BOOST_TEST(!(beginIterator == mappedIterator));
274  BOOST_TEST(!(beginIterator > mappedIterator));
275  BOOST_TEST(!(beginIterator >= mappedIterator));
276  BOOST_TEST( (beginIterator < mappedIterator));
277  BOOST_TEST( (beginIterator <= mappedIterator));
278  }
279  else {
280  BOOST_TEST(!(mappedIterator != beginIterator));
281  BOOST_TEST( (mappedIterator == beginIterator));
282  BOOST_TEST(!(mappedIterator > beginIterator));
283  BOOST_TEST( (mappedIterator >= beginIterator));
284  BOOST_TEST(!(mappedIterator < beginIterator));
285  BOOST_TEST( (mappedIterator <= beginIterator));
286  BOOST_TEST(!(beginIterator != mappedIterator));
287  BOOST_TEST( (beginIterator == mappedIterator));
288  BOOST_TEST(!(beginIterator > mappedIterator));
289  BOOST_TEST( (beginIterator >= mappedIterator));
290  BOOST_TEST(!(beginIterator < mappedIterator));
291  BOOST_TEST( (beginIterator <= mappedIterator));
292  }
293 
294  // since `data` is captured via reference, the returned elements should be
295  // exactly the same object (same physical memory location) as in the
296  // original collection:
297  BOOST_TEST(&mappedDataRef == &expectedRef);
298 
299  // mapping by reference should be equivalent to mapping by value;
300  // because of out test choice, the default values are different though
301  if (mapping1[i] != InvalidIndex)
302  BOOST_TEST(mappedData2[i] == mappedData1[i]);
303  BOOST_TEST(&mappedData2.map_index(i) == &expectedMappedIndex);
304 
305  ++i;
306  ++mappedIterator;
307  } // for
308  BOOST_TEST(i == expectedMappedData1.size());
309 
310  // let's check a bit of overflow
311  while (i < expectedMappedData1.size() + 3U) {
312  BOOST_TEST_CHECKPOINT("mapped item: " << i << " (overflow)");
313  BOOST_CHECK_THROW(mappedData1.at(i), std::out_of_range);
314  ++i;
315  } // for
316 
317 } // referenceTest()
A meta-container providing transparent mapping on top of another.
STL namespace.
static constexpr T invalidIndex()
const Index InvalidIndex
typename collection_value_type< Coll >::type collection_value_t
Type contained in the collection Coll.
Definition: ContainerMeta.h:65
if(!yymsg) yymsg