Skip to content

Commit

Permalink
Re-factor tests
Browse files Browse the repository at this point in the history
To avoid the re-definition of methods on `Bar`, and to explicitly import
the error types.
  • Loading branch information
topolarity committed Nov 23, 2023
1 parent 571b7de commit aa447f8
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AllocCheck
using AllocCheck: AllocatingRuntimeCall, DynamicDispatch, AllocationSite
using Test

mutable struct Foo{T}
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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)))

Expand Down

0 comments on commit aa447f8

Please sign in to comment.