diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 08:16:53 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 08:16:53 +0200 |
| commit | 43c7db49ff824577cff11cdd03d1f5bafcbd4b38 (patch) | |
| tree | b15154f8bf9128722653a257c206bcf5f3a10972 | |
| parent | 2fd8b9c88c00c6385c8113afa94ebd447e0a7e8b (diff) | |
| download | newtype-43c7db49ff824577cff11cdd03d1f5bafcbd4b38.tar.xz newtype-43c7db49ff824577cff11cdd03d1f5bafcbd4b38.zip | |
tests: port hash tests
| -rw-r--r-- | source/lib/include/newtype/newtype.hpp | 4 | ||||
| -rw-r--r-- | source/tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | source/tests/src/hash.cpp | 56 | ||||
| -rw-r--r-- | source/tests/src/hash_suite.cpp | 69 |
4 files changed, 59 insertions, 72 deletions
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<typename BaseType, typename TagType, auto DerivationClause> struct hash<nt::new_type<BaseType, TagType, DerivationClause>> { - template<typename BaseTypeT = BaseType, auto DerivationClauseV = DerivationClause> + template<typename BaseTypeT = BaseType> auto constexpr operator()(nt::new_type<BaseType, TagType, DerivationClause> const & object, - std::enable_if_t<DerivationClauseV(nt::Hash) && nt::impl::is_hashable_v<BaseTypeT>> * = nullptr) const + std::enable_if_t<DerivationClause(nt::Hash) && nt::impl::is_hashable_v<BaseTypeT>> * = nullptr) const -> std::size_t { return std::hash<BaseType>{}(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 <catch2/catch_test_macros.hpp> + +#include <unordered_map> + +SCENARIO("Hash", "[hash]") +{ + GIVEN("A new_type not deriving nt::Hash") + { + using type_alias = nt::new_type<int, struct tag>; + + THEN("it is not hashable") + { + REQUIRE_FALSE(nt::impl::is_hashable_v<type_alias>); + } + } + + GIVEN("A new_type over a hashable type deriving nt::Hash") + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Hash)>; + + THEN("it is hashable") + { + REQUIRE(nt::impl::is_hashable_v<type_alias>); + } + } + + GIVEN("A new_type over a non-hashable type deriving nt::Hash") + { + struct non_hashable + { + }; + using type_alias = nt::new_type<non_hashable, struct tag, deriving(nt::Hash)>; + + THEN("it is not hashable") + { + REQUIRE_FALSE(nt::impl::is_hashable_v<type_alias>); + } + } + + GIVEN("A hashable new_type") + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Hash)>; + + THEN("it can be used a the key in an unordered_map") + { + auto map = std::unordered_map<type_alias, int>{}; + 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 <cute/cute.h> - -#include <unordered_map> - -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<int, struct tag>; - ASSERT(!nt::impl::is_hashable_v<type_alias>); - } - - auto a_new__type_that_does_include_hash_in_its_derivation_clause_is_hashable() -> void - { - static_assert(nt::impl::is_hashable_v<int>, "Sanity Check"); - using type_alias = nt::new_type<int, struct tag, deriving(nt::Hash)>; - ASSERT(nt::impl::is_hashable_v<type_alias>); - } - - 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<not_hashable>, "Sanity Check"); - using type_alias = nt::new_type<not_hashable, struct tag, deriving(nt::Hash)>; - ASSERT(!nt::impl::is_hashable_v<type_alias>); - } - -} // 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<int>, "Sanity Check"); - using type_alias = nt::new_type<int, struct tag, deriving(nt::Hash)>; - - auto map = std::unordered_map<type_alias, int>{}; - map[type_alias{42}] = 43; - ASSERT_EQUAL(43, map[type_alias{42}]); - } - -} // namespace usage_tests - -auto hash_suite() -> std::pair<cute::suite, std::string> -{ - 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 |
