Skip to content

Commit

Permalink
Merge pull request #107 from invenia/fc/infinity-jl-tests
Browse files Browse the repository at this point in the history
Add tests using Infinity.jl
  • Loading branch information
omus authored Aug 13, 2020
2 parents 8aeb592 + 93b79df commit d7a27c3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 41 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[compat]
Documenter = "0.23, 0.24"
Infinity = "0.2.3"
RecipesBase = "0.7, 0.8, 1"
TimeZones = "0.7, 0.8, 0.9, 0.10, 0.11, 1"
julia = "1"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Infinity = "a303e19e-6eb4-11e9-3b09-cd9505f79100"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

[targets]
test = ["Documenter", "ImageMagick", "Plots", "Test", "VisualRegressionTests"]
test = ["Documenter", "ImageMagick", "Infinity", "Plots", "Test", "VisualRegressionTests"]
19 changes: 10 additions & 9 deletions src/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ AnchoredInterval{P,L,R}(anchor::T) where {P,T,L,R} = AnchoredInterval{P,T,L,R}(a
# When an interval is anchored to the lesser endpoint, default to Inclusivity(false, true)
# When an interval is anchored to the greater endpoint, default to Inclusivity(true, false)
function AnchoredInterval{P,T}(anchor) where {P,T}
L = bound_type(P zero(P))
R = bound_type(P zero(P))
s = sign(P)
L = bound_type(s 0)
R = bound_type(s 0)
return AnchoredInterval{P,T,L,R}(anchor)
end

Expand Down Expand Up @@ -160,11 +161,11 @@ end
# that is no longer comparable (e.g., `NaN`).

function Base.first(interval::AnchoredInterval{P}) where P
P < zero(P) ? (interval.anchor + P) : (interval.anchor)
sign(P) < 0 ? (interval.anchor + P) : (interval.anchor)
end

function Base.last(interval::AnchoredInterval{P}) where P
P < zero(P) ? (interval.anchor) : (interval.anchor + P)
sign(P) < 0 ? (interval.anchor) : (interval.anchor + P)
end

anchor(interval::AnchoredInterval) = interval.anchor
Expand All @@ -175,7 +176,7 @@ span(interval::AnchoredInterval{P}) where P = abs(P)
# Allows an interval to be converted to a scalar when the set contained by the interval only
# contains a single element.
function Base.convert(::Type{T}, interval::AnchoredInterval{P,T}) where {P,T}
if isclosed(interval) && (iszero(P) || first(interval) == last(interval))
if isclosed(interval) && (sign(P) == 0 || first(interval) == last(interval))
return first(interval)
else
# Remove deprecation in version 2.0.0
Expand Down Expand Up @@ -206,13 +207,13 @@ end
#=
function Base.convert(::Type{AnchoredInterval{P,T}}, interval::Interval{T}) where {P,T}
@assert abs(P) == span(interval)
anchor = P < zero(P) ? last(interval) : first(interval)
anchor = sign(P) < 0 ? last(interval) : first(interval)
AnchoredInterval{P,T}(last(interval), inclusivity(interval))
end
function Base.convert(::Type{AnchoredInterval{P}}, interval::Interval{T}) where {P,T}
@assert abs(P) == span(interval)
anchor = P < zero(P) ? last(interval) : first(interval)
anchor = sign(P) < 0 ? last(interval) : first(interval)
AnchoredInterval{P,T}(anchor, inclusivity(interval))
end
=#
Expand Down Expand Up @@ -308,15 +309,15 @@ end
##### SET OPERATIONS #####

function Base.isempty(interval::AnchoredInterval{P,T}) where {P,T}
return P == zero(P) && !isclosed(interval)
return sign(P) == 0 && !isclosed(interval)
end

# When intersecting two `AnchoredInterval`s attempt to return an `AnchoredInterval`
function Base.intersect(a::AnchoredInterval{P,T}, b::AnchoredInterval{Q,T}) where {P,Q,T}
interval = invoke(intersect, Tuple{AbstractInterval{T}, AbstractInterval{T}}, a, b)

sp = isa(P, Period) ? canonicalize(typeof(P), span(interval)) : span(interval)
if P zero(P)
if sign(P) 0
anchor = last(interval)
new_P = -sp
else
Expand Down
51 changes: 30 additions & 21 deletions test/anchoredinterval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,30 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
@testset "infinite" begin
x = 1 # Non-zero value representing any positive value

interval = 0 .. Inf
@test AnchoredInterval{Inf,Closed,Closed}(0.0) == interval
# Not-possible: AnchoredInterval{-?,Closed,Closed}(Inf)

interval = -Inf .. 0
# Not-possible: AnchoredInterval{+?,Closed,Closed}(-Inf)
@test AnchoredInterval{-Inf,Closed,Closed}(0.0) == interval

interval = -Inf .. Inf
@test_throws ArgumentError AnchoredInterval{Inf,Closed,Closed}(-Inf)
@test_throws ArgumentError AnchoredInterval{-Inf,Closed,Closed}(Inf)

interval = -Inf .. -Inf
@test AnchoredInterval{+x,Closed,Closed}(-Inf) == interval
@test AnchoredInterval{-x,Closed,Closed}(-Inf) == interval
@test AnchoredInterval{0}(-Inf) == interval

interval = Inf .. Inf
@test AnchoredInterval{+x,Closed,Closed}(Inf) == interval
@test AnchoredInterval{-x,Closed,Closed}(Inf) == interval
@test AnchoredInterval{0}(Inf) == interval
# Note: Ideally the exception raised would always be `ArgumentError`.
@testset "$inf" for (inf, Error) in zip((Inf, ∞), (ArgumentError, InfMinusInfError))
interval = 0 .. inf
@test AnchoredInterval{inf,Closed,Closed}(0.0) == interval
# Not-possible: AnchoredInterval{-?,Closed,Closed}(inf)

interval = -inf .. 0
# Not-possible: AnchoredInterval{+?,Closed,Closed}(-inf)
@test AnchoredInterval{-inf,Closed,Closed}(0.0) == interval

interval = -inf .. inf
@test_throws Error AnchoredInterval{inf,Closed,Closed}(-inf)
@test_throws Error AnchoredInterval{-inf,Closed,Closed}(inf)

interval = -inf .. -inf
@test AnchoredInterval{+x,Closed,Closed}(-inf) == interval
@test AnchoredInterval{-x,Closed,Closed}(-inf) == interval
@test AnchoredInterval{0}(-inf) == interval

interval = inf .. inf
@test AnchoredInterval{+x,Closed,Closed}(inf) == interval
@test AnchoredInterval{-x,Closed,Closed}(inf) == interval
@test AnchoredInterval{0}(inf) == interval
end
end

@testset "nan" begin
Expand Down Expand Up @@ -136,6 +139,12 @@ using Intervals: Bounded, Ending, Beginning, canonicalize, isunbounded
@test_throws ArgumentError convert(AnchoredInterval{Ending}, Interval(0, Inf))
@test convert(AnchoredInterval{Beginning}, Interval(0, Inf)) == AnchoredInterval{Inf,Float64,Closed,Closed}(0)

@test convert(AnchoredInterval{Ending}, Interval(-∞, 0)) == AnchoredInterval{-∞,Float64,Closed,Closed}(0)
@test_throws InfMinusInfError convert(AnchoredInterval{Beginning}, Interval(-∞, 0))

@test_throws InfMinusInfError convert(AnchoredInterval{Ending}, Interval(0, ∞))
@test convert(AnchoredInterval{Beginning}, Interval(0, ∞)) == AnchoredInterval{∞,Float64,Closed,Closed}(0)

@test_throws ArgumentError convert(AnchoredInterval{Ending}, Interval(nothing, 0))
@test_throws ArgumentError convert(AnchoredInterval{Beginning}, Interval(nothing, 0))

Expand Down
35 changes: 26 additions & 9 deletions test/comparisons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ end
[
Interval{Closed, Closed}(1, 2),
Interval{Closed, Closed}(-Inf, 2),
Interval{Closed, Closed}(-∞, 2),
Interval{Unbounded,Closed}(nothing, 2),
],
[
Interval{Closed, Closed}(4, 5),
Interval{Closed, Closed}(4, Inf),
Interval{Closed, Closed}(4, ∞),
Interval{Closed,Unbounded}(4, nothing),
],
)
Expand Down Expand Up @@ -87,11 +89,13 @@ end
[
Interval{Open, Open}(1, 3),
Interval{Open, Open}(-Inf, 3),
Interval{Open, Open}(-∞, 3),
Interval{Unbounded,Open}(nothing, 3),
],
[
Interval{Open, Open}(3, 5),
Interval{Open, Open}(3, Inf),
Interval{Open, Open}(3, ∞),
Interval{Open,Unbounded}(3, nothing),
],
)
Expand Down Expand Up @@ -140,11 +144,13 @@ end
[
Interval{Open, Open}(1, 3),
Interval{Open, Open}(-Inf, 3),
Interval{Open, Open}(-∞, 3),
Interval{Unbounded,Open}(nothing, 3),
],
[
Interval{Closed, Closed}(3, 5),
Interval{Closed, Closed}(3, Inf),
Interval{Closed, Closed}(3, ∞),
Interval{Closed,Unbounded}(3, nothing),
],
)
Expand Down Expand Up @@ -193,11 +199,13 @@ end
[
Interval{Closed, Closed}(1, 3),
Interval{Closed, Closed}(-Inf, 3),
Interval{Closed, Closed}(-∞, 3),
Interval{Unbounded,Closed}(nothing, 3),
],
[
Interval{Open, Open}(3, 5),
Interval{Open, Open}(3, Inf),
Interval{Open, Open}(3, ∞),
Interval{Open,Unbounded}(3, nothing),
],
)
Expand Down Expand Up @@ -246,11 +254,13 @@ end
[
Interval{Closed, Closed}(1, 3),
Interval{Closed, Closed}(-Inf, 3),
Interval{Closed, Closed}(-∞, 3),
Interval{Unbounded,Closed}(nothing, 3),
],
[
Interval{Closed, Closed}(3, 5),
Interval{Closed, Closed}(3, Inf),
Interval{Closed, Closed}(3, ∞),
Interval{Closed,Unbounded}(3, nothing),
],
)
Expand Down Expand Up @@ -299,11 +309,13 @@ end
[
Interval{Closed, Closed}(1, 4),
Interval{Closed, Closed}(-Inf, 4),
Interval{Closed, Closed}(-∞, 4),
Interval{Unbounded,Closed}(nothing, 4),
],
[
Interval{Closed, Closed}(2, 5),
Interval{Closed, Closed}(2, Inf),
Interval{Closed, Closed}(2, ∞),
Interval{Closed,Unbounded}(2, nothing),
],
)
Expand Down Expand Up @@ -348,7 +360,7 @@ end
Interval{Open, Open}(l, u),
Interval{Open, Open}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -391,7 +403,7 @@ end
Interval{Closed, Open}(l, u),
Interval{Open, Open}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -434,7 +446,7 @@ end
Interval{Open, Closed}(l, u),
Interval{Open, Open}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -477,7 +489,7 @@ end
Interval{Closed, Closed}(l, u),
Interval{Open, Open}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -520,7 +532,7 @@ end
Interval{Closed, Open}(l, u),
Interval{Closed, Closed}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -563,7 +575,7 @@ end
Interval{Open, Closed}(l, u),
Interval{Closed, Closed}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -606,7 +618,7 @@ end
Interval{Closed, Closed}(l, u),
Interval{Closed, Closed}(l, u),
]
for (l, u) in product((1, -Inf), (5, Inf))
for (l, u) in product((1, -Inf, -), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -649,7 +661,7 @@ end
Interval{Closed,Unbounded}(l, u),
Interval{Open,Unbounded}(l, u),
]
for (l, u) in product((1, -Inf), (nothing,))
for (l, u) in product((1, -Inf, -), (nothing,))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -692,7 +704,7 @@ end
Interval{Unbounded,Closed}(l, u),
Interval{Unbounded,Open}(l, u),
]
for (l, u) in product((nothing,), (5, Inf))
for (l, u) in product((nothing,), (5, Inf, ∞))
)

