zip_test.cc
Go to the documentation of this file.
1 /**
2  * @file zip_test.cc
3  * @brief Test of `util::zip()`.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date April 14, 2019
6  *
7  */
8 
9 
10 // testing library
12 
13 // Boost libraries
14 #define BOOST_TEST_MODULE ( zip_test )
15 #include <boost/test/unit_test.hpp>
16 
17 // C/C++ libraries
18 #include <vector>
19 #include <array>
20 #include <cstddef> // std::size_t
21 #include <cassert>
22 
23 
24 // -----------------------------------------------------------------------------
25 void test_zip() {
26 
27  //
28  // standard use case with zipping included
29  //
30 
31  // prepare the data set
32  constexpr std::size_t N = 7U;
33 
34  std::array<int, N> twice;
35  std::vector<double> thrice(N + 1);
36 
37  for (std::size_t i = 0; i < N; ++i) {
38  twice[i] = 2 * i;
39  thrice[i] = 3.0 * i;
40  } // for
41  thrice[N] = 3.0 * N;
42 
43  //
44  // iteration using the first element as lead
45  //
46  unsigned int iLoop = 0;
47  for (auto&& [ a, b ]: util::zip(twice, thrice)) {
48 
49  BOOST_TEST(a == twice[iLoop]);
50  BOOST_TEST(&a == &(twice[iLoop]));
51 
52  BOOST_TEST(b == thrice[iLoop]);
53  BOOST_TEST(&b == &thrice[iLoop]);
54 
55  ++iLoop;
56  } // for
57  BOOST_TEST(iLoop == twice.size());
58 
59  //
60  // iteration using the second element as lead
61  //
62 
63  // (make the second object shorter, so that we can check easily it leads)
64  thrice.pop_back();
65  thrice.pop_back();
66  assert(thrice.size() == N - 1);
67 
68  iLoop = 0;
69  for (auto&& [ a, b ]: util::zip<1>(twice, thrice)) {
70 
71  BOOST_TEST(a == twice[iLoop]);
72  BOOST_TEST(&a == &(twice[iLoop]));
73 
74  BOOST_TEST(b == thrice[iLoop]);
75  BOOST_TEST(&b == &thrice[iLoop]);
76 
77  ++iLoop;
78  } // for
79  BOOST_TEST(iLoop == thrice.size());
80 
81 
82 } // test_zip()
83 
84 
85 // -----------------------------------------------------------------------------
86 void test_no_zip() {
87 
88  unsigned int iLoop = 0U;
89  for (auto&& i [[gnu::unused]]: util::zip()) {}
90 
91  BOOST_TEST(iLoop == 0);
92 
93 } // test_no_zip()
94 
95 
96 // -----------------------------------------------------------------------------
97 // BEGIN Test cases -----------------------------------------------------------
98 // -----------------------------------------------------------------------------
99 BOOST_AUTO_TEST_CASE(zip_testcase) {
100 
101  test_zip();
102  test_no_zip();
103 
104 } // BOOST_AUTO_TEST_CASE(zip_testcase)
105 
106 
107 // -----------------------------------------------------------------------------
108 // END Test cases -------------------------------------------------------------
109 // -----------------------------------------------------------------------------
Definition of util::zip().
BOOST_AUTO_TEST_CASE(zip_testcase)
Definition: zip_test.cc:99
void test_no_zip()
Definition: zip_test.cc:86
const double a
void test_zip()
Definition: zip_test.cc:25
static bool * b
Definition: config.cpp:1043
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295