aggregate_t.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (Product aggregation Test)
2 #include "boost/test/unit_test.hpp"
3 
4 //=====================================================
5 // Unit-tests for non-CLHEP aggregate overloads
6 // N.B. art::PtrVector cannot be overloaded
7 
8 #include "MockRun.h"
9 #include "cetlib/map_vector.h"
10 
11 #include <map>
12 #include <memory>
13 #include <ostream>
14 #include <string>
15 #include <tuple>
16 #include <vector>
17 
18 using namespace std::string_literals;
19 using arttest::MockRun;
20 
21 namespace {
22 
23  double constexpr tolerance = std::numeric_limits<double>::epsilon();
24 
25  struct HoursPerWorker {
26  HoursPerWorker(std::string const& name, unsigned const hours)
27  : name_{name}, hours_{hours}
28  {}
29  std::string name_;
30  unsigned hours_;
31 
32  void
33  aggregate(HoursPerWorker const& b)
34  {
35  if (name_ != b.name_)
36  return;
37  hours_ += b.hours_;
38  }
39  };
40 
41  struct A {
42  A(std::string const& name) : name_{name} {}
43  std::string name_;
44  void
45  aggregate(A const&) const
46  {}
47  };
48 }
49 
50 // ICKY...but there aren't many options with the boost unit test if
51 // the type is an std::pair
52 namespace std {
53 
54  ostream&
55  operator<<(ostream& os, pair<string, unsigned> const& p)
56  {
57  os << '[' << p.first << ',' << p.second << ']';
58  return os;
59  }
60 
61  ostream&
62  operator<<(ostream& os, pair<cet::map_vector_key, string> const& entry)
63  {
64  os << '[' << entry.first.asUint() << ',' << entry.second << ']';
65  return os;
66  }
67 }
68 
69 BOOST_AUTO_TEST_SUITE(aggregate_t)
70 
72 {
73  MockRun r1;
74  r1.put<HoursPerWorker>(HoursPerWorker{"Sam", 12});
75  r1.put<HoursPerWorker>(HoursPerWorker{"Sam", 14});
76  auto sam = r1.get<HoursPerWorker>();
77  BOOST_TEST(sam.name_ == "Sam");
78  BOOST_TEST(sam.hours_ == 26u);
79 
80  MockRun r2;
81  r2.put<HoursPerWorker>(HoursPerWorker{"John", 8});
82  r2.put<HoursPerWorker>(HoursPerWorker{"Jim", 10});
83  auto john = r2.get<HoursPerWorker>();
84  BOOST_TEST(john.name_ == "John");
85  BOOST_TEST(john.hours_ == 8u);
86 }
87 
89 {
90  MockRun r;
91  r.put<double>(14.3);
92  r.put<double>(3.4);
93  BOOST_CHECK_CLOSE_FRACTION(r.get<double>(), 17.7, tolerance);
94 }
95 
97 {
98  using nums_t = std::vector<int>;
99  MockRun r;
100  r.put<nums_t>(nums_t{1, 3, 5});
101  r.put<nums_t>(nums_t{2, 4, 6});
102  auto ref = {1, 3, 5, 2, 4, 6};
103  BOOST_TEST(r.get<nums_t>() == ref, boost::test_tools::per_element{});
104 }
105 
107 {
108  using chars_t = std::list<char>;
109  MockRun r;
110  r.put<chars_t>(chars_t{'a', 'b', 'c'});
111  r.put<chars_t>(chars_t{'y', 'z'});
112  auto ref = {'a', 'b', 'c', 'y', 'z'};
113  BOOST_TEST(r.get<chars_t>() == ref, boost::test_tools::per_element{});
114 }
115 
117 {
118  using nums_t = std::deque<unsigned>;
119  MockRun r;
120  r.put<nums_t>(nums_t{0, 5, 8});
121  r.put<nums_t>(nums_t{1, 2, 4});
122  std::initializer_list<unsigned> const ref{0u, 5u, 8u, 1u, 2u, 4u};
123  BOOST_TEST(r.get<nums_t>() == ref, boost::test_tools::per_element{});
124 }
125 
127 {
128  using histo_t = std::array<int, 3>;
129  MockRun r;
130  r.put<histo_t>(histo_t{{1, 4, 7}});
131  r.put<histo_t>(histo_t{{-1, 6, 92}});
132  auto ref = {0, 10, 99};
133  BOOST_TEST(r.get<histo_t>() == ref, boost::test_tools::per_element{});
134 }
135 
137 {
138  using map_t = std::map<std::string, unsigned>;
139  map_t const children{{"Billy", 7}, {"Josephine", 9}, {"Gregory", 0}};
140  map_t const adults{{"Jennifer", 28}, {"Gregory", 47}, {"Ervin", 78}};
141  MockRun r;
142  r.put<map_t>(children);
143  r.put<map_t>(adults);
144  map_t const people{{"Billy", 7},
145  {"Ervin", 78},
146  {"Gregory", 0},
147  {"Jennifer", 28},
148  {"Josephine", 9}};
149  BOOST_TEST(r.get<map_t>() == people);
150 }
151 
153 {
154  using map_t = std::multimap<std::string, unsigned>;
155  map_t const children{{"Billy", 7}, {"Josephine", 9}, {"Gregory", 0}};
156  map_t const adults{{"Jennifer", 28}, {"Gregory", 47}, {"Ervin", 78}};
157  MockRun r;
158  r.put<map_t>(children);
159  r.put<map_t>(adults);
160  map_t const people{{"Billy", 7},
161  {"Ervin", 78},
162  {"Gregory", 0},
163  {"Gregory", 47},
164  {"Jennifer", 28},
165  {"Josephine", 9}};
166  BOOST_TEST(r.get<map_t>() == people);
167 }
168 
170 {
171  using set_t = std::set<std::string>;
172  MockRun r;
173  r.put<set_t>(set_t{"Brahms", "Beethoven"});
174  r.put<set_t>(set_t{"Bach", "Brahms"});
175  set_t const composers{"Bach", "Beethoven", "Brahms"};
176  BOOST_TEST(r.get<set_t>() == composers);
177 }
178 
180 {
181  using pair_t = std::pair<unsigned, double>;
182  MockRun r;
183  r.put<pair_t>(pair_t{0u, 14.});
184  r.put<pair_t>(pair_t{6u, 20.});
185  auto result = r.get<pair_t>();
186  BOOST_TEST(result.first == 6u);
187  BOOST_CHECK_CLOSE_FRACTION(result.second, 34., tolerance);
188 }
189 
191 {
192  using tuple_t = std::tuple<A, unsigned, double>;
193  MockRun r;
194  r.put<tuple_t>(tuple_t{"run1"s, 3, 1.2});
195  r.put<tuple_t>(tuple_t{"run2"s, 74, 2.9});
196  auto result = r.get<tuple_t>();
197  BOOST_TEST(std::get<A>(result).name_ == "run1");
198  BOOST_TEST(std::get<unsigned>(result) == 77u);
199  BOOST_CHECK_CLOSE_FRACTION(std::get<double>(result), 4.1, tolerance);
200 }
201 
203 {
204  using mv_t = cet::map_vector<std::string>;
205  using key_type = typename mv_t::key_type;
206  mv_t primes;
207  primes.insert({key_type{2}, "two"});
208  primes.insert({key_type{3}, "three"});
209  primes.insert({key_type{5}, "five"});
210 
211  mv_t more_nums;
212  more_nums.insert({key_type{7}, "seven"});
213  more_nums.insert({key_type{11}, "eleven"});
214  more_nums.insert({key_type{13}, "thirteen"});
215 
216  MockRun r;
217  r.put<mv_t>(primes);
218  r.put<mv_t>(more_nums);
219 
220  mv_t ref;
221  ref.insert({key_type{2}, "two"});
222  ref.insert({key_type{3}, "three"});
223  ref.insert({key_type{5}, "five"});
224  ref.insert({key_type{7}, "seven"});
225  ref.insert({key_type{11}, "eleven"});
226  ref.insert({key_type{13}, "thirteen"});
227 
228  BOOST_TEST(r.get<mv_t>() == ref, boost::test_tools::per_element{});
229 }
230 
232 {
233  MockRun r;
234  r.put<std::string>("howdy");
235  r.put<std::string>("dowdy");
236  BOOST_REQUIRE_EXCEPTION(
237  r.get<std::string>(), art::Exception, [](art::Exception const& e) {
238  return e.categoryCode() == art::errors::ProductCannotBeAggregated;
239  });
240 }
241 
243 {
244  MockRun r;
245  r.put<std::string>("howdy");
246  r.put<std::string>("howdy");
247  BOOST_TEST(r.get<std::string>() == "howdy");
248 }
249 
250 BOOST_AUTO_TEST_SUITE_END()
static QCString name
Definition: declinfo.cpp:673
BOOST_AUTO_TEST_CASE(class_type)
Definition: aggregate_t.cc:71
QList< Entry > entry
static QCString result
std::string string
Definition: nybbler.cc:12
auto const tolerance
std::pair< iterator, bool > insert(value_type const &x)
Definition: map_vector.h:471
STL namespace.
QCollection::Item first()
Definition: qglist.cpp:807
const double e
p
Definition: test.py:223
T get() const
Definition: MockRun.h:24
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
void put(ARGS &&...args)
Definition: MockRun.h:16
#define A
Definition: memgrp.cpp:38
static bool * b
Definition: config.cpp:1043
static QCString * s
Definition: config.cpp:1042