@testset "$a vs. $b" for (a, b) in test_intervals
Expand Down Expand Up @@ -818,12 +830,17 @@ end
[
Interval{Closed, Closed}(1, 5),
Interval{Closed, Closed}(1, Inf),
Interval{Closed, Closed}(1, ∞),
Interval{Closed, Closed}(-Inf, 5),
Interval{Closed, Closed}(-∞, 5),
Interval{Closed, Closed}(-Inf, Inf),
Interval{Closed, Closed}(-∞, ∞),
Interval{Closed, Unbounded}(1, nothing),
Interval{Closed, Unbounded}(-Inf, nothing),
Interval{Closed, Unbounded}(-∞, nothing),
Interval{Unbounded, Closed}(nothing, 5),
Interval{Unbounded, Closed}(nothing, Inf),
Interval{Unbounded, Closed}(nothing, ∞),
Interval{Unbounded, Unbounded}(nothing, nothing),
],
)
Expand Down
14 changes: 13 additions & 1 deletion test/interval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ isinf(::TimeType) = false
(-Inf, Inf, 1),
(-Inf, 1.0, 0.01),
(0.0, Inf, 0.01),

# Using Infinity.jl
(-∞, 10, 1),
(10, ∞, 1),
(-∞, ∞, 1),
(-∞, 1.0, 0.01),
(-∞, Date(2013, 3, 13), Day(1)),
(Date(2013, 2, 13), ∞, Day(1)),
(-∞, DateTime(2016, 8, 11, 1), Millisecond(1)),
(DateTime(2016, 8, 11, 0, 30), ∞, Millisecond(1)),
(-∞, Time(1), Millisecond(1)),
(Time(1), ∞, Millisecond(1)),
]

@testset "constructor" begin
Expand Down Expand Up @@ -362,7 +374,7 @@ isinf(::TimeType) = false
@test unit + interval == Interval{L, R}(a + unit, b + unit)
@test interval - unit == Interval{L, R}(a - unit, b - unit)

if a isa Number
if a isa Number && b isa Number
@test -interval == Interval{R, L}(-b, -a)
@test unit - interval == Interval{R, L}(unit - b, unit - a)
else
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Base.Iterators: product
using Dates
using Documenter: doctest
using Infinity: Infinite, InfExtendedReal, InfExtendedTime, InfMinusInfError, ∞
using Intervals
using Serialization: deserialize
using Test
Expand Down

0 comments on commit d7a27c3

Please sign in to comment.