Add a from_range(Container) overload to the generator helpers

This commit is contained in:
Martin Hořeňovský 2019-10-09 14:51:24 +02:00
parent 1c5749669e
commit c8db4e77c4
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 293 additions and 60 deletions

View File

@ -72,6 +72,7 @@ type, making their usage much nicer. These are
* `range(start, end)` for `RangeGenerator<T>` with a step size of `1` * `range(start, end)` for `RangeGenerator<T>` with a step size of `1`
* `range(start, end, step)` for `RangeGenerator<T>` with a custom step size * `range(start, end, step)` for `RangeGenerator<T>` with a custom step size
* `from_range(InputIterator from, InputIterator to)` for `IteratorGenerator<T>` * `from_range(InputIterator from, InputIterator to)` for `IteratorGenerator<T>`
* `from_range(Container const&)` for `IteratorGenerator<T>`
> `chunk()`, `random()` and both `range()` functions were introduced in Catch 2.7.0. > `chunk()`, `random()` and both `range()` functions were introduced in Catch 2.7.0.

View File

@ -161,6 +161,13 @@ GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(from, to)); return GeneratorWrapper<ResultType>(pf::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>(pf::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
}
} // namespace Generators } // namespace Generators
} // namespace Catch } // namespace Catch

View File

@ -320,6 +320,14 @@ Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1 Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: call_count == 1 for: 1 == 1
Generators.tests.cpp:<line number>: passed: make_data().size() == test_count for: 6 == 6
Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom() Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom()
Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom(), std::exception Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom exception - not std'; expression was: throwCustom(), std::exception
Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom std exception' Exception.tests.cpp:<line number>: failed: unexpected exception with message: 'custom std exception'

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
=============================================================================== ===============================================================================
test cases: 303 | 229 passed | 70 failed | 4 failed as expected test cases: 303 | 229 passed | 70 failed | 4 failed as expected
assertions: 1589 | 1437 passed | 131 failed | 21 failed as expected assertions: 1597 | 1445 passed | 131 failed | 21 failed as expected

View File

@ -2409,6 +2409,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2420,6 +2421,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2431,6 +2433,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2442,6 +2445,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2453,6 +2457,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2464,6 +2469,7 @@ with expansion:
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copy and then generate a range Copy and then generate a range
from var and iterators
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Generators.tests.cpp:<line number> Generators.tests.cpp:<line number>
............................................................................... ...............................................................................
@ -2473,6 +2479,95 @@ Generators.tests.cpp:<line number>: PASSED:
with expansion: with expansion:
1 == 1 1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
From a temporary container
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( elem % 2 == 1 )
with expansion:
1 == 1
-------------------------------------------------------------------------------
Copy and then generate a range
Final validation
-------------------------------------------------------------------------------
Generators.tests.cpp:<line number>
...............................................................................
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( call_count == 1 )
with expansion:
1 == 1
Generators.tests.cpp:<line number>: PASSED:
REQUIRE( make_data().size() == test_count )
with expansion:
6 == 6
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Custom exceptions can be translated when testing for nothrow Custom exceptions can be translated when testing for nothrow
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -12711,5 +12806,5 @@ Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 303 | 213 passed | 86 failed | 4 failed as expected test cases: 303 | 213 passed | 86 failed | 4 failed as expected
assertions: 1606 | 1437 passed | 148 failed | 21 failed as expected assertions: 1614 | 1445 passed | 148 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="132" tests="1607" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1615" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -255,7 +255,9 @@ Matchers.tests.cpp:<line number>
Matchers.tests.cpp:<line number> Matchers.tests.cpp:<line number>
</failure> </failure>
</testcase> </testcase>
<testcase classname="<exe-name>.global" name="Copy and then generate a range" time="{duration}"/> <testcase classname="<exe-name>.global" name="Copy and then generate a range/from var and iterators" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Copy and then generate a range/From a temporary container" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Copy and then generate a range/Final validation" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Custom exceptions can be translated when testing for nothrow" time="{duration}"> <testcase classname="<exe-name>.global" name="Custom exceptions can be translated when testing for nothrow" time="{duration}">
<error message="throwCustom()" type="REQUIRE_NOTHROW"> <error message="throwCustom()" type="REQUIRE_NOTHROW">
custom exception - not std custom exception - not std

