From 9cf5897a11154bab4d561c80ab66ef33ecc91838 Mon Sep 17 00:00:00 2001 From: David Sommerich Date: Sat, 29 Jun 2019 23:48:30 +1000 Subject: [PATCH] Fix ChunkGenerator with chunk-size 0 Fixes #1671 --- include/internal/catch_generators_generic.hpp | 12 +++-- .../Baselines/compact.sw.approved.txt | 3 ++ .../Baselines/console.std.approved.txt | 2 +- .../Baselines/console.sw.approved.txt | 41 ++++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 3 +- .../SelfTest/Baselines/xml.sw.approved.txt | 46 ++++++++++++++++++- .../SelfTest/UsageTests/Generators.tests.cpp | 4 ++ 7 files changed, 101 insertions(+), 10 deletions(-) diff --git a/include/internal/catch_generators_generic.hpp b/include/internal/catch_generators_generic.hpp index 2903ba69..fa3749c3 100644 --- a/include/internal/catch_generators_generic.hpp +++ b/include/internal/catch_generators_generic.hpp @@ -205,12 +205,14 @@ namespace Generators { m_chunk_size(size), m_generator(std::move(generator)) { m_chunk.reserve(m_chunk_size); - m_chunk.push_back(m_generator.get()); - for (size_t i = 1; i < m_chunk_size; ++i) { - if (!m_generator.next()) { - Catch::throw_exception(GeneratorException("Not enough values to initialize the first chunk")); - } + if (m_chunk_size != 0) { m_chunk.push_back(m_generator.get()); + for (size_t i = 1; i < m_chunk_size; ++i) { + if (!m_generator.next()) { + Catch::throw_exception(GeneratorException("Not enough values to initialize the first chunk")); + } + m_chunk.push_back(m_generator.get()); + } } } std::vector const& get() const override { diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt index b7163c1f..ebf3560a 100644 --- a/projects/SelfTest/Baselines/compact.sw.approved.txt +++ b/projects/SelfTest/Baselines/compact.sw.approved.txt @@ -467,6 +467,9 @@ Generators.tests.cpp:: passed: chunk2.front() < 3 for: 1 < 3 Generators.tests.cpp:: passed: chunk2.size() == 2 for: 2 == 2 Generators.tests.cpp:: passed: chunk2.front() == chunk2.back() for: 2 == 2 Generators.tests.cpp:: passed: chunk2.front() < 3 for: 2 < 3 +Generators.tests.cpp:: passed: chunk2.size() == 0 for: 0 == 0 +Generators.tests.cpp:: passed: chunk2.size() == 0 for: 0 == 0 +Generators.tests.cpp:: passed: chunk2.size() == 0 for: 0 == 0 Generators.tests.cpp:: passed: chunk(2, value(1)), Catch::GeneratorException Generators.tests.cpp:: passed: j < i for: -3 < 1 Generators.tests.cpp:: passed: j < i for: -2 < 1 diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 8cfd4beb..0f859df3 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -1381,5 +1381,5 @@ due to unexpected exception with message: =============================================================================== test cases: 295 | 221 passed | 70 failed | 4 failed as expected -assertions: 1547 | 1395 passed | 131 failed | 21 failed as expected +assertions: 1550 | 1398 passed | 131 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 501c76f8..a14034f7 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -3547,6 +3547,45 @@ Generators.tests.cpp:: PASSED: with expansion: 2 < 3 +------------------------------------------------------------------------------- +Generators -- adapters + Chunking a generator into sized pieces + Chunk size of zero +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( chunk2.size() == 0 ) +with expansion: + 0 == 0 + +------------------------------------------------------------------------------- +Generators -- adapters + Chunking a generator into sized pieces + Chunk size of zero +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( chunk2.size() == 0 ) +with expansion: + 0 == 0 + +------------------------------------------------------------------------------- +Generators -- adapters + Chunking a generator into sized pieces + Chunk size of zero +------------------------------------------------------------------------------- +Generators.tests.cpp: +............................................................................... + +Generators.tests.cpp:: PASSED: + REQUIRE( chunk2.size() == 0 ) +with expansion: + 0 == 0 + ------------------------------------------------------------------------------- Generators -- adapters Chunking a generator into sized pieces @@ -12335,5 +12374,5 @@ Misc.tests.cpp:: PASSED: =============================================================================== test cases: 295 | 205 passed | 86 failed | 4 failed as expected -assertions: 1564 | 1395 passed | 148 failed | 21 failed as expected +assertions: 1567 | 1398 passed | 148 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 7892601f..abfb0a99 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,7 +1,7 @@ - + @@ -416,6 +416,7 @@ Message.tests.cpp: + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 04f42837..57de31fb 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -4260,6 +4260,48 @@ Nor would this +
+
+ + + chunk2.size() == 0 + + + 0 == 0 + + + +
+ +
+
+
+ + + chunk2.size() == 0 + + + 0 == 0 + + + +
+ +
+
+
+ + + chunk2.size() == 0 + + + 0 == 0 + + + +
+ +
@@ -14692,7 +14734,7 @@ loose text artifact
- + - + diff --git a/projects/SelfTest/UsageTests/Generators.tests.cpp b/projects/SelfTest/UsageTests/Generators.tests.cpp index f5e3f6a5..903f6a03 100644 --- a/projects/SelfTest/UsageTests/Generators.tests.cpp +++ b/projects/SelfTest/UsageTests/Generators.tests.cpp @@ -167,6 +167,10 @@ TEST_CASE("Generators -- adapters", "[generators][generic]") { REQUIRE(chunk2.front() == chunk2.back()); REQUIRE(chunk2.front() < 3); } + SECTION("Chunk size of zero") { + auto chunk2 = GENERATE(take(3, chunk(0, value(1)))); + REQUIRE(chunk2.size() == 0); + } SECTION("Throws on too small generators") { using namespace Catch::Generators; REQUIRE_THROWS_AS(chunk(2, value(1)), Catch::GeneratorException);