123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #include <catch2/catch_test_macros.hpp>
- #include <catch2/generators/catch_generators.hpp>
- #include <catch2/generators/catch_generators_adapters.hpp>
- #include <random>
- namespace {
- class RandomIntGenerator : public Catch::Generators::IGenerator<int> {
- std::minstd_rand m_rand;
- std::uniform_int_distribution<> m_dist;
- int current_number;
- public:
- RandomIntGenerator(int low, int high):
- m_rand(std::random_device{}()),
- m_dist(low, high)
- {
- static_cast<void>(next());
- }
- int const& get() const override;
- bool next() override {
- current_number = m_dist(m_rand);
- return true;
- }
- };
- int const& RandomIntGenerator::get() const {
- return current_number;
- }
- Catch::Generators::GeneratorWrapper<int> random(int low, int high) {
- return Catch::Generators::GeneratorWrapper<int>(
- new RandomIntGenerator(low, high)
-
-
- );
- }
- }
- TEST_CASE("Generating random ints", "[example][generator]") {
- SECTION("Nice UX") {
- auto i = GENERATE(take(100, random(-100, 100)));
- REQUIRE(i >= -100);
- REQUIRE(i <= 100);
- }
- SECTION("Creating the random generator directly") {
- auto i = GENERATE(take(100, GeneratorWrapper<int>(Catch::Detail::make_unique<RandomIntGenerator>(-100, 100))));
- REQUIRE(i >= -100);
- REQUIRE(i <= 100);
- }
- }
|