lsst.afw  18.1.0-16-g27f8efa26+3
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 <numeric>
36 #include <set>
37 #include <sstream>
38 #include <string>
39 
40 #include <boost/mpl/list.hpp>
41 
42 #include "lsst/pex/exceptions.h"
43 
46 
47 namespace lsst {
48 namespace afw {
49 namespace typehandling {
50 namespace test {
51 
52 /*
53  * This include file defines tests that exercise the GenericMap and MutableGenericMap interfaces, and ensures
54  * that any implementation satisfies the requirements of these interfaces. Subclass authors should call either
55  * addGenericMapTestCases or addMutableGenericMapTestCases in a suitable entry point, such as a global fixture
56  * or a module initialization function.
57  */
58 
59 namespace {
60 class SimpleStorable : public Storable {
61 public:
62  virtual ~SimpleStorable() = default;
63 
64  std::shared_ptr<Storable> cloneStorable() const override { return std::make_unique<SimpleStorable>(); }
65 
66  std::string toString() const override { return "Simplest possible representation"; }
67 
68  bool equals(Storable const& other) const noexcept override { return singleClassEquals(*this, other); }
69  virtual bool operator==(SimpleStorable const& other) const { return true; }
70  bool operator!=(SimpleStorable const& other) const { return !(*this == other); }
71 };
72 
73 class ComplexStorable final : public SimpleStorable {
74 public:
75  constexpr ComplexStorable(double storage) : SimpleStorable(), storage(storage) {}
76 
77  ComplexStorable& operator=(double newValue) {
78  storage = newValue;
79  return *this;
80  }
81 
82  std::shared_ptr<Storable> cloneStorable() const override {
83  return std::make_unique<ComplexStorable>(storage);
84  }
85 
86  std::string toString() const override { return "ComplexStorable(" + std::to_string(storage) + ")"; }
87 
88  std::size_t hash_value() const noexcept override { return std::hash<double>()(storage); }
89 
90  // Warning: violates both substitution and equality symmetry!
91  bool equals(Storable const& other) const noexcept override {
92  auto complexOther = dynamic_cast<ComplexStorable const*>(&other);
93  if (complexOther) {
94  return this->storage == complexOther->storage;
95  } else {
96  return false;
97  }
98  }
99  bool operator==(SimpleStorable const& other) const override { return this->equals(other); }
100 
101 private:
102  double storage;
103 };
104 
105 template <typename T>
106 std::string universalToString(T const& value) {
107  std::stringstream buffer;
108  buffer << value;
109  return buffer.str();
110 };
111 
112 // Would make more sense as static constants in GenericFactory
113 // but neither string nor Storable qualify as literal types
114 // In anonymous namespace to ensure constants are internal to whatever test includes this header
115 auto const KEY0 = makeKey<bool>(0);
116 bool const VALUE0 = true;
117 auto const KEY1 = makeKey<int>(1);
118 int const VALUE1 = 42;
119 auto const KEY2 = makeKey<double>(2);
120 int const VALUE2 = VALUE1;
121 auto const KEY3 = makeKey<std::string>(3);
122 std::string const VALUE3 = "How many roads must a man walk down?";
123 auto const KEY4 = makeKey<std::shared_ptr<SimpleStorable const>>(4);
124 auto const VALUE4 = SimpleStorable();
125 auto const KEY5 = makeKey<ComplexStorable>(5);
126 auto const VALUE5 = ComplexStorable(-100.0);
127 auto const KEY6 = makeKey<std::shared_ptr<Storable const>>(6);
128 auto const VALUE6 = std::shared_ptr<Storable const>();
129 } // namespace
130 
135 public:
136  virtual ~GenericFactory() = default;
137 
149  virtual std::unique_ptr<GenericMap<int>> makeGenericMap() const = 0;
150 
152  virtual std::unique_ptr<MutableGenericMap<std::string>> makeMutableGenericMap() const = 0;
153 };
154 
155 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstAt, GenericMapFactory) {
156  static GenericMapFactory const factory;
157  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
158 
159  BOOST_TEST(demoMap->at(KEY0) == VALUE0);
160  BOOST_TEST(demoMap->at(KEY1) == VALUE1);
161  BOOST_TEST(demoMap->at(KEY2) == VALUE2);
162  BOOST_TEST(demoMap->at(KEY3) == VALUE3);
163  BOOST_TEST(*(demoMap->at(KEY4)) == VALUE4);
164  BOOST_TEST(demoMap->at(KEY5) == VALUE5);
165  BOOST_TEST(demoMap->at(KEY6) == VALUE6);
166 }
167 
168 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestAt, GenericMapFactory) {
169  using namespace std::string_literals;
170 
171  static GenericMapFactory const factory;
172  std::unique_ptr<GenericMap<int>> demoMap = factory.makeGenericMap();
173 
174  BOOST_TEST(demoMap->at(KEY0) == VALUE0);
175  demoMap->at(KEY0) = false;
176  BOOST_TEST(demoMap->at(KEY0) == false);
177  BOOST_CHECK_THROW(demoMap->at(makeKey<int>(KEY0.getId())), pex::exceptions::OutOfRangeError);
178 
179  BOOST_TEST(demoMap->at(KEY1) == VALUE1);
180  demoMap->at(KEY1)++;
181  BOOST_TEST(demoMap->at(KEY1) == VALUE1 + 1);
182  BOOST_CHECK_THROW(demoMap->at(makeKey<bool>(KEY1.getId())), pex::exceptions::OutOfRangeError);
183 
184  BOOST_TEST(demoMap->at(KEY2) == VALUE2);
185  demoMap->at(KEY2) = 0.0;
186  BOOST_TEST(demoMap->at(KEY2) == 0.0);
187  // VALUE2 is of a different type than KEY2, check that alternate key is absent
188  using Type2 = std::remove_const_t<decltype(VALUE2)>;
189  BOOST_CHECK_THROW(demoMap->at(makeKey<Type2>(KEY2.getId())), pex::exceptions::OutOfRangeError);
190 
191  BOOST_TEST(demoMap->at(KEY3) == VALUE3);
192  demoMap->at(KEY3).append(" Oops, wrong question."s);
193  BOOST_TEST(demoMap->at(KEY3) == VALUE3 + " Oops, wrong question."s);
194 
195  BOOST_TEST(*(demoMap->at(KEY4)) == VALUE4);
196  // VALUE4 is of a different type than KEY4, check that alternate key is absent
197  using Type4 = std::remove_const_t<decltype(VALUE4)>;
198  BOOST_CHECK_THROW(demoMap->at(makeKey<Type4>(KEY4.getId())), pex::exceptions::OutOfRangeError);
199 
200  BOOST_TEST(demoMap->at(KEY5) == VALUE5);
201  BOOST_TEST(demoMap->at(makeKey<SimpleStorable>(KEY5.getId())) == VALUE5);
202 
203  ComplexStorable newValue(5.0);
204  demoMap->at(KEY5) = newValue;
205  BOOST_TEST(demoMap->at(KEY5) == newValue);
206 
207  BOOST_TEST(demoMap->at(KEY6) == VALUE6);
208 }
209 
210 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestEquals, GenericMapFactory) {
211  static GenericMapFactory const factory;
212  auto map1 = factory.makeGenericMap();
213 
214  // Use BOOST_CHECK to avoid BOOST_TEST bug from GenericMap being unprintable
215  BOOST_CHECK(*map1 == *map1);
216  // Maps are unequal because shared_ptr members point to different objects
217  BOOST_CHECK(*map1 != *(factory.makeGenericMap()));
218 }
219 
220 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstVisitor, GenericMapFactory) {
221  static GenericMapFactory const factory;
222  std::unique_ptr<GenericMap<int> const> const map = factory.makeGenericMap();
223  std::vector<int> mapKeys = map->keys();
224 
225  // Visitors return string because it's one of the few operations valid for all types of interest
226  // This lets us test generic lambdas as visitors
227  auto bruteForcePrinter = [&map](int key) {
228  switch (key) {
229  case 0:
230  return universalToString(map->at(KEY0));
231  case 1:
232  return universalToString(map->at(KEY1));
233  case 2:
234  return universalToString(map->at(KEY2));
235  case 3:
236  return universalToString(map->at(KEY3));
237  case 4:
238  return universalToString(map->at(KEY4));
239  case 5:
240  return universalToString(map->at(KEY5));
241  case 6:
242  return universalToString(map->at(KEY6));
243  default:
244  throw std::invalid_argument("Bad key found");
245  };
246  };
247  std::vector<std::string> expected;
248  for (int key : mapKeys) {
249  expected.push_back(bruteForcePrinter(key));
250  }
251 
252  // Test local class that returns void
253  class {
254  public:
255  std::vector<std::string> results;
256 
257  // Local classes can't have method templates
258  void operator()(int, bool value) { results.push_back(universalToString(value)); }
259  void operator()(int, std::int32_t const& value) { results.push_back(universalToString(value)); }
260  void operator()(int, std::int64_t value) { results.push_back(universalToString(value)); }
261  void operator()(int, float const& value) { results.push_back(universalToString(value)); }
262  void operator()(int, double value) { results.push_back(universalToString(value)); }
263  void operator()(int, std::string const& value) { results.push_back(universalToString(value)); }
264  void operator()(int, Storable const& value) { results.push_back(universalToString(value)); }
265  void operator()(int, std::shared_ptr<Storable const> value) {
266  results.push_back(universalToString(value));
267  }
268  } printer;
269  map->apply(printer);
270  BOOST_REQUIRE(printer.results.size() == expected.size());
271  for (std::size_t i = 0; i < printer.results.size(); ++i) {
272  BOOST_TEST(printer.results[i] == expected[i],
273  printer.results[i] << " != " << expected[i] << ", key = " << mapKeys[i]);
274  }
275 
276  // Test lambda that returns string
277  std::vector<std::string> strings =
278  map->apply([](int, auto const& value) { return universalToString(value); });
279  BOOST_REQUIRE(strings.size() == expected.size());
280  for (std::size_t i = 0; i < strings.size(); ++i) {
281  BOOST_TEST(strings[i] == expected[i],
282  strings[i] << " != " << expected[i] << ", key = " << mapKeys[i]);
283  }
284 }
285 
286 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestModifyingVoidVisitor, GenericMapFactory) {
287  static GenericMapFactory const factory;
288  std::unique_ptr<GenericMap<int>> map = factory.makeGenericMap();
289  std::vector<int> originalKeys = map->keys();
290 
291  // Test local class that returns void
292  class {
293  public:
294  // Local classes can't have method templates
295  void operator()(int, bool& value) { value = !value; }
296  void operator()(int, std::int32_t& value) { value *= 2; }
297  void operator()(int, std::int64_t& value) { value *= 2; }
298  void operator()(int, float& value) { value *= 2; }
299  void operator()(int, double& value) { value *= 2; }
300  void operator()(int, std::string& value) { value += "Appendix"; }
301  void operator()(int, Storable& value) {
302  auto complexStorable = dynamic_cast<ComplexStorable*>(&value);
303  if (complexStorable != nullptr) {
304  *complexStorable = 42;
305  }
306  }
307  void operator()(int, std::shared_ptr<Storable const>) {}
308  } grower;
309  map->apply(grower);
310  std::vector<int> newKeys = map->keys();
311 
312  BOOST_TEST(newKeys == originalKeys);
313  BOOST_TEST(map->at(KEY0) == !VALUE0);
314  BOOST_TEST(map->at(KEY1) == 2 * VALUE1);
315  BOOST_TEST(map->at(KEY2) == 2 * VALUE2);
316  BOOST_TEST(map->at(KEY3) == VALUE3 + "Appendix");
317  BOOST_TEST(*(map->at(KEY4)) == VALUE4);
318  BOOST_TEST(map->at(KEY5) != VALUE5);
319  BOOST_TEST(map->at(KEY5) == ComplexStorable(42));
320  BOOST_TEST(map->at(KEY6) == nullptr);
321 }
322 
323 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestModifyingReturningVisitor, GenericMapFactory) {
324  static GenericMapFactory const factory;
325  std::unique_ptr<GenericMap<int>> map = factory.makeGenericMap();
326  std::vector<int> originalKeys = map->keys();
327 
328  // Test local class that returns int
329  class {
330  public:
331  // Local classes can't have method templates
332  int operator()(int key, bool& value) {
333  value = !value;
334  return key;
335  }
336  int operator()(int key, std::int32_t& value) {
337  value *= 2;
338  return key;
339  }
340  int operator()(int key, std::int64_t& value) {
341  value *= 2;
342  return key;
343  }
344  int operator()(int key, float& value) {
345  value *= 2;
346  return key;
347  }
348  int operator()(int key, double& value) {
349  value *= 2;
350  return key;
351  }
352  int operator()(int key, std::string& value) {
353  value += "Appendix";
354  return key;
355  }
356  int operator()(int key, Storable& value) {
357  auto complexStorable = dynamic_cast<ComplexStorable*>(&value);
358  if (complexStorable != nullptr) {
359  *complexStorable = 42;
360  }
361  return key;
362  }
363  int operator()(int key, std::shared_ptr<Storable const>) { return key; }
364  } grower;
365  std::vector<int> editedKeys = map->apply(grower);
366  BOOST_TEST(editedKeys == originalKeys);
367 
368  std::vector<int> newKeys = map->keys();
369  BOOST_TEST(newKeys == originalKeys);
370  BOOST_TEST(map->at(KEY0) == !VALUE0);
371  BOOST_TEST(map->at(KEY1) == 2 * VALUE1);
372  BOOST_TEST(map->at(KEY2) == 2 * VALUE2);
373  BOOST_TEST(map->at(KEY3) == VALUE3 + "Appendix");
374  BOOST_TEST(*(map->at(KEY4)) == VALUE4);
375  BOOST_TEST(map->at(KEY5) != VALUE5);
376  BOOST_TEST(map->at(KEY5) == ComplexStorable(42));
377  BOOST_TEST(map->at(KEY6) == nullptr);
378 }
379 
380 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestMutableEquals, GenericMapFactory) {
381  using namespace std::string_literals;
382 
383  static GenericMapFactory const factory;
384  auto map1 = factory.makeMutableGenericMap();
385  auto map2 = factory.makeMutableGenericMap();
386 
387  // Use BOOST_CHECK to avoid BOOST_TEST bug from GenericMap being unprintable
388  BOOST_CHECK(*map1 == *map2);
389 
390  auto primitiveKey = makeKey<int>("primitive"s);
391  map1->insert(primitiveKey, 42);
392  BOOST_CHECK(*map1 != *map2);
393  map2->insert(primitiveKey, 42);
394  BOOST_CHECK(*map1 == *map2);
395 
396  auto sharedKey = makeKey<std::shared_ptr<SimpleStorable const>>("shared"s);
397  auto common = std::make_shared<SimpleStorable const>(VALUE4);
398  map1->insert(sharedKey, common);
399  BOOST_CHECK(*map1 != *map2);
400  map2->insert(sharedKey, std::make_shared<SimpleStorable const>(VALUE4));
401  BOOST_CHECK(*map1 != *map2);
402  map2->erase(sharedKey);
403  map2->insert(sharedKey, common);
404  BOOST_CHECK(*map1 == *map2);
405 
406  auto storableKey = makeKey<ComplexStorable>("storable"s);
407  map1->insert(storableKey, VALUE5);
408  BOOST_CHECK(*map1 != *map2);
409  map2->insert(storableKey, VALUE5);
410  BOOST_CHECK(*map1 == *map2);
411 
412  auto nullKey = makeKey<std::shared_ptr<ComplexStorable const>>("null"s);
413  map1->insert(nullKey, std::static_pointer_cast<ComplexStorable const>(VALUE6));
414  BOOST_CHECK(*map1 != *map2);
415  map2->insert(nullKey, std::static_pointer_cast<ComplexStorable const>(VALUE6));
416  BOOST_CHECK(*map1 == *map2);
417 }
418 
419 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestSize, GenericMapFactory) {
420  static GenericMapFactory const factory;
421  std::unique_ptr<GenericMap<int>> demoMap = factory.makeGenericMap();
422 
423  BOOST_TEST(demoMap->size() == 7);
424  BOOST_TEST(!demoMap->empty());
425 }
426 
427 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestMutableSize, GenericMapFactory) {
428  using namespace std::string_literals;
429 
430  static GenericMapFactory const factory;
431  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
432 
433  BOOST_TEST_REQUIRE(demoMap->size() == 0);
434  BOOST_TEST_REQUIRE(demoMap->empty());
435 
436  demoMap->insert(makeKey<int>("Negative One"s), -1);
437  BOOST_TEST(demoMap->size() == 1);
438  BOOST_TEST(!demoMap->empty());
439 
440  demoMap->erase(makeKey<int>("Negative One"s));
441  BOOST_TEST(demoMap->size() == 0);
442  BOOST_TEST(demoMap->empty());
443 }
444 
445 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakContains, GenericMapFactory) {
446  using namespace std::string_literals;
447 
448  static GenericMapFactory const factory;
449  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
450 
451  BOOST_TEST(demoMap->contains(KEY0.getId()));
452  BOOST_TEST(demoMap->contains(KEY1.getId()));
453  BOOST_TEST(demoMap->contains(KEY2.getId()));
454  BOOST_TEST(demoMap->contains(KEY3.getId()));
455  BOOST_TEST(demoMap->contains(KEY4.getId()));
456  BOOST_TEST(demoMap->contains(KEY5.getId()));
457  BOOST_TEST(demoMap->contains(KEY6.getId()));
458 }
459 
460 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestContains, GenericMapFactory) {
461  static GenericMapFactory const factory;
462  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
463 
464  BOOST_TEST(demoMap->contains(KEY0));
465  BOOST_TEST(!demoMap->contains(makeKey<int>(KEY0.getId())));
466 
467  BOOST_TEST(demoMap->contains(KEY1));
468  BOOST_TEST(!demoMap->contains(makeKey<bool>(KEY1.getId())));
469 
470  BOOST_TEST(demoMap->contains(KEY2));
471  // VALUE2 is of a different type than KEY2, check that alternate key is absent
472  BOOST_TEST(!demoMap->contains(makeKey<decltype(VALUE2)>(KEY2.getId())));
473 
474  BOOST_TEST(demoMap->contains(KEY3));
475 
476  BOOST_TEST(demoMap->contains(KEY4));
477  // VALUE4 is of a different type than KEY4, check that alternate key is absent
478  BOOST_TEST(!demoMap->contains(makeKey<decltype(VALUE4)>(KEY4.getId())));
479 
480  BOOST_TEST(demoMap->contains(KEY5));
481  BOOST_TEST(demoMap->contains(makeKey<SimpleStorable>(KEY5.getId())));
482  BOOST_TEST(demoMap->contains(makeKey<Storable>(KEY5.getId())));
483 
484  BOOST_TEST(demoMap->contains(KEY6));
485  BOOST_TEST(demoMap->contains(makeKey<std::shared_ptr<SimpleStorable const>>(KEY6.getId())));
486  BOOST_TEST(demoMap->contains(makeKey<std::shared_ptr<ComplexStorable const>>(KEY6.getId())));
487 }
488 
489 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestKeys, GenericMapFactory) {
490  static GenericMapFactory const factory;
491  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
492  auto orderedKeys = demoMap->keys();
493  // GenericMap allows keys in any order, so just check they're the same
494  std::set<int> keys(orderedKeys.begin(), orderedKeys.end());
495 
496  BOOST_TEST(keys == std::set<int>({KEY0.getId(), KEY1.getId(), KEY2.getId(), KEY3.getId(), KEY4.getId(),
497  KEY5.getId(), KEY6.getId()}));
498 }
499 
500 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestKeyOrder, GenericMapFactory) {
501  static GenericMapFactory const factory;
502  std::unique_ptr<GenericMap<int> const> demoMap = factory.makeGenericMap();
503  auto keys = demoMap->keys();
504 
505  std::vector<int> iterOrder;
506  demoMap->apply([&iterOrder](int key, auto value) { iterOrder.push_back(key); });
507  BOOST_TEST(keys == iterOrder);
508 }
509 
510 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestClearIdempotent, GenericMapFactory) {
511  static GenericMapFactory const factory;
512  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
513 
514  BOOST_TEST_REQUIRE(demoMap->empty());
515  demoMap->clear();
516  BOOST_TEST(demoMap->empty());
517 }
518 
519 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestClear, GenericMapFactory) {
520  using namespace std::string_literals;
521 
522  static GenericMapFactory const factory;
523  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
524 
525  demoMap->insert(makeKey<int>("prime"s), 3);
526  demoMap->insert(makeKey<std::string>("foo"s), "bar"s);
527 
528  BOOST_TEST_REQUIRE(!demoMap->empty());
529  demoMap->clear();
530  BOOST_TEST(demoMap->empty());
531 }
532 
533 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertInt, GenericMapFactory) {
534  using namespace std::string_literals;
535 
536  static GenericMapFactory const factory;
537  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
538 
539  BOOST_TEST_REQUIRE(demoMap->empty());
540 
541  int x = 27;
542  BOOST_TEST(demoMap->insert(makeKey<int>("cube"s), x) == true);
543  BOOST_TEST(demoMap->insert(makeKey<int>("cube"s), 0) == false);
544 
545  BOOST_TEST(!demoMap->empty());
546  BOOST_TEST(demoMap->size() == 1);
547  BOOST_TEST(demoMap->contains("cube"s));
548  BOOST_TEST(demoMap->contains(makeKey<int>("cube"s)));
549  BOOST_TEST(!demoMap->contains(makeKey<double>("cube"s)));
550  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == x);
551 
552  x = 0;
553  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) != x);
554 
555  demoMap->at(makeKey<int>("cube"s)) = 0;
556  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == 0);
557 }
558 
559 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakInsertInt, GenericMapFactory) {
560  using namespace std::string_literals;
561 
562  static GenericMapFactory const factory;
563  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
564 
565  BOOST_TEST_REQUIRE(demoMap->empty());
566 
567  auto insertResult = demoMap->insert("cube"s, 27);
568  BOOST_TEST(insertResult.second == true);
569  BOOST_TEST(demoMap->insert("cube"s, 0).second == false);
570 
571  BOOST_TEST(!demoMap->empty());
572  BOOST_TEST(demoMap->size() == 1);
573  BOOST_TEST(demoMap->contains("cube"s));
574  BOOST_TEST(demoMap->contains(insertResult.first));
575  BOOST_TEST(demoMap->contains(makeKey<int>("cube"s)));
576  BOOST_TEST(!demoMap->contains(makeKey<double>("cube"s)));
577  BOOST_TEST(demoMap->at(insertResult.first) == 27);
578  BOOST_TEST(demoMap->at(makeKey<int>("cube"s)) == 27);
579 
580  demoMap->at(insertResult.first) = 0;
581  BOOST_TEST(demoMap->at(insertResult.first) == 0);
582 }
583 
584 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertString, GenericMapFactory) {
585  using namespace std::string_literals;
586 
587  static GenericMapFactory const factory;
588  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
589 
590  BOOST_TEST_REQUIRE(demoMap->empty());
591 
592  std::string answer(
593  "I have a most elegant and wonderful proof, but this string is too small to contain it."s);
594  BOOST_TEST(demoMap->insert(makeKey<std::string>("Ultimate answer"s), answer) == true);
595  BOOST_TEST(demoMap->insert(makeKey<std::string>("OK"s), "Ook!"s) == true);
596  BOOST_TEST(demoMap->insert(makeKey<std::string>("Ultimate answer"s), "Something philosophical"s) ==
597  false);
598 
599  BOOST_TEST(!demoMap->empty());
600  BOOST_TEST(demoMap->size() == 2);
601  BOOST_TEST(demoMap->contains("OK"s));
602  BOOST_TEST(demoMap->contains(makeKey<std::string>("Ultimate answer"s)));
603  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) == answer);
604  BOOST_TEST(demoMap->at(makeKey<std::string>("OK"s)) == "Ook!"s);
605 
606  answer = "I don't know"s;
607  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) != answer);
608 }
609 
610 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestWeakInsertString, GenericMapFactory) {
611  using namespace std::string_literals;
612 
613  static GenericMapFactory const factory;
614  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
615 
616  BOOST_TEST_REQUIRE(demoMap->empty());
617 
618  auto insertResult1 = demoMap->insert("Ultimate answer"s, "Something philosophical"s);
619  BOOST_TEST(insertResult1.second == true);
620  auto insertResult2 = demoMap->insert("OK"s, "Ook!"s);
621  BOOST_TEST(insertResult2.second == true);
622 
623  BOOST_TEST(!demoMap->empty());
624  BOOST_TEST(demoMap->size() == 2);
625  BOOST_TEST(demoMap->contains(insertResult1.first));
626  BOOST_TEST(demoMap->contains(insertResult2.first));
627  BOOST_TEST(demoMap->contains("OK"s));
628  BOOST_TEST(demoMap->contains(makeKey<std::string>("Ultimate answer"s)));
629  BOOST_TEST(demoMap->at(insertResult1.first) == "Something philosophical"s);
630  BOOST_TEST(demoMap->at(makeKey<std::string>("Ultimate answer"s)) == "Something philosophical"s);
631  BOOST_TEST(demoMap->at(insertResult2.first) == "Ook!"s);
632  BOOST_TEST(demoMap->at(makeKey<std::string>("OK"s)) == "Ook!"s);
633 }
634 
635 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertStorable, GenericMapFactory) {
636  using namespace std::string_literals;
637 
638  static GenericMapFactory const factory;
639  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
640 
641  BOOST_TEST_REQUIRE(demoMap->empty());
642 
643  ComplexStorable object(3.1416);
644  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("foo"s), object) == true);
645  BOOST_TEST(demoMap->insert(makeKey<std::shared_ptr<ComplexStorable const>>("bar"s),
646  std::make_shared<ComplexStorable const>(3.141)) == true);
647  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("foo"s), SimpleStorable()) == false);
648  BOOST_TEST(demoMap->insert(makeKey<std::shared_ptr<SimpleStorable const>>("bar"s),
649  std::make_shared<SimpleStorable const>()) == false);
650  BOOST_TEST(demoMap->insert(makeKey<std::shared_ptr<SimpleStorable const>>("null"s),
651  std::make_shared<SimpleStorable const>()) == true);
652 
653  BOOST_TEST(!demoMap->empty());
654  BOOST_TEST(demoMap->size() == 3);
655  BOOST_TEST(demoMap->contains("foo"s));
656  BOOST_TEST(demoMap->contains(makeKey<Storable>("foo"s)));
657  BOOST_TEST(demoMap->contains(makeKey<std::shared_ptr<ComplexStorable const>>("bar"s)));
658  BOOST_TEST(demoMap->contains(makeKey<std::shared_ptr<SimpleStorable const>>("null"s)));
659 
660  // ComplexStorable::operator== is asymmetric
661  BOOST_TEST(object == demoMap->at(makeKey<SimpleStorable>("foo"s)));
662  object = ComplexStorable(1.4);
663  BOOST_TEST(object != demoMap->at(makeKey<SimpleStorable>("foo"s)));
664  BOOST_TEST(*(demoMap->at(makeKey<std::shared_ptr<ComplexStorable const>>("bar"s))) ==
665  ComplexStorable(3.141));
666 }
667 
668 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInterleavedInserts, GenericMapFactory) {
669  using namespace std::string_literals;
670 
671  static GenericMapFactory const factory;
672  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
673 
674  BOOST_TEST_REQUIRE(demoMap->empty());
675 
676  BOOST_TEST(demoMap->insert(makeKey<int>("key1"s), 3) == true);
677  BOOST_TEST(demoMap->insert(makeKey<double>("key1"s), 1.0) == false);
678  BOOST_TEST(demoMap->insert<Storable>(makeKey<Storable>("key2"s), SimpleStorable()) == true);
679  BOOST_TEST(demoMap->insert(makeKey<std::string>("key3"s), "Test value"s) == true);
680  BOOST_TEST(demoMap->insert(makeKey<std::string>("key4"s), "This is some text"s) == true);
681  std::string const message = "Unknown value for key5."s;
682  BOOST_TEST(demoMap->insert(makeKey<std::string>("key5"s), message) == true);
683  BOOST_TEST(demoMap->insert(makeKey<int>("key3"s), 20) == false);
684  BOOST_TEST(demoMap->insert<double>(makeKey<double>("key6"s), 42) == true);
685 
686  BOOST_TEST(!demoMap->empty());
687  BOOST_TEST(demoMap->size() == 6);
688  BOOST_TEST(demoMap->at(makeKey<int>("key1"s)) == 3);
689  BOOST_TEST(demoMap->at(makeKey<double>("key6"s)) == 42);
690  BOOST_TEST(demoMap->at(makeKey<SimpleStorable>("key2"s)) == SimpleStorable());
691  BOOST_TEST(demoMap->at(makeKey<std::string>("key3"s)) == "Test value"s);
692  BOOST_TEST(demoMap->at(makeKey<std::string>("key4"s)) == "This is some text"s);
693  BOOST_TEST(demoMap->at(makeKey<std::string>("key5"s)) == message);
694 }
695 
696 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestErase, GenericMapFactory) {
697  using namespace std::string_literals;
698 
699  static GenericMapFactory const factory;
700  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
701 
702  demoMap->insert(makeKey<int>("Ultimate answer"s), 42);
703  BOOST_TEST_REQUIRE(demoMap->size() == 1);
704 
705  BOOST_TEST(demoMap->erase(makeKey<std::string>("Ultimate answer"s)) == false);
706  BOOST_TEST(demoMap->size() == 1);
707  BOOST_TEST(demoMap->erase(makeKey<int>("Ultimate answer"s)) == true);
708  BOOST_TEST(demoMap->size() == 0);
709 }
710 
711 BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestInsertEraseInsert, GenericMapFactory) {
712  using namespace std::string_literals;
713 
714  static GenericMapFactory const factory;
715  std::unique_ptr<MutableGenericMap<std::string>> demoMap = factory.makeMutableGenericMap();
716 
717  BOOST_TEST_REQUIRE(demoMap->empty());
718 
719  BOOST_TEST(demoMap->insert(makeKey<int>("Ultimate answer"s), 42) == true);
720  BOOST_TEST(demoMap->insert(makeKey<int>("OK"s), 200) == true);
721  BOOST_TEST(demoMap->erase(makeKey<int>("Ultimate answer"s)) == true);
722  BOOST_TEST(demoMap->insert(makeKey<double>("Ultimate answer"s), 3.1415927) == true);
723 
724  BOOST_TEST(!demoMap->empty());
725  BOOST_TEST(demoMap->size() == 2);
726  BOOST_TEST(demoMap->contains("OK"s));
727  BOOST_TEST(!demoMap->contains(makeKey<int>("Ultimate answer"s)));
728  BOOST_TEST(demoMap->contains(makeKey<double>("Ultimate answer"s)));
729  BOOST_TEST(demoMap->at(makeKey<double>("Ultimate answer"s)) == 3.1415927);
730 }
731 
739 template <class GenericMapFactory>
740 void addGenericMapTestCases(boost::unit_test::test_suite* const suite) {
741  using factories = boost::mpl::list<GenericMapFactory>;
742 
743  suite->add(BOOST_TEST_CASE_TEMPLATE(TestConstAt, factories));
744  suite->add(BOOST_TEST_CASE_TEMPLATE(TestAt, factories));
745  suite->add(BOOST_TEST_CASE_TEMPLATE(TestEquals, factories));
746  suite->add(BOOST_TEST_CASE_TEMPLATE(TestSize, factories));
747  suite->add(BOOST_TEST_CASE_TEMPLATE(TestWeakContains, factories));
748  suite->add(BOOST_TEST_CASE_TEMPLATE(TestContains, factories));
749  suite->add(BOOST_TEST_CASE_TEMPLATE(TestKeys, factories));
750  suite->add(BOOST_TEST_CASE_TEMPLATE(TestKeyOrder, factories));
751  suite->add(BOOST_TEST_CASE_TEMPLATE(TestConstVisitor, factories));
752  suite->add(BOOST_TEST_CASE_TEMPLATE(TestModifyingVoidVisitor, factories));
753  suite->add(BOOST_TEST_CASE_TEMPLATE(TestModifyingReturningVisitor, factories));
754 }
755 
765 template <class GenericMapFactory>
766 void addMutableGenericMapTestCases(boost::unit_test::test_suite* const suite) {
767  using factories = boost::mpl::list<GenericMapFactory>;
768 
769  addGenericMapTestCases<GenericMapFactory>(suite);
770 
771  suite->add(BOOST_TEST_CASE_TEMPLATE(TestMutableEquals, factories));
772  suite->add(BOOST_TEST_CASE_TEMPLATE(TestMutableSize, factories));
773  suite->add(BOOST_TEST_CASE_TEMPLATE(TestClear, factories));
774  suite->add(BOOST_TEST_CASE_TEMPLATE(TestClearIdempotent, factories));
775  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertInt, factories));
776  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertString, factories));
777  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertStorable, factories));
778  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInterleavedInserts, factories));
779  suite->add(BOOST_TEST_CASE_TEMPLATE(TestErase, factories));
780  suite->add(BOOST_TEST_CASE_TEMPLATE(TestInsertEraseInsert, factories));
781 }
782 
791 template <class GenericMapFactory>
792 inline void addGenericMapTestCases() {
793  addGenericMapTestCases<GenericMapFactory>(&(boost::unit_test::framework::master_test_suite()));
794 }
795 
805 template <class GenericMapFactory>
807  addMutableGenericMapTestCases<GenericMapFactory>(&(boost::unit_test::framework::master_test_suite()));
808 }
809 
810 } // namespace test
811 } // namespace typehandling
812 } // namespace afw
813 } // namespace lsst
814 
815 #endif
def keys(self)
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:56
T to_string(T... args)
BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstAt, GenericMapFactory)
Definition: test.h:155
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:162
void addMutableGenericMapTestCases(boost::unit_test::test_suite *const suite)
Create generic test cases for a specific MutableGenericMap implementation.
Definition: test.h:766
STL class.
T push_back(T... args)
A base class for image defects.
Key< U > key
Definition: Schema.cc:281
T str(T... args)
double x
Abstract factory that creates GenericMap and MutableGenericMap instances as needed.
Definition: test.h:134
T size(T... args)
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:740