Add ChunkGenerator

This generator collects values from the underlying generator until it
has a specified amount of them, and then returns them in one "chunk".
In case the underlying generator does not have enough elements for
a specific chunk, the left-over elements are discarded.

Closes #1538
This commit is contained in:
Martin Hořeňovský
2019-02-23 20:22:46 +01:00
parent 288387fa10
commit 693647c43f
11 changed files with 526 additions and 178 deletions

View File

@@ -114,7 +114,7 @@ SCENARIO("Eating cucumbers", "[generators][approvals]") {
#endif
// There are also some generic generator manipulators
TEST_CASE("Generators -- adapters", "[generators]") {
TEST_CASE("Generators -- adapters", "[generators][generic]") {
// TODO: This won't work yet, introduce GENERATE_VAR?
//auto numbers = Catch::Generators::values({ 1, 2, 3, 4, 5, 6 });
SECTION("Filtering by predicate") {
@@ -144,6 +144,23 @@ TEST_CASE("Generators -- adapters", "[generators]") {
auto j = GENERATE(repeat(2, values({ 1, 2, 3 })));
REQUIRE(j > 0);
}
SECTION("Chunking a generator into sized pieces") {
SECTION("Number of elements in source is divisible by chunk size") {
auto chunk2 = GENERATE(chunk(2, values({ 1, 1, 2, 2, 3, 3 })));
REQUIRE(chunk2.size() == 2);
REQUIRE(chunk2.front() == chunk2.back());
}
SECTION("Number of elements in source is not divisible by chunk size") {
auto chunk2 = GENERATE(chunk(2, values({ 1, 1, 2, 2, 3 })));
REQUIRE(chunk2.size() == 2);
REQUIRE(chunk2.front() == chunk2.back());
REQUIRE(chunk2.front() < 3);
}
SECTION("Throws on too small generators") {
using namespace Catch::Generators;
REQUIRE_THROWS_AS(chunk(2, value(1)), Catch::GeneratorException);
}
}
}
// Note that because of the non-reproducibility of distributions,