Skip to content

Commit e8e8201

Browse files
committed
tests: convert println() into @testset
reduces the amount of noise in the output
1 parent 8608f0b commit e8e8201

File tree

7 files changed

+70
-53
lines changed

7 files changed

+70
-53
lines changed

test/DerivativeTest.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Random.seed!(1)
1717

1818
const x = 1
1919

20-
for f in DiffTests.NUMBER_TO_NUMBER_FUNCS
21-
println(" ...testing $f")
20+
@testset "Derivative test vs Calculus.jl" begin
21+
22+
@testset "$f" for f in DiffTests.NUMBER_TO_NUMBER_FUNCS
2223
v = f(x)
2324
d = ForwardDiff.derivative(f, x)
2425
@test isapprox(d, Calculus.derivative(f, x), atol=FINITEDIFF_ERROR)
@@ -29,8 +30,7 @@ for f in DiffTests.NUMBER_TO_NUMBER_FUNCS
2930
@test isapprox(DiffResults.derivative(out), d)
3031
end
3132

32-
for f in DiffTests.NUMBER_TO_ARRAY_FUNCS
33-
println(" ...testing $f")
33+
@testset "$f" for f in DiffTests.NUMBER_TO_ARRAY_FUNCS
3434
v = f(x)
3535
d = ForwardDiff.derivative(f, x)
3636

@@ -47,8 +47,7 @@ for f in DiffTests.NUMBER_TO_ARRAY_FUNCS
4747
@test isapprox(DiffResults.derivative(out), d)
4848
end
4949

50-
for f! in DiffTests.INPLACE_NUMBER_TO_ARRAY_FUNCS
51-
println(" ...testing $f!")
50+
@testset "$f!" for f! in DiffTests.INPLACE_NUMBER_TO_ARRAY_FUNCS
5251
m, n = 3, 2
5352
y = fill(0.0, m, n)
5453
f = x -> (tmp = similar(y, promote_type(eltype(y), typeof(x)), m, n); f!(tmp, x); tmp)
@@ -89,6 +88,8 @@ for f! in DiffTests.INPLACE_NUMBER_TO_ARRAY_FUNCS
8988
@test isapprox(DiffResults.derivative(out), d)
9089
end
9190

91+
end
92+
9293
@testset "exponential function at base zero" begin
9394
@test (x -> ForwardDiff.derivative(y -> x^y, -0.5))(0.0) === -Inf
9495
@test (x -> ForwardDiff.derivative(y -> x^y, 0.0))(0.0) === -Inf

test/DualTest.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ dual_isapprox(a::Dual{T,T1,T2}, b::Dual{T3,T4,T5}) where {T,T1,T2,T3,T4,T5} = er
2727
ForwardDiff.:(::Type{TestTag()}, ::Int) = true
2828
ForwardDiff.:(::Int, ::Type{TestTag()}) = false
2929

30-
for N in (0,3), M in (0,4), V in (Int, Float32)
31-
println(" ...testing Dual{TestTag(),$V,$N} and Dual{TestTag(),Dual{TestTag(),$V,$M},$N}")
30+
@testset "Dual{TestTag(),$V,$N} and Dual{TestTag(),Dual{TestTag(),$V,$M},$N}" for N in (0,3), M in (0,4), V in (Int, Float32)
3231

3332
PARTIALS = Partials{N,V}(ntuple(n -> intrand(V), N))
3433
PRIMAL = intrand(V)
@@ -439,13 +438,12 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
439438
@test abs(NESTED_FDNUM) === NESTED_FDNUM
440439

441440
if V != Int
442-
for (M, f, arity) in DiffRules.diffrules(filter_modules = nothing)
441+
@testset "auto-testing $(M).$(f) with $arity arguments" for (M, f, arity) in DiffRules.diffrules(filter_modules = nothing)
443442
if f in (:/, :rem2pi)
444443
continue # Skip these rules
445444
elseif !(isdefined(@__MODULE__, M) && isdefined(getfield(@__MODULE__, M), f))
446445
continue # Skip rules for methods not defined in the current scope
447446
end
448-
println(" ...auto-testing $(M).$(f) with $arity arguments")
449447
if arity == 1
450448
deriv = DiffRules.diffrule(M, f, :x)
451449
modifier = if in(f, (:asec, :acsc, :asecd, :acscd, :acosh, :acoth))

