Testing: add composable property generators with shrinking (M-TESTING-04)

This commit is contained in:
faumaray committed 2026-12-04 01:52:27 +03:00
1 parent e40e2ca715
commit d338305cc7
2 files changed
+313

No files matched your search

+115
View File
@@ -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);
}
}