diff --git a/include/Void/Testing/Generators.hpp b/include/Void/Testing/Generators.hpp new file mode 100644 index 0000000..03d6311 --- /dev/null +++ b/include/Void/Testing/Generators.hpp @@ -0,0 +1,198 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include +#include + +namespace Void::Testing +{ + /// A generator paired with a shrinker, so a failing case can be reduced + /// to a small replayable counterexample. + template struct Gen { + std::function generate{}; + std::function(const T&)> shrink{}; + }; + + namespace Gen_ + { + template + [[nodiscard]] inline Gen just(T value) + { + return Gen{[value](DeterministicRng&) { return value; }, + [](const T&) { return std::vector{}; }}; + } + + [[nodiscard]] inline Gen int_range(int lo, int hi) + { + if (hi < lo) { + std::swap(lo, hi); + } + return Gen{ + [lo, hi](DeterministicRng& rng) { + return static_cast(rng.next_int(lo, hi)); + }, + [lo](const int& value) { + std::vector 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 boolean() + { + return Gen{ + [](DeterministicRng& rng) { return rng.next_bool(); }, + [](const bool& value) { + return value ? std::vector{false} : std::vector{}; + }}; + } + + template + [[nodiscard]] inline Gen element_of(std::vector values) + { + return Gen{ + [values](DeterministicRng& rng) { + return values[rng.next_bounded(values.size())]; + }, + [values](const T& value) { + std::vector out; + if (!values.empty() && !(value == values.front())) { + out.push_back(values.front()); + } + return out; + }}; + } + + template + [[nodiscard]] inline Gen> + vector_of(Gen element, std::size_t max_len) + { + return Gen>{ + [element, max_len](DeterministicRng& rng) { + const std::size_t len = + max_len == 0 ? 0 + : static_cast( + rng.next_bounded(max_len + 1)); + std::vector 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& values) { + std::vector> out; + if (values.empty()) { + return out; + } + out.push_back({}); + if (values.size() > 1) { + out.push_back(std::vector( + values.begin(), + values.begin() + + static_cast(values.size() / 2))); + } + for (std::size_t i = 0; i < values.size(); ++i) { + auto copy = values; + copy.erase(copy.begin() + + static_cast(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 + [[nodiscard]] inline Gen> + optional_of(Gen element) + { + return Gen>{ + [element](DeterministicRng& rng) -> std::optional { + if (!rng.next_bool()) { + return std::nullopt; + } + return element.generate(rng); + }, + [element](const std::optional& value) { + std::vector> 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 + [[nodiscard]] inline auto map(Gen source, F transform) + { + using U = std::remove_cvref_t>; + return Gen{ + [source, transform](DeterministicRng& rng) { + return transform(source.generate(rng)); + }, + [](const U&) { return std::vector{}; }}; + } + + template + [[nodiscard]] inline Gen such_that(Gen source, + std::function predicate) + { + return Gen{ + [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 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 + [[nodiscard]] inline auto for_all(Gen gen, Predicate&& predicate, + const PropertyOptions& options = {}, + ReplayToken token = {}) + { + return check_property(std::move(gen.generate), + std::forward(predicate), + std::move(gen.shrink), options, token); + } +} // namespace Void::Testing diff --git a/tests/standalone/Testing/Generators.cpp b/tests/standalone/Testing/Generators.cpp new file mode 100644 index 0000000..c67ee32 --- /dev/null +++ b/tests/standalone/Testing/Generators.cpp @@ -0,0 +1,115 @@ +#include +#include +#include + +#include +#include +#include +#include + +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& 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& 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 values; + const auto length = static_cast(rng.next_bounded(7)); + for (std::size_t i = 0; i < length; ++i) { + values.push_back(static_cast(rng.next_int(-8, 8))); + } + return values; + }, + [](const std::vector& 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& 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{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); + } +}