filterRangeFor_test.cc
Go to the documentation of this file.
1 /**
2  * @file filterRangeFor_test.cc
3  * @brief Test for `util::filterRangeFor()`.
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date May 1, 2018
6  * @see `lardata/Utilities/filterRangeFor.h`
7  *
8  * The test is run with no arguments.
9  *
10  */
11 
12 // LArSoft libraries
14 
15 // Boost libraries
16 #define BOOST_TEST_MODULE ( filterRangeFor_test )
17 #include "boost/test/unit_test.hpp"
18 
19 // C/C++ standard libraries
20 #include <algorithm> // std::count_if()
21 #include <numeric> // std::iota()
22 
23 
24 //-----------------------------------------------------------------------------
25 template <typename Cont, typename Pred>
26 void testPredicate(Cont& data, Pred pred) {
27 
28  auto const nPass = std::count_if(data.begin(), data.end(), pred);
29 
30  unsigned int n = 0;
31  for (auto const& v: util::filterRangeFor(data, pred)) {
32  ++n;
33  BOOST_TEST_CHECKPOINT(" testing value: " << v);
34  BOOST_TEST(pred(v));
35  } // for
36 
37  BOOST_TEST(n == nPass);
38 } // testPredicate()
39 
40 
41 //-----------------------------------------------------------------------------
42 BOOST_AUTO_TEST_CASE(filterRangeFor_testCase) {
43 
44  std::vector<int> data(20);
45  std::iota(data.begin(), data.end(), 0);
46 
47  BOOST_TEST_MESSAGE("Selecting multiples of 3");
48  testPredicate<std::vector<int> const>
49  (data, [](int v){ return (v % 3) == 0; });
50  testPredicate(data, [](int v){ return (v % 3) == 0; });
51 
52  BOOST_TEST_MESSAGE("Selecting values that are not 9");
53  testPredicate<std::vector<int> const>(data, [](int v){ return v != 9; });
54  testPredicate(data, [](int v){ return v != 9; });
55 
56  BOOST_TEST_MESSAGE("Selecting values that are 50");
57  testPredicate<std::vector<int> const>(data, [](int v){ return v == 50; });
58  testPredicate(data, [](int v){ return v == 50; });
59 
60 } // BOOST_AUTO_TEST_CASE(filterRangeFor_testCase)
void testPredicate(Cont &data, Pred pred)
BOOST_AUTO_TEST_CASE(filterRangeFor_testCase)
auto filterRangeFor(Range &&range, Pred &&pred) -> decltype(auto)
Provides iteration only through elements passing a condition.
std::void_t< T > n
Utilities to manipulate range for loops.