From 33a0f38d5446b4615f36dbf43563934e9e3dd9d4 Mon Sep 17 00:00:00 2001 From: Bagaev Dmitry Date: Tue, 8 Aug 2023 15:13:24 +0200 Subject: [PATCH] Import Base.in (#1758) * import Base.in * revert `DiscreteInterval`, modify tests * qualify Base.in --- src/univariates.jl | 13 +++++++------ test/testutils.jl | 28 ++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/univariates.jl b/src/univariates.jl index 726fd8e429..0e2ae05e32 100644 --- a/src/univariates.jl +++ b/src/univariates.jl @@ -6,10 +6,11 @@ struct RealInterval{T<:Real} end RealInterval(lb::Real, ub::Real) = RealInterval(promote(lb, ub)...) -minimum(r::RealInterval) = r.lb -maximum(r::RealInterval) = r.ub -extrema(r::RealInterval) = (r.lb, r.ub) -in(x::Real, r::RealInterval) = r.lb <= x <= r.ub + +Base.minimum(r::RealInterval) = r.lb +Base.maximum(r::RealInterval) = r.ub +Base.extrema(r::RealInterval) = (r.lb, r.ub) +Base.in(x::Real, r::RealInterval) = r.lb <= x <= r.ub isbounded(d::Union{D,Type{D}}) where {D<:UnivariateDistribution} = isupperbounded(d) && islowerbounded(d) @@ -124,8 +125,8 @@ macro distr_support(D, lb, ub) # overall esc(quote - minimum($(paramdecl)) = $lb - maximum($(paramdecl)) = $ub + Base.minimum($(paramdecl)) = $lb + Base.maximum($(paramdecl)) = $ub end) end diff --git a/test/testutils.jl b/test/testutils.jl index c26d1bbb8e..ad109e58e3 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -341,13 +341,29 @@ function test_support(d::UnivariateDistribution, vs::AbstractVector) @test isbounded(d) == (isupperbounded(d) && islowerbounded(d)) - if isbounded(d) - if isa(d, DiscreteUnivariateDistribution) - s = support(d) - @test isa(s, AbstractUnitRange) - @test first(s) == minimum(d) - @test last(s) == maximum(d) + # Test the `Base.in` or `∈` operator + # The `support` function is buggy for unbounded `DiscreteUnivariateDistribution`s + if isbounded(d) || isa(d, ContinuousUnivariateDistribution) + s = support(d) + for v in vs + @test v ∈ s + end + + if islowerbounded(d) + @test minimum(d) ∈ s + @test (minimum(d) - 1) ∉ s end + if isupperbounded(d) + @test maximum(d) ∈ s + @test (maximum(d) + 1) ∉ s + end + end + + if isbounded(d) && isa(d, DiscreteUnivariateDistribution) + s = support(d) + @test isa(s, AbstractUnitRange) + @test first(s) == minimum(d) + @test last(s) == maximum(d) end end