diff --git a/lib/NonlinearProblemLibrary/Project.toml b/lib/NonlinearProblemLibrary/Project.toml index 8c8c9ba..7681d54 100644 --- a/lib/NonlinearProblemLibrary/Project.toml +++ b/lib/NonlinearProblemLibrary/Project.toml @@ -7,13 +7,17 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462" [compat] +AllocCheck = "0.2" Aqua = "0.8" LinearAlgebra = "1.6" SciMLBase = "2" +Test = "1" julia = "1.10" [extras] +AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua"] +test = ["AllocCheck", "Aqua", "Test"] diff --git a/lib/NonlinearProblemLibrary/src/NonlinearProblemLibrary.jl b/lib/NonlinearProblemLibrary/src/NonlinearProblemLibrary.jl index 7b6cdf1..cf19775 100644 --- a/lib/NonlinearProblemLibrary/src/NonlinearProblemLibrary.jl +++ b/lib/NonlinearProblemLibrary/src/NonlinearProblemLibrary.jl @@ -429,16 +429,17 @@ p22_dict = Dict("n" => n, "start" => x_start, "sol" => x_sol, # ------------------------------------- Problem 23 ----------------------------------------- @inbounds function p23_f!(out, x, p = nothing) + n = length(x) c = 0.9 - out[1:n] = x[1:n] - μ = zeros(n) - for i in 1:n - μ[i] = (2 * i) / (2 * n) + @simd for i in 1:n + out[i] = x[i] end for i in 1:n + μi = i / n # μ[i] = (2*i)/(2*n) = i/n s = 0.0 for j in 1:n - s = s + (μ[i] * x[j]) / (μ[i] + μ[j]) + μj = j / n + s = s + (μi * x[j]) / (μi + μj) end term = 1.0 - c * s / (2 * n) out[i] -= 1.0 / term diff --git a/lib/NonlinearProblemLibrary/test/runtests.jl b/lib/NonlinearProblemLibrary/test/runtests.jl index dbded51..9fe5dbc 100644 --- a/lib/NonlinearProblemLibrary/test/runtests.jl +++ b/lib/NonlinearProblemLibrary/test/runtests.jl @@ -1,8 +1,45 @@ # The test is simply that all of the examples build! using NonlinearProblemLibrary +using Test # Check that there are no undefined exports, stale dependencies, etc. # Ambiguity checks are disabled since tests fail due to ambiguities # in dependencies using Aqua Aqua.test_all(NonlinearProblemLibrary; ambiguities = false) + +# Allocation tests - ensure key functions don't allocate +if get(ENV, "GROUP", "all") == "all" || get(ENV, "GROUP", "all") == "nopre" + @testset "Allocation Tests" begin + using AllocCheck + + # Test p23_f! (Chandrasekhar function) - the fixed function + @testset "p23_f! zero allocations" begin + x = ones(10) + out = zeros(10) + # Warmup + NonlinearProblemLibrary.p23_f!(out, x) + # Test + allocs = @allocated NonlinearProblemLibrary.p23_f!(out, x) + @test allocs == 0 + end + + # Test other key functions + @testset "p1_f! zero allocations" begin + x = ones(10) + x[1] = -1.2 + out = zeros(10) + NonlinearProblemLibrary.p1_f!(out, x) + allocs = @allocated NonlinearProblemLibrary.p1_f!(out, x) + @test allocs == 0 + end + + @testset "p2_f! zero allocations" begin + x = [3.0, -1.0, 0.0, 1.0] + out = zeros(4) + NonlinearProblemLibrary.p2_f!(out, x) + allocs = @allocated NonlinearProblemLibrary.p2_f!(out, x) + @test allocs == 0 + end + end +end