Skip to content

Commit

Permalink
Add a function to test 0-allocations (#88)
Browse files Browse the repository at this point in the history
* add function to test 0-allocations

* skip julia 1.6 in test 0
  • Loading branch information
tmigot authored Sep 13, 2022
1 parent 798c704 commit 429788b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/allocs_model.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export test_allocs_nlpmodels, test_allocs_nlsmodels, print_nlp_allocations
export test_allocs_nlpmodels, test_allocs_nlsmodels, test_zero_allocations, print_nlp_allocations

"""
test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclude = [])
Expand Down Expand Up @@ -388,3 +388,29 @@ function print_nlp_allocations(io, nlp::AbstractNLPModel, table::Dict; only_nonz
println(io, join(lines, "\n") * "\n")
return table
end

"""
test_zero_allocations(table::Dict, name::String = "Generic")
test_zero_allocations(nlp::AbstractNLPModel; kwargs...)
Test wether the result of `test_allocs_nlpmodels(nlp)` and `test_allocs_nlsmodels(nlp)` is 0.
"""
function test_zero_allocations(nlp::AbstractNLPModel; kwargs...)
table = test_allocs_nlpmodels(nlp; kwargs...)
return test_zero_allocations(table, get_name(nlp))
end

function test_zero_allocations(nlp::AbstractNLSModel; linear_api = linear_api, kwargs...)
table_nlp = test_allocs_nlpmodels(nlp; linear_api = linear_api, kwargs...)
table_nls = test_allocs_nlsmodels(nlp; kwargs...)
table = merge(table_nlp, table_nls)
return test_zero_allocations(table, get_name(nlp))
end

function test_zero_allocations(table::Dict, name::String = "Generic")
@testset "Test 0-allocations of NLPModel API for $name" begin
for k in keys(table)
isnan(table[k]) || @test table[k] == 0
end
end
end
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ map(
map(x -> eval(Symbol(x))(), setdiff(NLPModelsTest.nls_problems, ["LLS"])),
)

if v"1.7" <= VERSION
map(
nlp -> test_zero_allocations(nlp, linear_api = true),
map(x -> eval(Symbol(x))(), NLPModelsTest.nlp_problems),
)
test_zero_allocations(LLS(), linear_api = true, exclude = [hess])
map(
nlp -> test_zero_allocations(nlp, linear_api = true),
map(x -> eval(Symbol(x))(), setdiff(NLPModelsTest.nls_problems, ["LLS"])),
)
end

rmprocs()

0 comments on commit 429788b

Please sign in to comment.