From 43c7db49ff824577cff11cdd03d1f5bafcbd4b38 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 Jun 2023 08:16:53 +0200 Subject: tests: port hash tests --- source/lib/include/newtype/newtype.hpp | 4 +- source/tests/CMakeLists.txt | 2 +- source/tests/src/hash.cpp | 56 +++++++++++++++++++++++++++ source/tests/src/hash_suite.cpp | 69 ---------------------------------- 4 files changed, 59 insertions(+), 72 deletions(-) create mode 100644 source/tests/src/hash.cpp delete mode 100644 source/tests/src/hash_suite.cpp diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index e2704f3..2e71553 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -537,9 +537,9 @@ namespace std template struct hash> { - template + template auto constexpr operator()(nt::new_type const & object, - std::enable_if_t> * = nullptr) const + std::enable_if_t> * = nullptr) const -> std::size_t { return std::hash{}(object.decay()); diff --git a/source/tests/CMakeLists.txt b/source/tests/CMakeLists.txt index f2c39e0..594b518 100644 --- a/source/tests/CMakeLists.txt +++ b/source/tests/CMakeLists.txt @@ -11,7 +11,7 @@ add_executable("${PROJECT_NAME}_tests" "src/conversion.cpp" "src/derivation_clause.cpp" # "src/equality_comparison_suite.cpp" - # "src/hash_suite.cpp" + "src/hash.cpp" # "src/io_operators_suite.cpp" # "src/iterable_suite.cpp" # "src/new_type_constructor_suite.cpp" diff --git a/source/tests/src/hash.cpp b/source/tests/src/hash.cpp new file mode 100644 index 0000000..173770a --- /dev/null +++ b/source/tests/src/hash.cpp @@ -0,0 +1,56 @@ +#include "newtype/derivable.hpp" +#include "newtype/deriving.hpp" +#include "newtype/impl/type_traits_extensions.hpp" +#include "newtype/newtype.hpp" + +#include + +#include + +SCENARIO("Hash", "[hash]") +{ + GIVEN("A new_type not deriving nt::Hash") + { + using type_alias = nt::new_type; + + THEN("it is not hashable") + { + REQUIRE_FALSE(nt::impl::is_hashable_v); + } + } + + GIVEN("A new_type over a hashable type deriving nt::Hash") + { + using type_alias = nt::new_type; + + THEN("it is hashable") + { + REQUIRE(nt::impl::is_hashable_v); + } + } + + GIVEN("A new_type over a non-hashable type deriving nt::Hash") + { + struct non_hashable + { + }; + using type_alias = nt::new_type; + + THEN("it is not hashable") + { + REQUIRE_FALSE(nt::impl::is_hashable_v); + } + } + + GIVEN("A hashable new_type") + { + using type_alias = nt::new_type; + + THEN("it can be used a the key in an unordered_map") + { + auto map = std::unordered_map{}; + map[type_alias{42}] = 43; + REQUIRE(map[type_alias{42}] == 43); + } + } +} diff --git a/source/tests/src/hash_suite.cpp b/source/tests/src/hash_suite.cpp deleted file mode 100644 index b482414..0000000 --- a/source/tests/src/hash_suite.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "hash_suite.hpp" - -#include "kawaii.hpp" -#include "newtype/derivable.hpp" -#include "newtype/deriving.hpp" -#include "newtype/impl/type_traits_extensions.hpp" -#include "newtype/newtype.hpp" - -#include - -#include - -inline namespace hashable_tests -{ - - auto a_new__type_that_does_not_include_hash_in_its_derivation_clause_is_not_hashable() -> void - { - using type_alias = nt::new_type; - ASSERT(!nt::impl::is_hashable_v); - } - - auto a_new__type_that_does_include_hash_in_its_derivation_clause_is_hashable() -> void - { - static_assert(nt::impl::is_hashable_v, "Sanity Check"); - using type_alias = nt::new_type; - ASSERT(nt::impl::is_hashable_v); - } - - auto a_new__type_that_does_include_hash_in_its_derivation_clause_but_whose_base_type_is_not_hashable_is_also_not_hashable() -> void - { - struct not_hashable - { - }; - - static_assert(!nt::impl::is_hashable_v, "Sanity Check"); - using type_alias = nt::new_type; - ASSERT(!nt::impl::is_hashable_v); - } - -} // namespace hashable_tests - -inline namespace usage_tests -{ - - auto a_new__type_that_is_hashable_can_be_used_in_an_unordered__map() -> void - { - static_assert(nt::impl::is_hashable_v, "Sanity Check"); - using type_alias = nt::new_type; - - auto map = std::unordered_map{}; - map[type_alias{42}] = 43; - ASSERT_EQUAL(43, map[type_alias{42}]); - } - -} // namespace usage_tests - -auto hash_suite() -> std::pair -{ - return {{ - // Hashable Tests - KAWAII(a_new__type_that_does_not_include_hash_in_its_derivation_clause_is_not_hashable), - KAWAII(a_new__type_that_does_include_hash_in_its_derivation_clause_is_hashable), - KAWAII(a_new__type_that_does_include_hash_in_its_derivation_clause_but_whose_base_type_is_not_hashable_is_also_not_hashable), - - // Usage Tests - KAWAII(a_new__type_that_is_hashable_can_be_used_in_an_unordered__map), - }, - "std::hash Support Tests"}; -} \ No newline at end of file -- cgit v1.2.3