From 30fec218cd7323ab7216ff3a835c367a39bd65af Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 25 Nov 2024 13:03:05 +0100 Subject: [PATCH] Add tests --- test/test_gradient.jl | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/test_gradient.jl b/test/test_gradient.jl index 2c515e1..87dc127 100644 --- a/test/test_gradient.jl +++ b/test/test_gradient.jl @@ -123,6 +123,36 @@ J(f, x) = jacobian_sparsity(f, x, detector) x -> x[1] > x[2] ? x[3] : x[4], [1.0, 2.0, 3.0, 4.0] ) == [0 0 1 1;] end + + @testset "Output of type Vector{Any}" begin + function f(x::AbstractVector) + n = length(x) + ret = [] # return type will be Vector{Any} + for i in 1:(n - 1) + append!( + ret, + abs2(x[i + 1]) - abs2(x[i]) + abs2(x[n - i]) - abs2(x[n - i + 1]), + ) + end + return ret + end + x = [ + 0.263914 + 0.605532 + 1.281598 + 1.413663 + 0.178133 + -1.705427 + ] + @test J(f, x) == [ + 1 1 0 0 1 1 + 0 1 1 1 1 0 + 0 0 1 1 0 0 + 0 1 1 1 1 0 + 1 1 0 0 1 1 + ] + end + yield() end end @@ -248,6 +278,35 @@ end @test J(x -> log(det(x)), [1.0 -1.0; 2.0 2.0]) == [1 1 1 1] @test J(x -> dot(x[1:2], x[4:5]), [0, 1, 0, 1, 0]) == [1 0 0 0 1] end + @testset "Output of type Vector{Any}" begin + function f(x::AbstractVector) + n = length(x) + ret = [] # return type will be Vector{Any} + for i in 1:(n - 1) + append!( + ret, + abs2(x[i + 1]) - abs2(x[i]) + abs2(x[n - i]) - abs2(x[n - i + 1]), + ) + end + return ret + end + x = [ + 0.263914 + 0.605532 + 1.281598 + 1.413663 + 0.178133 + -1.705427 + ] + @test J(f, x) == [ + 1 1 0 0 1 1 + 0 1 1 1 1 0 + 0 0 1 1 0 0 + 0 1 1 1 1 0 + 1 1 0 0 1 1 + ] + end + yield() end end