From 14dec2d3ad7f60f1ccbe82c662471e8c463e72b7 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Tue, 5 Nov 2024 08:00:03 -0600 Subject: [PATCH] Fix and test and overflow issue in `searchsorted` And remove `searchsorted` special cases for offset arrays in tests that had the impact of bypassing actually testing searchsorted behavior on offset arrays --- base/sort.jl | 4 ++-- test/sorting.jl | 4 ++++ test/testhelpers/OffsetArrays.jl | 11 ----------- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index ef0f208209fc8..2251d0b965228 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -231,8 +231,8 @@ function searchsorted(v::AbstractVector, x, ilo::T, ihi::T, o::Ordering)::UnitRa elseif lt(o, x, v[m]) hi = m else - a = searchsortedfirst(v, x, max(lo,ilo), m, o) - b = searchsortedlast(v, x, m, min(hi,ihi), o) + a = searchsortedfirst(v, x, lo+u, m, o) + b = searchsortedlast(v, x, m, hi-u, o) return a : b end end diff --git a/test/sorting.jl b/test/sorting.jl index 2714197f58823..8cbdb94f02b16 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -581,6 +581,10 @@ end @test searchsortedfirst(o, 1.5) == 0 @test searchsortedlast(o, 0) == firstindex(o) - 1 @test searchsortedlast(o, 1.5) == -1 + + # Issue #56457 + o2 = OffsetArray([2,2,3], typemax(Int)-3); + @test searchsorted(o2, 2) == firstindex(o2):firstindex(o2)+1 end function adaptive_sort_test(v; trusted=InsertionSort, kw...) diff --git a/test/testhelpers/OffsetArrays.jl b/test/testhelpers/OffsetArrays.jl index 3463d5a94393d..06e65f8928036 100644 --- a/test/testhelpers/OffsetArrays.jl +++ b/test/testhelpers/OffsetArrays.jl @@ -821,17 +821,6 @@ centered(A::AbstractArray, cp::Dims=center(A)) = OffsetArray(A, .-cp) centered(A::AbstractArray, i::CartesianIndex) = centered(A, Tuple(i)) -# we may pass the searchsorted* functions to the parent, and wrap the offset -for f in [:searchsortedfirst, :searchsortedlast, :searchsorted] - _safe_f = Symbol("_safe_" * String(f)) - @eval function $_safe_f(v::OffsetArray, x, ilo, ihi, o::Base.Ordering) - offset = firstindex(v) - firstindex(parent(v)) - $f(parent(v), x, ilo - offset, ihi - offset, o) .+ offset - end - @eval Base.$f(v::OffsetVector, x, ilo::T, ihi::T, o::Base.Ordering) where T<:Integer = - $_safe_f(v, x, ilo, ihi, o) -end - ## # Deprecations ##