Skip to content

Commit 31b1cea

Browse files
committed
tests
1 parent b108fab commit 31b1cea

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

test/DualTest.jl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
219219

220220
# Predicates #
221221
#------------#
222+
@testset "Predicates" begin
222223

223224
@test ForwardDiff.isconstant(zero(FDNUM))
224225
@test ForwardDiff.isconstant(one(FDNUM))
@@ -231,10 +232,13 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
231232
@test isequal(FDNUM, Dual{TestTag()}(PRIMAL, PARTIALS2)) == (N == 0)
232233
@test isequal(PRIMAL, PRIMAL2) == isequal(FDNUM, FDNUM2)
233234

234-
@test isequal(NESTED_FDNUM, Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS2), NESTED_PARTIALS2)) == (N == M == 0)
235-
# @test isequal(PRIMAL, PRIMAL2) == isequal(NESTED_FDNUM, NESTED_FDNUM2)
235+
# Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
236+
# and FDNUM2 has everything with a 2, and all random numbers nonzero.
237+
# M is the length of M_PARTIALS, which affects:
238+
# NESTED_FDNUM = Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS), NESTED_PARTIALS)
236239

237-
@info "Predicates" N M V PRIMAL PRIMAL2 NESTED_FDNUM NESTED_FDNUM2
240+
@test isequal(NESTED_FDNUM, Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS2), NESTED_PARTIALS2)) == (N == M == 0)
241+
@test isequal(NESTED_FDNUM, NESTED_FDNUM2) == isequal(PRIMAL, PRIMAL2) && (N == M == 0)
238242

239243
@test (FDNUM == Dual{TestTag()}(PRIMAL, PARTIALS2)) == (N == 0)
240244
@test (PRIMAL == PRIMAL2) == (FDNUM == FDNUM2)
@@ -319,6 +323,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
319323
@test isodd(Dual{TestTag()}(Dual{TestTag()}(1)))
320324
@test !(isodd(Dual{TestTag()}(Dual{TestTag()}(2))))
321325

326+
end
322327
########################
323328
# Promotion/Conversion #
324329
########################
@@ -392,8 +397,11 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
392397

393398
# Division #
394399
#----------#
400+
@testset "Division" begin
395401

396402
if M > 0 && N > 0
403+
# Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
404+
# all random numbers nonzero, and FDNUM2 another draw. M only affects NESTED_FDNUM.
397405
@test Dual{1}(FDNUM) / Dual{1}(PRIMAL) === Dual{1}(FDNUM / PRIMAL)
398406
@test Dual{1}(PRIMAL) / Dual{1}(FDNUM) === Dual{1}(PRIMAL / FDNUM)
399407
@test_broken Dual{1}(FDNUM) / FDNUM2 === Dual{1}(FDNUM / FDNUM2)
@@ -410,8 +418,12 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
410418
@test dual_isapprox(NESTED_FDNUM / PRIMAL, Dual{TestTag()}(value(NESTED_FDNUM) / PRIMAL, partials(NESTED_FDNUM) / PRIMAL))
411419
@test dual_isapprox(PRIMAL / NESTED_FDNUM, Dual{TestTag()}(PRIMAL / value(NESTED_FDNUM), (-(PRIMAL) / value(NESTED_FDNUM)^2) * partials(NESTED_FDNUM)))
412420

421+
end
422+
413423
# Exponentiation #
414424
#----------------#
425+
@testset "Exponentiation" begin
426+
415427
# If V == Int, the LHS terms are Int's. Large inputs cause integer overflow
416428
# within the generic fallback of `isapprox`, resulting in a DomainError.
417429
# Promote to Float64 to avoid issues.
@@ -425,6 +437,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
425437

426438
@test partials(NaNMath.pow(Dual{TestTag()}(-2.0, 1.0), Dual{TestTag()}(2.0, 0.0)), 1) == -4.0
427439

440+
end
428441
###################################
429442
# General Mathematical Operations #
430443
###################################
@@ -517,6 +530,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
517530

518531
# Special Cases #
519532
#---------------#
533+
@testset "Special Cases" begin
520534

521535
@test_broken dual_isapprox(hypot(FDNUM, FDNUM2, FDNUM), sqrt(2*(FDNUM^2) + FDNUM2^2))
522536
@test_broken dual_isapprox(hypot(FDNUM, FDNUM2, FDNUM3), sqrt(FDNUM^2 + FDNUM2^2 + FDNUM3^2))
@@ -541,8 +555,14 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
541555
@test dual_isapprox(f(FDNUM, PRIMAL2, PRIMAL3), Dual{TestTag()}(f(PRIMAL, PRIMAL2, PRIMAL3), PRIMAL2*PARTIALS))
542556
@test dual_isapprox(f(PRIMAL, PRIMAL2, FDNUM3), Dual{TestTag()}(f(PRIMAL, PRIMAL2, PRIMAL3), PARTIALS3))
543557
end
558+
559+
end # testset
544560
end
545561

