Skip to content

Commit 429788b

Browse files
authored
Add a function to test 0-allocations (#88)
* add function to test 0-allocations * skip julia 1.6 in test 0
1 parent 798c704 commit 429788b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/allocs_model.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export test_allocs_nlpmodels, test_allocs_nlsmodels, print_nlp_allocations
1+
export test_allocs_nlpmodels, test_allocs_nlsmodels, test_zero_allocations, print_nlp_allocations
22

33
"""
44
test_allocs_nlpmodels(nlp::AbstractNLPModel; linear_api = false, exclude = [])
@@ -388,3 +388,29 @@ function print_nlp_allocations(io, nlp::AbstractNLPModel, table::Dict; only_nonz
388388
println(io, join(lines, "\n") * "\n")
389389
return table
390390
end
391+
392+
"""
393+
test_zero_allocations(table::Dict, name::String = "Generic")
394+
test_zero_allocations(nlp::AbstractNLPModel; kwargs...)
395+
396+
Test wether the result of `test_allocs_nlpmodels(nlp)` and `test_allocs_nlsmodels(nlp)` is 0.
397+
"""
398+
function test_zero_allocations(nlp::AbstractNLPModel; kwargs...)
399+
table = test_allocs_nlpmodels(nlp; kwargs...)
400+
return test_zero_allocations(table, get_name(nlp))
401+
end
402+
403+
function test_zero_allocations(nlp::AbstractNLSModel; linear_api = linear_api, kwargs...)
404+
table_nlp = test_allocs_nlpmodels(nlp; linear_api = linear_api, kwargs...)
405+
table_nls = test_allocs_nlsmodels(nlp; kwargs...)
406+
table = merge(table_nlp, table_nls)
407+
return test_zero_allocations(table, get_name(nlp))
408+
end
409+
410+
function test_zero_allocations(table::Dict, name::String = "Generic")
411+
@testset "Test 0-allocations of NLPModel API for $name" begin
412+
for k in keys(table)
413+
isnan(table[k]) || @test table[k] == 0
414+
end
415+
end
416+
end

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ map(
6161
map(x -> eval(Symbol(x))(), setdiff(NLPModelsTest.nls_problems, ["LLS"])),
6262
)
6363

64+
if v"1.7" <= VERSION
65+
map(
66+
nlp -> test_zero_allocations(nlp, linear_api = true),
67+
map(x -> eval(Symbol(x))(), NLPModelsTest.nlp_problems),
68+
)
69+
test_zero_allocations(LLS(), linear_api = true, exclude = [hess])
70+
map(
71+
nlp -> test_zero_allocations(nlp, linear_api = true),
72+
map(x -> eval(Symbol(x))(), setdiff(NLPModelsTest.nls_problems, ["LLS"])),
73+
)
74+
end
75+
6476
rmprocs()

0 commit comments

Comments
 (0)