diff options
| author | Martin Hořeňovský | 2019-03-31 14:11:10 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2019-03-31 14:11:10 +0200 |
| commit | 3816e99d0c74882eeaaf30ebb48a07a9418f295c (patch) | |
| tree | f745a40820742d6e324cc2f219c1ff654c1ad25a /examples/310-Gen-VariablesInGenerators.cpp | |
| parent | b77cec05c0b3a089e69a709e07c964ff842e5611 (diff) | |
| download | catch3-3816e99d0c74882eeaaf30ebb48a07a9418f295c.tar.gz | |
Add GENERATE_COPY and GENERATE_VAR capturing generator macros
Diffstat (limited to 'examples/310-Gen-VariablesInGenerators.cpp')
| -rw-r--r-- | examples/310-Gen-VariablesInGenerators.cpp | 51 |
1 files changed, 6 insertions, 45 deletions
diff --git a/examples/310-Gen-VariablesInGenerators.cpp b/examples/310-Gen-VariablesInGenerators.cpp index 96840bbb..422815d2 100644 --- a/examples/310-Gen-VariablesInGenerators.cpp +++ b/examples/310-Gen-VariablesInGenerators.cpp @@ -8,41 +8,6 @@ #include <catch2/catch.hpp> -#include <random> - -// Lets start by implementing a parametrizable double generator -class RandomDoubleGenerator : public Catch::Generators::IGenerator<double> { - std::minstd_rand m_rand; - std::uniform_real_distribution<> m_dist; - double current_number; -public: - - RandomDoubleGenerator(double low, double high): - m_rand(std::random_device{}()), - m_dist(low, high) - { - static_cast<void>(next()); - } - - double const& get() const override; - bool next() override { - current_number = m_dist(m_rand); - return true; - } -}; - -// Avoids -Wweak-vtables -double const& RandomDoubleGenerator::get() const { - return current_number; -} - - -// Also provide a nice shortcut for creating the generator -Catch::Generators::GeneratorWrapper<double> random(double low, double high) { - return Catch::Generators::GeneratorWrapper<double>(std::unique_ptr<Catch::Generators::IGenerator<double>>(new RandomDoubleGenerator(low, high))); -} - - TEST_CASE("Generate random doubles across different ranges", "[generator][example][advanced]") { // Workaround for old libstdc++ @@ -55,16 +20,12 @@ TEST_CASE("Generate random doubles across different ranges", })); // This will not compile (intentionally), because it accesses a variable - // auto number = GENERATE(take(50, random(r.first, r.second))); - - // We have to manually register the generators instead - // Notice that we are using value capture in the lambda, to avoid lifetime issues - auto number = Catch::Generators::generate( CATCH_INTERNAL_LINEINFO, - [=]{ - using namespace Catch::Generators; - return makeGenerators(take(50, random(std::get<0>(r), std::get<1>(r)))); - } - ); + // auto number = GENERATE(take(50, random(std::get<0>(r), std::get<1>(r)))); + + // GENERATE_COPY copies all variables mentioned inside the expression + // thus this will work. + auto number = GENERATE_COPY(take(50, random(std::get<0>(r), std::get<1>(r)))); + REQUIRE(std::abs(number) > 0); } |