diff --git a/src/allocs_model.jl b/src/allocs_model.jl index 59e59c4..a565c26 100644 --- a/src/allocs_model.jl +++ b/src/allocs_model.jl @@ -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 = []) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index f3e7efc..6f005ca 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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()