Skip to content

Commit

Permalink
Throw errors for set operations
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHnt committed Oct 7, 2023
1 parent d55ba7f commit 26e221e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/intervals/real_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ end
"""
hash(x::Interval, h)
Compute the integer hash code for an interval using the method for composite
types used in `AutoHashEquals.jl`.
Note that in `IntervalArithmetic.jl`, equality of intervals is given by
`isequal_interval` rather than the `==` operator.
The latter is reserved for the pointwise extension of equality to intervals
and uses three-way logic by default.
Compute the integer hash code for an interval. Note that in
`IntervalArithmetic.jl`, equality of intervals is given by `isequal_interval`
rather than the `==` operator.
"""
hash(x::Interval, h::UInt) = hash(sup(x), hash(inf(x), hash(Interval, h)))

Expand Down Expand Up @@ -90,3 +86,22 @@ Base.isnan(::Interval) =

Base.isinteger(::Interval) =
throw(ArgumentError("`isinteger` is purposely not supported for intervals. See instead `isthininteger`."))

Base.intersect(::Interval) =
throw(ArgumentError("`intersect` is purposely not supported for intervals. See instead `intersect_interval`."))

Base.union!(::AbstractSet, ::Interval) = # also returned when calling `intersect`, `symdiff` with intervals
throw(ArgumentError("`union!` is purposely not supported for intervals. See instead `hull`."))
Base.union!(::AbstractVector{T}, ::Interval) where {T} =
throw(ArgumentError("`union!` is purposely not supported for intervals. See instead `hull`."))
Base.union!(::AbstractVector{T}, ::Interval, ::Any, ::Any...) where {T} =
throw(ArgumentError("`union!` is purposely not supported for intervals. See instead `hull`."))
Base.union!(::AbstractVector{T}, ::Interval, ::Interval, ::Any...) where {T} =
throw(ArgumentError("`union!` is purposely not supported for intervals. See instead `hull`."))
Base.union!(::AbstractVector{T}, ::Any, ::Interval, ::Any...) where {T} =
throw(ArgumentError("`union!` is purposely not supported for intervals. See instead `hull`."))

Base.setdiff(::Interval) =
throw(ArgumentError("`setdiff` is purposely not supported for intervals. See instead `setdiff_interval`."))
Base.setdiff!(::AbstractSet, ::Interval) =
throw(ArgumentError("`setdiff!` is purposely not supported for intervals. See instead `setdiff_interval`."))
13 changes: 13 additions & 0 deletions test/interval_tests/set_operations.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
using Test
using IntervalArithmetic

@testset "removed interval" begin
@test_throws ArgumentError intersect(interval(1))
@test_throws ArgumentError intersect(interval(1), 2, [1], 4., 5)
@test_throws ArgumentError intersect(interval(1), interval(2.), interval(3.))
@test_throws ArgumentError union(interval(1))
@test_throws ArgumentError union(interval(1), 2, [1], 4., 5)
@test_throws ArgumentError union(interval(1), interval(2.), interval(3.))
@test_throws ArgumentError setdiff(interval(1))
@test_throws ArgumentError setdiff(interval(1), 2, [1], 4., 5)
@test_throws ArgumentError setdiff(interval(1), interval(2.), interval(3.))
@test_throws ArgumentError symdiff(interval(1), interval(2.), interval(3.))
end

@testset "setdiff_interval" begin
x = interval(2, 4)
y = interval(3, 5)
Expand Down

0 comments on commit 26e221e

Please sign in to comment.