diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2019-12-28 17:22:06 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2019-12-28 17:22:06 +0100 |
| commit | 65f0b622e390edb39a6cfd8d614374314472a5a9 (patch) | |
| tree | 54df59e27b4f1441d5146c13601765f1a4f54bb9 | |
| parent | 2006e67303f31da2e5a359993e08d25c02751c5a (diff) | |
| download | newtype-65f0b622e390edb39a6cfd8d614374314472a5a9.tar.xz newtype-65f0b622e390edb39a6cfd8d614374314472a5a9.zip | |
new_type: add equality comparison tests
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 10 | ||||
| -rw-r--r-- | test/include/equality_comparison_suite.hpp | 11 | ||||
| -rw-r--r-- | test/src/driver.cpp | 2 | ||||
| -rw-r--r-- | test/src/equality_comparison_suite.cpp | 145 |
5 files changed, 166 insertions, 3 deletions
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<typename BaseType, typename TagType, auto DerivationClause> auto constexpr operator==(new_type<BaseType, TagType, DerivationClause> const & lhs, - new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(noexcept(lhs.decay() == rhs.decay())) -> bool + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(noexcept(std::declval<BaseType const &>() == + std::declval<BaseType const &>())) + -> bool { return lhs.decay() == rhs.decay(); } @@ -144,9 +146,11 @@ namespace nt */ template<typename BaseType, typename TagType, auto DerivationClause> auto constexpr operator!=(new_type<BaseType, TagType, DerivationClause> const & lhs, - new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(noexcept(!(lhs == rhs))) -> bool + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(noexcept(std::declval<BaseType const &>() != + std::declval<BaseType const &>())) + -> 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 <cute/cute_suite.h> + +#include <string> +#include <utility> + +auto equality_comparison_suite() -> std::pair<cute::suite, std::string>; + +#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 <cute/cute.h> @@ -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 <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 eq_test_tag_1>; + + 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 eq_test_tag_1>; + + 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 eq_test_tag_1>; + + 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 eq_test_tag_1>; + + 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 eq_test_tag_2>; + 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 eq_test_tag_3>; + 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 eq_test_tag_4>; + 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 eq_test_tag_5>; + 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 eq_test_tag_6>; + 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 eq_test_tag_7>; + ASSERT((std::is_same_v<bool, decltype(std::declval<type_alias &>() != std::declval<type_alias &>())>)); + } + +} // namespace equality_comparison_return_type_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), + }, + "Equality Comparison Tests"}; +}
\ No newline at end of file |
