• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 311-Gen-CustomCapture.cpp
2 // Shows how to provide custom capture list to the generator expression
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. Also, even if you know what you are
7 // doing, you should probably use GENERATE_COPY or GENERATE_REF macros
8 // instead. However, if your use case requires having a
9 // per-variable custom capture list, this example shows how to achieve
10 // that.
11 
12 #include <catch2/catch.hpp>
13 
14 TEST_CASE("Generate random doubles across different ranges",
15           "[generator][example][advanced]") {
16     // Workaround for old libstdc++
17     using record = std::tuple<double, double>;
18     // Set up 3 ranges to generate numbers from
19     auto r1 = GENERATE(table<double, double>({
20         record{3, 4},
21         record{-4, -3},
22         record{10, 1000}
23     }));
24 
25     auto r2(r1);
26 
27     // This will take r1 by reference and r2 by value.
28     // Note that there are no advantages for doing so in this example,
29     // it is done only for expository purposes.
30     auto number = Catch::Generators::generate( CATCH_INTERNAL_LINEINFO,
__anon6bcbf3f30102null31         [&r1, r2]{
32             using namespace Catch::Generators;
33             return makeGenerators(take(50, random(std::get<0>(r1), std::get<1>(r2))));
34         }
35     );
36 
37     REQUIRE(std::abs(number) > 0);
38 }
39 
40 // Compiling and running this file will result in 150 successful assertions
41 
42