Add tracking allocator to test runner. #1809

This commit is contained in:
Christoffer Lerno
2025-02-09 03:10:35 +01:00
parent ce06de4b18
commit 63f619e5b6
16 changed files with 121 additions and 37 deletions

View File

@@ -113,8 +113,14 @@ fn void TrackingAllocator.clear(&self)
self.map.clear();
}
fn bool TrackingAllocator.has_leaks(&self)
{
return self.map.len() > 0;
}
fn void TrackingAllocator.print_report(&self) => self.fprint_report(io::stdout())!!;
fn void! TrackingAllocator.fprint_report(&self, OutStream out) => @pool()
{
usz total = 0;

View File

@@ -260,21 +260,31 @@ fn bool run_tests(String[] args, TestUnit[] tests) @private
if (libc::setjmp(&context.buf) == 0)
{
mute_output();
$if(!$$OLD_TEST):
unit.func();
$else
if (catch err = unit.func())
{
io::printf("[FAIL] Failed due to: %s", err);
continue;
}
$endif
TrackingAllocator mem;
mem.init(allocator::heap());
bool has_leaks;
mem::@scoped(&mem)
{
$if(!$$OLD_TEST):
unit.func();
$else
if (catch err = unit.func())
{
io::printf("[FAIL] Failed due to: %s", err);
continue;
}
$endif
has_leaks = mem.has_leaks();
};
mem.free();
unmute_output(false); // all good, discard output
io::printfn(test_context.has_ansi_codes ? "[\e[0;32mPASS\e[0m]" : "[PASS]");
io::printf(test_context.has_ansi_codes ? "[\e[0;32mPASS\e[0m]" : "[PASS]");
tests_passed++;
if (has_leaks) io::print(" LEAKS!");
io::printn();
if (test_context.teardown_fn)
{
test_context.teardown_fn();