diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2020-01-03 13:10:02 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2020-01-03 13:10:02 +0100 |
| commit | d5970f8ed49a15d19265c71c8618e32a9534eeee (patch) | |
| tree | c03bc611027f32d9639bfb645523e9bd29b68f42 /test | |
| parent | 8551177740f435bd510df6af3fa3d32da39bb526 (diff) | |
| download | newtype-d5970f8ed49a15d19265c71c8618e32a9534eeee.tar.xz newtype-d5970f8ed49a15d19265c71c8618e32a9534eeee.zip | |
new_type: implement support for derivation of Hash
Diffstat (limited to 'test')
| -rw-r--r-- | test/include/hash_suite.hpp | 11 | ||||
| -rw-r--r-- | test/include/kawaii.hpp | 2 | ||||
| -rw-r--r-- | test/src/arithmetic_suite.cpp | 3 | ||||
| -rw-r--r-- | test/src/driver.cpp | 2 | ||||
| -rw-r--r-- | test/src/hash_suite.cpp | 69 |
5 files changed, 85 insertions, 2 deletions
diff --git a/test/include/hash_suite.hpp b/test/include/hash_suite.hpp new file mode 100644 index 0000000..0ef51bc --- /dev/null +++ b/test/include/hash_suite.hpp @@ -0,0 +1,11 @@ +#ifndef NEWTYPE_TEST_HASH_SUITE_HPP +#define NEWTYPE_TEST_HASH_SUITE_HPP + +#include <cute/cute_suite.h> + +#include <string> +#include <utility> + +auto hash_suite() -> std::pair<cute::suite, std::string>; + +#endif
\ No newline at end of file diff --git a/test/include/kawaii.hpp b/test/include/kawaii.hpp index 69da012..9084b56 100644 --- a/test/include/kawaii.hpp +++ b/test/include/kawaii.hpp @@ -18,7 +18,7 @@ namespace nt::test { auto constexpr prepositions = std::array{"a", "an", "and", "as", "at", "by", "for", "in", "of", "on", "or", "the", "to"}; auto constexpr keywords = std::array{"noexcept"}; - auto constexpr type_names = std::array{"new_type", "derivation_clause"}; + auto constexpr type_names = std::array{"new_type", "derivation_clause", "unordered_map"}; auto inline replace_template_argument_syntax(std::string const & name) -> std::string { diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index f831b98..2e7f1fd 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -1,4 +1,5 @@ -#include "conversion_suite.hpp" +#include "arithmetic_suite.hpp" + #include "kawaii.hpp" #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" diff --git a/test/src/driver.cpp b/test/src/driver.cpp index c93f157..cfd6b90 100644 --- a/test/src/driver.cpp +++ b/test/src/driver.cpp @@ -2,6 +2,7 @@ #include "conversion_suite.hpp" #include "derivation_clause_suite.hpp" #include "equality_comparison_suite.hpp" +#include "hash_suite.hpp" #include "io_operators_suite.hpp" #include "new_type_constructor_suite.hpp" #include "relational_operators_suite.hpp" @@ -59,6 +60,7 @@ int main(int argc, char ** argv) relational_operators_suite(), io_operators_suite(), arithmetic_suite(), + hash_suite(), }; auto selectors = get_test_selectors(suites); diff --git a/test/src/hash_suite.cpp b/test/src/hash_suite.cpp new file mode 100644 index 0000000..9ca362c --- /dev/null +++ b/test/src/hash_suite.cpp @@ -0,0 +1,69 @@ +#include "hash_suite.hpp" + +#include "kawaii.hpp" +#include "newtype/derivable.hpp" +#include "newtype/deriving.hpp" +#include "newtype/impl/type_traits_extensions.hpp" +#include "newtype/new_type.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 |
