Skip to content

Commit

Permalink
Import Base.in (#1758)
Browse files Browse the repository at this point in the history
* import Base.in

* revert `DiscreteInterval`, modify tests

* qualify Base.in
  • Loading branch information
bvdmitri authored Aug 8, 2023
1 parent 72ef1ad commit 33a0f38
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/univariates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down
28 changes: 22 additions & 6 deletions test/testutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 33a0f38

Please sign in to comment.