diff options
| author | Martin Hořeňovský | 2022-11-01 08:48:23 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2022-11-04 19:24:42 +0100 |
| commit | d7f8c36e4cb856ee632b4ca01f8d5847984aa276 (patch) | |
| tree | 002a27d20dcf90798070da1c4c254608aaf22961 /tests/SelfTest | |
| parent | b3dbd83da2f793a8630647e0053b6613ba5fb874 (diff) | |
| download | catch3-d7f8c36e4cb856ee632b4ca01f8d5847984aa276.tar.gz | |
Add traits for checking whether types are comparable
Diffstat (limited to 'tests/SelfTest')
| -rw-r--r-- | tests/SelfTest/IntrospectiveTests/Traits.tests.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp b/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp new file mode 100644 index 00000000..35d30c76 --- /dev/null +++ b/tests/SelfTest/IntrospectiveTests/Traits.tests.cpp @@ -0,0 +1,74 @@ + +// 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 + +#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 +}; + + +#define ADD_TRAIT_TEST_CASE( op ) \ + TEST_CASE( "is_" #op "_comparable", \ + "[traits][is_comparable][approvals]" ) { \ + using Catch::Detail::is_##op##_0_comparable; \ + using Catch::Detail::is_##op##_comparable; \ + \ + STATIC_REQUIRE( is_##op##_comparable<int, int>::value ); \ + STATIC_REQUIRE( \ + is_##op##_comparable<std::string, std::string>::value ); \ + STATIC_REQUIRE( !is_##op##_comparable<int, std::string>::value ); \ + STATIC_REQUIRE( \ + !is_##op##_comparable<TypeWithLit0Comparisons, int>::value ); \ + STATIC_REQUIRE( \ + !is_##op##_comparable<int, TypeWithLit0Comparisons>::value ); \ + \ + STATIC_REQUIRE( is_##op##_0_comparable<int>::value ); \ + STATIC_REQUIRE( \ + is_##op##_0_comparable<TypeWithLit0Comparisons>::value ); \ + STATIC_REQUIRE( !is_##op##_0_comparable<std::string>::value ); \ + \ + /* This test fails with MSVC in permissive mode, because of course it does */ \ + /* STATIC_REQUIRE( !is_##op##_0_comparable<int*>::value ); */ \ +} + +ADD_TRAIT_TEST_CASE(lt) +ADD_TRAIT_TEST_CASE(gt) +ADD_TRAIT_TEST_CASE(le) +ADD_TRAIT_TEST_CASE(ge) +ADD_TRAIT_TEST_CASE(eq) +ADD_TRAIT_TEST_CASE(ne) + +#undef ADD_TRAIT_TEST_CASE |