From ca7549d12391f5810562c59a12024320e69f4590 Mon Sep 17 00:00:00 2001 From: adrhill Date: Mon, 2 Sep 2024 19:08:12 +0200 Subject: [PATCH] Add tests --- test/Project.toml | 1 + test/classification.jl | 10 ++++-- test/ext/test_NaNMath.jl | 78 ++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 2 +- 4 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 test/ext/test_NaNMath.jl diff --git a/test/Project.toml b/test/Project.toml index d2d23b8..e5ec5cf 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -17,6 +17,7 @@ LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688" NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6" NLPModelsJuMP = "792afdf1-32c1-5681-94e0-d7bf7a5df49e" NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd" +NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" OptimizationProblems = "5049e819-d29b-5fba-b941-0eee7e64c1c6" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" PkgJogger = "10150987-6cc1-4b76-abee-b1c1cbd91c01" diff --git a/test/classification.jl b/test/classification.jl index b53759a..57ea82b 100644 --- a/test/classification.jl +++ b/test/classification.jl @@ -30,6 +30,7 @@ using SparseConnectivityTracer: # testing using SpecialFunctions: SpecialFunctions using NNlib: NNlib using LogExpFunctions: LogExpFunctions +using NaNMath: NaNMath using Test using ForwardDiff: derivative, gradient, hessian @@ -50,6 +51,9 @@ random_input(::typeof(LogExpFunctions.log2mexp)) = -rand() # log2mexp(x) is def random_input(::typeof(LogExpFunctions.logitexp)) = -rand() # logitexp(x) is defined for x < 0 random_input(::typeof(LogExpFunctions.logit1mexp)) = -rand() # logit1mexp(x) is defined for x < 0 +# NaNMath.jl +random_input(::typeof(NaNMath.acosh)) = 1 + rand() # Range: [1, ∞) + random_first_input(op) = random_input(op) random_second_input(op) = random_input(op) @@ -97,7 +101,7 @@ function correct_classification_1_to_1(op, x; atol) end @testset verbose = true "1-to-1" begin - @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions) + @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions, NaNMath) @testset "$op" for op in test_operators_1_to_1(Val(Symbol(m))) @test all( correct_classification_1_to_1(op, random_input(op); atol=DEFAULT_ATOL) for @@ -140,7 +144,7 @@ function correct_classification_2_to_1(op, x, y; atol) end @testset verbose = true "2-to-1" begin - @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions) + @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions, NaNMath) @testset "$op" for op in test_operators_2_to_1(Val(Symbol(m))) @test all( correct_classification_2_to_1( @@ -180,7 +184,7 @@ function correct_classification_1_to_2(op, x; atol) end @testset verbose = true "1-to-2" begin - @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions) + @testset "$m" for m in (Base, SpecialFunctions, NNlib, LogExpFunctions, NaNMath) @testset "$op" for op in test_operators_1_to_2(Val(Symbol(m))) @test all( correct_classification_1_to_2(op, random_input(op); atol=DEFAULT_ATOL) for diff --git a/test/ext/test_NaNMath.jl b/test/ext/test_NaNMath.jl new file mode 100644 index 0000000..1504142 --- /dev/null +++ b/test/ext/test_NaNMath.jl @@ -0,0 +1,78 @@ +using SparseConnectivityTracer +using NaNMath +using Test + +# Load definitions of GRADIENT_TRACERS, GRADIENT_PATTERNS, HESSIAN_TRACERS and HESSIAN_PATTERNS +include("../tracers_definitions.jl") + +nan_1_to_1 = ( + NaNMath.sqrt, + NaNMath.sin, + NaNMath.cos, + NaNMath.tan, + NaNMath.asin, + NaNMath.acos, + NaNMath.acosh, + NaNMath.atanh, + NaNMath.log, + NaNMath.log2, + NaNMath.log10, + NaNMath.log1p, + NaNMath.lgamma, +) + +@testset "Jacobian Global" begin + method = TracerSparsityDetector() + J(f, x) = jacobian_sparsity(f, x, method) + + @testset "1-to-1 functions" begin + @testset "$f" for f in nan_1_to_1 + @test J(x -> f(x[1]), rand(2)) == [1 0] + end + end + @testset "2-to-1 functions" begin + @test J(x -> NaNMath.pow(x[1], x[2]), rand(3)) == [1 1 0] + @test J(x -> NaNMath.max(x[1], x[2]), rand(3)) == [1 1 0] + @test J(x -> NaNMath.min(x[1], x[2]), rand(3)) == [1 1 0] + end +end + +@testset "Jacobian Local" begin + method = TracerLocalSparsityDetector() + J(f, x) = jacobian_sparsity(f, x, method) + + @testset "2-to-1 functions" begin + @test J(x -> NaNMath.max(x[1], x[2]), [1.0, 2.0, 0.0]) == [0 1 0] + @test J(x -> NaNMath.max(x[1], x[2]), [2.0, 1.0, 0.0]) == [1 0 0] + @test J(x -> NaNMath.min(x[1], x[2]), [1.0, 2.0, 0.0]) == [1 0 0] + @test J(x -> NaNMath.min(x[1], x[2]), [2.0, 1.0, 0.0]) == [0 1 0] + end +end + +@testset "Hessian Global" begin + method = TracerSparsityDetector() + H(f, x) = hessian_sparsity(f, x, method) + + @testset "1-to-1 functions" begin + @testset "$f" for f in nan_1_to_1 + @test H(x -> f(x[1]), rand(2)) == [1 0; 0 0] + end + end + @testset "2-to-1 functions" begin + @test H(x -> NaNMath.pow(x[1], x[2]), rand(3)) == [1 1 0; 1 1 0; 0 0 0] + @test H(x -> NaNMath.max(x[1], x[2]), rand(3)) == zeros(Bool, 3, 3) + @test H(x -> NaNMath.min(x[1], x[2]), rand(3)) == zeros(Bool, 3, 3) + end +end + +@testset "Hessian Local" begin + method = TracerLocalSparsityDetector() + H(f, x) = hessian_sparsity(f, x, method) + + @testset "2-to-1 functions" begin + @test H(x -> NaNMath.max(x[1], x[2]), [1.0, 2.0, 0.0]) == zeros(Bool, 3, 3) + @test H(x -> NaNMath.max(x[1], x[2]), [2.0, 1.0, 0.0]) == zeros(Bool, 3, 3) + @test H(x -> NaNMath.min(x[1], x[2]), [1.0, 2.0, 0.0]) == zeros(Bool, 3, 3) + @test H(x -> NaNMath.min(x[1], x[2]), [2.0, 1.0, 0.0]) == zeros(Bool, 3, 3) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 89f2962..04e253d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -94,7 +94,7 @@ GROUP = get(ENV, "JULIA_SCT_TEST_GROUP", "Core") if GROUP in ("Core", "All") @info "Testing package extensions..." @testset verbose = true "Package extensions" begin - for ext in (:NNlib, :SpecialFunctions, :LogExpFunctions) + for ext in (:NNlib, :SpecialFunctions, :LogExpFunctions, :NaNMath) @testset "$ext" begin @info "...$ext" include("ext/test_$ext.jl")