test/GradientTest.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ include(joinpath(dirname(@__FILE__), "utils.jl"))
1414
# hardcoded test #
1515
##################
1616

17+
@testset "hardcoded tests" begin
18+
1719
f = DiffTests.rosenbrock_1
1820
x = [0.1, 0.2, 0.3]
1921
v = f(x)
2022
g = [-9.4, 15.6, 52.0]
2123

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

2628
@test eltype(cfg) == Dual{typeof(tag), eltype(x), c}
@@ -50,17 +52,19 @@ cfgx = ForwardDiff.GradientConfig(sin, x)
5052
@test_throws ForwardDiff.InvalidTagException ForwardDiff.gradient(f, x, cfgx)
5153
@test ForwardDiff.gradient(f, x, cfgx, Val{false}()) == ForwardDiff.gradient(f,x)
5254

55+
end
5356

5457
########################
5558
# test vs. Calculus.jl #
5659
########################
60+
@testset "Comparison vs Calculus.jl" begin
5761

58-
for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
62+
@testset "$f" for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
5963
v = f(X)
6064
g = ForwardDiff.gradient(f, X)
6165
@test isapprox(g, Calculus.gradient(f, X), atol=FINITEDIFF_ERROR)
62-
for c in CHUNK_SIZES, tag in (nothing, Tag(f, eltype(x)))
63-
println(" ...testing $f with chunk size = $c and tag = $(repr(tag))")
66+
67+
@testset "chunk size = $c and tag = $(repr(tag))" for c in CHUNK_SIZES, tag in (nothing, Tag(f, eltype(X)))
6468
cfg = ForwardDiff.GradientConfig(f, X, ForwardDiff.Chunk{c}(), tag)
6569

6670
out = ForwardDiff.gradient(f, X, cfg)
@@ -77,15 +81,15 @@ for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
7781
end
7882
end
7983

84+
end
85+
8086
##########################################
8187
# test specialized StaticArray codepaths #
8288
##########################################
8389

84-
println(" ...testing specialized StaticArray codepaths")
85-
8690
x = rand(3, 3)
8791

88-
for T in (StaticArrays.SArray, StaticArrays.MArray)
92+
@testset "Specialized $T codepaths" for T in (StaticArrays.SArray, StaticArrays.MArray)
8993
sx = T{Tuple{3,3}}(x)
9094

9195
cfg = ForwardDiff.GradientConfig(nothing, x)

test/HessianTest.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ h = [-66.0 -40.0 0.0;
2222
-40.0 130.0 -80.0;
2323
0.0 -80.0 200.0]
2424

25-
for c in HESSIAN_CHUNK_SIZES, tag in (nothing, Tag((f,ForwardDiff.gradient), eltype(x)))
26-
println(" ...running hardcoded test with chunk size = $c and tag = $(repr(tag))")
25+
@testset "hardcoded" begin
26+
27+
@testset "chunk size = $c and tag = $(repr(tag))" for c in HESSIAN_CHUNK_SIZES, tag in (nothing, Tag((f,ForwardDiff.gradient), eltype(x)))
2728
cfg = ForwardDiff.HessianConfig(f, x, ForwardDiff.Chunk{c}(), tag)
2829
resultcfg = ForwardDiff.HessianConfig(f, DiffResults.HessianResult(x), x, ForwardDiff.Chunk{c}(), tag)
2930

