Support C arrays and ADL ranges in from_range generator

Closes #2737
This commit is contained in:
Martin Hořeňovský 2023-08-29 15:37:46 +02:00
parent 85eb4652b4
commit f24d39e42b
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
2 changed files with 34 additions and 4 deletions

View File

@ -96,10 +96,11 @@ GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(from, to));
}
template <typename Container,
typename ResultType = typename Container::value_type>
GeneratorWrapper<ResultType> from_range(Container const& cnt) {
return GeneratorWrapper<ResultType>(Catch::Detail::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
template <typename Container>
auto from_range(Container const& cnt) {
using std::begin;
using std::end;
return from_range( begin( cnt ), end( cnt ) );
}

View File

@ -10,6 +10,8 @@
# pragma GCC diagnostic ignored "-Wfloat-equal"
#endif
#include <helpers/range_test_helpers.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generator_exception.hpp>
@ -545,3 +547,30 @@ TEST_CASE("Filter generator throws exception for empty generator",
filter( []( int ) { return false; }, value( 3 ) ),
Catch::GeneratorException );
}
TEST_CASE("from_range(container) supports ADL begin/end and arrays", "[generators][from-range][approvals]") {
using namespace Catch::Generators;
SECTION("C array") {
int arr[3]{ 5, 6, 7 };
auto gen = from_range( arr );
REQUIRE( gen.get() == 5 );
REQUIRE( gen.next() );
REQUIRE( gen.get() == 6 );
REQUIRE( gen.next() );
REQUIRE( gen.get() == 7 );
REQUIRE_FALSE( gen.next() );
}
SECTION( "ADL range" ) {
unrelated::needs_ADL_begin<int> range{ 1, 2, 3 };
auto gen = from_range( range );
REQUIRE( gen.get() == 1 );
REQUIRE( gen.next() );
REQUIRE( gen.get() == 2 );
REQUIRE( gen.next() );
REQUIRE( gen.get() == 3 );
REQUIRE_FALSE( gen.next() );
}
}