Skip to content

Commit 65778de

Browse files
committed
fix invalid input for .., capture warnings in tests
1 parent 80875fe commit 65778de

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

src/intervals/intervals.jl

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct Interval{T<:Real} <: AbstractInterval{T}
2525
new(a, b)
2626

2727
else
28-
throw(ArgumentError("Interval of form [$a, $b] not allowed. Must have a ≤ b to construct interval(a, b)."))
28+
@warn "Invalid input, empty interval is returned"
29+
return new(T(Inf), T(-Inf))
2930
end
3031

3132
end
@@ -82,13 +83,7 @@ function is_valid_interval(a::Real, b::Real)
8283
return false
8384
end
8485

85-
if a > b
86-
if isinf(a) && isinf(b)
87-
return true # empty interval = [∞,-∞]
88-
else
89-
return false
90-
end
91-
end
86+
a > b && return false
9287

9388
if a == Inf || b == -Inf
9489
return false
@@ -138,21 +133,32 @@ include("complex.jl")
138133
# Syntax for intervals
139134

140135
function ..(a::T, b::S) where {T, S}
141-
interval(atomic(Interval{T}, a).lo, atomic(Interval{S}, b).hi)
136+
if !is_valid_interval(a, b)
137+
@warn "Invalid input, empty interval is returned"
138+
return emptyinterval(promote_type(T, S, Float64))
139+
end
140+
Interval(atomic(Interval{T}, a).lo, atomic(Interval{S}, b).hi)
142141
end
143142

144143
function ..(a::T, b::Irrational{S}) where {T, S}
144+
if !is_valid_interval(a, b)
145+
@warn "Invalid input, empty interval is returned"
146+
return emptyinterval(promote_type(T, Irrational{S}, Float64))
147+
end
145148
R = promote_type(T, Irrational{S})
146-
interval(atomic(Interval{R}, a).lo, R(b, RoundUp))
149+
Interval(atomic(Interval{R}, a).lo, R(b, RoundUp))
147150
end
148151

149152
function ..(a::Irrational{T}, b::S) where {T, S}
153+
if !is_valid_interval(a, b)
154+
@warn "Invalid input, empty interval is returned"
155+
return emptyinterval(promote_type(Irrational{T}, S, Float64))
156+
end
150157
R = promote_type(Irrational{T}, S)
151-
return interval(R(a, RoundDown), atomic(Interval{R}, b).hi)
158+
return Interval(R(a, RoundDown), atomic(Interval{R}, b).hi)
152159
end
153160

154161
function ..(a::Irrational{T}, b::Irrational{S}) where {T, S}
155-
R = promote_type(Irrational{T}, Irrational{S})
156162
return interval(a, b)
157163
end
158164

test/interval_tests/consistency.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ setprecision(Interval, Float64)
380380
@test interval(1, 2) == Interval(1, 2)
381381

382382
@test inf(Interval(3, 2)) == 3
383-
@test isempty(interval(3, 2))
383+
@test_logs (:warn,) @test isempty(interval(3, 2))
384384

385385
@test sup(Interval(Inf, Inf)) == Inf
386-
@test isempty(interval(Inf, Inf))
386+
@test_logs (:warn,) @test isempty(interval(Inf, Inf))
387387

388388
end
389389

test/interval_tests/construction.jl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ const eeuler = Base.MathConstants.e
6969

7070
# Disallowed conversions with a > b
7171

