mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-16 18:52:25 +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.
146 lines
4.1 KiB
C++
146 lines
4.1 KiB
C++
#include <catch2/catch_test_macros.hpp>
|
|
#include <catch2/benchmark/catch_benchmark.hpp>
|
|
#include <catch2/benchmark/catch_constructor.hpp>
|
|
#include <catch2/generators/catch_generators_range.hpp>
|
|
|
|
#include <map>
|
|
|
|
namespace {
|
|
std::uint64_t Fibonacci(std::uint64_t number) {
|
|
return number < 2 ? 1 : Fibonacci(number - 1) + Fibonacci(number - 2);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Benchmark Fibonacci", "[!benchmark]") {
|
|
CHECK(Fibonacci(0) == 1);
|
|
// some more asserts..
|
|
CHECK(Fibonacci(5) == 8);
|
|
// some more asserts..
|
|
|
|
BENCHMARK("Fibonacci 20") {
|
|
return Fibonacci(20);
|
|
};
|
|
|
|
BENCHMARK("Fibonacci 25") {
|
|
return Fibonacci(25);
|
|
};
|
|
|
|
BENCHMARK("Fibonacci 30") {
|
|
return Fibonacci(30);
|
|
};
|
|
|
|
BENCHMARK("Fibonacci 35") {
|
|
return Fibonacci(35);
|
|
};
|
|
}
|
|
|
|
TEST_CASE("Benchmark containers", "[!benchmark]") {
|
|
static const int size = 100;
|
|
|
|
std::vector<int> v;
|
|
std::map<int, int> m;
|
|
|
|
SECTION("without generator") {
|
|
BENCHMARK("Load up a vector") {
|
|
v = std::vector<int>();
|
|
for (int i = 0; i < size; ++i)
|
|
v.push_back(i);
|
|
};
|
|
REQUIRE(v.size() == size);
|
|
|
|
// test optimizer control
|
|
BENCHMARK("Add up a vector's content") {
|
|
uint64_t add = 0;
|
|
for (int i = 0; i < size; ++i)
|
|
add += v[i];
|
|
return add;
|
|
};
|
|
|
|
BENCHMARK("Load up a map") {
|
|
m = std::map<int, int>();
|
|
for (int i = 0; i < size; ++i)
|
|
m.insert({ i, i + 1 });
|
|
};
|
|
REQUIRE(m.size() == size);
|
|
|
|
BENCHMARK("Reserved vector") {
|
|
v = std::vector<int>();
|
|
v.reserve(size);
|
|
for (int i = 0; i < size; ++i)
|
|
v.push_back(i);
|
|
};
|
|
REQUIRE(v.size() == size);
|
|
|
|
BENCHMARK("Resized vector") {
|
|
v = std::vector<int>();
|
|
v.resize(size);
|
|
for (int i = 0; i < size; ++i)
|
|
v[i] = i;
|
|
};
|
|
REQUIRE(v.size() == size);
|
|
|
|
int array[size];
|
|
BENCHMARK("A fixed size array that should require no allocations") {
|
|
for (int i = 0; i < size; ++i)
|
|
array[i] = i;
|
|
};
|
|
int sum = 0;
|
|
for (int i = 0; i < size; ++i)
|
|
sum += array[i];
|
|
REQUIRE(sum > size);
|
|
|
|
SECTION("XYZ") {
|
|
|
|
BENCHMARK_ADVANCED("Load up vector with chronometer")(Catch::Benchmark::Chronometer meter) {
|
|
std::vector<int> k;
|
|
meter.measure([&](int idx) {
|
|
k = std::vector<int>();
|
|
for (int i = 0; i < size; ++i)
|
|
k.push_back(idx);
|
|
});
|
|
REQUIRE(k.size() == size);
|
|
};
|
|
|
|
int runs = 0;
|
|
BENCHMARK("Fill vector indexed", benchmarkIndex) {
|
|
v = std::vector<int>();
|
|
v.resize(size);
|
|
for (int i = 0; i < size; ++i)
|
|
v[i] = benchmarkIndex;
|
|
runs = benchmarkIndex;
|
|
};
|
|
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
|
REQUIRE(v[i] == runs);
|
|
}
|
|
}
|
|
}
|
|
|
|
SECTION("with generator") {
|
|
auto generated = GENERATE(range(0, 10));
|
|
BENCHMARK("Fill vector generated") {
|
|
v = std::vector<int>();
|
|
v.resize(size);
|
|
for (int i = 0; i < size; ++i)
|
|
v[i] = generated;
|
|
};
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
|
REQUIRE(v[i] == generated);
|
|
}
|
|
}
|
|
|
|
SECTION("construct and destroy example") {
|
|
BENCHMARK_ADVANCED("construct")(Catch::Benchmark::Chronometer meter) {
|
|
std::vector<Catch::Benchmark::storage_for<std::string>> storage(meter.runs());
|
|
meter.measure([&](int i) { storage[i].construct("thing"); });
|
|
};
|
|
|
|
BENCHMARK_ADVANCED("destroy")(Catch::Benchmark::Chronometer meter) {
|
|
std::vector<Catch::Benchmark::destructable_object<std::string>> storage(meter.runs());
|
|
for(auto&& o : storage)
|
|
o.construct("thing");
|
|
meter.measure([&](int i) { storage[i].destruct(); });
|
|
};
|
|
}
|
|
}
|