mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-05 05:39:53 +01:00
e1e6872c4c
This is both a really big and a really small commit. It is small in that it only contains renaming, moving and modification of include directives caused by this. It is really big in the obvious way of touching something like 200 files. The new rules for naming files is simple: headers use the `.hpp` extension. The rules for physical file layout is still kinda in progress, but the basics are also simple: * Significant parts of functionality get their own subfolder * Benchmarking is in `catch2/benchmark` * Matchers are in `catch2/matchers` * Generators are in `catch2/generators` * Reporters are in `catch2/reporters` * Baseline testing facilities are in `catch2/` * Various top level folders also contain `internal` subfolder, with files that users probably do not want to include directly, at least not until they have to write something like their own reporter. * The exact files in these subfolders is likely to change later on Note that while some includes were cleaned up in this commit, it is only the low hanging fruit and further cleanup using automatic tooling will happen later. Also note that various include guards, copyright notices and file headers will also be standardized later, rather than in this commit.
68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
// 300-Gen-OwnGenerator.cpp
|
|
// Shows how to define a custom generator.
|
|
|
|
// Specifically we will implement a random number generator for integers
|
|
// It will have infinite capacity and settable lower/upper bound
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/generators/catch_generators.hpp>
|
|
#include <catch2/generators/catch_generators_adapters.hpp>
|
|
|
|
#include <random>
|
|
|
|
namespace {
|
|
|
|
// This class shows how to implement a simple generator for Catch tests
|
|
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;
|
|
}
|
|
};
|
|
|
|
// Avoids -Wweak-vtables
|
|
int const& RandomIntGenerator::get() const {
|
|
return current_number;
|
|
}
|
|
|
|
// This helper function provides a nicer UX when instantiating the generator
|
|
// Notice that it returns an instance of GeneratorWrapper<int>, which
|
|
// is a value-wrapper around std::unique_ptr<IGenerator<int>>.
|
|
Catch::Generators::GeneratorWrapper<int> random(int low, int high) {
|
|
return Catch::Generators::GeneratorWrapper<int>(
|
|
std::make_unique<RandomIntGenerator>(low, high)
|
|
);
|
|
}
|
|
|
|
} // end anonymous namespaces
|
|
|
|
// The two sections in this test case are equivalent, but the first one
|
|
// is much more readable/nicer to use
|
|
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>(std::unique_ptr<IGenerator<int>>(new RandomIntGenerator(-100, 100)))));
|
|
REQUIRE(i >= -100);
|
|
REQUIRE(i <= 100);
|
|
}
|
|
}
|
|
|
|
// Compiling and running this file will result in 400 successful assertions
|