Skip to content

Commit

Permalink
fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Nov 26, 2024
1 parent 27ed4aa commit 37e0c1a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,46 @@ maximum(d::TypedEndpointsInterval{L,:open}) where L = throw(ArgumentError("$d is

extrema(I::TypedEndpointsInterval) = (infimum(I), supremum(I))

minimum(::typeof(abs), I::TypedEndpointsInterval{:closed,:closed}) =
minimum(I) < 0 < maximum(I) ?
zero(minimum(I)) :
minimum(abs, endpoints(I))
maximum(::typeof(abs), I::TypedEndpointsInterval{:closed,:closed}) = maximum(abs, endpoints(I))
function minimum(::typeof(abs), I::AbstractInterval)
a, b = abs.(endpoints(I))
z = zero(promote_type(typeof(a), typeof(b)))
ac, bc = closedendpoints(I)
if isempty(I)
throw(ArgumentError("minimum(abs, $I) is undefined for empty intervals"))

Check warning on line 345 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L345

Added line #L345 was not covered by tests
elseif z I
return z
elseif a < b
ac && return a
throw(ArgumentError("$I is open on the left, no minimum(abs)"))
elseif a > b
bc && return b
throw(ArgumentError("$I is open on the right, no minimum(abs)"))

Check warning on line 353 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L350-L353

Added lines #L350 - L353 were not covered by tests
else
throw(ArgumentError("cannot determine minimum(abs, $I)"))

Check warning on line 355 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L355

Added line #L355 was not covered by tests
end
end

function maximum(::typeof(abs), I::AbstractInterval)
a, b = abs.(endpoints(I))
ac, bc = closedendpoints(I)
if isempty(I)
throw(ArgumentError("maximum(abs, $I) is undefined for empty intervals"))

Check warning on line 363 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L363

Added line #L363 was not covered by tests
elseif a == b
ac && return a
bc && return b
throw(ArgumentError("$I is open on both sides, no maximum(abs)"))

Check warning on line 367 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L365-L367

Added lines #L365 - L367 were not covered by tests
elseif a > b
ac && return a
throw(ArgumentError("$I is open on the left, no maximum(abs)"))

Check warning on line 370 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L370

Added line #L370 was not covered by tests
elseif a < b
bc && return b
throw(ArgumentError("$I is open on the right, no maximum(abs)"))
else
throw(ArgumentError("cannot determine maximum(abs, $I)"))

Check warning on line 375 in src/IntervalSets.jl

View check run for this annotation

Codecov / codecov/patch

src/IntervalSets.jl#L375

Added line #L375 was not covered by tests
end
end

extrema(::typeof(abs), I::AbstractInterval) = (minimum(abs, I), maximum(abs, I))

# Open and closed at endpoints
isleftclosed(d::TypedEndpointsInterval{:closed}) = true
Expand Down
20 changes: 20 additions & 0 deletions test/base_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ end
IntervalSets.endpoints(::RandTestUnitInterval) = (-1.0, 1.0)
@test rand(RandTestUnitInterval()) in -1.0..1.0
end

@testset "min/maximum(abs)" begin
@test maximum(abs, 1..2) == 2
@test maximum(abs, -3..2) == 3
@test minimum(abs, 1..2) == 1
@test minimum(abs, -3..2) == 0
@test_throws Exception maximum(abs, iv"[1, 2)")
@test minimum(abs, iv"[1, 2)") == 1
@test minimum(abs, iv"(-3, 2)") == 0

@test maximum(abs, 1u"m"..2u"m") == 2u"m"
@test maximum(abs, -3u"m"..2u"m") == 3u"m"
@test minimum(abs, 1u"m"..2u"m") == 1u"m"
@test minimum(abs, -3u"m"..2u"m") == 0u"m"

@test extrema(abs, 1..2) == (1, 2)
@test extrema(abs, -3..2) == (0, 3)
@test_throws Exception extrema(abs, iv"[1, 2)")
@test extrema(abs, iv"[-3, 2)") == (0, 3)
end

0 comments on commit 37e0c1a

Please sign in to comment.