Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill committed Sep 2, 2024
1 parent a392de3 commit ca7549d
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions test/classification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions test/ext/test_NaNMath.jl
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ca7549d

Please sign in to comment.