diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 08:48:12 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 08:48:12 +0200 |
| commit | 97ccfa11b73e5aabb90be44dc8e37b25d3236961 (patch) | |
| tree | ab3b97ad2774964901160ee1c2e6fa23063e5144 | |
| parent | 43c7db49ff824577cff11cdd03d1f5bafcbd4b38 (diff) | |
| download | newtype-97ccfa11b73e5aabb90be44dc8e37b25d3236961.tar.xz newtype-97ccfa11b73e5aabb90be44dc8e37b25d3236961.zip | |
tests: port equality comparison tests
| -rw-r--r-- | source/tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | source/tests/src/equality_comparison.cpp | 104 | ||||
| -rw-r--r-- | source/tests/src/equality_comparison_suite.cpp | 188 |
3 files changed, 105 insertions, 189 deletions
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 <catch2/catch_test_macros.hpp> + +#include <string> +#include <type_traits> +#include <utility> + +SCENARIO("Equality Comparison", "[compare]") +{ + GIVEN("A new_type over an equality comparable type") + { + using type_alias = nt::new_type<int, struct tag>; + + 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<bool, decltype(std::declval<type_alias>() == std::declval<type_alias>())>); + } + + THEN("inequality comparison returns bool") + { + REQUIRE(std::is_same_v<bool, decltype(std::declval<type_alias>() != std::declval<type_alias>())>); + } + } + + GIVEN("A new_type deriving nt::EqBase") + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::EqBase)>; + + 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<int, struct tag>; + + static_assert(noexcept(std::declval<type_alias::base_type>() == std::declval<type_alias::base_type>())); + static_assert(noexcept(std::declval<type_alias::base_type>() != std::declval<type_alias::base_type>())); + + THEN("it is nothrow-equality-comparable") + { + REQUIRE(noexcept(std::declval<type_alias>() == std::declval<type_alias>())); + } + + THEN("it is nothrow-inequality-comparable") + { + REQUIRE(noexcept(std::declval<type_alias>() != std::declval<type_alias>())); + } + } + + 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<not_nothrow_comparable, struct tag>; + + static_assert(!noexcept(std::declval<type_alias::base_type>() == std::declval<type_alias::base_type>())); + static_assert(!noexcept(std::declval<type_alias::base_type>() != std::declval<type_alias::base_type>())); + + THEN("it is not nothrow-equality-comparable") + { + REQUIRE_FALSE(noexcept(std::declval<type_alias>() == std::declval<type_alias>())); + } + + THEN("it is not nothrow-inequality-comparable") + { + REQUIRE_FALSE(noexcept(std::declval<type_alias>() != std::declval<type_alias>())); + } + } +} 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 <cute/cute.h> - -#include <string> -#include <type_traits> -#include <utility> - -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<int, struct tag>; - - 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<int, struct tag>; - - 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<int, struct tag>; - - 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<int, struct tag>; - - 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<int, struct tag>; - static_assert(noexcept(std::declval<int &>() == std::declval<int &>()), "Sanity Check"); - ASSERT(noexcept(std::declval<type_alias &>() == std::declval<type_alias &>())); - } - - 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<strange_type, struct tag>; - static_assert(!noexcept(std::declval<strange_type &>() == std::declval<strange_type &>()), "Sanity Check"); - ASSERT(!noexcept(std::declval<type_alias &>() == std::declval<type_alias &>())); - } - - auto inequality_comparison_on_a_new__type_is_noexcept_if_the_base_type_is_noexcept_comparable() -> void - { - using type_alias = nt::new_type<int, struct tag>; - static_assert(noexcept(std::declval<int &>() != std::declval<int &>()), "Sanity Check"); - ASSERT(noexcept(std::declval<type_alias &>() != std::declval<type_alias &>())); - } - - 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<strange_type, struct tag>; - static_assert(!noexcept(std::declval<strange_type &>() != std::declval<strange_type &>()), "Sanity Check"); - ASSERT(!noexcept(std::declval<type_alias &>() != std::declval<type_alias &>())); - } - -} // 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<int, struct tag>; - ASSERT((std::is_same_v<bool, decltype(std::declval<type_alias &>() == std::declval<type_alias &>())>)); - } - - auto inequality_comparsion_of_two_new__type_instances_returns_bool() -> void - { - using type_alias = nt::new_type<int, struct tag>; - ASSERT((std::is_same_v<bool, decltype(std::declval<type_alias &>() != std::declval<type_alias &>())>)); - } - -} // 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<int, struct tag, deriving(nt::EqBase)>; - 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<int, struct tag, deriving(nt::EqBase)>; - 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<int, struct tag, deriving(nt::EqBase)>; - 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<int, struct tag, deriving(nt::EqBase)>; - auto lhs = 43; - auto rhs = type_alias{42}; - ASSERT(lhs != rhs); - } - -} // namespace base_type_equality_comparison_tests - -auto equality_comparison_suite() -> std::pair<cute::suite, std::string> -{ - 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 |
