Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
300-Gen-OwnGenerator.cpp
Go to the documentation of this file.
1// 300-Gen-OwnGenerator.cpp
2// Shows how to define a custom generator.
3
4// Specifically we will implement a random number generator for integers
5// It will have infinite capacity and settable lower/upper bound
6
7#include <catch2/catch.hpp>
8
9#include <random>
10
11// This class shows how to implement a simple generator for Catch tests
13 std::minstd_rand m_rand;
14 std::uniform_int_distribution<> m_dist;
15 int current_number;
16public:
17
18 RandomIntGenerator(int low, int high):
19 m_rand(std::random_device{}()),
20 m_dist(low, high)
21 {
22 static_cast<void>(next());
23 }
24
25 int const& get() const override;
26 bool next() override {
27 current_number = m_dist(m_rand);
28 return true;
29 }
30};
31
32// Avoids -Wweak-vtables
33int const& RandomIntGenerator::get() const {
34 return current_number;
35}
36
37// This helper function provides a nicer UX when instantiating the generator
38// Notice that it returns an instance of GeneratorWrapper<int>, which
39// is a value-wrapper around std::unique_ptr<IGenerator<int>>.
43
44// The two sections in this test case are equivalent, but the first one
45// is much more readable/nicer to use
46TEST_CASE("Generating random ints", "[example][generator]") {
47 SECTION("Nice UX") {
48 auto i = GENERATE(take(100, random(-100, 100)));
49 REQUIRE(i >= -100);
50 REQUIRE(i <= 100);
51 }
52 SECTION("Creating the random generator directly") {
53 auto i = GENERATE(take(100, GeneratorWrapper<int>(std::unique_ptr<IGenerator<int>>(new RandomIntGenerator(-100, 100)))));
54 REQUIRE(i >= -100);
55 REQUIRE(i <= 100);
56 }
57}
58
59// Compiling and running this file will result in 400 successful assertions
#define GENERATE(...)
RandomIntGenerator(int low, int high)
int const & get() const override
#define TEST_CASE(...)
Definition catch.hpp:222
#define SECTION(...)
Definition catch.hpp:226
#define REQUIRE(...)
Definition catch.hpp:185
Definition name.hpp:106
uint16_t random
Definition yubico_otp.c:47