diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 12:31:03 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 12:31:03 +0100 |
| commit | 61656a5a1b213b919c7361e804c56e83cb3b1985 (patch) | |
| tree | fc7a2c99fd6900b8804d3b4f2db0de9e20577857 | |
| parent | de5d0cbb9c58852ff0038be5495fa7e5d656dd1d (diff) | |
| download | newtype-61656a5a1b213b919c7361e804c56e83cb3b1985.tar.xz newtype-61656a5a1b213b919c7361e804c56e83cb3b1985.zip | |
impl: fix subtractable traits
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 6 | ||||
| -rw-r--r-- | test/src/arithmetic_suite.cpp | 86 |
2 files changed, 75 insertions, 17 deletions
diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index 56824e2..d3e6d8b 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -595,7 +595,7 @@ namespace nt::impl * @note This specialization forms the case for subtractable T */ template<typename T> - struct is_subtractable<T, std::void_t<decltype(std::declval<T const &>() + std::declval<T const &>())>> : std::true_type + struct is_subtractable<T, std::void_t<decltype(std::declval<T const &>() - std::declval<T const &>())>> : std::true_type { }; @@ -625,8 +625,8 @@ namespace nt::impl * @note This specialization forms the case for subtractable T detemining if T is noexcept subtractable */ template<typename T> - struct is_nothrow_subtractable<T, std::void_t<decltype(std::declval<T const &>() + std::declval<T const &>())>> - : std::bool_constant<noexcept(std::declval<T const &>() + std::declval<T const &>())> + struct is_nothrow_subtractable<T, std::void_t<decltype(std::declval<T const &>() - std::declval<T const &>())>> + : std::bool_constant<noexcept(std::declval<T const &>() - std::declval<T const &>())> { }; diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index 57c06e8..b7713e0 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -9,6 +9,43 @@ #include <type_traits> +namespace +{ + + struct addable_type + { + auto constexpr operator+(addable_type const &) const -> addable_type + { + return {}; + }; + }; + + struct subtractable_type + { + auto constexpr operator-(subtractable_type const &) const -> subtractable_type + { + return {}; + }; + }; + + struct multipliable_type + { + auto constexpr operator*(multipliable_type const &)const -> multipliable_type + { + return {}; + }; + }; + + struct dividable_type + { + auto constexpr operator/(dividable_type const &) const -> dividable_type + { + return {}; + }; + }; + +} // namespace + inline namespace addition_tests { @@ -24,6 +61,14 @@ inline namespace addition_tests ASSERT(nt::impl::is_addable_v<type_alias>); } + template<typename T> + auto a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself_if_the_base_type_is_addable() -> void + { + static_assert(nt::impl::is_addable_v<T>, "Sanity Check"); + using type_alias = nt::new_type<T, struct tag, deriving(nt::Arithmetic)>; + ASSERT_EQUAL(nt::impl::is_addable_v<T>, nt::impl::is_addable_v<type_alias>); + } + auto addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void { using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; @@ -55,6 +100,14 @@ inline namespace subtraction_tests ASSERT(nt::impl::is_subtractable_v<type_alias>); } + template<typename T> + auto a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself_if_the_base_type_is_subtractable() -> void + { + static_assert(nt::impl::is_subtractable_v<T>, "Sanity Check"); + using type_alias = nt::new_type<T, struct tag, deriving(nt::Arithmetic)>; + ASSERT_EQUAL(nt::impl::is_subtractable_v<T>, nt::impl::is_subtractable_v<type_alias>); + } + auto subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void { using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; @@ -73,18 +126,23 @@ inline namespace subtraction_tests auto arithmetic_suite() -> std::pair<cute::suite, std::string> { - return {{ - /// Addition Tests - KAWAII(a_new__type_not_deriving_arithmetic_is_not_addable_with_instances_of_itself), - KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself), - KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), - KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), - - /// Subtraction Tests - KAWAII(a_new__type_not_deriving_arithmetic_is_not_subtractable_with_instances_of_itself), - KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself), - KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), - KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), - }, - "Arithmetic Operators Tests"}; + return { + { + /// Addition Tests + KAWAII(a_new__type_not_deriving_arithmetic_is_not_addable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself_if_the_base_type_is_addable<int>), + KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself_if_the_base_type_is_addable<addable_type>), + KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), + KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + + /// Subtraction Tests + KAWAII(a_new__type_not_deriving_arithmetic_is_not_subtractable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself), + KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself_if_the_base_type_is_subtractable<int>), + KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself_if_the_base_type_is_subtractable<subtractable_type>), + KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), + KAWAII(subtraction_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + }, + "Arithmetic Operators Tests"}; }
\ No newline at end of file |
