map_vector_test.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (map_vector test)
2 #include "boost/test/unit_test.hpp"
3 
5 #include "cetlib/map_vector.h"
6 #include <boost/test/tools/output_test_stream.hpp>
7 #include <iostream>
8 
9 using cet::map_vector;
11 
12 BOOST_TEST_DONT_PRINT_LOG_VALUE(map_vector<int>::const_iterator)
13 BOOST_TEST_DONT_PRINT_LOG_VALUE(map_vector<int>::iterator)
14 
15 BOOST_AUTO_TEST_SUITE(map_vector_test)
16 
18 public:
19  cout_redirect(std::streambuf* new_buf) : old_buf(std::cout.rdbuf(new_buf)) {}
20 
21  ~cout_redirect() { std::cout.rdbuf(old_buf); }
22 
23 private:
24  std::streambuf* old_buf;
25 };
26 
28 {
29  {
30  boost::test_tools::output_test_stream output;
31  {
32  cout_redirect guard(output.rdbuf());
33  map_vector_key k(3);
34  std::cout << k << std::endl;
35  }
36  BOOST_TEST(output.is_equal("3\n"));
37  }
38 
39  {
40  map_vector_key k1(11), k2(12);
41  BOOST_TEST(k1 == k1);
42  BOOST_TEST(k1 != k2);
43  BOOST_TEST(k1 <= k2);
44  BOOST_TEST(k1 < k2);
45  }
46 
47  {
48  map_vector_key k1(123u), k2(123uL);
49  BOOST_TEST(k1 == k2);
50  }
51 }
52 
53 BOOST_AUTO_TEST_CASE(emptymap_test)
54 {
55  map_vector_key k(3);
56  {
58  BOOST_TEST(m.empty());
59  BOOST_TEST(m.size() == 0u);
60  BOOST_TEST(!m.has(k));
61  BOOST_TEST(m.begin() == m.end());
62  BOOST_TEST(m.find(k) == m.end());
63  BOOST_TEST(m.getOrNull(k) == nullptr);
64  BOOST_CHECK_THROW(m.getOrThrow(k), cet::exception);
65  BOOST_CHECK_THROW(m.front(), cet::exception);
66  BOOST_CHECK_THROW(m.back(), cet::exception);
67  }
68 
69  {
70  map_vector<int> const m;
71  BOOST_TEST(m.empty());
72  BOOST_TEST(m.size() == 0u);
73  BOOST_TEST(!m.has(k));
74  BOOST_TEST(m.begin() == m.end());
75  BOOST_TEST(m.find(k) == m.end());
76  BOOST_TEST(m.getOrNull(k) == nullptr);
77  BOOST_CHECK_THROW(m.getOrThrow(k), cet::exception);
78  BOOST_CHECK_THROW(m.front(), cet::exception);
79  BOOST_CHECK_THROW(m.back(), cet::exception);
80  }
81 }
82 
83 BOOST_AUTO_TEST_CASE(nonemptymap_test)
84 {
85  using value_t = int;
87  std::size_t sz(0);
88 
89  {
90  map_vector_key k(1);
91  value_t v(10 + k.asInt());
92  m[k] = v;
93  BOOST_TEST(!m.empty());
94  BOOST_TEST(m.size() == ++sz);
95  BOOST_TEST(m.begin() != m.end());
96  BOOST_TEST(m.has(k));
97  BOOST_TEST(m.find(k)->second == v);
98  BOOST_TEST(*m.getOrNull(k) == v);
99  BOOST_TEST(m.getOrThrow(k) == v);
100  }
101 
102  {
103  map_vector_key k(3);
104  value_t v(10 + k.asInt());
105  m[k] = v;
106  BOOST_TEST(!m.empty());
107  BOOST_TEST(m.size() == ++sz);
108  BOOST_TEST(m.begin() != m.end());
109  BOOST_TEST(m.has(k));
110  BOOST_TEST(m.find(k)->second == v);
111  BOOST_TEST(*m.getOrNull(k) == v);
112  BOOST_TEST(m.getOrThrow(k) == v);
113  }
114 
115  {
116  map_vector_key k(5);
117  value_t v(10 + k.asInt());
118  m[k] = v;
119  BOOST_TEST(!m.empty());
120  BOOST_TEST(m.size() == ++sz);
121  BOOST_TEST(m.begin() != m.end());
122  BOOST_TEST(m.has(k));
123  BOOST_TEST(m.find(k)->second == v);
124  BOOST_TEST(*m.getOrNull(k) == v);
125  BOOST_TEST(m.getOrThrow(k) == v);
126  }
127 
128  {
129  map_vector_key k(2);
130  value_t v(10 + k.asInt());
131  m[k] = v;
132  BOOST_TEST(!m.empty());
133  BOOST_TEST(m.size() == ++sz);
134  BOOST_TEST(m.begin() != m.end());
135  BOOST_TEST(m.has(k));
136  BOOST_TEST(m.find(k)->second == v);
137  BOOST_TEST(*m.getOrNull(k) == v);
138  BOOST_TEST(m.getOrThrow(k) == v);
139  }
140 
141  {
142  // Test that insertion works
143  auto m2 = m;
144  BOOST_TEST(m2.size() == sz);
145 
146  auto result = m2.insert({map_vector_key{4}, value_t{14}});
147  BOOST_TEST(result.second);
148  }
149 
150  {
151  // Insertion should fail for already-existing keys, and the
152  // iterator to the existing element should be returned.
153  auto m2 = m;
154  BOOST_TEST(m2.size() == sz);
155 
156  auto result = m2.insert({map_vector_key{2}, value_t{13}});
157  BOOST_TEST(!result.second);
158  BOOST_TEST(result.first->second == 12);
159  }
160 
161  {
162  auto m2 = m, m3 = m;
163  BOOST_TEST(m2.size() == m.size());
164 
165  // Now change the mapped values
166  cet::for_all(m2, [](auto& pr) { pr.second = pr.second + 2; });
167 
168  // Attempt to insert all elements, even though they have the same
169  // keys as in m3.
170  m3.insert(m2.begin(), m2.end());
171 
172  // Verify that the inserts were not successful (that the values of
173  // m3 have not changed).
174  auto b = m.begin(), b2 = m3.begin();
175  auto e = m.end();
176  for (; b != e; ++b, ++b2) {
177  BOOST_TEST(b->second == b2->second);
178  BOOST_TEST(b->first.asInt() == b2->first.asInt());
179  }
180  }
181 
182  {
183  // Test appending elements
184  decltype(m) new_m;
185  auto const offset = m.delta();
186  auto const old_size = m.size();
187  new_m[map_vector_key{offset}] = value_t{43};
188  new_m[map_vector_key{offset + 1}] = value_t{17};
189  m.append(new_m.cbegin(), new_m.cend());
190  BOOST_TEST(m.size() == old_size + new_m.size());
191  }
192 }
193 
194 BOOST_AUTO_TEST_SUITE_END()
bool empty() const noexcept
Definition: map_vector.h:119
intermediate_table::iterator iterator
static QCString result
BOOST_AUTO_TEST_CASE(key_test)
bool has(key_type key) const
Definition: map_vector.h:373
iterator find(key_type key)
Definition: map_vector.h:381
iterator end() noexcept
Definition: map_vector.h:190
STL namespace.
size_t delta() const
Definition: map_vector.h:152
const double e
static constexpr double m2
Definition: Units.h:72
void append(InIter b, InIter e)
Definition: map_vector.h:499
std::streambuf * old_buf
value_type const & front() const
Definition: map_vector.h:355
static constexpr double m3
Definition: Units.h:73
iterator begin() noexcept
Definition: map_vector.h:179
value_type const & back() const
Definition: map_vector.h:364
auto for_all(FwdCont &, Func)
static bool * b
Definition: config.cpp:1043
constexpr unsigned long asInt() const noexcept
Definition: map_vector.h:44
mapped_type & getOrThrow(key_type key)
Definition: map_vector.h:433
size_type size() const noexcept
Definition: map_vector.h:124
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
cout_redirect(std::streambuf *new_buf)
mapped_type * getOrNull(key_type key)
Definition: map_vector.h:417