Wire Sysio
Wire Sysion 1.0.0
Loading...
Searching...
No Matches
311-Gen-CustomCapture.cpp
Go to the documentation of this file.
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
,
31
[&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
CATCH_INTERNAL_LINEINFO
#define CATCH_INTERNAL_LINEINFO
Definition
catch_common.h:88
GENERATE
#define GENERATE(...)
Definition
catch_generators.hpp:203
TEST_CASE
#define TEST_CASE(...)
Definition
catch.hpp:222
REQUIRE
#define REQUIRE(...)
Definition
catch.hpp:185
Catch::Generators
Definition
catch_generators.cpp:23
Catch::Generators::generate
auto generate(SourceLineInfo const &lineInfo, L const &generatorExpression) -> decltype(std::declval< decltype(generatorExpression())>().get())
Definition
catch_generators.hpp:188
catch.hpp
random
uint16_t random
Definition
yubico_otp.c:47
libraries
sys-vm
external
Catch2
examples
311-Gen-CustomCapture.cpp
Generated by
1.12.0