Use test runner's own tracking allocator.

This commit is contained in:
Jeroen van Rijn
2024-08-08 20:58:25 +02:00
parent 80d1e1ba82
commit 4d27898418
2 changed files with 13 additions and 8 deletions
+7 -8
View File
@@ -141,21 +141,20 @@ expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> boo
Memory_Verifier_Proc :: #type proc(t: ^T, ta: ^mem.Tracking_Allocator) Memory_Verifier_Proc :: #type proc(t: ^T, ta: ^mem.Tracking_Allocator)
expect_leaks :: proc(t: ^T, client_test: proc(t: ^T), verifier: Memory_Verifier_Proc) { expect_leaks :: proc(t: ^T, client_test: proc(t: ^T), verifier: Memory_Verifier_Proc) {
{ when TRACKING_MEMORY {
ta: mem.Tracking_Allocator
mem.tracking_allocator_init(&ta, context.allocator)
defer mem.tracking_allocator_destroy(&ta)
context.allocator = mem.tracking_allocator(&ta)
client_test(t) client_test(t)
ta := (^mem.Tracking_Allocator)(context.allocator.data)
sync.mutex_lock(&ta.mutex) sync.mutex_lock(&ta.mutex)
// The verifier can inspect this local tracking allocator. // The verifier can inspect this local tracking allocator.
// And then call `testing.expect_*` as makes sense for the client test. // And then call `testing.expect_*` as makes sense for the client test.
verifier(t, &ta) verifier(t, ta)
sync.mutex_unlock(&ta.mutex) sync.mutex_unlock(&ta.mutex)
}
clear(&ta.bad_free_array)
free_all(context.allocator) free_all(context.allocator)
} }
}
set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) { set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
chan.send(t.channel, Event_Set_Fail_Timeout { chan.send(t.channel, Event_Set_Fail_Timeout {
@@ -87,9 +87,15 @@ test_intentional_leaks :: proc(t: ^testing.T) {
// Not tagged with @(test) because it's run through `test_intentional_leaks` // Not tagged with @(test) because it's run through `test_intentional_leaks`
intentionally_leaky_test :: proc(t: ^testing.T) { intentionally_leaky_test :: proc(t: ^testing.T) {
a: [dynamic]int a: [dynamic]int
// Intentional leak
append(&a, 42) append(&a, 42)
// Intentional bad free
b := uintptr(&a[0]) + 42
free(rawptr(b))
} }
leak_verifier :: proc(t: ^testing.T, ta: ^mem.Tracking_Allocator) { leak_verifier :: proc(t: ^testing.T, ta: ^mem.Tracking_Allocator) {
testing.expect_value(t, len(ta.allocation_map), 1) testing.expect_value(t, len(ta.allocation_map), 1)
testing.expect_value(t, len(ta.bad_free_array), 1)
} }