From ad7037920f8f2761c0e285bc9f502f32aaba745d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 2 Nov 2024 18:48:17 +0100 Subject: [PATCH 1/5] Promote bound of Interval if they have different types --- src/sets.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sets.jl b/src/sets.jl index 70ab9430c6..0784ffeceb 100644 --- a/src/sets.jl +++ b/src/sets.jl @@ -286,6 +286,10 @@ struct Interval{T<:Real} <: AbstractScalarSet upper::T end +function Interval(lower, upper) + return Interval(promote(lower, upper)...) +end + function Base.:(==)(a::Interval{T}, b::Interval{T}) where {T} return a.lower == b.lower && a.upper == b.upper end From 0cec84b0a1ac072f7ce4c5b074738924532c2e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 2 Nov 2024 20:18:54 +0100 Subject: [PATCH 2/5] Real Co-authored-by: Oscar Dowson --- src/sets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sets.jl b/src/sets.jl index 0784ffeceb..82205e4715 100644 --- a/src/sets.jl +++ b/src/sets.jl @@ -286,7 +286,7 @@ struct Interval{T<:Real} <: AbstractScalarSet upper::T end -function Interval(lower, upper) +function Interval(lower::Real, upper::Real) return Interval(promote(lower, upper)...) end From 34b4edfb0b8b6a6a730cfa9d75b504a18efbcfcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 2 Nov 2024 20:24:01 +0100 Subject: [PATCH 3/5] Add tests --- src/sets.jl | 8 ++++++++ test/sets.jl | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/sets.jl b/src/sets.jl index 82205e4715..3cc6f42572 100644 --- a/src/sets.jl +++ b/src/sets.jl @@ -394,6 +394,10 @@ struct Semicontinuous{T<:Real} <: AbstractScalarSet upper::T end +function Semicontinuous(lower::Real, upper::Real) + return Semicontinuous(promote(lower, upper)...) +end + function Base.:(==)(a::Semicontinuous{T}, b::Semicontinuous{T}) where {T} return a.lower == b.lower && a.upper == b.upper end @@ -426,6 +430,10 @@ struct Semiinteger{T<:Real} <: AbstractScalarSet upper::T end +function Semiinteger(lower::Real, upper::Real) + return Semiinteger(promote(lower, upper)...) +end + function Base.:(==)(a::Semiinteger{T}, b::Semiinteger{T}) where {T} return a.lower == b.lower && a.upper == b.upper end diff --git a/test/sets.jl b/test/sets.jl index 28b0c5000a..f3c240db05 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -363,6 +363,19 @@ function runtests() end end +function test_interval_promote() + for S in [MOI.Interval, MOI.Semiinteger, MOI.Semicontinuous] + set = S(1.0, π) + @test set isa S{Float64} + @test set.lower == 1.0 + @test set.upper == π + set = S(big(1), 2) + @test set isa S{BigInt} + @test set.lower == 1 + @test set.upper == 2 + end +end + end TestSets.runtests() From b29d8c9fa81cb8e23f40d7bfd5b8ba5db45aeb28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 2 Nov 2024 20:26:08 +0100 Subject: [PATCH 4/5] Fix --- test/sets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sets.jl b/test/sets.jl index f3c240db05..72f098e165 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -368,7 +368,7 @@ function test_interval_promote() set = S(1.0, π) @test set isa S{Float64} @test set.lower == 1.0 - @test set.upper == π + @test set.upper ≈ π set = S(big(1), 2) @test set isa S{BigInt} @test set.lower == 1 From aaec99cd421051de0ee42b1f58ec67714b2a245c Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sun, 3 Nov 2024 11:48:58 +1300 Subject: [PATCH 5/5] Apply suggestions from code review --- test/sets.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/sets.jl b/test/sets.jl index 72f098e165..bf52326974 100644 --- a/test/sets.jl +++ b/test/sets.jl @@ -364,7 +364,7 @@ function runtests() end function test_interval_promote() - for S in [MOI.Interval, MOI.Semiinteger, MOI.Semicontinuous] + for S in (MOI.Interval, MOI.Semiinteger, MOI.Semicontinuous) set = S(1.0, π) @test set isa S{Float64} @test set.lower == 1.0 @@ -374,6 +374,7 @@ function test_interval_promote() @test set.lower == 1 @test set.upper == 2 end + return end end