From 32b1001eb966c1dd58a7243805bee7e7b50c14b9 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 23 Nov 2023 10:28:39 -0500 Subject: [PATCH] Re-factor tests To avoid the re-definition of methods on `Bar`, and to explicitly import the error types. --- test/runtests.jl | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 6121878..e87e0fa 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,5 @@ using AllocCheck +using AllocCheck: AllocatingRuntimeCall, DynamicDispatch, AllocationSite using Test mutable struct Foo{T} @@ -93,6 +94,10 @@ end @test my_mod4(x, y) == y * y @test Bar(x)(y) == mod(x, y) + struct Baz + val::Float64 + end + # Standard function syntax (w/ kwargs) @check_allocs function my_mod5(; offset=1.0) return 2 * offset @@ -103,10 +108,10 @@ end @check_allocs function my_mod7(x::Float64, y; offset) return mod(x, y) + offset end - @check_allocs function (x::Bar)(y::Float64; a, b) + @check_allocs function (x::Baz)(y::Float64; a, b) return mod(x.val, y) + a + b end - @check_allocs function (x::Bar)(y::Float32; a, b=1.0) + @check_allocs function (x::Baz)(y::Float32; a, b=1.0) return mod(x.val, y) + a + b end @@ -117,10 +122,10 @@ end @test my_mod6(x, y; offset) == mod(x, y) + offset @test my_mod6(x, y) == mod(x, y) + 1.0 @test my_mod7(x, y; offset) == mod(x, y) + offset - @test Bar(x)(y; a, b) == mod(x, y) + a + b - @test Bar(x)(y; b, a) == mod(x, y) + a + b - @test Bar(x)(Float32(y); b, a) == mod(x, Float32(y)) + a + b - @test Bar(x)(Float32(y); a) == mod(x, Float32(y)) + a + 1.0 + @test Baz(x)(y; a, b) == mod(x, y) + a + b + @test Baz(x)(y; b, a) == mod(x, y) + a + b + @test Baz(x)(Float32(y); b, a) == mod(x, Float32(y)) + a + b + @test Baz(x)(Float32(y); a) == mod(x, Float32(y)) + a + 1.0 # (x,y) -> x*y f0 = @check_allocs () -> 1.5 @@ -139,8 +144,8 @@ end @test mysum1(x, y) == x + y @check_allocs mysum2(x::Float64, y::Float64) = x + y @test mysum2(x, y) == x + y - @check_allocs (x::Bar)(y::Bar) = x.val + y.val - @test Bar(x)(Bar(y)) == x + y + @check_allocs (x::Baz)(y::Baz) = x.val + y.val + @test Baz(x)(Baz(y)) == x + y end @@ -187,35 +192,35 @@ end # All error types should support Base.show() iob = IOBuffer() - alloc_with_no_bt = AllocCheck.AllocationSite(Float32, Base.StackTraces.StackFrame[]) + alloc_with_no_bt = AllocationSite(Float32, Base.StackTraces.StackFrame[]) show(iob, alloc_with_no_bt) @test occursin("unknown location", String(take!(iob))) - alloc_with_bt = AllocCheck.AllocationSite(Float32, Base.stacktrace()) + alloc_with_bt = AllocationSite(Float32, Base.stacktrace()) show(iob, alloc_with_bt) === nothing @test !occursin("unknown location", String(take!(iob))) - call_with_no_bt = AllocCheck.AllocatingRuntimeCall("jl_subtype", Base.StackTraces.StackFrame[]) + call_with_no_bt = AllocatingRuntimeCall("jl_subtype", Base.StackTraces.StackFrame[]) show(iob, call_with_no_bt) @test occursin("unknown location", String(take!(iob))) - call_with_bt = AllocCheck.AllocatingRuntimeCall("jl_subtype", Base.stacktrace()) + call_with_bt = AllocatingRuntimeCall("jl_subtype", Base.stacktrace()) show(iob, call_with_bt) === nothing @test !occursin("unknown location", String(take!(iob))) - dispatch_with_no_bt_nothing = AllocCheck.DynamicDispatch(Base.StackTraces.StackFrame[], nothing) + dispatch_with_no_bt_nothing = DynamicDispatch(Base.StackTraces.StackFrame[], nothing) show(iob, dispatch_with_no_bt_nothing) @test occursin("unknown location", String(take!(iob))) - dispatch_with_no_bt_foo = AllocCheck.DynamicDispatch(Base.StackTraces.StackFrame[], :foo) + dispatch_with_no_bt_foo = DynamicDispatch(Base.StackTraces.StackFrame[], :foo) show(iob, dispatch_with_no_bt_foo) @test occursin("to function foo", String(take!(iob))) - dispatch_with_bt_nothing = AllocCheck.DynamicDispatch(Base.stacktrace(), nothing) + dispatch_with_bt_nothing = DynamicDispatch(Base.stacktrace(), nothing) show(iob, dispatch_with_bt_nothing) === nothing @test !occursin("unknown location", String(take!(iob))) - dispatch_with_bt_foo = AllocCheck.DynamicDispatch(Base.stacktrace(), :foo) + dispatch_with_bt_foo = DynamicDispatch(Base.stacktrace(), :foo) show(iob, dispatch_with_bt_foo) === nothing @test !occursin("unknown location", String(take!(iob)))