562+
#############
563+
# bug fixes #
564+
#############
565+
546566
@testset "Exponentiation of zero" begin
547567
x0 = 0.0
548568
x1 = Dual{:t1}(x0, 1.0)

test/GradientTest.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ x = [0.1, 0.2, 0.3]
2020
v = f(x)
2121
g = [-9.4, 15.6, 52.0]
2222

23-
for c in (1, 2, 3), tag in (nothing, Tag(f, eltype(x)))
23+
@testset "Rosenbrock, chunk size = $c and tag = $(repr(tag))" for c in (1, 2, 3), tag in (nothing, Tag(f, eltype(x)))
2424
println(" ...running hardcoded test with chunk size = $c and tag = $(repr(tag))")
2525
cfg = ForwardDiff.GradientConfig(f, x, ForwardDiff.Chunk{c}(), tag)
2626

@@ -56,7 +56,7 @@ cfgx = ForwardDiff.GradientConfig(sin, x)
5656
# test vs. Calculus.jl #
5757
########################
5858

59-
for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
59+
@testset "$f" for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
6060
v = f(X)
6161
g = ForwardDiff.gradient(f, X)
6262
@test isapprox(g, Calculus.gradient(f, X), atol=FINITEDIFF_ERROR)

test/runtests.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
using ForwardDiff, Test
22

33
@testset "ForwardDiff" begin
4+
t0 = time()
45
@testset "Partials" begin
5-
println("Testing Partials...")
6+
println("##### Testing Partials...")
67
t = @elapsed include("PartialsTest.jl")
7-
println("done (took $t seconds).")
8+
println("##### done (took $t seconds).")
89
end
910
@testset "Dual" begin
10-
println("Testing Dual...")
11+
println("##### Testing Dual...")
1112
t = @elapsed include("DualTest.jl")
12-
println("done (took $t seconds).")
13+
println("##### done (took $t seconds).")
1314
end
14-
@testset "Derivative" begin
15-
println("Testing derivative functionality...")
15+
@testset "Derivatives" begin
16+
println("##### Testing derivative functionality...")
1617
t = @elapsed include("DerivativeTest.jl")
17-
println("done (took $t seconds).")
18+
println("##### done (took $t seconds).")
1819
end
19-
@testset "Gradient" begin
20-
println("Testing gradient functionality...")
20+
@testset "Gradients" begin
21+
println("##### Testing gradient functionality...")
2122
t = @elapsed include("GradientTest.jl")
22-
println("done (took $t seconds).")
23+
println("##### done (took $t seconds).")
2324
end
24-
@testset "Jacobian" begin
25-
println("Testing jacobian functionality...")
25+
@testset "Jacobians" begin
26+
println("##### Testing jacobian functionality...")
2627
t = @elapsed include("JacobianTest.jl")
27-
println("done (took $t seconds).")
28+
println("##### done (took $t seconds).")
2829
end
29-
@testset "Hessian" begin
30-
println("Testing hessian functionality...")
30+
@testset "Hessians" begin
31+
println("##### Testing hessian functionality...")
3132
t = @elapsed include("HessianTest.jl")
32-
println("done (took $t seconds).")
33+
println("##### done (took $t seconds).")
3334
end
34-
@testset "Perturbation confusion" begin
35-
println("Testing perturbation confusion functionality...")
35+
@testset "Perturbation Confusion" begin
36+
println("##### Testing perturbation confusion functionality...")
3637
t = @elapsed include("ConfusionTest.jl")
37-
println("done (took $t seconds).")
38+
println("##### done (took $t seconds).")
3839
end
3940
@testset "Miscellaneous" begin
40-
println("Testing miscellaneous functionality...")
41+
println("##### Testing miscellaneous functionality...")
4142
t = @elapsed include("MiscTest.jl")
42-
println("done (took $t seconds).")
43+
println("##### done (took $t seconds).")
4344
end
4445
if VERSION >= v"1.5-"
4546
@testset "Allocations" begin
46-
println("Testing allocations...")
47+
println("##### Testing allocations...")
4748
t = @elapsed include("AllocationsTest.jl")
48-
println("done (took $t seconds).")
49+
println("##### done (took $t seconds).")
4950
end
5051
end
52+
println("##### Running all ForwardDiff tests took $(t0 - time()) seconds.")
5153
end

0 commit comments

Comments
 (0)