Skip to content

Commit

Permalink
Merge pull request #83 from JuliaDiff/ox/approx
Browse files Browse the repository at this point in the history
support isapprox still
  • Loading branch information
oxinabox authored Dec 15, 2020
2 parents 7bbd60e + f688e14 commit a630c9a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
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 = "0.5.6"
version = "0.5.7"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
33 changes: 27 additions & 6 deletions src/check_result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,30 @@ for (T1, T2) in ((AbstractThunk, Any), (AbstractThunk, AbstractThunk), (Any, Abs
end
end

"""
_can_pass_early(actual, expected; kwargs...)
Used to check if `actual` is basically equal to `expected`, so we don't need to check deeper;
and can just report `check_equal` as passing.
If either `==` or `≈` return true then so does this.
The `kwargs` are passed on to `isapprox`
"""
function _can_pass_early(actual, expected; kwargs...)
actual == expected && return true
try
return isapprox(actual, expected; kwargs...)
catch err
# Might MethodError, might DimensionMismatch, might fail for some other reason
# we don't care, whatever errored it means we can't quit early
end
return false
end




function check_equal(actual::AbstractArray, expected::AbstractArray; kwargs...)
if actual == expected # if equal then we don't need to be smarter
if _can_pass_early(actual, expected)
@test true
else
@test eachindex(actual) == eachindex(expected)
Expand All @@ -38,7 +59,7 @@ function check_equal(actual::AbstractArray, expected::AbstractArray; kwargs...)
end

function check_equal(actual::Composite{P}, expected::Composite{P}; kwargs...) where P
if actual == expected # if equal then we don't need to be smarter
if _can_pass_early(actual, expected)
@test true
else
all_keys = union(keys(actual), keys(expected))
Expand All @@ -58,17 +79,17 @@ end

# This catches comparisons of Composites and Tuples/NamedTuple
# and gives a error messaage complaining about that
check_equal(::C, expected::T) where {C<:Composite, T} = @test C === T
check_equal(::T, expected::C) where {C<:Composite, T} = @test C === T

const LegacyZygoteCompTypes = Union{Tuple,NamedTuple}
check_equal(::C, expected::T) where {C<:Composite,T<:LegacyZygoteCompTypes} = @test C === T
check_equal(::T, expected::C) where {C<:Composite,T<:LegacyZygoteCompTypes} = @test T === C

check_equal(::Zero, x; kwargs...) = check_equal(zero(x), x; kwargs...)
check_equal(x, ::Zero; kwargs...) = check_equal(x, zero(x); kwargs...)
check_equal(x::Zero, y::Zero; kwargs...) = @test true

# Generic fallback, probably a tuple or something
function check_equal(actual::A, expected::E; kwargs...) where {A, E}
if actual == expected # if equal then we don't need to be smarter
if _can_pass_early(actual, expected)
@test true
else
c_actual = collect(actual)
Expand Down
21 changes: 21 additions & 0 deletions test/check_result.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@

struct FakeNaturalDiffWithIsApprox # For testing overloading isapprox(::Composite) works:
x
end
function Base.isapprox(c::Composite, d::FakeNaturalDiffWithIsApprox; kwargs...)
return isapprox(c.x, d.x, kwargs...)
end
function Base.isapprox(d::FakeNaturalDiffWithIsApprox, c::Composite; kwargs...)
return isapprox(c.x, d.x, kwargs...)
end


@testset "check_result.jl" begin
@testset "_check_add!!_behavour" begin
check = ChainRulesTestUtils._check_add!!_behavour
Expand Down Expand Up @@ -54,6 +66,15 @@
Composite{typeof(T)}(a=1.0),
Composite{typeof(T)}(a=1.0+1e-10, b=Zero())
)

check_equal(
Composite{FakeNaturalDiffWithIsApprox}(; x=1.4),
FakeNaturalDiffWithIsApprox(1.4)
)
check_equal(
FakeNaturalDiffWithIsApprox(1.4),
Composite{FakeNaturalDiffWithIsApprox}(; x=1.4)
)
end
@testset "negative case" begin
@test fails(()->check_equal(1.0, 2.0))
Expand Down

2 comments on commit a630c9a

@oxinabox
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/26428

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.7 -m "<description of version>" a630c9a70cfd0e54ca7ee31e8508147dd8e1b717
git push origin v0.5.7

Please sign in to comment.