Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesTestUtils"
uuid = "cdddcdb0-9152-4a09-a978-84456f9df70a"
version = "1.9.2"
version = "1.9.3"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
13 changes: 9 additions & 4 deletions src/check_result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ for (T1, T2) in
end

test_approx(::AbstractZero, x, msg=""; kwargs...) = test_approx(zero(x), x, msg; kwargs...)
test_approx(::AbstractZero, x::AbstractArray{<:AbstractArray}, msg=""; kwargs...) = test_approx(map(zero, x), x, msg; kwargs...)
test_approx(x, ::AbstractZero, msg=""; kwargs...) = test_approx(x, zero(x), msg; kwargs...)
test_approx(x::AbstractArray{<:AbstractArray}, ::AbstractZero, msg=""; kwargs...) = test_approx(x, map(zero, x), msg; kwargs...)
test_approx(x::ZeroTangent, y::ZeroTangent, msg=""; kwargs...) = @test true
test_approx(x::NoTangent, y::NoTangent, msg=""; kwargs...) = @test true

function test_approx(z::AbstractZero, x::AbstractArray{<:AbstractArray}, msg=""; kwargs...)
for el in x
test_approx(el, z, msg; kwargs...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter but arguably a bit more consistent would be

Suggested change
test_approx(el, z, msg; kwargs...)
test_approx(z, el, msg; kwargs...)

end
end
test_approx(x::AbstractArray{<:AbstractArray}, z::AbstractZero, msg=""; kwargs...) = test_approx(z, x, msg; kwargs...)

# remove once https://github.com/JuliaDiff/ChainRulesTestUtils.jl/issues/113
test_approx(x::NoTangent, y::Nothing, msg=""; kwargs...) = @test true
test_approx(x::Nothing, y::NoTangent, msg=""; kwargs...) = @test true
Expand Down Expand Up @@ -134,8 +139,8 @@ function test_approx(actual::Tangent{P,T}, expected, msg=""; kwargs...) where {T
end
test_approx(x, y::Tangent, msg=""; kwargs...) = test_approx(y, x, msg; kwargs...)

test_approx(z::NoTangent, t::Tangent, msg=""; kwargs...) = all(==(NoTangent()), t)
test_approx(t::Tangent, z::NoTangent, msg=""; kwargs...) = all(==(NoTangent()), t)
test_approx(z::NoTangent, t::Tangent, msg=""; kwargs...) = @test all(==(NoTangent()), t)
test_approx(t::Tangent, z::NoTangent, msg=""; kwargs...) = @test all(==(NoTangent()), t)

# This catches comparisons of Tangents and Tuples/NamedTuple
# and gives an error message complaining about that. the `@test` will definitely fail
Expand Down
13 changes: 13 additions & 0 deletions test/check_result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ end
test_approx([[1.0], [2.0]], [[1.0], [2.0]])
test_approx([[0.0], [0.0]], ZeroTangent())
test_approx(ZeroTangent(), [[0.0], [0.0]])
test_approx(ZeroTangent(), [[0.0, 0.0], [[0.0, 0.0], [0.0, 0.0]]])
test_approx([[0.0, 0.0], [[0.0, 0.0], [0.0, 0.0]]], NoTangent())
test_approx(Broadcast.broadcasted(identity, [1.0 2.0; 3.0 4.0]), [1.0 2.0; 3.0 4.0])

test_approx(@thunk(10 * 0.1 * [[1.0], [2.0]]), [[1.0], [2.0]])
Expand Down Expand Up @@ -112,6 +114,17 @@ end
@test fails(() -> test_approx([[1.0], [2.0]], [[1.1], [2.0]]))
@test fails(() -> test_approx([[0.0], [0.1]], ZeroTangent()))
@test fails(() -> test_approx(ZeroTangent(), [[0.1], [0.0]]))
@test fails(() -> test_approx([[0.0], [0.0], [[0.0, 0.1], [0.0]]], ZeroTangent()))
@test fails(() -> test_approx(ZeroTangent(), [[0.0], [0.0], [[0.0, 0.1], [0.0]]]))

@test fails(() -> test_approx(
Tangent{Tuple{Float64,Float64}}(NoTangent(), 0.1),
NoTangent(),
))
@test fails(() -> test_approx(
NoTangent(),
Tangent{Tuple{Float64,Float64}}(NoTangent(), 0.1),
))

@test fails(() -> test_approx(@thunk(10 * [[1.0], [2.0]]), [[1.0], [2.0]]))

Expand Down