View File

@ -2852,54 +2852,157 @@ Nor would this
<OverallResult success="false"/> <OverallResult success="false"/>
</TestCase> </TestCase>
<TestCase name="Copy and then generate a range" tags="[generators]" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > <TestCase name="Copy and then generate a range" tags="[generators]" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > <Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original> <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
elem % 2 == 1 <Original>
</Original> elem % 2 == 1
<Expanded> </Original>
1 == 1 <Expanded>
</Expanded> 1 == 1
</Expression> </Expanded>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > </Expression>
<Original> <OverallResults successes="1" failures="0" expectedFailures="0"/>
elem % 2 == 1 </Section>
</Original> <Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expanded> <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
1 == 1 <Original>
</Expanded> elem % 2 == 1
</Expression> </Original>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > <Expanded>
<Original> 1 == 1
elem % 2 == 1 </Expanded>
</Original> </Expression>
<Expanded> <OverallResults successes="1" failures="0" expectedFailures="0"/>
1 == 1 </Section>
</Expanded> <Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
</Expression> <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > <Original>
<Original> elem % 2 == 1
elem % 2 == 1 </Original>
</Original> <Expanded>
<Expanded> 1 == 1
1 == 1 </Expanded>
</Expanded> </Expression>
</Expression> <OverallResults successes="1" failures="0" expectedFailures="0"/>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > </Section>
<Original> <Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
elem % 2 == 1 <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
</Original> <Original>
<Expanded> elem % 2 == 1
1 == 1 </Original>
</Expanded> <Expanded>
</Expression> 1 == 1
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" > </Expanded>
<Original> </Expression>
elem % 2 == 1 <OverallResults successes="1" failures="0" expectedFailures="0"/>
</Original> </Section>
<Expanded> <Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
1 == 1 <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
</Expanded> <Original>
</Expression> elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="from var and iterators" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="From a temporary container" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
elem % 2 == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section>
<Section name="Final validation" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
call_count == 1
</Original>
<Expanded>
1 == 1
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Generators.tests.cpp" >
<Original>
make_data().size() == test_count
</Original>
<Expanded>
6 == 6
</Expanded>
</Expression>
<OverallResults successes="2" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Custom exceptions can be translated when testing for nothrow" tags="[!throws][.][failing]" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" > <TestCase name="Custom exceptions can be translated when testing for nothrow" tags="[!throws][.][failing]" filename="projects/<exe-name>/UsageTests/Exception.tests.cpp" >
@ -15117,7 +15220,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1437" failures="149" expectedFailures="21"/> <OverallResults successes="1445" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1437" failures="148" expectedFailures="21"/> <OverallResults successes="1445" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -214,9 +214,15 @@ TEST_CASE("Nested generators and captured variables", "[generators]") {
} }
namespace { namespace {
size_t call_count = 0;
size_t test_count = 0;
std::vector<int> make_data() { std::vector<int> make_data() {
return { 1, 3, 5, 7, 9, 11 }; return { 1, 3, 5, 7, 9, 11 };
} }
std::vector<int> make_data_counted() {
++call_count;
return make_data();
}
} }
#if defined(__clang__) #if defined(__clang__)
@ -225,13 +231,24 @@ namespace {
#endif #endif
TEST_CASE("Copy and then generate a range", "[generators]") { TEST_CASE("Copy and then generate a range", "[generators]") {
static auto data = make_data(); SECTION("from var and iterators") {
static auto data = make_data();
// It is important to notice that a generator is only initialized // It is important to notice that a generator is only initialized
// **once** per run. What this means is that modifying data will not // **once** per run. What this means is that modifying data will not
// modify the underlying generator. // modify the underlying generator.
auto elem = GENERATE_REF(from_range(data.begin(), data.end())); auto elem = GENERATE_REF(from_range(data.begin(), data.end()));
REQUIRE(elem % 2 == 1); REQUIRE(elem % 2 == 1);
}
SECTION("From a temporary container") {
auto elem = GENERATE(from_range(make_data_counted()));
++test_count;
REQUIRE(elem % 2 == 1);
}
SECTION("Final validation") {
REQUIRE(call_count == 1);
REQUIRE(make_data().size() == test_count);
}
} }
#if defined(__clang__) #if defined(__clang__)