From 61656a5a1b213b919c7361e804c56e83cb3b1985 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 2 Jan 2020 12:31:03 +0100 Subject: impl: fix subtractable traits --- include/newtype/impl/type_traits_extensions.hpp | 6 +- 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 - struct is_subtractable() + std::declval())>> : std::true_type + struct is_subtractable() - std::declval())>> : 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 - struct is_nothrow_subtractable() + std::declval())>> - : std::bool_constant() + std::declval())> + struct is_nothrow_subtractable() - std::declval())>> + : std::bool_constant() - std::declval())> { }; 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 +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); } + template + 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, "Sanity Check"); + using type_alias = nt::new_type; + ASSERT_EQUAL(nt::impl::is_addable_v, nt::impl::is_addable_v); + } + 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; @@ -55,6 +100,14 @@ inline namespace subtraction_tests ASSERT(nt::impl::is_subtractable_v); } + template + 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, "Sanity Check"); + using type_alias = nt::new_type; + ASSERT_EQUAL(nt::impl::is_subtractable_v, nt::impl::is_subtractable_v); + } + 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; @@ -73,18 +126,23 @@ inline namespace subtraction_tests auto arithmetic_suite() -> std::pair { - 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), + KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself_if_the_base_type_is_addable), + 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), + KAWAII(a_new__type_deriving_arithmetic_is_subtractable_with_instances_of_itself_if_the_base_type_is_subtractable), + 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 -- cgit v1.2.3