From 97ccfa11b73e5aabb90be44dc8e37b25d3236961 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 8 Jun 2023 08:48:12 +0200 Subject: tests: port equality comparison tests --- source/tests/CMakeLists.txt | 2 +- source/tests/src/equality_comparison.cpp | 104 ++++++++++++++ source/tests/src/equality_comparison_suite.cpp | 188 ------------------------- 3 files changed, 105 insertions(+), 189 deletions(-) create mode 100644 source/tests/src/equality_comparison.cpp delete mode 100644 source/tests/src/equality_comparison_suite.cpp diff --git a/source/tests/CMakeLists.txt b/source/tests/CMakeLists.txt index 594b518..1a33700 100644 --- a/source/tests/CMakeLists.txt +++ b/source/tests/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable("${PROJECT_NAME}_tests" "src/arithmetic.cpp" "src/conversion.cpp" "src/derivation_clause.cpp" - # "src/equality_comparison_suite.cpp" + "src/equality_comparison.cpp" "src/hash.cpp" # "src/io_operators_suite.cpp" # "src/iterable_suite.cpp" diff --git a/source/tests/src/equality_comparison.cpp b/source/tests/src/equality_comparison.cpp new file mode 100644 index 0000000..0c4055c --- /dev/null +++ b/source/tests/src/equality_comparison.cpp @@ -0,0 +1,104 @@ +#include "newtype/derivable.hpp" +#include "newtype/deriving.hpp" +#include "newtype/newtype.hpp" + +#include + +#include +#include +#include + +SCENARIO("Equality Comparison", "[compare]") +{ + GIVEN("A new_type over an equality comparable type") + { + using type_alias = nt::new_type; + + THEN("two objects of it with the same value compare equal") + { + REQUIRE(type_alias{42} == type_alias{42}); + } + + THEN("two objects of it with the same value do not compare not-equal") + { + REQUIRE_FALSE(type_alias{42} != type_alias{42}); + } + + THEN("two object of it with different values do not compare equal") + { + REQUIRE_FALSE(type_alias{42} == type_alias{43}); + } + + THEN("two object of it with different values compare not-equal") + { + REQUIRE(type_alias{42} != type_alias{43}); + } + + THEN("equality comparison returns bool") + { + REQUIRE(std::is_same_v() == std::declval())>); + } + + THEN("inequality comparison returns bool") + { + REQUIRE(std::is_same_v() != std::declval())>); + } + } + + GIVEN("A new_type deriving nt::EqBase") + { + using type_alias = nt::new_type; + + THEN("an instance of it compares equal to the equivalent base type value") + { + REQUIRE(type_alias{42} == 42); + } + + THEN("an instance of it comapres not-equal to a different base type value") + { + REQUIRE(type_alias{42} != 43); + } + } + + GIVEN("A new_type over a nothrow-comparable type") + { + using type_alias = nt::new_type; + + static_assert(noexcept(std::declval() == std::declval())); + static_assert(noexcept(std::declval() != std::declval())); + + THEN("it is nothrow-equality-comparable") + { + REQUIRE(noexcept(std::declval() == std::declval())); + } + + THEN("it is nothrow-inequality-comparable") + { + REQUIRE(noexcept(std::declval() != std::declval())); + } + } + + GIVEN("A new_type over a non-nothrow-comparable type") + { + struct not_nothrow_comparable + { + auto operator==(not_nothrow_comparable) const noexcept(false) -> bool; + auto operator!=(not_nothrow_comparable) const noexcept(false) -> bool; + }; + + using type_alias = nt::new_type; + + static_assert(!noexcept(std::declval() == std::declval())); + static_assert(!noexcept(std::declval() != std::declval())); + + THEN("it is not nothrow-equality-comparable") + { + REQUIRE_FALSE(noexcept(std::declval() == std::declval())); + } + + THEN("it is not nothrow-inequality-comparable") + { + REQUIRE_FALSE(noexcept(std::declval() != std::declval())); + } + } +} diff --git a/source/tests/src/equality_comparison_suite.cpp b/source/tests/src/equality_comparison_suite.cpp deleted file mode 100644 index 4e484f4..0000000 --- a/source/tests/src/equality_comparison_suite.cpp +++ /dev/null @@ -1,188 +0,0 @@ -#include "equality_comparison_suite.hpp" - -#include "kawaii.hpp" -#include "newtype/derivable.hpp" -#include "newtype/deriving.hpp" -#include "newtype/newtype.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 - -inline namespace base_type_equality_comparison_tests -{ - - auto an_instance_of_a_new__type_compares_equal_to_an_instance_of_its_base_type_with_the_same_value() -> void - { - using type_alias = nt::new_type; - auto lhs = type_alias{42}; - auto rhs = 42; - ASSERT_EQUAL(lhs, rhs); - } - - auto an_instance_of_the_base_type_of_a_new__type_compares_equal_to_an_instance_of_the_new__type_with_the_same_value() -> void - { - using type_alias = nt::new_type; - auto lhs = 42; - auto rhs = type_alias{42}; - ASSERT_EQUAL(lhs, rhs); - } - - auto an_instance_of_a_new__type_compares_not_equal_to_an_instance_of_its_base_type_with_a_differing_value() -> void - { - using type_alias = nt::new_type; - auto lhs = type_alias{42}; - auto rhs = 43; - ASSERT(lhs != rhs); - } - - auto an_instance_of_the_base_type_of_a_new__type_compares_not_equal_to_an_instance_of_the_new__type_with_a_differing_value() -> void - { - using type_alias = nt::new_type; - auto lhs = 43; - auto rhs = type_alias{42}; - ASSERT(lhs != rhs); - } - -} // namespace base_type_equality_comparison_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), - - // Base-type Equality Comparison Tests - KAWAII(an_instance_of_a_new__type_compares_equal_to_an_instance_of_its_base_type_with_the_same_value), - KAWAII(an_instance_of_the_base_type_of_a_new__type_compares_equal_to_an_instance_of_the_new__type_with_the_same_value), - KAWAII(an_instance_of_a_new__type_compares_not_equal_to_an_instance_of_its_base_type_with_a_differing_value), - KAWAII(an_instance_of_the_base_type_of_a_new__type_compares_not_equal_to_an_instance_of_the_new__type_with_a_differing_value), - }, - "Equality Comparison Tests"}; -} \ No newline at end of file -- cgit v1.2.3