test_static_map.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <catch2/catch.hpp>
  2. #include <string_view>
  3. #include "libslic3r/StaticMap.hpp"
  4. TEST_CASE("Empty static map should be possible to create and should be empty", "[StaticMap]")
  5. {
  6. using namespace Slic3r;
  7. static const constexpr StaticSet EmptySet;
  8. static const constexpr auto EmptyMap = make_staticmap<int, int>();
  9. constexpr bool is_map_empty = EmptyMap.empty();
  10. constexpr bool is_set_empty = EmptySet.empty();
  11. REQUIRE(is_map_empty);
  12. REQUIRE(is_set_empty);
  13. }
  14. TEST_CASE("StaticSet should derive it's type from the initializer", "[StaticMap]") {
  15. using namespace Slic3r;
  16. static const constexpr StaticSet iOneSet = { 1 };
  17. static constexpr size_t iOneSetSize = iOneSet.size();
  18. REQUIRE(iOneSetSize == 1);
  19. static const constexpr StaticSet iManySet = { 1, 3, 5, 80, 40 };
  20. static constexpr size_t iManySetSize = iManySet.size();
  21. REQUIRE(iManySetSize == 5);
  22. }
  23. TEST_CASE("StaticMap should derive it's type using make_staticmap", "[StaticMap]") {
  24. using namespace Slic3r;
  25. static const constexpr auto ciOneMap = make_staticmap<char, int>({
  26. {'a', 1},
  27. });
  28. static constexpr size_t ciOneMapSize = ciOneMap.size();
  29. static constexpr bool ciOneMapValid = query(ciOneMap, 'a').value_or(0) == 1;
  30. REQUIRE(ciOneMapSize == 1);
  31. REQUIRE(ciOneMapValid);
  32. static const constexpr auto ciManyMap = make_staticmap<char, int>({
  33. {'a', 1}, {'b', 2}, {'A', 10}
  34. });
  35. static constexpr size_t ciManyMapSize = ciManyMap.size();
  36. static constexpr bool ciManyMapValid =
  37. query(ciManyMap, 'a').value_or(0) == 1 &&
  38. query(ciManyMap, 'b').value_or(0) == 2 &&
  39. query(ciManyMap, 'A').value_or(0) == 10 &&
  40. !contains(ciManyMap, 'B') &&
  41. !query(ciManyMap, 'c').has_value();
  42. REQUIRE(ciManyMapSize == 3);
  43. REQUIRE(ciManyMapValid);
  44. for (auto &[k, v] : ciManyMap) {
  45. auto val = query(ciManyMap, k);
  46. REQUIRE(val.has_value());
  47. REQUIRE(*val == v);
  48. }
  49. }
  50. TEST_CASE("StaticSet should be able to find contained values", "[StaticMap]")
  51. {
  52. using namespace Slic3r;
  53. using namespace std::string_view_literals;
  54. auto cmp = [](const char *a, const char *b) constexpr {
  55. return std::string_view{a} < std::string_view{b};
  56. };
  57. static constexpr StaticSet CStrSet = {cmp, "One", "Two", "Three"};
  58. static constexpr StaticSet StringSet = {"One"sv, "Two"sv, "Three"sv};
  59. static constexpr bool CStrSetValid = query(CStrSet, "One").has_value() &&
  60. contains(CStrSet, "Two") &&
  61. contains(CStrSet, "Three") &&
  62. !contains(CStrSet, "one") &&
  63. !contains(CStrSet, "two") &&
  64. !contains(CStrSet, "three");
  65. static constexpr bool StringSetValid = contains(StringSet, "One"sv) &&
  66. contains(StringSet, "Two"sv) &&
  67. contains(StringSet, "Three"sv) &&
  68. !contains(StringSet, "one"sv) &&
  69. !contains(StringSet, "two"sv) &&
  70. !contains(StringSet, "three"sv);
  71. REQUIRE(CStrSetValid);
  72. REQUIRE(StringSetValid);
  73. REQUIRE(CStrSet.size() == 3);
  74. REQUIRE(StringSet.size() == 3);
  75. }