@@ -53,6 +54,8 @@ for c in HESSIAN_CHUNK_SIZES, tag in (nothing, Tag((f,ForwardDiff.gradient), elt
5354
@test isapprox(DiffResults.hessian(out), h)
5455
end
5556

57+
end
58+
5659
cfgx = ForwardDiff.HessianConfig(sin, x)
5760
@test_throws ForwardDiff.InvalidTagException ForwardDiff.hessian(f, x, cfgx)
5861
@test ForwardDiff.hessian(f, x, cfgx, Val{false}()) == ForwardDiff.hessian(f,x)
@@ -62,14 +65,16 @@ cfgx = ForwardDiff.HessianConfig(sin, x)
6265
# test vs. Calculus.jl #
6366
########################
6467

65-
for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
68+
@testset "Comparison vs Calculus.jl" begin
69+
70+
@testset "$f" for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
6671
v = f(X)
6772
g = ForwardDiff.gradient(f, X)
6873
h = ForwardDiff.hessian(f, X)
6974
# finite difference approximation error is really bad for Hessians...
7075
@test isapprox(h, Calculus.hessian(f, X), atol=0.02)
71-
for c in HESSIAN_CHUNK_SIZES, tag in (nothing, Tag((f,ForwardDiff.gradient), eltype(x)))
72-
println(" ...testing $f with chunk size = $c and tag = $(repr(tag))")
76+
77+
@testset "chunk size = $c and tag = $(repr(tag))" for c in HESSIAN_CHUNK_SIZES, tag in (nothing, Tag((f,ForwardDiff.gradient), eltype(x)))
7378
cfg = ForwardDiff.HessianConfig(f, X, ForwardDiff.Chunk{c}(), tag)
7479
resultcfg = ForwardDiff.HessianConfig(f, DiffResults.HessianResult(X), X, ForwardDiff.Chunk{c}(), tag)
7580

@@ -88,14 +93,14 @@ for f in DiffTests.VECTOR_TO_NUMBER_FUNCS
8893
end
8994
end
9095

96+
end
97+
9198
##########################################
9299
# test specialized StaticArray codepaths #
93100
##########################################
94101

95-
println(" ...testing specialized StaticArray codepaths")
96-
97102
x = rand(3, 3)
98-
for T in (StaticArrays.SArray, StaticArrays.MArray)
103+
@testset "Specialized $T codepaths" for T in (StaticArrays.SArray, StaticArrays.MArray)
99104
sx = T{Tuple{3,3}}(x)
100105

101106
cfg = ForwardDiff.HessianConfig(nothing, x)

test/JacobianTest.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ include(joinpath(dirname(@__FILE__), "utils.jl"))
1515
# hardcoded test #
1616
##################
1717

18+
@testset "hardcoded" begin
19+
1820
f! = (y, x) -> begin
1921
y[1] = x[1] * x[2]
2022
y[1] *= sin(x[3]^2)
@@ -31,9 +33,8 @@ j = [0.8242369704835132 0.4121184852417566 -10.933563142616123
3133
0.169076696546684 0.084538348273342 -2.299173530851733
3234
0.0 0.0 1.0]
3335

34-
for c in (1, 2, 3), tags in ((nothing, nothing),
36+
@testset "chunk size = $c and tag = $(repr(tags))" for c in (1, 2, 3), tags in ((nothing, nothing),
3537
(Tag(f, eltype(x)), Tag(f!, eltype(x))))
36-
println(" ...running hardcoded test with chunk size = $c and tag = $(repr(tags))")
3738
cfg = JacobianConfig(f, x, ForwardDiff.Chunk{c}(), tags[1])
3839
ycfg = JacobianConfig(f!, fill(0.0, 4), x, ForwardDiff.Chunk{c}(), tags[2])
3940

@@ -95,19 +96,20 @@ cfgx = ForwardDiff.JacobianConfig(sin, x)
9596
@test_throws ForwardDiff.InvalidTagException ForwardDiff.jacobian(f, x, cfgx)
9697
@test ForwardDiff.jacobian(f, x, cfgx, Val{false}()) == ForwardDiff.jacobian(f,x)
9798

99+
end
100+
98101
########################
99102
# test vs. Calculus.jl #
100103
########################
101104

102-
for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
105+
@testset "Comparison vs Calculus.jl" begin
106+
107+
@testset "$f" for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
103108
v = f(X)
104109
j = ForwardDiff.jacobian(f, X)
105110
@test isapprox(j, Calculus.jacobian(x -> vec(f(x)), X, :forward), atol=1.3FINITEDIFF_ERROR)
106-
for c in CHUNK_SIZES, tag in (nothing, Tag)
107-
if tag == Tag
108-
tag = Tag(f, eltype(X))
109-
end
110-
println(" ...testing $f with chunk size = $c and tag = $(repr(tag))")
111+
112+
@testset "chunk size = $c and tag = $(repr(tag))" for c in CHUNK_SIZES, tag in (nothing, Tag(f, eltype(X)))
111113
cfg = JacobianConfig(f, X, ForwardDiff.Chunk{c}(), tag)
112114

113115
out = ForwardDiff.jacobian(f, X, cfg)
@@ -124,13 +126,13 @@ for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
124126
end
125127
end
126128

127-
for f! in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
129+
@testset "$f!" for f! in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
128130
v = fill!(similar(Y), 0.0)
129131
f!(v, X)
130132
j = ForwardDiff.jacobian(f!, fill!(similar(Y), 0.0), X)
131133
@test isapprox(j, Calculus.jacobian(x -> (y = fill!(similar(Y), 0.0); f!(y, x); vec(y)), X, :forward), atol=FINITEDIFF_ERROR)
132-
for c in CHUNK_SIZES, tag in (nothing, Tag(f!, eltype(X)))
133-
println(" ...testing $(f!) with chunk size = $c and tag = $(repr(tag))")
134+
135+
@testset "chunk size = $c and tag = $(repr(tag))" for c in CHUNK_SIZES, tag in (nothing, Tag(f!, eltype(X)))
134136
ycfg = JacobianConfig(f!, fill!(similar(Y), 0.0), X, ForwardDiff.Chunk{c}(), tag)
135137

136138
y = fill!(similar(Y), 0.0)
@@ -160,14 +162,14 @@ for f! in DiffTests.INPLACE_ARRAY_TO_ARRAY_FUNCS
160162
end
161163
end
162164

165+
end
166+
163167
##########################################
164168
# test specialized StaticArray codepaths #
165169
##########################################
166170

167-
println(" ...testing specialized StaticArray codepaths")
168-
169171
x = rand(3, 3)
170-
for T in (StaticArrays.SArray, StaticArrays.MArray)
172+
@testset "Specialized $T codepaths" for T in (StaticArrays.SArray, StaticArrays.MArray)
171173
sx = T{Tuple{3,3}}(x)
172174

173175
cfg = ForwardDiff.JacobianConfig(nothing, x)

test/PartialsTest.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ using ForwardDiff: Partials
77

88
samerng() = MersenneTwister(1)
99

10-
for N in (0, 3), T in (Int, Float32, Float64)
11-
println(" ...testing Partials{$N,$T}")
12-
10+
@testset "Partials{$N,$T}" for N in (0, 3), T in (Int, Float32, Float64)
1311
VALUES = (rand(T,N)...,)
1412
PARTIALS = Partials{N,T}(VALUES)
1513

test/runtests.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
using ForwardDiff
1+
using Test, ForwardDiff
22

3-
println("Testing Partials...")
3+
@testset "Partials" begin
44
t = @elapsed include("PartialsTest.jl")
55
println("done (took $t seconds).")
6+
end
67

7-
println("Testing Dual...")
8+
@testset "Dual" begin
89
t = @elapsed include("DualTest.jl")
910
println("done (took $t seconds).")
11+
end
1012

11-
println("Testing derivative functionality...")
13+
@testset "Derivative" begin
1214
t = @elapsed include("DerivativeTest.jl")
1315
println("done (took $t seconds).")
16+
end
1417

15-
println("Testing gradient functionality...")
18+
@testset "Gradient" begin
1619
t = @elapsed include("GradientTest.jl")
1720
println("done (took $t seconds).")
21+
end
1822

19-
println("Testing jacobian functionality...")
23+
@testset "Jacobian" begin
2024
t = @elapsed include("JacobianTest.jl")
2125
println("done (took $t seconds).")
26+
end
2227

23-
println("Testing hessian functionality...")
28+
@testset "Hessian" begin
2429
t = @elapsed include("HessianTest.jl")
2530
println("done (took $t seconds).")
31+
end
2632

27-
println("Testing perturbation confusion functionality...")
33+
@testset "Perturbation confusion" begin
2834
t = @elapsed include("ConfusionTest.jl")
2935
println("done (took $t seconds).")
36+
end
3037

31-
println("Testing miscellaneous functionality...")
38+
@testset "Miscellaneous" begin
3239
t = @elapsed include("MiscTest.jl")
3340
println("done (took $t seconds).")
41+
end
3442

3543
if VERSION >= v"1.5-"
36-
println("Testing allocations...")
44+
@testset "Allocations" begin
3745
t = @elapsed include("AllocationsTest.jl")
3846
println("done (took $t seconds).")
47+
end
3948
end

0 commit comments

Comments
 (0)