From 65f0b622e390edb39a6cfd8d614374314472a5a9 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 28 Dec 2019 17:22:06 +0100 Subject: new_type: add equality comparison tests --- CMakeLists.txt | 1 + include/newtype/new_type.hpp | 10 +- test/include/equality_comparison_suite.hpp | 11 +++ test/src/driver.cpp | 2 + test/src/equality_comparison_suite.cpp | 145 +++++++++++++++++++++++++++++ 5 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 test/include/equality_comparison_suite.hpp create mode 100644 test/src/equality_comparison_suite.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9977012..b78a58e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ if(BUILD_TESTING) "${PROJECT_SOURCE_DIR}/test/src/derivation_clause_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/conversion_suite.cpp" + "${PROJECT_SOURCE_DIR}/test/src/equality_comparison_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/new_type_constructor_suite.cpp" ) diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 67e29c3..c0fb8d3 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -134,7 +134,9 @@ namespace nt */ template auto constexpr operator==(new_type const & lhs, - new_type const & rhs) noexcept(noexcept(lhs.decay() == rhs.decay())) -> bool + new_type const & rhs) noexcept(noexcept(std::declval() == + std::declval())) + -> bool { return lhs.decay() == rhs.decay(); } @@ -144,9 +146,11 @@ namespace nt */ template auto constexpr operator!=(new_type const & lhs, - new_type const & rhs) noexcept(noexcept(!(lhs == rhs))) -> bool + new_type const & rhs) noexcept(noexcept(std::declval() != + std::declval())) + -> bool { - return !(lhs == rhs); + return lhs.decay() != rhs.decay(); } } // namespace nt diff --git a/test/include/equality_comparison_suite.hpp b/test/include/equality_comparison_suite.hpp new file mode 100644 index 0000000..80abbe7 --- /dev/null +++ b/test/include/equality_comparison_suite.hpp @@ -0,0 +1,11 @@ +#ifndef NEWTYPE_TEST_EQUALITY_COMPARISON_SUITE_HPP +#define NEWTYPE_TEST_EQUALITY_COMPARISON_SUITE_HPP + +#include + +#include +#include + +auto equality_comparison_suite() -> std::pair; + +#endif \ No newline at end of file diff --git a/test/src/driver.cpp b/test/src/driver.cpp index 6c1ae7d..f6afe58 100644 --- a/test/src/driver.cpp +++ b/test/src/driver.cpp @@ -1,5 +1,6 @@ #include "conversion_suite.hpp" #include "derivation_clause_suite.hpp" +#include "equality_comparison_suite.hpp" #include "new_type_constructor_suite.hpp" #include @@ -51,6 +52,7 @@ int main(int argc, char ** argv) derivation_clause_suite(), new_type_constructor_suite(), conversion_suite(), + equality_comparison_suite(), }; auto selectors = get_test_selectors(suites); diff --git a/test/src/equality_comparison_suite.cpp b/test/src/equality_comparison_suite.cpp new file mode 100644 index 0000000..64c7521 --- /dev/null +++ b/test/src/equality_comparison_suite.cpp @@ -0,0 +1,145 @@ +#include "equality_comparison_suite.hpp" + +#include "kawaii.hpp" +#include "newtype/derivable.hpp" +#include "newtype/deriving.hpp" +#include "newtype/new_type.hpp" + +#include + +#include +#include +#include + +inline namespace basic_equality_comparsion_tests +{ + + auto two_instances_of_the_same_new__type_with_the_same_value_compare_equal() -> void + { + using type_alias = nt::new_type; + + auto constexpr lhs = type_alias{42}; + auto constexpr rhs = type_alias{42}; + + ASSERT_EQUAL(lhs, rhs); + } + + auto two_instances_of_the_same_new__type_with_the_same_value_do_not_compare_not_equal() -> void + { + using type_alias = nt::new_type; + + auto constexpr lhs = type_alias{42}; + auto constexpr rhs = type_alias{42}; + + ASSERT(!(lhs != rhs)); + } + + auto two_instances_of_the_same_new__type_with_differing_values_do_compare_not_equal() -> void + { + using type_alias = nt::new_type; + + auto constexpr lhs = type_alias{42}; + auto constexpr rhs = type_alias{43}; + + ASSERT_NOT_EQUAL_TO(lhs, rhs); + } + + auto two_instances_of_the_same_new__type_with_differing_values_do_not_compare_equal() -> void + { + using type_alias = nt::new_type; + + auto constexpr lhs = type_alias{42}; + auto constexpr rhs = type_alias{43}; + + ASSERT(!(lhs == rhs)); + } + +} // namespace basic_equality_comparsion_tests + +inline namespace equality_comparsion_noexcept_tests +{ + + auto equality_comparison_on_a_new__type_is_noexcept_if_the_base_type_is_noexcept_comparable() -> void + { + using type_alias = nt::new_type; + static_assert(noexcept(std::declval() == std::declval()), "Sanity Check"); + ASSERT(noexcept(std::declval() == std::declval())); + } + + auto equality_comparison_on_a_new__type_is_not_noexcept_if_the_base_type_is_not_noexcept_comparable() -> void + { + struct strange_type + { + auto constexpr operator==(strange_type const &) const noexcept(false) -> bool + { + return false; + } + }; + + using type_alias = nt::new_type; + static_assert(!noexcept(std::declval() == std::declval()), "Sanity Check"); + ASSERT(!noexcept(std::declval() == std::declval())); + } + + auto inequality_comparison_on_a_new__type_is_noexcept_if_the_base_type_is_noexcept_comparable() -> void + { + using type_alias = nt::new_type; + static_assert(noexcept(std::declval() != std::declval()), "Sanity Check"); + ASSERT(noexcept(std::declval() != std::declval())); + } + + auto inequality_comparison_on_a_new__type_is_not_noexcept_if_the_base_type_is_not_noexcept_comparable() -> void + { + struct strange_type + { + auto constexpr operator!=(strange_type const &) const noexcept(false) -> bool + { + return false; + } + }; + + using type_alias = nt::new_type; + static_assert(!noexcept(std::declval() != std::declval()), "Sanity Check"); + ASSERT(!noexcept(std::declval() != std::declval())); + } + +} // namespace equality_comparsion_noexcept_tests + +inline namespace equality_comparison_return_type_tests +{ + + auto equality_comparsion_of_two_new__type_instances_returns_bool() -> void + { + using type_alias = nt::new_type; + ASSERT((std::is_same_v() == std::declval())>)); + } + + auto inequality_comparsion_of_two_new__type_instances_returns_bool() -> void + { + using type_alias = nt::new_type; + ASSERT((std::is_same_v() != std::declval())>)); + } + +} // namespace equality_comparison_return_type_tests + +auto equality_comparison_suite() -> std::pair +{ + return {{ + // Basic Equality Comparison Tests + KAWAII(two_instances_of_the_same_new__type_with_the_same_value_compare_equal), + KAWAII(two_instances_of_the_same_new__type_with_the_same_value_do_not_compare_not_equal), + KAWAII(two_instances_of_the_same_new__type_with_differing_values_do_compare_not_equal), + KAWAII(two_instances_of_the_same_new__type_with_differing_values_do_not_compare_equal), + + // Equality Comparison noexcept Tests + KAWAII(equality_comparison_on_a_new__type_is_noexcept_if_the_base_type_is_noexcept_comparable), + KAWAII(equality_comparison_on_a_new__type_is_not_noexcept_if_the_base_type_is_not_noexcept_comparable), + KAWAII(inequality_comparison_on_a_new__type_is_noexcept_if_the_base_type_is_noexcept_comparable), + KAWAII(inequality_comparison_on_a_new__type_is_not_noexcept_if_the_base_type_is_not_noexcept_comparable), + + // Equality Comparison Return Type Tests + KAWAII(equality_comparsion_of_two_new__type_instances_returns_bool), + KAWAII(inequality_comparsion_of_two_new__type_instances_returns_bool), + }, + "Equality Comparison Tests"}; +} \ No newline at end of file -- cgit v1.2.3