Skip to content

Commit 734739c

Browse files
committed
add OptionsTests
1 parent d065a44 commit 734739c

File tree

9 files changed

+102
-34
lines changed

9 files changed

+102
-34
lines changed

src/api/options.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ immutable HessianOptions{G<:Options,J<:Options} <: AbstractOptions
5151
jacobian_options::J
5252
end
5353

54-
HessianOptions(x, gtp::Tape = Tape(), jtp::Tape = Tape()) = HessianOptions(x, eltype(x), gtp, jtp)
54+
HessianOptions(x::AbstractArray, gtp::Tape = Tape(), jtp::Tape = Tape()) = HessianOptions(x, eltype(x), gtp, jtp)
5555

56-
function HessianOptions{A}(x, ::Type{A}, gtp::Tape = Tape(), jtp::Tape = Tape())
56+
function HessianOptions{A}(x::AbstractArray, ::Type{A}, gtp::Tape = Tape(), jtp::Tape = Tape())
5757
jopts = Options(x, A, jtp)
5858
gopts = Options(jopts.state, gtp)
5959
return HessianOptions(gopts, jopts)
6060
end
6161

62-
function HessianOptions(y, x, gtp::Tape = Tape(), jtp::Tape = Tape())
62+
function HessianOptions(y::AbstractArray, x::AbstractArray, gtp::Tape = Tape(), jtp::Tape = Tape())
6363
jopts = Options(y, x, jtp)
6464
gopts = Options(jopts.state[2], gtp)
6565
return HessianOptions(gopts, jopts)

test/TapeTests.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
module TapeTests
22

33
using ReverseDiffPrototype, Base.Test
4-
using ReverseDiffPrototype: TapeNode, Tape, Tracked
5-
6-
const RDP = ReverseDiffPrototype
74

85
include("utils.jl")
96

test/TrackedTests.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
module TrackedTests
22

33
using ReverseDiffPrototype, Base.Test
4-
using ReverseDiffPrototype: Tape, Tracked, value, adjoint, tape, valtype, adjtype
5-
6-
const RDP = ReverseDiffPrototype
74

85
include("utils.jl")
96

@@ -12,8 +9,6 @@ tic()
129

1310
############################################################################################
1411

15-
tracked_is(a, b) = value(a) === value(b) && adjoint(a) === adjoint(b) && tape(a) === tape(b)
16-
1712
##########################
1813
# Constructors/Accessors #
1914
##########################

test/UtilsTests.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
module UtilsTests
22

33
using ReverseDiffPrototype, Base.Test
4-
using ReverseDiffPrototype: Tape, TapeNode, Tracked, track, track!, value, adjoint, tape
5-
6-
const RDP = ReverseDiffPrototype
74

85
include("utils.jl")
96

@@ -12,10 +9,6 @@ tic()
129

1310
############################################################################################
1411

15-
tracked_is(a, b) = value(a) === value(b) && adjoint(a) === adjoint(b) && tape(a) === tape(b)
16-
tracked_is(a::AbstractArray, b::AbstractArray) = all(map(tracked_is, a, b))
17-
tracked_is(a::Tuple, b::Tuple) = all(map(tracked_is, a, b))
18-
1912
################
2013
# track/track! #
2114
################

test/api/GradientTests.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module GradientTests
22

33
using DiffBase, ForwardDiff, ReverseDiffPrototype, Base.Test
44

5-
const RDP = ReverseDiffPrototype
6-
75
include("../utils.jl")
86

97
println("testing gradient/gradient!...")

test/api/HessianTests.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module HessianTests
22

33
using DiffBase, ForwardDiff, ReverseDiffPrototype, Base.Test
44

5-
const RDP = ReverseDiffPrototype
6-
75
include("../utils.jl")
86

97
println("testing hessian/hessian!...")
@@ -25,9 +23,9 @@ function test_unary_hessian(f, x)
2523

2624
result = DiffBase.HessianResult(x)
2725
RDP.hessian!(result, f, x)
28-
# @test_approx_eq_eps DiffBase.value(result) DiffBase.value(test) EPS
29-
# @test_approx_eq_eps DiffBase.gradient(result) DiffBase.gradient(test) EPS
30-
# @test_approx_eq_eps DiffBase.hessian(result) DiffBase.hessian(test) EPS
26+
@test_approx_eq_eps DiffBase.value(result) DiffBase.value(test) EPS
27+
@test_approx_eq_eps DiffBase.gradient(result) DiffBase.gradient(test) EPS
28+
@test_approx_eq_eps DiffBase.hessian(result) DiffBase.hessian(test) EPS
3129

3230
# with HessianOptions
3331

@@ -39,12 +37,12 @@ function test_unary_hessian(f, x)
3937
RDP.hessian!(out, f, x, opts)
4038
@test_approx_eq_eps out DiffBase.hessian(test) EPS
4139

42-
# result = DiffBase.HessianResult(x)
43-
# opts = RDP.HessianOptions(result, x)
44-
# RDP.hessian!(result, f, x, opts)
45-
# @test_approx_eq_eps DiffBase.value(result) DiffBase.value(test) EPS
46-
# @test_approx_eq_eps DiffBase.gradient(result) DiffBase.gradient(test) EPS
47-
# @test_approx_eq_eps DiffBase.hessian(result) DiffBase.hessian(test) EPS
40+
result = DiffBase.HessianResult(x)
41+
opts = RDP.HessianOptions(result, x)
42+
RDP.hessian!(result, f, x, opts)
43+
@test_approx_eq_eps DiffBase.value(result) DiffBase.value(test) EPS
44+
@test_approx_eq_eps DiffBase.gradient(result) DiffBase.gradient(test) EPS
45+
@test_approx_eq_eps DiffBase.hessian(result) DiffBase.hessian(test) EPS
4846
end
4947

