diff options
| author | Martin Hořeňovský | 2022-11-01 15:01:43 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-11-04 19:24:44 +0100 |
| commit | ec59cd8736b7ee8fcd69c76c71c2a0b72293b00c (patch) | |
| tree | 1cb8b66b56e6fe4fe6307e88e32c18d69c2a89a7 /tests/SelfTest | |
| parent | d7f8c36e4cb856ee632b4ca01f8d5847984aa276 (diff) | |
| download | catch3-ec59cd8736b7ee8fcd69c76c71c2a0b72293b00c.tar.gz | |
Support decomposing types that only compare with literal 0
This is primarily done to support new `std::*_ordering` types,
but the refactoring also supports any other type with this
property.
The compilation overhead is surprisingly low. Testing it with
clang on a Linux machine, compiling our SelfTest project takes
only 2-3% longer with these changes than it takes otherwise.
Closes #2555
Diffstat (limited to 'tests/SelfTest')
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/Traits.tests.cpp | 31 | ||||
| -rw-r--r-- | tests/SelfTest/UsageTests/Compilation.tests.cpp | 19 | ||||
| -rw-r--r-- | tests/SelfTest/helpers/type_with_lit_0_comparisons.hpp | 44 |
3 files changed, 64 insertions, 30 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp b/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp index 35d30c76..459e0d48 100644 --- a/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp @@ -8,36 +8,7 @@ #include <catch2/catch_test_macros.hpp> #include <catch2/internal/catch_compare_traits.hpp> - -// Should only be constructible from literal 0. -// Used by `TypeWithLit0Comparisons` for testing comparison -// ops that only work with literal zero, the way std::*orderings do -struct ZeroLiteralDetector { - constexpr ZeroLiteralDetector( ZeroLiteralDetector* ) noexcept {} - - template <typename T, - typename = std::enable_if_t<!std::is_same<T, int>::value>> - constexpr ZeroLiteralDetector( T ) = delete; -}; - -struct TypeWithLit0Comparisons { -#define DEFINE_COMP_OP( op ) \ - friend bool operator op( TypeWithLit0Comparisons, ZeroLiteralDetector ) { \ - return true; \ - } \ - friend bool operator op( ZeroLiteralDetector, TypeWithLit0Comparisons ) { \ - return false; \ - } - - DEFINE_COMP_OP( < ) - DEFINE_COMP_OP( <= ) - DEFINE_COMP_OP( > ) - DEFINE_COMP_OP( >= ) - DEFINE_COMP_OP( == ) - DEFINE_COMP_OP( != ) - -#undef DEFINE_COMP_OP -}; +#include <helpers/type_with_lit_0_comparisons.hpp> #define ADD_TRAIT_TEST_CASE( op ) \ diff --git a/tests/SelfTest/UsageTests/Compilation.tests.cpp b/tests/SelfTest/UsageTests/Compilation.tests.cpp index 5998ce62..a61a9674 100644 --- a/tests/SelfTest/UsageTests/Compilation.tests.cpp +++ b/tests/SelfTest/UsageTests/Compilation.tests.cpp @@ -6,6 +6,8 @@ // SPDX-License-Identifier: BSL-1.0 +#include <helpers/type_with_lit_0_comparisons.hpp> + #include <type_traits> // Setup for #1403 -- look for global overloads of operator << for classes @@ -310,3 +312,20 @@ TEST_CASE("ADL universal operators don't hijack expression deconstruction", "[co REQUIRE(0 & adl::always_true{}); REQUIRE(0 ^ adl::always_true{}); } + +TEST_CASE( "#2555 - types that can only be compared with 0 literal (not int/long) are supported", "[compilation][approvals]" ) { + REQUIRE( TypeWithLit0Comparisons{} < 0 ); + REQUIRE_FALSE( 0 < TypeWithLit0Comparisons{} ); + REQUIRE( TypeWithLit0Comparisons{} <= 0 ); + REQUIRE_FALSE( 0 > TypeWithLit0Comparisons{} ); + + REQUIRE( TypeWithLit0Comparisons{} > 0 ); + REQUIRE_FALSE( 0 > TypeWithLit0Comparisons{} ); + REQUIRE( TypeWithLit0Comparisons{} >= 0 ); + REQUIRE_FALSE( 0 >= TypeWithLit0Comparisons{} ); + + REQUIRE( TypeWithLit0Comparisons{} == 0 ); + REQUIRE_FALSE( 0 == TypeWithLit0Comparisons{} ); + REQUIRE( TypeWithLit0Comparisons{} != 0 ); + REQUIRE_FALSE( 0 != TypeWithLit0Comparisons{} ); +} diff --git a/tests/SelfTest/helpers/type_with_lit_0_comparisons.hpp b/tests/SelfTest/helpers/type_with_lit_0_comparisons.hpp new file mode 100644 index 00000000..202c3af4 --- /dev/null +++ b/tests/SelfTest/helpers/type_with_lit_0_comparisons.hpp @@ -0,0 +1,44 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#ifndef CATCH_TEST_HELPERS_TYPE_WITH_LIT_0_COMPARISONS_HPP_INCLUDED +#define CATCH_TEST_HELPERS_TYPE_WITH_LIT_0_COMPARISONS_HPP_INCLUDED + +#include <type_traits> + +// Should only be constructible from literal 0. +// Used by `TypeWithLit0Comparisons` for testing comparison +// ops that only work with literal zero, the way std::*orderings do +struct ZeroLiteralDetector { + constexpr ZeroLiteralDetector( ZeroLiteralDetector* ) noexcept {} + + template <typename T, + typename = std::enable_if_t<!std::is_same<T, int>::value>> + constexpr ZeroLiteralDetector( T ) = delete; +}; + +struct TypeWithLit0Comparisons { +#define DEFINE_COMP_OP( op ) \ + friend bool operator op( TypeWithLit0Comparisons, ZeroLiteralDetector ) { \ + return true; \ + } \ + friend bool operator op( ZeroLiteralDetector, TypeWithLit0Comparisons ) { \ + return false; \ + } + + DEFINE_COMP_OP( < ) + DEFINE_COMP_OP( <= ) + DEFINE_COMP_OP( > ) + DEFINE_COMP_OP( >= ) + DEFINE_COMP_OP( == ) + DEFINE_COMP_OP( != ) + +#undef DEFINE_COMP_OP +}; + +#endif // CATCH_TEST_HELPERS_TYPE_WITH_LIT_0_COMPARISONS_HPP_INCLUDED |