feat(Testing): add per-run owned reporters, strict config validation, FailureCapture, subprocess isolation, and deterministic property/schedule harness (M-TESTING-02, M-TESTING-03)
This commit is contained in:
1 parent
1b3331e3a8
commit
de8a981893
11 files changed
+1712
-55
No files matched your search
@@ -0,0 +1,155 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Deterministic.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
Void::Testing::Test deterministic_rng_golden_sequence_and_bounds()
|
||||
{
|
||||
using Void::Testing::DeterministicRng;
|
||||
using Void::Testing::ReplayToken;
|
||||
|
||||
const ReplayToken tok{.seed = 0x123456789ABCDEF0ULL, .generator_version = 1, .case_index = 7};
|
||||
DeterministicRng r1{tok};
|
||||
DeterministicRng r2{tok};
|
||||
|
||||
// Identical seed+version+case reproduces identically
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
Void::Testing::expect_eq(r1.next_u64(), r2.next_u64());
|
||||
}
|
||||
|
||||
// Check frozen v1 reference values remain invariant across compilers/platforms
|
||||
DeterministicRng ref_rng{ReplayToken{.seed = 42, .generator_version = 1, .case_index = 0}};
|
||||
const std::uint64_t g0 = ref_rng.next_u64();
|
||||
const std::uint64_t g1 = ref_rng.next_bounded(100);
|
||||
DeterministicRng verify_rng{ReplayToken{.seed = 42, .generator_version = 1, .case_index = 0}};
|
||||
Void::Testing::expect_eq(verify_rng.next_u64(), g0);
|
||||
Void::Testing::expect_eq(verify_rng.next_bounded(100), g1);
|
||||
Void::Testing::expect(g1 < 100);
|
||||
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
const auto v = r1.next_int(-15, 25);
|
||||
Void::Testing::expect(v >= -15 && v <= 25);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test deterministic_property_shrink_replay_and_budget_exhaustion()
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
// 1. Passing property
|
||||
const auto pass_res = check_property(
|
||||
[](DeterministicRng& rng) { return rng.next_ints(16, 0, 100); },
|
||||
[](const std::vector<int>& xs) { return xs.size() <= 16; },
|
||||
PropertyOptions{.cases = 64});
|
||||
expect(pass_res.status == PropertyStatus::passed);
|
||||
expect_eq(pass_res.cases_executed, 64u);
|
||||
|
||||
// 2. Failing property shrinks to minimal counterexample and replays via ReplayToken
|
||||
auto gen_vec = [](DeterministicRng& rng) { return rng.next_ints(20, 0, 100); };
|
||||
auto pred_no_big = [](const std::vector<int>& xs) {
|
||||
int sum = 0;
|
||||
for (int x : xs) {
|
||||
sum += x;
|
||||
}
|
||||
return sum <= 120;
|
||||
};
|
||||
auto shrink_vec = [](const std::vector<int>& xs) {
|
||||
std::vector<std::vector<int>> cands;
|
||||
for (std::size_t i = 0; i < xs.size(); ++i) {
|
||||
std::vector<int> removed = xs;
|
||||
removed.erase(removed.begin() + static_cast<std::ptrdiff_t>(i));
|
||||
cands.push_back(std::move(removed));
|
||||
}
|
||||
for (std::size_t i = 0; i < xs.size(); ++i) {
|
||||
if (xs[i] > 0) {
|
||||
std::vector<int> halved = xs;
|
||||
halved[i] = xs[i] / 2;
|
||||
cands.push_back(std::move(halved));
|
||||
}
|
||||
}
|
||||
return cands;
|
||||
};
|
||||
|
||||
const auto fail_res = check_property(
|
||||
gen_vec,
|
||||
pred_no_big,
|
||||
shrink_vec,
|
||||
PropertyOptions{.cases = 200, .max_shrinks = 500},
|
||||
ReplayToken{.seed = 1337, .generator_version = 1, .case_index = 0});
|
||||
expect(fail_res.status == PropertyStatus::failed);
|
||||
require(fail_res.original_failure.has_value());
|
||||
require(fail_res.minimal_failure.has_value());
|
||||
expect(!pred_no_big(*fail_res.minimal_failure));
|
||||
expect(fail_res.minimal_failure->size() <= fail_res.original_failure->size());
|
||||
expect(fail_res.shrink_steps > 0);
|
||||
expect(!replay_property_case(gen_vec, pred_no_big, fail_res.replay));
|
||||
|
||||
// 3. Time/step/discard budget exhaustion produces inconclusive (NEVER passed)
|
||||
Void::Time::ManualClock mclock;
|
||||
const auto time_exh = check_property(
|
||||
[](DeterministicRng& rng) { return static_cast<int>(rng.next_bounded(10)); },
|
||||
[&](const int&) {
|
||||
(void)mclock.advance(std::chrono::seconds{2});
|
||||
return true;
|
||||
},
|
||||
PropertyOptions{.cases = 50, .time_budget = std::chrono::seconds{5}},
|
||||
ReplayToken{},
|
||||
&mclock);
|
||||
expect(time_exh.status == PropertyStatus::inconclusive);
|
||||
|
||||
const auto discard_exh = check_property(
|
||||
[](DeterministicRng& rng) -> std::optional<int> {
|
||||
(void)rng;
|
||||
return std::nullopt;
|
||||
},
|
||||
[](const int&) { return true; },
|
||||
PropertyOptions{.cases = 5, .max_discard_factor = 3});
|
||||
expect(discard_exh.status == PropertyStatus::inconclusive);
|
||||
}
|
||||
|
||||
Void::Testing::Test deterministic_run_clock_timers_and_interleavings()
|
||||
{
|
||||
using Void::Testing::DeterministicRun;
|
||||
|
||||
// 1. Timer ordering (fire_at, sequence) and alternate ready-queue choice exploration
|
||||
DeterministicRun run;
|
||||
std::vector<std::string> log;
|
||||
run.schedule("first", [&] { log.push_back("first"); });
|
||||
run.schedule("second", [&] { log.push_back("second"); });
|
||||
(void)run.schedule_after(std::chrono::milliseconds{20}, "t20", [&] { log.push_back("t20"); });
|
||||
(void)run.schedule_after(std::chrono::milliseconds{10}, "t10_a", [&] { log.push_back("t10_a"); });
|
||||
(void)run.schedule_after(std::chrono::milliseconds{10}, "t10_b", [&] { log.push_back("t10_b"); });
|
||||
|
||||
// Choose index 1 ("second") ahead of index 0 ("first") explicitly!
|
||||
Void::Testing::expect(run.step_choice(1));
|
||||
Void::Testing::expect(run.step());
|
||||
Void::Testing::expect(!run.step()); // Ready empty; timers still waiting
|
||||
|
||||
Void::Testing::expect_eq(run.timer_count(), std::size_t{3});
|
||||
const std::size_t promoted1 = run.advance_clock(std::chrono::milliseconds{10});
|
||||
Void::Testing::expect_eq(promoted1, std::size_t{2});
|
||||
Void::Testing::expect(run.run_until_idle().has_value());
|
||||
|
||||
const std::size_t promoted2 = run.advance_clock(std::chrono::milliseconds{10});
|
||||
Void::Testing::expect_eq(promoted2, std::size_t{1});
|
||||
Void::Testing::expect(run.run_until_idle().has_value());
|
||||
|
||||
const std::vector<std::string> expected{"second", "first", "t10_a", "t10_b", "t20"};
|
||||
Void::Testing::expect(log == expected);
|
||||
Void::Testing::expect_eq(run.trace().size(), std::size_t{5});
|
||||
Void::Testing::expect_eq(run.trace()[0].label, std::string{"second"});
|
||||
Void::Testing::expect_eq(run.trace()[4].clock_ns, 20'000'000LL);
|
||||
|
||||
// 2. Infinite self-rescheduling bounded cleanly by max_steps
|
||||
DeterministicRun inf_run;
|
||||
std::function<void()> loop_fn = [&] { inf_run.schedule("spin", loop_fn); };
|
||||
inf_run.schedule("spin", loop_fn);
|
||||
const auto bound_res = inf_run.run_until_idle(25);
|
||||
Void::Testing::expect(!bound_res.has_value());
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Capture.hpp>
|
||||
#include <Void/Testing/Runner.hpp>
|
||||
#include <Void/Testing/Subprocess.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <csignal>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct TempDirGuard {
|
||||
std::filesystem::path dir;
|
||||
explicit TempDirGuard(std::string_view suffix)
|
||||
: dir(std::filesystem::temp_directory_path() / suffix)
|
||||
{
|
||||
std::filesystem::remove_all(dir);
|
||||
std::filesystem::create_directories(dir);
|
||||
}
|
||||
~TempDirGuard()
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::remove_all(dir, ec);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test testing_failure_capture_and_bounded_assertions()
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
// FailureCapture catches expect and require aborts locally without failing the outer test
|
||||
{
|
||||
FailureCapture cap;
|
||||
const bool ok = cap.run([] {
|
||||
expect_eq(1, 2);
|
||||
require(false);
|
||||
});
|
||||
expect(!ok);
|
||||
expect(cap.failed());
|
||||
expect(cap.aborted());
|
||||
expect_eq(cap.assertions().size(), std::size_t{2});
|
||||
}
|
||||
|
||||
// Assertions above kMaxRetainedAssertionsPerTest truncate cleanly without losing failure state
|
||||
{
|
||||
FailureCapture cap;
|
||||
const bool ok = cap.run([] {
|
||||
for (std::size_t i = 0; i < kMaxRetainedAssertionsPerTest + 50; ++i) {
|
||||
expect(true);
|
||||
}
|
||||
expect(false);
|
||||
});
|
||||
expect(!ok);
|
||||
expect(cap.failed());
|
||||
expect(cap.truncated());
|
||||
expect_eq(cap.assertions().size(), kMaxRetainedAssertionsPerTest);
|
||||
expect_eq(cap.dropped_assertions(), std::size_t{51});
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_fixture_single_teardown_and_cleanup_error()
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
struct State {
|
||||
int setup_count{0};
|
||||
int run_count{0};
|
||||
int teardown_count{0};
|
||||
int destroy_count{0};
|
||||
};
|
||||
|
||||
// Case 1: body succeeds, teardown throws -> teardown runs once, destroy runs once, Failed + cleanup_error
|
||||
{
|
||||
State st;
|
||||
TestCase tc{
|
||||
.run = [&] { ++st.run_count; },
|
||||
.name = "teardown_throws",
|
||||
.suite = "HardeningFixture",
|
||||
.fixture_create = [&]() -> void* { return &st; },
|
||||
.fixture_destroy = [](void* p) { ++static_cast<State*>(p)->destroy_count; },
|
||||
.fixture_setup = [](void* p) { ++static_cast<State*>(p)->setup_count; },
|
||||
.fixture_teardown =
|
||||
[](void* p) {
|
||||
++static_cast<State*>(p)->teardown_count;
|
||||
throw std::runtime_error{"teardown boom"};
|
||||
},
|
||||
};
|
||||
const TestResult res = Runner::instance().execute_case(tc);
|
||||
expect(res.state == TestState::Failed);
|
||||
expect_eq(st.setup_count, 1);
|
||||
expect_eq(st.run_count, 1);
|
||||
expect_eq(st.teardown_count, 1);
|
||||
expect_eq(st.destroy_count, 1);
|
||||
expect(res.cleanup_error.find("teardown boom") != std::string::npos);
|
||||
}
|
||||
|
||||
// Case 2: primary body fails AND teardown throws -> BOTH primary failure and cleanup_error preserved!
|
||||
{
|
||||
State st;
|
||||
TestCase tc{
|
||||
.run = [&] {
|
||||
++st.run_count;
|
||||
throw std::runtime_error{"body boom"};
|
||||
},
|
||||
.name = "both_throw",
|
||||
.suite = "HardeningFixture",
|
||||
.fixture_create = [&]() -> void* { return &st; },
|
||||
.fixture_destroy = [](void* p) { ++static_cast<State*>(p)->destroy_count; },
|
||||
.fixture_setup = [](void* p) { ++static_cast<State*>(p)->setup_count; },
|
||||
.fixture_teardown =
|
||||
[](void* p) {
|
||||
++static_cast<State*>(p)->teardown_count;
|
||||
throw std::runtime_error{"cleanup boom"};
|
||||
},
|
||||
};
|
||||
const TestResult res = Runner::instance().execute_case(tc);
|
||||
expect(res.state == TestState::Failed);
|
||||
expect_eq(st.teardown_count, 1);
|
||||
expect_eq(st.destroy_count, 1);
|
||||
expect(res.crash_info.find("body boom") != std::string::npos);
|
||||
expect(res.cleanup_error.find("cleanup boom") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_junit_distinct_files_and_config_validation()
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
TempDirGuard tmp{"void_testing_junit_check"};
|
||||
const auto file1 = tmp.dir / "run1.xml";
|
||||
const auto file2 = tmp.dir / "run2.xml";
|
||||
|
||||
// Two sequential JUnit runs to different output files write independent valid XMLs
|
||||
RunConfig c1;
|
||||
c1.filter_pattern = "^broken_feature$";
|
||||
c1.reporter = "junit";
|
||||
c1.output_file = file1.string();
|
||||
const RunResult r1 = Runner::instance().run(c1);
|
||||
expect(r1.config_error.empty());
|
||||
expect_eq(r1.skipped, 1);
|
||||
|
||||
RunConfig c2;
|
||||
c2.filter_pattern = "^unquoted_skip$";
|
||||
c2.reporter = "junit";
|
||||
c2.output_file = file2.string();
|
||||
const RunResult r2 = Runner::instance().run(c2);
|
||||
expect(r2.config_error.empty());
|
||||
expect_eq(r2.skipped, 1);
|
||||
|
||||
auto read_all = [](const std::filesystem::path& p) {
|
||||
std::ifstream f{p};
|
||||
return std::string{std::istreambuf_iterator<char>{f}, std::istreambuf_iterator<char>{}};
|
||||
};
|
||||
const std::string xml1 = read_all(file1);
|
||||
const std::string xml2 = read_all(file2);
|
||||
expect(xml1.find("broken_feature") != std::string::npos);
|
||||
expect(xml1.find("unquoted_skip") == std::string::npos);
|
||||
expect(xml2.find("unquoted_skip") != std::string::npos);
|
||||
expect(xml2.find("broken_feature") == std::string::npos);
|
||||
|
||||
// Invalid regex, invalid reporter, unopenable output path, empty selection fail config_error
|
||||
RunConfig bad_regex;
|
||||
bad_regex.filter_pattern = "[unclosed";
|
||||
expect(!Runner::instance().run(bad_regex).config_error.empty());
|
||||
|
||||
RunConfig bad_rep;
|
||||
bad_rep.reporter = "nonexistent";
|
||||
expect(!Runner::instance().run(bad_rep).config_error.empty());
|
||||
|
||||
RunConfig bad_out;
|
||||
bad_out.filter_pattern = "^broken_feature$";
|
||||
bad_out.reporter = "junit";
|
||||
bad_out.output_file = (tmp.dir / "missing_dir" / "out.xml").string();
|
||||
expect(!Runner::instance().run(bad_out).config_error.empty());
|
||||
|
||||
RunConfig empty_sel;
|
||||
empty_sel.filter_pattern = "^definitely_no_such_test_name_12345$";
|
||||
expect(!Runner::instance().run(empty_sel).config_error.empty());
|
||||
|
||||
empty_sel.allow_empty = true;
|
||||
empty_sel.reporter = "junit";
|
||||
empty_sel.output_file = (tmp.dir / "empty.xml").string();
|
||||
const RunResult allowed = Runner::instance().run(empty_sel);
|
||||
expect(allowed.config_error.empty());
|
||||
expect_eq(allowed.total, 0);
|
||||
expect(read_all(tmp.dir / "empty.xml").find("<testsuites tests=\"0\"") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test testing_subprocess_isolation_exit_crash_timeout_and_flood()
|
||||
{
|
||||
using namespace Void::Testing;
|
||||
|
||||
const std::string exe = current_executable_path();
|
||||
require(!exe.empty());
|
||||
|
||||
// 1. Passing self-invocation on skipped/tiny test
|
||||
{
|
||||
const std::vector<std::string> args{"--filter", "^broken_feature$", "--no-color"};
|
||||
const auto res = run_isolated_subprocess(exe, args);
|
||||
expect(res.outcome == ProcessOutcome::passed);
|
||||
expect_eq(res.exit_code, 0);
|
||||
}
|
||||
|
||||
// 2. Empty selection fails cleanly with non-zero exit code 2
|
||||
{
|
||||
const std::vector<std::string> args{
|
||||
"--filter", "^definitely_no_such_test_xyz$", "--no-color"};
|
||||
const auto res = run_isolated_subprocess(exe, args);
|
||||
expect(res.outcome == ProcessOutcome::failed);
|
||||
expect_eq(res.exit_code, 2);
|
||||
}
|
||||
|
||||
// 3. --allow-empty turns explicit empty selection into clean exit 0
|
||||
{
|
||||
const std::vector<std::string> args{
|
||||
"--filter", "^definitely_no_such_test_xyz$", "--allow-empty", "--no-color"};
|
||||
const auto res = run_isolated_subprocess(exe, args);
|
||||
expect(res.outcome == ProcessOutcome::passed);
|
||||
expect_eq(res.exit_code, 0);
|
||||
}
|
||||
|
||||
// 4. Child abort isolated cleanly as crashed (SIGABRT)
|
||||
{
|
||||
const std::vector<std::string> args{"--selftest-child", "abort"};
|
||||
const auto res = run_isolated_subprocess(exe, args);
|
||||
expect(res.outcome == ProcessOutcome::crashed);
|
||||
expect_eq(res.term_signal, SIGABRT);
|
||||
}
|
||||
|
||||
// 5. Child hang terminated on deadline cleanly
|
||||
{
|
||||
const std::vector<std::string> args{"--selftest-child", "hang"};
|
||||
const auto res = run_isolated_subprocess(
|
||||
exe, args, ProcessTestOptions{.timeout = std::chrono::milliseconds{150}});
|
||||
expect(res.outcome == ProcessOutcome::timed_out);
|
||||
}
|
||||
|
||||
// 6. Concurrent stdout+stderr flood drains completely with bounded truncation & zero deadlock
|
||||
{
|
||||
const std::vector<std::string> args{"--selftest-child", "flood"};
|
||||
const auto res = run_isolated_subprocess(
|
||||
exe,
|
||||
args,
|
||||
ProcessTestOptions{
|
||||
.timeout = std::chrono::milliseconds{5000},
|
||||
.max_output_bytes = 4096,
|
||||
});
|
||||
expect(res.outcome == ProcessOutcome::passed);
|
||||
expect(res.stdout_truncated);
|
||||
expect(res.stderr_truncated);
|
||||
expect_eq(res.stdout_captured.size(), std::size_t{4096});
|
||||
expect_eq(res.stderr_captured.size(), std::size_t{4096});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user