5048
for f in DiffBase.MATRIX_TO_NUMBER_FUNCS

test/api/JacobianTests.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module JacobianTests
22

33
using DiffBase, ForwardDiff, ReverseDiffPrototype, Base.Test
44

5-
const RDP = ReverseDiffPrototype
6-
75
include("../utils.jl")
86

97
println("testing jacobian/jacobian!...")
@@ -31,7 +29,7 @@ function test_unary_jacobian(f, x)
3129

3230
opts = RDP.Options(x)
3331

34-
@test_approx_eq_eps RDP.jacobian(f, x, opts) DiffBase.jacobian(test) EPS
32+
@test_approx_eq_eps RDP.jacobian(f, x, opts) DiffBase.jacobian(test) EPS
3533

3634
out = similar(DiffBase.jacobian(test))
3735
RDP.jacobian!(out, f, x, opts)

test/api/OptionsTests.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module UtilsTests
2+
3+
using ReverseDiffPrototype, Base.Test
4+
5+
include("../utils.jl")
6+
7+
println("testing Options...")
8+
tic()
9+
10+
issimilar(x::AbstractArray, y::AbstractArray) = typeof(x) === typeof(y) && size(x) === size(y)
11+
issimilar(x::Options, y::Options) = issimilar(x.state, y.state) && x.tape === y.tape
12+
issimilar(x::Tuple, y::Tuple) = all(map(issimilar, x, y))
13+
14+
############################################################################################
15+
16+
###########
17+
# Options #
18+
###########
19+
20+
tp = Tape()
21+
x, y = rand(3), rand(3, 2)
22+
z = rand(Int, 4)
23+
24+
opts = Options(x, tp)
25+
@test issimilar(opts.state, track(x, tp))
26+
@test opts.tape === tp
27+
28+
opts = Options(x, Int, tp)
29+
@test issimilar(opts.state, track(x, Int, tp))
30+
@test opts.tape === tp
31+
32+
opts = Options((x, y), tp)
33+
@test issimilar(opts.state, track((x, y), tp))
34+
@test opts.tape === tp
35+
36+
opts = Options((x, y), Int, tp)
37+
@test issimilar(opts.state, track((x, y), Int, tp))
38+
@test opts.tape === tp
39+
40+
opts = Options(z, x, tp)
41+
@test issimilar(opts.state, (track(z, tp), track(x, eltype(z), tp)))
42+
@test opts.tape === tp
43+
44+
opts = Options(z, (x, y), tp)
45+
@test issimilar(opts.state, (track(z, tp), track((x, y), eltype(z), tp)))
46+
@test opts.tape === tp
47+
48+
opts1 = Options(z, (x, y), tp)
49+
opts2 = Options(DiffBase.JacobianResult(z), (x, y), tp)
50+
@test issimilar(opts1, opts2)
51+
52+
##################
53+
# HessianOptions #
54+
##################
55+
56+
gtp, jtp = Tape(), Tape()
57+
x, y = rand(3), rand(Int, 3)
58+
59+
opts = HessianOptions(x, gtp, jtp)
60+
@test issimilar(RDP.gradient_options(opts), Options(track(x), gtp))
61+
@test issimilar(RDP.jacobian_options(opts), Options(x, jtp))
62+
63+
opts = HessianOptions(x, Int, gtp, jtp)
64+
@test issimilar(RDP.gradient_options(opts), Options(track(x, Int), gtp))
65+
@test issimilar(RDP.jacobian_options(opts), Options(x, Int, jtp))
66+
67+
opts = HessianOptions(y, x, gtp, jtp)
68+
@test issimilar(RDP.gradient_options(opts), Options(track(x, Int), gtp))
69+
@test issimilar(RDP.jacobian_options(opts), Options(y, x, jtp))
70+
71+
opts1 = HessianOptions(x, Int, gtp, jtp)
72+
opts2 = HessianOptions(DiffBase.HessianResult(x), Int, gtp, jtp)
73+
@test issimilar(RDP.gradient_options(opts1), RDP.gradient_options(opts2))
74+
@test issimilar(RDP.jacobian_options(opts1), RDP.jacobian_options(opts2))
75+
76+
############################################################################################
77+
78+
println("done (took $(toq()) seconds)")
79+
80+
end # module

test/utils.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using ReverseDiffPrototype: Tape, TapeNode, Tracked, Options, HessianOptions,
2+
value, adjoint, tape, valtype, adjtype, track, track!
3+
4+
const RDP = ReverseDiffPrototype
5+
16
const EPS = 1e-5
27

38
# make RNG deterministic, and thus make result inaccuracies
@@ -6,6 +11,10 @@ srand(1)
611

712
testprintln(kind, f, pad = " ") = println(pad, "testing $(kind): `$(f)`...")
813

14+
tracked_is(a, b) = value(a) === value(b) && adjoint(a) === adjoint(b) && tape(a) === tape(b)
15+
tracked_is(a::AbstractArray, b::AbstractArray) = all(map(tracked_is, a, b))
16+
tracked_is(a::Tuple, b::Tuple) = all(map(tracked_is, a, b))
17+
918
# function test4(x)
1019
# k = length(x)
1120
# N = isqrt(k)

0 commit comments

Comments
 (0)