From fcca2f78fb6cf26d9a6df1a45e07821dae45c896 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Sun, 6 Oct 2024 21:56:09 -0400 Subject: [PATCH] test: bring over more tests --- .github/workflows/CI_SimpleNonlinearSolve.yml | 5 +++ .../test/core/allocation_tests.jl | 40 +++++++++++++++++++ .../test/core/matrix_resizing_tests.jl | 19 +++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/SimpleNonlinearSolve/test/core/allocation_tests.jl create mode 100644 lib/SimpleNonlinearSolve/test/core/matrix_resizing_tests.jl diff --git a/.github/workflows/CI_SimpleNonlinearSolve.yml b/.github/workflows/CI_SimpleNonlinearSolve.yml index 9854b6d99..1f8306a8c 100644 --- a/.github/workflows/CI_SimpleNonlinearSolve.yml +++ b/.github/workflows/CI_SimpleNonlinearSolve.yml @@ -32,6 +32,9 @@ jobs: - ubuntu-latest - macos-latest - windows-latest + group: + - core + - adjoint steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 @@ -60,6 +63,8 @@ jobs: Pkg.instantiate() Pkg.test(; coverage=true) shell: julia --color=yes --code-coverage=user --depwarn=yes --project=lib/SimpleNonlinearSolve {0} + env: + GROUP: ${{ matrix.group }} - uses: julia-actions/julia-processcoverage@v1 with: directories: lib/SimpleNonlinearSolve/src,lib/SimpleNonlinearSolve/ext diff --git a/lib/SimpleNonlinearSolve/test/core/allocation_tests.jl b/lib/SimpleNonlinearSolve/test/core/allocation_tests.jl new file mode 100644 index 000000000..5da872b71 --- /dev/null +++ b/lib/SimpleNonlinearSolve/test/core/allocation_tests.jl @@ -0,0 +1,40 @@ +@itesitem "Allocation Tests" tags=[:core] begin + using SimpleNonlinearSolve, StaticArrays, AllocCheck + + quadratic_f(u, p) = u .* u .- p + quadratic_f!(du, u, p) = (du .= u .* u .- p) + + @testset "$(nameof(typeof(alg)))" for alg in ( + SimpleNewtonRaphson(), + SimpleTrustRegion(), + SimpleTrustRegion(; nlsolve_update_rule = Val(true)), + SimpleBroyden(), + SimpleLimitedMemoryBroyden(), + SimpleKlement(), + SimpleHalley(), + SimpleBroyden(; linesearch = Val(true)), + SimpleLimitedMemoryBroyden(; linesearch = Val(true)) + ) + @check_allocs nlsolve(prob, alg) = SciMLBase.solve(prob, alg; abstol = 1e-9) + + nlprob_scalar = NonlinearProblem{false}(quadratic_f, 1.0, 2.0) + nlprob_sa = NonlinearProblem{false}(quadratic_f, @SVector[1.0, 1.0], 2.0) + + try + nlsolve(nlprob_scalar, alg) + @test true + catch e + @error e + @test false + end + + # ForwardDiff allocates for hessian since we don't propagate the chunksize + try + nlsolve(nlprob_sa, alg) + @test true + catch e + @error e + @test false broken = (alg isa SimpleHalley) + end + end +end diff --git a/lib/SimpleNonlinearSolve/test/core/matrix_resizing_tests.jl b/lib/SimpleNonlinearSolve/test/core/matrix_resizing_tests.jl new file mode 100644 index 000000000..17d9a6f42 --- /dev/null +++ b/lib/SimpleNonlinearSolve/test/core/matrix_resizing_tests.jl @@ -0,0 +1,19 @@ +@testitem "Matrix Resizing" tags=[:core] begin + ff(u, p) = u .* u .- p + u0 = ones(2, 3) + p = 2.0 + vecprob = NonlinearProblem(ff, vec(u0), p) + prob = NonlinearProblem(ff, u0, p) + + @testset "$(nameof(typeof(alg)))" for alg in ( + SimpleKlement(), + SimpleBroyden(), + SimpleNewtonRaphson(), + SimpleDFSane(), + SimpleLimitedMemoryBroyden(; threshold = Val(2)), + SimpleTrustRegion(), + SimpleTrustRegion(; nlsolve_update_rule = Val(true)) + ) + @test vec(solve(prob, alg).u) ≈ solve(vecprob, alg).u + end +end