Skip to content

Commit 4f058be

Browse files
committed
add at-test-allocations
1 parent 307aacf commit 4f058be

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/TrixiTest.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ include("auxiliary.jl")
77
include("macros.jl")
88

99
export get_kwarg, append_to_kwargs
10-
export @trixi_test_nowarn, @test_trixi_include_base, @timed_testset, @trixi_testset
10+
export @trixi_test_nowarn, @test_trixi_include_base, @timed_testset, @trixi_testset, @test_allocations
1111

1212
# re-export methods from TrixiBase
1313
export mpi_isroot, trixi_include

src/macros.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,19 @@ macro trixi_testset(name, expr)
192192
nothing
193193
end
194194
end
195+
196+
"""
197+
@test_allocations(mod, semi, sol, allocs)
198+
199+
Test that the memory allocations of `mod.rhs!` are below `allocs`
200+
(e.g., from type instabilities), where `mod` is a module containing
201+
a method `rhs!(du, u, semi, t)`.
202+
"""
203+
macro test_allocations(mod, semi, sol, allocs)
204+
quote
205+
t = $(esc(sol)).t[end]
206+
u = $(esc(sol)).u[end]
207+
du = similar(u)
208+
@test (@allocated $(esc(mod)).rhs!(du, u, $(esc(semi)), t)) < $(esc(allocs))
209+
end
210+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ using TrixiTest
66
include("test_testset.jl")
77
include("test_test_trixi_include.jl")
88
include("test_trixi_test_nowarn.jl")
9+
include("test_test_allocations.jl")
910
end

test/test_test_allocations.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module TestAllocations
2+
function rhs!(du, u, semi, t)
3+
# Simulate some computation
4+
du .= u .+ t
5+
return du
6+
end
7+
end
8+
9+
@testset "@test_allocations" begin
10+
semi = nothing
11+
sol = (t = [1.0], u = [[1.0, 2.0]])
12+
allocs = 1
13+
@test_allocations(TestAllocations, semi, sol, allocs)
14+
end

0 commit comments

Comments
 (0)