container_algs_test.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (container_algorithms test)
2 #include "boost/test/unit_test.hpp"
3 
5 
6 #include <map>
7 #include <utility>
8 
9 namespace {
10 
11  template <class T>
12  struct A {
13  A(T t) : t_(t) {}
14  T t_;
15 
16  bool
17  operator<(A<T> const& r) const
18  {
19  return t_ < r.t_;
20  }
21 
22  bool
23  operator==(A<T> const& r) const
24  {
25  return t_ == r.t_;
26  }
27 
28  bool
29  operator!=(A<T> const& r) const
30  {
31  return !operator==(r);
32  }
33  };
34 
35  template <class T>
36  std::ostream&
37  operator<<(std::ostream& os, A<T> const& a)
38  {
39  return os << a.t_;
40  }
41 
42  template <class T>
43  struct MakeA {
44  auto
45  operator()(T const val) const
46  {
47  return A<T>{val};
48  }
49  };
50 
51  template <class T, class U>
52  struct MakeAPair {
53  auto
54  operator()(T const t, U const u) const
55  {
56  return std::pair<T, U>{t, u};
57  }
58  };
59 }
60 
61 BOOST_AUTO_TEST_SUITE(container_algorithms)
62 
64 {
65  std::vector<int> a{1, 2, 3, 4};
66  std::vector<int> b;
67  cet::copy_all(a, std::back_inserter(b));
68  BOOST_TEST(a == b);
69 }
70 
72 {
73  using namespace std;
74 
75  vector<int> const v1{1, 2, 3, 4};
76  vector<char> const v2{'a', 'b', 'c', 'd'};
77 
78  // One-input version
79  vector<A<int>> is1, is2;
80  vector<A<char>> cs1, cs2;
81  map<A<int>, A<char>> p1, p2;
82 
83  transform(cbegin(v1), cend(v1), back_inserter(is1), MakeA<int>());
84  transform(cbegin(v2), cend(v2), back_inserter(cs1), MakeA<char>());
85  transform(cbegin(v1),
86  cend(v1),
87  cbegin(v2),
88  inserter(p1, begin(p1)),
89  MakeAPair<int, char>());
90 
91  cet::transform_all(v1, back_inserter(is2), MakeA<int>());
92  cet::transform_all(v2, back_inserter(cs2), MakeA<char>());
93  cet::transform_all(v1, v2, inserter(p2, begin(p2)), MakeAPair<int, char>());
94 
95  BOOST_TEST(is1 == is2);
96  BOOST_TEST(cs1 == cs2);
97 
98  BOOST_TEST(p1.size() == p2.size());
99 
100  auto p1_it = cbegin(p1);
101  auto p2_it = cbegin(p1);
102  for (; p1_it != cend(p1); ++p1_it, ++p2_it) {
103  BOOST_TEST(p1_it->first.t_ == p2_it->first.t_);
104  BOOST_TEST(p1_it->second.t_ == p2_it->second.t_);
105  }
106 }
107 
109 {
110  std::vector<std::string> names{"Alice", "Bob", "Cathy", "Dylan"};
111  auto firstNames = names;
112  auto const& refNames = names;
113  // Adjust names using non-const version of 'for_all_with_index'
115  names, [](unsigned const i, auto& name) { name += std::to_string(i); });
116  // Check adjusted names using const version
118  refNames, [&firstNames](unsigned const i, auto const& refName) {
119  auto const assembledName = firstNames[i] + std::to_string(i);
120  BOOST_TEST(refName == assembledName);
121  });
122 }
123 
124 BOOST_AUTO_TEST_SUITE_END()
static QCString name
Definition: declinfo.cpp:673
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
void for_all_with_index(FwdCont &, Func)
STL namespace.
bool operator!=(ModuleKeyAndType const &a, ModuleKeyAndType const &b) noexcept
const double a
auto transform_all(Container &, OutputIt, UnaryOp)
auto copy_all(FwdCont &, FwdIter)
BOOST_AUTO_TEST_CASE(copy_all)
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:82
#define A
Definition: memgrp.cpp:38
static bool * b
Definition: config.cpp:1043
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
static std::vector< std::string > const names
Definition: FragmentType.hh:8
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
bool operator==(ModuleKeyAndType const &a, ModuleKeyAndType const &b) noexcept