Skip to content

Commit

Permalink
Support C arrays and ADL ranges in from_range generator
Browse files Browse the repository at this point in the history
Closes #2737
  • Loading branch information
horenmar committed Aug 29, 2023
1 parent 85eb465 commit f24d39e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/catch2/generators/catch_generators_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}


Expand Down
29 changes: 29 additions & 0 deletions tests/SelfTest/IntrospectiveTests/GeneratorsImpl.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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() );
}

}

0 comments on commit f24d39e

Please sign in to comment.