lsst.afw  17.0.1-26-ga1fda76f0
test.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * This file is part of afw.
4  *
5  * Developed for the LSST Data Management System.
6  * This product includes software developed by the LSST Project
7  * (https://www.lsst.org).
8  * See the COPYRIGHT file at the top-level directory of this distribution
9  * for details of code ownership.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
25 #ifndef LSST_AFW_TYPEHANDLING_TEST_H
26 #define LSST_AFW_TYPEHANDLING_TEST_H
27 
28 #define BOOST_TEST_DYN_LINK
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wunused-variable"
31 #include "boost/test/unit_test.hpp"
32 #pragma clang diagnostic pop
33 
34 #include <memory>
35 #include <set>
36 #include <string>
37 
38 #include <boost/mpl/list.hpp>
39 
40 #include "lsst/pex/exceptions.h"
41 
44 
45 namespace lsst {
46 namespace afw {
47 namespace typehandling {
48 namespace test {
49 
50 /*
51  * This include file defines tests that exercise the GenericMap and MutableGenericMap interfaces, and ensures
52  * that any implementation satisfies the requirements of these interfaces. Subclass authors should call either
53  * addGenericMapTestCases or addMutableGenericMapTestCases in a suitable entry point, such as a global fixture
54  * or a module initialization function.
55  */
56 
57 namespace {
58 class SimpleStorable : public Storable {
59 public:
60  virtual ~SimpleStorable() = default;
61 
62  std::unique_ptr<Storable> clone() const override { return std::make_unique<SimpleStorable>(); }
63 
64  std::string toString() const override { return "Simplest possible representation"; }
65 
66  bool equals(Storable const& other) const noexcept override {
67  auto simpleOther = dynamic_cast<SimpleStorable const*>(&other);
68  return simpleOther != nullptr;
69  }
70  virtual bool operator==(SimpleStorable const& other) const { return true; }
71  bool operator!=(SimpleStorable const& other) const { return !(*this == other); }
72 };
73 
74 class ComplexStorable final : public SimpleStorable {
75 public:
76  constexpr ComplexStorable(double storage) : SimpleStorable(), storage(storage) {}
77 
78  ComplexStorable& operator=(double newValue) {
79  storage = newValue;
80  return *this;
81  }
82 
83  std::unique_ptr<Storable> clone() const override { return std::make_unique<ComplexStorable>(storage); }
84 
85  std::string toString() const override { return "ComplexStorable(" + std::to_string(storage) + ")"; }
86 
87  std::size_t hash_value() const noexcept override { return std::hash<double>()(storage); }
88 
89  // Warning: violates both substitution and equality symmetry!
90  bool equals(Storable const& other) const noexcept override {
91  auto complexOther = dynamic_cast<ComplexStorable const*>(&other);
92  if (complexOther) {
93  return this->storage == complexOther->storage;
94  } else {
95  return false;
96  }
97  }
98  bool operator==(SimpleStorable const& other) const override { return this->equals(other); }
99 
100 private:
101  double storage;
102 };
103 
104 // Would make more sense as static constants in GenericFactory
105 // but neither string nor Storable qualify as literal types
106 // In anonymous namespace to ensure constants are internal to whatever test includes this header
107 auto const KEY0 = makeKey<bool>(0);
108 bool const VALUE0 = true;
109 auto const KEY1 = makeKey<int>(1);
110 int const VALUE1 = 42;
111 auto const KEY2 = makeKey<double>(2);
112 int const VALUE2 = VALUE1;
113 auto const KEY3 = makeKey<std::string>(3);
114 std::string const VALUE3 = "How many roads must a man walk down?";
115 auto const KEY4 = makeKey<std::shared_ptr<SimpleStorable>>(4);
116 auto const VALUE4 = SimpleStorable();
117 auto const KEY5 = makeKey<ComplexStorable>(5);
118 auto const VALUE5 = ComplexStorable(-100.0);
119 } // namespace
120 
125 public:
126  virtual ~GenericFactory() = default;
127 
138  virtual std::unique_ptr<GenericMap<int>> makeGenericMap() const = 0;
139 
141  virtual std::unique_ptr<MutableGenericMap<std::string>> makeMutableGenericMap() const = 0;
142 };
143 
144 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstAt, GenericMapFactory) {
145  static GenericMapFactory const factory;
146  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
147 
148  BOOST_TEST(demoMap->at(KEY0) == VALUE0);
149  BOOST_TEST(demoMap->at(KEY1) == VALUE1);
150  BOOST_TEST(demoMap->at(KEY2) == VALUE2);
151  BOOST_TEST(demoMap->at(KEY3) == VALUE3);
152  BOOST_TEST(*(demoMap->at(KEY4)) == VALUE4);
153  BOOST_TEST(demoMap->at(KEY5) == VALUE5);
154 }
155 
156 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestAt, GenericMapFactory) {
157  using namespace std::string_literals;
158 
159  static GenericMapFactory const factory;
160  std::unique_ptr<GenericMap<int>> demoMap = factory.makeGenericMap();
161 
162  BOOST_TEST(demoMap->at(KEY0) == VALUE0);
163  demoMap->at(KEY0) = false;
164  BOOST_TEST(demoMap->at(KEY0) == false);
165  BOOST_CHECK_THROW(demoMap->at(makeKey<int>(KEY0.getId())), pex::exceptions::OutOfRangeError);
166 
167  BOOST_TEST(demoMap->at(KEY1) == VALUE1);
168  demoMap->at(KEY1)++;
169  BOOST_TEST(demoMap->at(KEY1) == VALUE1 + 1);
170  BOOST_CHECK_THROW(demoMap->at(makeKey<bool>(KEY1.getId())), pex::exceptions::OutOfRangeError);
171 
172  BOOST_TEST(demoMap->at(KEY2) == VALUE2);
173  demoMap->at(KEY2) = 0.0;
174  BOOST_TEST(demoMap->at(KEY2) == 0.0);
175  // VALUE2 is of a different type than KEY2, check that alternate key is absent
176  using Type2 = std::remove_const_t<decltype(VALUE2)>;
177  BOOST_CHECK_THROW(demoMap->at(makeKey<Type2>(KEY2.getId())), pex::exceptions::OutOfRangeError);
178 
179  BOOST_TEST(demoMap->at(KEY3) == VALUE3);
180  demoMap->at(KEY3).append(" Oops, wrong question."s);
181  BOOST_TEST(demoMap->at(KEY3) == VALUE3 + " Oops, wrong question."s);
182 
183  BOOST_TEST(*(demoMap->at(KEY4)) == VALUE4);
184  // VALUE4 is of a different type than KEY4, check that alternate key is absent
185  using Type4 = std::remove_const_t<decltype(VALUE4)>;
186  BOOST_CHECK_THROW(demoMap->at(makeKey<Type4>(KEY4.getId())), pex::exceptions::OutOfRangeError);
187 
188  BOOST_TEST(demoMap->at(KEY5) == VALUE5);
189  BOOST_TEST(demoMap->at(makeKey<SimpleStorable>(KEY5.getId())) == VALUE5);
190 
191  ComplexStorable newValue(5.0);
192  demoMap->at(KEY5) = newValue;
193  BOOST_TEST(demoMap->at(KEY5) == newValue);
194 }
195 
196 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestEquals, GenericMapFactory) {
197  static GenericMapFactory const factory;
198  auto map1 = factory.makeGenericMap();
199 
200  // Use BOOST_CHECK to avoid BOOST_TEST bug from GenericMap being unprintable
201  BOOST_CHECK(*map1 == *map1);
202  // Maps are unequal because shared_ptr members point to different objects
203  BOOST_CHECK(*map1 != *(factory.makeGenericMap()));
204 }
205 
206 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestMutableEquals, GenericMapFactory) {
207  using namespace std::string_literals;
208 
209  static GenericMapFactory const factory;
210  auto map1 = factory.makeMutableGenericMap();
211  auto map2 = factory.makeMutableGenericMap();
212 
213  // Use BOOST_CHECK to avoid BOOST_TEST bug from GenericMap being unprintable
214  BOOST_CHECK(*map1 == *map2);
215 
216  auto primitiveKey = makeKey<int>("primitive"s);
217  map1->insert(primitiveKey, 42);
218  BOOST_CHECK(*map1 != *map2);
219  map2->insert(primitiveKey, 42);
220  BOOST_CHECK(*map1 == *map2);
221 
222  auto sharedKey = makeKey<std::shared_ptr<SimpleStorable>>("shared"s);
223  auto common = std::make_shared<SimpleStorable>(VALUE4);
224  map1->insert(sharedKey, common);
225  BOOST_CHECK(*map1 != *map2);
226  map2->insert(sharedKey, std::make_shared<SimpleStorable>(VALUE4));
227  BOOST_CHECK(*map1 != *map2);
228  map2->erase(sharedKey);
229  map2->insert(sharedKey, common);
230  BOOST_CHECK(*map1 == *map2);
231 
232  auto storableKey = makeKey<ComplexStorable>("storable"s);
233  map1->insert(storableKey, VALUE5);
234  BOOST_CHECK(*map1 != *map2);
235  map2->insert(storableKey, VALUE5);
236  BOOST_CHECK(*map1 == *map2);
237 }
238 
239 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestSize, GenericMapFactory) {
240  static GenericMapFactory const factory;
241  std::unique_ptr<GenericMap<int>> demoMap = factory.makeGenericMap();
242 
243  BOOST_TEST(demoMap->size() == 6);
244  BOOST_TEST(!demoMap->empty());
245 }
246 
247 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestMutableSize, GenericMapFactory) {
248  using namespace std::string_literals;
249 
250  static GenericMapFactory const factory;
251  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
252 
253  BOOST_TEST_REQUIRE(demoMap->size() == 0);
254  BOOST_TEST_REQUIRE(demoMap->empty());
255 
256  demoMap->insert(makeKey<int>("Negative One"s), -1);
257  BOOST_TEST(demoMap->size() == 1);
258  BOOST_TEST(!demoMap->empty());
259 
260  demoMap->erase(makeKey<int>("Negative One"s));
261  BOOST_TEST(demoMap->size() == 0);
262  BOOST_TEST(demoMap->empty());
263 }
264 
265 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakContains, GenericMapFactory) {
266  using namespace std::string_literals;
267 
268  static GenericMapFactory const factory;
269  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
270 
271  BOOST_TEST(demoMap->contains(KEY0.getId()));
272  BOOST_TEST(demoMap->contains(KEY1.getId()));
273  BOOST_TEST(demoMap->contains(KEY2.getId()));
274  BOOST_TEST(demoMap->contains(KEY3.getId()));
275  BOOST_TEST(demoMap->contains(KEY4.getId()));
276  BOOST_TEST(demoMap->contains(KEY5.getId()));
277  BOOST_TEST(!demoMap->contains(6));
278 }
279 
280 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestContains, GenericMapFactory) {
281  static GenericMapFactory const factory;
282  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
283 
284  BOOST_TEST(demoMap->contains(KEY0));
285  BOOST_TEST(!demoMap->contains(makeKey<int>(KEY0.getId())));
286 
287  BOOST_TEST(demoMap->contains(KEY1));
288  BOOST_TEST(!demoMap->contains(makeKey<bool>(KEY1.getId())));
289 
290  BOOST_TEST(demoMap->contains(KEY2));
291  // VALUE2 is of a different type than KEY2, check that alternate key is absent
292  BOOST_TEST(!demoMap->contains(makeKey<decltype(VALUE2)>(KEY2.getId())));
293 
294  BOOST_TEST(demoMap->contains(KEY3));
295 
296  BOOST_TEST(demoMap->contains(KEY4));
297  // VALUE4 is of a different type than KEY4, check that alternate key is absent
298  BOOST_TEST(!demoMap->contains(makeKey<decltype(VALUE4)>(KEY4.getId())));
299 
300  BOOST_TEST(demoMap->contains(KEY5));
301  BOOST_TEST(demoMap->contains(makeKey<SimpleStorable>(KEY5.getId())));
302  BOOST_TEST(demoMap->contains(makeKey<Storable>(KEY5.getId())));
303 }
304 
305 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestKeys, GenericMapFactory) {
306  static GenericMapFactory const factory;
307  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
308  auto orderedKeys = demoMap->keys();
309  // GenericMaps don't have an iteration order yet
310  std::set<int> keys(orderedKeys.begin(), orderedKeys.end());
311 
312  BOOST_TEST(keys == std::set<int>({KEY0.getId(), KEY1.getId(), KEY2.getId(), KEY3.getId(), KEY4.getId(),
313  KEY5.getId()}));
314 }
315 
316 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestClearIdempotent, GenericMapFactory) {
317  static GenericMapFactory const factory;
318  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
319 
320  BOOST_TEST_REQUIRE(demoMap->empty());
321  demoMap->clear();
322  BOOST_TEST(demoMap->empty());
323 }
324 
325 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestClear, GenericMapFactory) {
326  using namespace std::string_literals;
327 
328  static GenericMapFactory const factory;
329  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
330 
331  demoMap->insert(makeKey<int>("prime"s), 3);
332  demoMap->insert(makeKey<std::string>("foo"s), "bar"s);
333 
334  BOOST_TEST_REQUIRE(!demoMap->empty());
335  demoMap->clear();
336  BOOST_TEST(demoMap->empty());
337 }
338 
339 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertInt, GenericMapFactory) {
340  using namespace std::string_literals;
341 
342  static GenericMapFactory const factory;
343  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
344 
345  BOOST_TEST_REQUIRE(demoMap->empty());
346 
347  int x = 27;
348  BOOST_TEST(demoMap->insert(makeKey<int>("cube"s), x) == true);
349  BOOST_TEST(demoMap->insert(makeKey<int>("cube"s), 0) == false);
350 
351  BOOST_TEST(!demoMap->empty());
352  BOOST_TEST(demoMap->size() == 1);
353  BOOST_TEST(demoMap->contains("cube"s));
354  BOOST_TEST(demoMap->contains(makeKey<int>("cube"s)));
355  BOOST_TEST(!demoMap->contains(makeKey<double>("cube"s)));
356  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == x);
357 
358  x = 0;
359  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) != x);
360 
361  demoMap->at(makeKey<int>("cube"s)) = 0;
362  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == 0);
363 }
364 
365 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakInsertInt, GenericMapFactory) {
366  using namespace std::string_literals;
367 
368  static GenericMapFactory const factory;
369  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
370 
371  BOOST_TEST_REQUIRE(demoMap->empty());
372 
373  auto insertResult = demoMap->insert("cube"s, 27);
374  BOOST_TEST(insertResult.second == true);
375  BOOST_TEST(demoMap->insert("cube"s, 0).second == false);
376 
377  BOOST_TEST(!demoMap->empty());
378  BOOST_TEST(demoMap->size() == 1);
379  BOOST_TEST(demoMap->contains("cube"s));
380  BOOST_TEST(demoMap->contains(insertResult.first));
381  BOOST_TEST(demoMap->contains(makeKey<int>("cube"s)));
382  BOOST_TEST(!demoMap->contains(makeKey<double>("cube"s)));
383  BOOST_TEST(demoMap->at(insertResult.first) == 27);
384  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == 27);
385 
386  demoMap->at(insertResult.first) = 0;
387  BOOST_TEST(demoMap->at(insertResult.first) == 0);
388 }
389 
390 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertString, GenericMapFactory) {
391  using namespace std::string_literals;
392 
393  static GenericMapFactory const factory;
394  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
395 
396  BOOST_TEST_REQUIRE(demoMap->empty());
397 
398  std::string answer(
399  "I have a most elegant and wonderful proof, but this string is too small to contain it."s);
400  BOOST_TEST(demoMap->insert(makeKey<std::string>("Ultimate answer"s), answer) == true);
401  BOOST_TEST(demoMap->insert(makeKey<std::string>("OK"s), "Ook!"s) == true);
402  BOOST_TEST(demoMap->insert(makeKey<std::string>("Ultimate answer"s), "Something philosophical"s) ==
403  false);
404 
405  BOOST_TEST(!demoMap->empty());
406  BOOST_TEST(demoMap->size() == 2);
407  BOOST_TEST(demoMap->contains("OK"s));
408  BOOST_TEST(demoMap->contains(makeKey<std::string>("Ultimate answer"s)));
409  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) == answer);
410  BOOST_TEST(demoMap->at(makeKey<std::string>("OK"s)) == "Ook!"s);
411 
412  answer = "I don't know"s;
413  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) != answer);
414 }
415 
416 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakInsertString, GenericMapFactory) {
417  using namespace std::string_literals;
418 
419  static GenericMapFactory const factory;
420  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
421 
422  BOOST_TEST_REQUIRE(demoMap->empty());
423 
424  auto insertResult1 = demoMap->insert("Ultimate answer"s, "Something philosophical"s);
425  BOOST_TEST(insertResult1.second == true);
426  auto insertResult2 = demoMap->insert("OK"s, "Ook!"s);
427  BOOST_TEST(insertResult2.second == true);
428 
429  BOOST_TEST(!demoMap->empty());
430  BOOST_TEST(demoMap->size() == 2);
431  BOOST_TEST(demoMap->contains(insertResult1.first));
432  BOOST_TEST(demoMap->contains(insertResult2.first));
433  BOOST_TEST(demoMap->contains("OK"s));
434  BOOST_TEST(demoMap->contains(makeKey<std::string>("Ultimate answer"s)));
435  BOOST_TEST(demoMap->at(insertResult1.first) == "Something philosophical"s);
436  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) == "Something philosophical"s);
437  BOOST_TEST(demoMap->at(insertResult2.first) == "Ook!"s);
438  BOOST_TEST(demoMap->at(makeKey<std::string>("OK"s)) == "Ook!"s);
439 }
440 
441 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertStorable, GenericMapFactory) {
442  using namespace std::string_literals;
443 
444  static GenericMapFactory const factory;
445  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
446 
447  BOOST_TEST_REQUIRE(demoMap->empty());
448 
449  ComplexStorable object(3.1416);
450  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("foo"s), object) == true);
451  BOOST_TEST(demoMap->insert(makeKey<std::shared_ptr<ComplexStorable>>("bar"s),
452  std::make_shared<ComplexStorable>(3.141)) == true);
453  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("foo"s), SimpleStorable()) == false);
454  BOOST_TEST(demoMap->insert(makeKey<std::shared_ptr<SimpleStorable>>("bar"s),
455  std::make_shared<SimpleStorable>()) == false);
456 
457  BOOST_TEST(!demoMap->empty());
458  BOOST_TEST(demoMap->size() == 2);
459  BOOST_TEST(demoMap->contains("foo"s));
460  BOOST_TEST(demoMap->contains(makeKey<Storable>("foo"s)));
461  BOOST_TEST(demoMap->contains(makeKey<std::shared_ptr<ComplexStorable>>("bar"s)));
462 
463  // ComplexStorable::operator== is asymmetric
464  // Use BOOST_CHECK_EQUAL to avoid BOOST_TEST bug from Storable being abstract
465  BOOST_TEST(object == demoMap->at(makeKey<SimpleStorable>("foo"s)));
466  object = ComplexStorable(1.4);
467  BOOST_TEST(object != demoMap->at(makeKey<SimpleStorable>("foo"s)));
468  BOOST_TEST(*(demoMap->at(makeKey<std::shared_ptr<ComplexStorable>>("bar"s))) == ComplexStorable(3.141));
469 }
470 
471 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInterleavedInserts, GenericMapFactory) {
472  using namespace std::string_literals;
473 
474  static GenericMapFactory const factory;
475  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
476 
477  BOOST_TEST_REQUIRE(demoMap->empty());
478 
479  BOOST_TEST(demoMap->insert(makeKey<int>("key1"s), 3) == true);
480  BOOST_TEST(demoMap->insert(makeKey<double>("key1"s), 1.0) == false);
481  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("key2"s), SimpleStorable()) == true);
482  BOOST_TEST(demoMap->insert(makeKey<std::string>("key3"s), "Test value"s) == true);
483  BOOST_TEST(demoMap->insert(makeKey<std::string>("key4"s), "This is some text"s) == true);
484  std::string const message = "Unknown value for key5."s;
485  BOOST_TEST(demoMap->insert(makeKey<std::string>("key5"s), message) == true);
486  BOOST_TEST(demoMap->insert(makeKey<int>("key3"s), 20) == false);
487  BOOST_TEST(demoMap->insert<double>(makeKey<double>("key6"s), 42) == true);
488 
489  BOOST_TEST(!demoMap->empty());
490  BOOST_TEST(demoMap->size() == 6);
491  BOOST_TEST(demoMap->at(makeKey<int>("key1"s)) == 3);
492  BOOST_TEST(demoMap->at(makeKey<double>("key6"s)) == 42);
493  BOOST_TEST(demoMap->at(makeKey<SimpleStorable>("key2"s)) == SimpleStorable());
494  BOOST_TEST(demoMap->at(makeKey<std::string>("key3"s)) == "Test value"s);
495  BOOST_TEST(demoMap->at(makeKey<std::string>("key4"s)) == "This is some text"s);
496  BOOST_TEST(demoMap->at(makeKey<std::string>("key5"s)) == message);
497 }
498 
499 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestErase, GenericMapFactory) {
500  using namespace std::string_literals;
501 
502  static GenericMapFactory const factory;
503  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
504 
505  demoMap->insert(makeKey<int>("Ultimate answer"s), 42);
506  BOOST_TEST_REQUIRE(demoMap->size() == 1);
507 
508  BOOST_TEST(demoMap->erase(makeKey<std::string>("Ultimate answer"s)) == false);
509  BOOST_TEST(demoMap->size() == 1);
510  BOOST_TEST(demoMap->erase(makeKey<int>("Ultimate answer"s)) == true);
511  BOOST_TEST(demoMap->size() == 0);
512 }
513 
514 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertEraseInsert, GenericMapFactory) {
515  using namespace std::string_literals;
516 
517  static GenericMapFactory const factory;
518  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
519 
520  BOOST_TEST_REQUIRE(demoMap->empty());
521 
522  BOOST_TEST(demoMap->insert(makeKey<int>("Ultimate answer"s), 42) == true);
523  BOOST_TEST(demoMap->insert(makeKey<int>("OK"s), 200) == true);
524  BOOST_TEST(demoMap->erase(makeKey<int>("Ultimate answer"s)) == true);
525  BOOST_TEST(demoMap->insert(makeKey<double>("Ultimate answer"s), 3.1415927) == true);
526 
527  BOOST_TEST(!demoMap->empty());
528  BOOST_TEST(demoMap->size() == 2);
529  BOOST_TEST(demoMap->contains("OK"s));
530  BOOST_TEST(!demoMap->contains(makeKey<int>("Ultimate answer"s)));
531  BOOST_TEST(demoMap->contains(makeKey<double>("Ultimate answer"s)));
532  BOOST_TEST(demoMap->at(makeKey<double>("Ultimate answer"s)) == 3.1415927);
533 }
534 
542 template <class GenericMapFactory>
543 void addGenericMapTestCases(boost::unit_test::test_suite* const suite) {
544  using factories = boost::mpl::list<GenericMapFactory>;
545 
546  suite->add(BOOST_TEST_CASE_TEMPLATE(TestConstAt, factories));
547  suite->add(BOOST_TEST_CASE_TEMPLATE(TestAt, factories));
548  suite->add(BOOST_TEST_CASE_TEMPLATE(TestEquals, factories));
549  suite->add(BOOST_TEST_CASE_TEMPLATE(TestSize, factories));
550  suite->add(BOOST_TEST_CASE_TEMPLATE(TestWeakContains, factories));
551  suite->add(BOOST_TEST_CASE_TEMPLATE(TestContains, factories));
552  suite->add(BOOST_TEST_CASE_TEMPLATE(TestKeys, factories));
553 }
554 
564 template <class GenericMapFactory>
565 void addMutableGenericMapTestCases(boost::unit_test::test_suite* const suite) {
566  using factories = boost::mpl::list<GenericMapFactory>;
567 
568  addGenericMapTestCases<GenericMapFactory>(suite);
569 
570  suite->add(BOOST_TEST_CASE_TEMPLATE(TestMutableEquals, factories));
571  suite->add(BOOST_TEST_CASE_TEMPLATE(TestMutableSize, factories));
572  suite->add(BOOST_TEST_CASE_TEMPLATE(TestClear, factories));
573  suite->add(BOOST_TEST_CASE_TEMPLATE(TestClearIdempotent, factories));
574  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertInt, factories));
575  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertString, factories));
576  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertStorable, factories));
577  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInterleavedInserts, factories));
578  suite->add(BOOST_TEST_CASE_TEMPLATE(TestErase, factories));
579  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertEraseInsert, factories));
580 }
581 
590 template <class GenericMapFactory>
591 inline void addGenericMapTestCases() {
592  addGenericMapTestCases<GenericMapFactory>(&(boost::unit_test::framework::master_test_suite()));
593 }
594 
604 template <class GenericMapFactory>
606  addMutableGenericMapTestCases<GenericMapFactory>(&(boost::unit_test::framework::master_test_suite()));
607 }
608 
609 } // namespace test
610 } // namespace typehandling
611 } // namespace afw
612 } // namespace lsst
613 
614 #endif
def keys(self)
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:61
T to_string(T... args)
BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstAt, GenericMapFactory)
Definition: test.h:144
ItemVariant const * other
Definition: Schema.cc:56
std::size_t hash_value(Extent< T, N > const &extent) noexcept
constexpr Key< K, V > makeKey(K const &id)
Factory function for Key, to enable type parameter inference.
Definition: GenericMap.h:161
void addMutableGenericMapTestCases(boost::unit_test::test_suite *const suite)
Create generic test cases for a specific MutableGenericMap implementation.
Definition: test.h:565
STL class.
A base class for image defects.
double x
Abstract factory that creates GenericMap and MutableGenericMap instances as needed.
Definition: test.h:124
STL class.
STL class.
void addGenericMapTestCases(boost::unit_test::test_suite *const suite)
Create generic test cases for a specific GenericMap implementation.
Definition: test.h:543