72-
@test isempty(interval(2, 1))
73-
@test isempty(interval(big(2), big(1)))
74-
@test isempty(interval(BigInt(1), 1//10))
75-
@test isempty(interval(1, 0.1))
76-
@test isempty(interval(big(1), big(0.1)))
77-
78-
@test isempty(@interval(2, 1))
79-
@test isempty(@interval(big(2), big(1)))
80-
@test isempty(@interval(big(1), 1//10))
81-
@test isempty(@interval(1, 0.1))
82-
@test isempty(@interval(big(1), big(0.1)))
83-
@test isempty(interval(Inf))
84-
@test isempty(interval(-Inf, -Inf))
85-
@test isempty(interval(Inf, Inf))
72+
@test_logs (:warn,) @test isempty(interval(2, 1))
73+
@test_logs (:warn,) @test isempty(interval(big(2), big(1)))
74+
@test_logs (:warn,) @test isempty(interval(BigInt(1), 1//10))
75+
@test_logs (:warn,) @test isempty(interval(1, 0.1))
76+
@test_logs (:warn,) @test isempty(interval(big(1), big(0.1)))
77+
78+
@test_logs (:warn,) @test isempty(@interval(2, 1))
79+
@test_logs (:warn,) @test isempty(@interval(big(2), big(1)))
80+
@test_logs (:warn,) @test isempty(@interval(big(1), 1//10))
81+
@test_logs (:warn,) @test isempty(@interval(1, 0.1))
82+
@test_logs (:warn,) @test isempty(@interval(big(1), big(0.1)))
83+
@test_logs (:warn,) @test isempty(interval(Inf))
84+
@test_logs (:warn,) @test isempty(interval(-Inf, -Inf))
85+
@test_logs (:warn,) @test isempty(interval(Inf, Inf))
8686

8787
# Conversion to Interval without type
8888
@test convert(Interval, 1) == Interval(1.0)
@@ -237,10 +237,10 @@ end
237237
a = big(0.1)..2
238238
@test typeof(a) == Interval{BigFloat}
239239

240-
@test isempty(2..1)
241-
@test isempty..1)
242-
@test isempty..eeuler)
243-
@test isempty(4..π)
240+
@test_logs (:warn, ) @test isempty(2..1)
241+
@test_logs (:warn, ) @test isempty..1)
242+
@test_logs (:warn, ) @test isempty..eeuler)
243+
@test_logs (:warn, ) @test isempty(4..π)
244244
@test 1..π == Interval(1, π)
245245
end
246246

@@ -293,9 +293,9 @@ end
293293

294294
# issue 192:
295295
@testset "Disallow NaN in an interval" begin
296-
@test isempty(interval(NaN, 2))
297-
@test isempty(interval(Inf, NaN))
298-
@test isempty(interval(NaN, NaN))
296+
@test_logs (:warn, ) @test isempty(interval(NaN, 2))
297+
@test_logs (:warn, ) @test isempty(interval(Inf, NaN))
298+
@test_logs (:warn, ) @test isempty(interval(NaN, NaN))
299299
end
300300

301301
# issue 206:
@@ -343,7 +343,7 @@ end
343343
end
344344

345345
@testset "a..b with a > b" begin
346-
@test isempty(3..2)
346+
@test_logs (:warn,) @test isempty(3..2)
347347
end
348348

349349
@testset "Hashing of Intervals" begin
@@ -375,7 +375,7 @@ import IntervalArithmetic: force_interval
375375
@test force_interval(4, Inf) == Interval(4, Inf)
376376
@test force_interval(Inf, 4) == Interval(4, Inf)
377377
@test force_interval(Inf, -Inf) == Interval(-Inf, Inf)
378-
@test isempty(force_interval(NaN, 3))
378+
@test_logs (:warn,) @test isempty(force_interval(NaN, 3))
379379
end
380380

381381
@testset "Zero interval" begin

test/interval_tests/numeric.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ end
115115
@test Interval(1,2) ^ -3 == Interval(1/8, 1.0)
116116
@test Interval(0,3) ^ -3 == @interval(1/27, Inf)
117117
@test Interval(-1,2) ^ -3 == entireinterval()
118-
@test isempty(interval(-1, -2) ^ -3) # wrong way round
118+
@test_logs (:warn, ) @test isempty(interval(-1, -2) ^ -3)
119119
@test Interval(-3,2) ^ (3//1) == Interval(-27, 8)
120120
@test Interval(0.0) ^ 1.1 == Interval(0, 0)
121121
@test Interval(0.0) ^ 0.0 == emptyinterval()

0 commit comments

Comments
 (0)