1 // 310-Gen-VariablesInGenerator.cpp
2 // Shows how to use variables when creating generators.
3
4 // Note that using variables inside generators is dangerous and should
5 // be done only if you know what you are doing, because the generators
6 // _WILL_ outlive the variables -- thus they should be either captured
7 // by value directly, or copied by the generators during construction.
8
9 #include <catch2/catch.hpp>
10
11 #include <random>
12
13 // Lets start by implementing a parametrizable double generator
14 class RandomDoubleGenerator : public Catch::Generators::IGenerator<double> {
15 std::minstd_rand m_rand;
16 std::uniform_real_distribution<> m_dist;
17 double current_number;
18 public:
19
RandomDoubleGenerator(double low,double high)20 RandomDoubleGenerator(double low, double high):
21 m_rand(std::random_device{}()),
22 m_dist(low, high)
23 {
24 static_cast<void>(next());
25 }
26
27 double const& get() const override;
next()28 bool next() override {
29 current_number = m_dist(m_rand);
30 return true;
31 }
32 };
33
34 // Avoids -Wweak-vtables
get() const35 double const& RandomDoubleGenerator::get() const {
36 return current_number;
37 }
38
39
40 // Also provide a nice shortcut for creating the generator
random(double low,double high)41 Catch::Generators::GeneratorWrapper<double> random(double low, double high) {
42 return Catch::Generators::GeneratorWrapper<double>(std::unique_ptr<Catch::Generators::IGenerator<double>>(new RandomDoubleGenerator(low, high)));
43 }
44
45
46 TEST_CASE("Generate random doubles across different ranges",
47 "[generator][example][advanced]") {
48 // Workaround for old libstdc++
49 using record = std::tuple<double, double>;
50 // Set up 3 ranges to generate numbers from
51 auto r = GENERATE(table<double, double>({
52 record{3, 4},
53 record{-4, -3},
54 record{10, 1000}
55 }));
56
57 // This will not compile (intentionally), because it accesses a variable
58 // auto number = GENERATE(take(50, random(r.first, r.second)));
59
60 // We have to manually register the generators instead
61 // Notice that we are using value capture in the lambda, to avoid lifetime issues
62 auto number = Catch::Generators::generate( CATCH_INTERNAL_LINEINFO,
__anon4badb12f0102null63 [=]{
64 using namespace Catch::Generators;
65 return makeGenerators(take(50, random(std::get<0>(r), std::get<1>(r))));
66 }
67 );
68 REQUIRE(std::abs(number) > 0);
69 }
70
71 // Compiling and running this file will result in 150 successful assertions
72
73