Debian Codegraph @

3rdparty/catch2/examples/311-Gen-CustomCapture.cpp

raw symbols SCIP
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_test_macros.hpp>
13 #include <catch2/generators/catch_generators_adapters.hpp>
14 #include <catch2/generators/catch_generators_random.hpp>
15
16 TEST_CASE("Generate random doubles across different ranges",
17 "[generator][example][advanced]") {
18 // Workaround for old libstdc++
19 using record = std::tuple<double, double>;
20 // Set up 3 ranges to generate numbers from
21 auto r1 = GENERATE(table<double, double>({
22 record{3, 4},
23 record{-4, -3},
24 record{10, 1000}
25 }));
26
27 auto r2(r1);
28
29 // This will take r1 by reference and r2 by value.
30 // Note that there are no advantages for doing so in this example,
31 // it is done only for expository purposes.
32 auto number = Catch::Generators::generate( "custom capture generator", CATCH_INTERNAL_LINEINFO,
33 [&r1, r2]{
34 using namespace Catch::Generators;
35 return makeGenerators(take(50, random(std::get<0>(r1), std::get<1>(r2))));
36 }
37 );
38
39 REQUIRE(std::abs(number) > 0);
40 }
41
42 // Compiling and running this file will result in 150 successful assertions
43