diff options
Diffstat (limited to 'projects/SelfTest/UsageTests/Generators.tests.cpp')
-rw-r--r-- | projects/SelfTest/UsageTests/Generators.tests.cpp | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/projects/SelfTest/UsageTests/Generators.tests.cpp b/projects/SelfTest/UsageTests/Generators.tests.cpp index f5e3f6a5..0e39bd5c 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); @@ -176,16 +180,18 @@ TEST_CASE("Generators -- adapters", "[generators][generic]") { // Note that because of the non-reproducibility of distributions, // anything involving the random generators cannot be part of approvals -TEST_CASE("Random generator", "[generators][.][approvals]") { +TEST_CASE("Random generator", "[generators][approvals]") { SECTION("Infer int from integral arguments") { auto val = GENERATE(take(4, random(0, 1))); STATIC_REQUIRE(std::is_same<decltype(val), int>::value); - static_cast<void>(val); // Silence VS 2015 unused variable warning + REQUIRE(0 <= val); + REQUIRE(val <= 1); } SECTION("Infer double from double arguments") { auto val = GENERATE(take(4, random(0., 1.))); STATIC_REQUIRE(std::is_same<decltype(val), double>::value); - static_cast<void>(val); // Silence VS 2015 unused variable warning + REQUIRE(0. <= val); + REQUIRE(val < 1); } } @@ -206,3 +212,45 @@ TEST_CASE("Nested generators and captured variables", "[generators]") { auto values = GENERATE_COPY(range(from, to)); REQUIRE(values > -6); } + +namespace { + size_t call_count = 0; + size_t test_count = 0; + std::vector<int> make_data() { + return { 1, 3, 5, 7, 9, 11 }; + } + std::vector<int> make_data_counted() { + ++call_count; + return make_data(); + } +} + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +TEST_CASE("Copy and then generate a range", "[generators]") { + SECTION("from var and iterators") { + static auto data = make_data(); + + // It is important to notice that a generator is only initialized + // **once** per run. What this means is that modifying data will not + // modify the underlying generator. + auto elem = GENERATE_REF(from_range(data.begin(), data.end())); + 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__) +#pragma clang diagnostic pop +#endif |