From bdee4da46d7a678695ad19dd97fa1dd5e83184ee Mon Sep 17 00:00:00 2001 From: Raphael Schaller Date: Sun, 14 Nov 2021 14:36:29 +0100 Subject: [PATCH] add C++17 tests --- .../UsageTests/MatchersRanges.tests.cpp | 90 +++++++++++-------- 1 file changed, 54 insertions(+), 36 deletions(-) diff --git a/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp b/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp index a9b95af98d..3cd6ddcd1e 100644 --- a/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp +++ b/tests/SelfTest/UsageTests/MatchersRanges.tests.cpp @@ -53,22 +53,23 @@ namespace unrelated { # pragma clang diagnostic ignored "-Wunused-function" #endif +template class has_different_begin_end_types { - std::array elements{ {1, 2, 3, 4, 5} }; + std::array m_elements; // Different type for the "end" iterator struct iterator_end {}; // Just a fake forward iterator, that only compares to a different // type (so we can test two-type ranges). struct iterator { - int const* start; - int const* end; + T const* start; + T const* end; using iterator_category = std::forward_iterator_tag; using difference_type = std::ptrdiff_t; - using value_type = int; - using reference = int const&; - using pointer = int const*; + using value_type = T; + using reference = T const&; + using pointer = T const*; friend bool operator==( iterator iter, iterator_end ) { @@ -96,8 +97,12 @@ class has_different_begin_end_types { public: + template + explicit has_different_begin_end_types( Args&&... args ): + m_elements{ make_array( CATCH_FORWARD( args )... ) } {} + iterator begin() const { - return { elements.data(), elements.data() + elements.size() }; + return { m_elements.data(), m_elements.data() + m_elements.size() }; } iterator_end end() const { @@ -549,35 +554,6 @@ TEST_CASE("Usage of NoneMatch range matcher", "[matchers][templated][quantifiers } } - -// This is a C++17 extension, and GCC refuses to compile such code -// unless it is set to C++17 or later -#if defined(CATCH_CPP17_OR_GREATER) - -TEST_CASE( "The quantifier range matchers support types with different types returned from begin and end", - "[matchers][templated][quantifiers][approvals]" ) { - using Catch::Matchers::AllMatch; - using Catch::Matchers::AnyMatch; - using Catch::Matchers::NoneMatch; - - using Catch::Matchers::Predicate; - - has_different_begin_end_types diff_types; - REQUIRE_THAT( diff_types, !AllMatch( Predicate( []( int elem ) { - return elem < 3; - } ) ) ); - - REQUIRE_THAT( diff_types, AnyMatch( Predicate( []( int elem ) { - return elem < 2; - } ) ) ); - - REQUIRE_THAT( diff_types, !NoneMatch( Predicate( []( int elem ) { - return elem < 3; - } ) ) ); -} - -#endif - namespace { struct ConvertibleToBool { @@ -814,3 +790,45 @@ TEST_CASE( "Usage of AnyTrue range matcher", "[matchers][templated][quantifiers] } } } + +// This is a C++17 extension, and GCC refuses to compile such code +// unless it is set to C++17 or later +#if defined(CATCH_CPP17_OR_GREATER) + +TEST_CASE( "The quantifier range matchers support types with different types returned from begin and end", + "[matchers][templated][quantifiers][approvals]" ) { + using Catch::Matchers::AllMatch; + using Catch::Matchers::AllTrue; + using Catch::Matchers::AnyMatch; + using Catch::Matchers::AnyTrue; + using Catch::Matchers::NoneMatch; + using Catch::Matchers::NoneTrue; + + using Catch::Matchers::Predicate; + + SECTION( "AllAnyNoneMatch" ) { + has_different_begin_end_types diff_types{ 1, 2, 3, 4, 5 }; + REQUIRE_THAT( diff_types, !AllMatch( Predicate( []( int elem ) { + return elem < 3; + } ) ) ); + + REQUIRE_THAT( diff_types, AnyMatch( Predicate( []( int elem ) { + return elem < 2; + } ) ) ); + + REQUIRE_THAT( diff_types, !NoneMatch( Predicate( []( int elem ) { + return elem < 3; + } ) ) ); + } + SECTION( "AllAnyNoneTrue" ) { + has_different_begin_end_types diff_types{ + false, false, true, false, false }; + REQUIRE_THAT( diff_types, !AllTrue() ); + + REQUIRE_THAT( diff_types, AnyTrue() ); + + REQUIRE_THAT( diff_types, !NoneTrue() ); + } +} + +#endif