Testing: add composable property generators with shrinking (M-TESTING-04)
This commit is contained in:
1 parent
e40e2ca715
commit
d338305cc7
2 files changed
+313
No files matched your search
@@ -0,0 +1,198 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Testing/Deterministic.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing
|
||||
{
|
||||
/// A generator paired with a shrinker, so a failing case can be reduced
|
||||
/// to a small replayable counterexample.
|
||||
template <typename T> struct Gen {
|
||||
std::function<T(DeterministicRng&)> generate{};
|
||||
std::function<std::vector<T>(const T&)> shrink{};
|
||||
};
|
||||
|
||||
namespace Gen_
|
||||
{
|
||||
template <typename T>
|
||||
[[nodiscard]] inline Gen<T> just(T value)
|
||||
{
|
||||
return Gen<T>{[value](DeterministicRng&) { return value; },
|
||||
[](const T&) { return std::vector<T>{}; }};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Gen<int> int_range(int lo, int hi)
|
||||
{
|
||||
if (hi < lo) {
|
||||
std::swap(lo, hi);
|
||||
}
|
||||
return Gen<int>{
|
||||
[lo, hi](DeterministicRng& rng) {
|
||||
return static_cast<int>(rng.next_int(lo, hi));
|
||||
},
|
||||
[lo](const int& value) {
|
||||
std::vector<int> out;
|
||||
const int target = lo > 0 ? lo : 0;
|
||||
if (value == target) {
|
||||
return out;
|
||||
}
|
||||
out.push_back(target);
|
||||
const int mid = target + (value - target) / 2;
|
||||
if (mid != target && mid != value) {
|
||||
out.push_back(mid);
|
||||
}
|
||||
return out;
|
||||
}};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Gen<bool> boolean()
|
||||
{
|
||||
return Gen<bool>{
|
||||
[](DeterministicRng& rng) { return rng.next_bool(); },
|
||||
[](const bool& value) {
|
||||
return value ? std::vector<bool>{false} : std::vector<bool>{};
|
||||
}};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] inline Gen<T> element_of(std::vector<T> values)
|
||||
{
|
||||
return Gen<T>{
|
||||
[values](DeterministicRng& rng) {
|
||||
return values[rng.next_bounded(values.size())];
|
||||
},
|
||||
[values](const T& value) {
|
||||
std::vector<T> out;
|
||||
if (!values.empty() && !(value == values.front())) {
|
||||
out.push_back(values.front());
|
||||
}
|
||||
return out;
|
||||
}};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] inline Gen<std::vector<T>>
|
||||
vector_of(Gen<T> element, std::size_t max_len)
|
||||
{
|
||||
return Gen<std::vector<T>>{
|
||||
[element, max_len](DeterministicRng& rng) {
|
||||
const std::size_t len =
|
||||
max_len == 0 ? 0
|
||||
: static_cast<std::size_t>(
|
||||
rng.next_bounded(max_len + 1));
|
||||
std::vector<T> out;
|
||||
out.reserve(len);
|
||||
for (std::size_t i = 0; i < len; ++i) {
|
||||
out.push_back(element.generate(rng));
|
||||
}
|
||||
return out;
|
||||
},
|
||||
[element](const std::vector<T>& values) {
|
||||
std::vector<std::vector<T>> out;
|
||||
if (values.empty()) {
|
||||
return out;
|
||||
}
|
||||
out.push_back({});
|
||||
if (values.size() > 1) {
|
||||
out.push_back(std::vector<T>(
|
||||
values.begin(),
|
||||
values.begin() +
|
||||
static_cast<std::ptrdiff_t>(values.size() / 2)));
|
||||
}
|
||||
for (std::size_t i = 0; i < values.size(); ++i) {
|
||||
auto copy = values;
|
||||
copy.erase(copy.begin() +
|
||||
static_cast<std::ptrdiff_t>(i));
|
||||
out.push_back(std::move(copy));
|
||||
}
|
||||
for (std::size_t i = 0; i < values.size(); ++i) {
|
||||
for (const auto& shrunk : element.shrink(values[i])) {
|
||||
auto copy = values;
|
||||
copy[i] = shrunk;
|
||||
out.push_back(std::move(copy));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] inline Gen<std::optional<T>>
|
||||
optional_of(Gen<T> element)
|
||||
{
|
||||
return Gen<std::optional<T>>{
|
||||
[element](DeterministicRng& rng) -> std::optional<T> {
|
||||
if (!rng.next_bool()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return element.generate(rng);
|
||||
},
|
||||
[element](const std::optional<T>& value) {
|
||||
std::vector<std::optional<T>> out;
|
||||
if (!value.has_value()) {
|
||||
return out;
|
||||
}
|
||||
out.push_back(std::nullopt);
|
||||
for (const auto& shrunk : element.shrink(*value)) {
|
||||
out.push_back(shrunk);
|
||||
}
|
||||
return out;
|
||||
}};
|
||||
}
|
||||
|
||||
/// Transform generated values. The result does not shrink because the
|
||||
/// inverse transform is unknown.
|
||||
template <typename T, typename F>
|
||||
[[nodiscard]] inline auto map(Gen<T> source, F transform)
|
||||
{
|
||||
using U = std::remove_cvref_t<std::invoke_result_t<F&, const T&>>;
|
||||
return Gen<U>{
|
||||
[source, transform](DeterministicRng& rng) {
|
||||
return transform(source.generate(rng));
|
||||
},
|
||||
[](const U&) { return std::vector<U>{}; }};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] inline Gen<T> such_that(Gen<T> source,
|
||||
std::function<bool(const T&)> predicate)
|
||||
{
|
||||
return Gen<T>{
|
||||
[source, predicate](DeterministicRng& rng) {
|
||||
for (int attempt = 0; attempt < 100; ++attempt) {
|
||||
T candidate = source.generate(rng);
|
||||
if (predicate(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return source.generate(rng);
|
||||
},
|
||||
[source, predicate](const T& value) {
|
||||
std::vector<T> out;
|
||||
for (const auto& shrunk : source.shrink(value)) {
|
||||
if (predicate(shrunk)) {
|
||||
out.push_back(shrunk);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}};
|
||||
}
|
||||
} // namespace Gen_
|
||||
|
||||
/// Run a predicate over generated cases and shrink the first failure.
|
||||
template <typename T, typename Predicate>
|
||||
[[nodiscard]] inline auto for_all(Gen<T> gen, Predicate&& predicate,
|
||||
const PropertyOptions& options = {},
|
||||
ReplayToken token = {})
|
||||
{
|
||||
return check_property(std::move(gen.generate),
|
||||
std::forward<Predicate>(predicate),
|
||||
std::move(gen.shrink), options, token);
|
||||
}
|
||||
} // namespace Void::Testing
|
||||
@@ -0,0 +1,115 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Generators.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
// Deliberately wrong: seeds the running maximum with 0, so an all-negative
|
||||
// input reports 0 instead of its true maximum.
|
||||
int faulty_max(const std::vector<int>& values)
|
||||
{
|
||||
int result = 0;
|
||||
for (const int value : values) {
|
||||
result = std::max(result, value);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test generators_find_and_shrink_a_faulty_algorithm()
|
||||
{
|
||||
auto gen = Gen_::vector_of(Gen_::int_range(-8, 8), 6);
|
||||
auto result = for_all(std::move(gen), [](const std::vector<int>& values) {
|
||||
if (values.empty()) {
|
||||
return true;
|
||||
}
|
||||
return faulty_max(values) ==
|
||||
*std::max_element(values.begin(), values.end());
|
||||
});
|
||||
|
||||
Void::Testing::expect(result.status == PropertyStatus::failed);
|
||||
Void::Testing::require(result.minimal_failure.has_value());
|
||||
const auto& minimal = *result.minimal_failure;
|
||||
// The shrunk counterexample is the smallest failing case: a single
|
||||
// negative element.
|
||||
Void::Testing::expect_eq(minimal.size(), std::size_t{1});
|
||||
Void::Testing::expect(minimal[0] < 0);
|
||||
Void::Testing::expect(result.shrink_steps > 0);
|
||||
Void::Testing::require(result.original_failure.has_value());
|
||||
Void::Testing::expect(result.original_failure->size() >= minimal.size());
|
||||
|
||||
// The replay token reproduces the same original failure.
|
||||
auto replay = replay_property_case(
|
||||
[](DeterministicRng& rng) {
|
||||
std::vector<int> values;
|
||||
const auto length = static_cast<std::size_t>(rng.next_bounded(7));
|
||||
for (std::size_t i = 0; i < length; ++i) {
|
||||
values.push_back(static_cast<int>(rng.next_int(-8, 8)));
|
||||
}
|
||||
return values;
|
||||
},
|
||||
[](const std::vector<int>& values) {
|
||||
if (values.empty()) {
|
||||
return true;
|
||||
}
|
||||
return faulty_max(values) ==
|
||||
*std::max_element(values.begin(), values.end());
|
||||
},
|
||||
result.replay);
|
||||
// replay_property_case returns the predicate: a reproduced failure is false.
|
||||
Void::Testing::expect(!replay);
|
||||
}
|
||||
|
||||
Void::Testing::Test generators_correct_algorithm_passes()
|
||||
{
|
||||
auto gen = Gen_::vector_of(Gen_::int_range(-8, 8), 6);
|
||||
auto result = for_all(std::move(gen), [](const std::vector<int>& values) {
|
||||
if (values.empty()) {
|
||||
return true;
|
||||
}
|
||||
return *std::max_element(values.begin(), values.end()) >=
|
||||
*std::min_element(values.begin(), values.end());
|
||||
});
|
||||
Void::Testing::expect(result.status == PropertyStatus::passed);
|
||||
Void::Testing::expect(!result.minimal_failure.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test generators_combinators_compose()
|
||||
{
|
||||
DeterministicRng rng{ReplayToken{42}};
|
||||
|
||||
auto optional = Gen_::optional_of(Gen_::int_range(0, 10));
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
const auto value = optional.generate(rng);
|
||||
if (value.has_value()) {
|
||||
Void::Testing::expect(*value >= 0 && *value <= 10);
|
||||
const auto shrunk = optional.shrink(value);
|
||||
Void::Testing::expect(!shrunk.empty());
|
||||
Void::Testing::expect(!shrunk.front().has_value());
|
||||
}
|
||||
}
|
||||
|
||||
auto choice = Gen_::element_of(std::vector<int>{1, 2, 3});
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
const auto value = choice.generate(rng);
|
||||
Void::Testing::expect(value >= 1 && value <= 3);
|
||||
if (value != 1) {
|
||||
Void::Testing::expect(choice.shrink(value).front() == 1);
|
||||
}
|
||||
}
|
||||
|
||||
auto doubled = Gen_::map(Gen_::int_range(0, 5), [](int value) {
|
||||
return value * 2;
|
||||
});
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
const auto value = doubled.generate(rng);
|
||||
Void::Testing::expect(value % 2 == 0 && value >= 0 && value <= 10);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user