From 482c79a5661d3e38097a391f56926fd9e98afc61 Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Mon, 17 Apr 2023 15:13:04 -0500 Subject: [PATCH 1/2] fix and test sorting arrays with >1 dimension and custom `similar` --- base/sort.jl | 2 +- test/sorting.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/base/sort.jl b/base/sort.jl index 1041c7e4288b3..ec307e566ea49 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -1781,7 +1781,7 @@ function sort!(A::AbstractArray{T}; by=identity, rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward, # TODO stop eagerly over-allocating. - scratch::Union{Vector{T}, Nothing}=similar(A, size(A, dims))) where T + scratch::Union{Vector{T}, Nothing}=Vector{T}(undef, size(A, dims))) where T __sort!(A, Val(dims), maybe_apply_initial_optimizations(alg), ord(lt, by, rev, order), scratch) end function __sort!(A::AbstractArray{T}, ::Val{K}, diff --git a/test/sorting.jl b/test/sorting.jl index ec1666dabb2fb..76428ccdae80c 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -980,6 +980,20 @@ end end end +struct MyArrayNNNNN{T, N} <: AbstractArray{T, N} + data::Array{T, N} +end +Base.size(A::MyArrayNNNNN) = size(A.data) +Base.getindex(A::MyArrayNNNNN, i...) = getindex(A.data, i...) +Base.setindex!(A::MyArrayNNNNN, v, i...) = setindex!(A.data, v, i...) +Base.similar(A::MyArrayNNNNN, ::Type{T}, dims::Dims{N}) where {T, N} = MyArrayNNNNN(similar(A.data, T, dims)) + +@testset "Custom matrices (#NNNNN)" begin + x = rand(10, 10) + y = MyArrayNNNNN(copy(x)) + @test all(sort!(y, dims=2) .== sort!(x,dims=2)) +end + # This testset is at the end of the file because it is slow. @testset "searchsorted" begin numTypes = [ Int8, Int16, Int32, Int64, Int128, From 05fdb4865d2c1f88b096ae02240fee22a0e02c63 Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Mon, 17 Apr 2023 15:17:06 -0500 Subject: [PATCH 2/2] add PR number --- test/sorting.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/sorting.jl b/test/sorting.jl index 76428ccdae80c..c957d96ff47d7 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -980,17 +980,17 @@ end end end -struct MyArrayNNNNN{T, N} <: AbstractArray{T, N} +struct MyArray49392{T, N} <: AbstractArray{T, N} data::Array{T, N} end -Base.size(A::MyArrayNNNNN) = size(A.data) -Base.getindex(A::MyArrayNNNNN, i...) = getindex(A.data, i...) -Base.setindex!(A::MyArrayNNNNN, v, i...) = setindex!(A.data, v, i...) -Base.similar(A::MyArrayNNNNN, ::Type{T}, dims::Dims{N}) where {T, N} = MyArrayNNNNN(similar(A.data, T, dims)) +Base.size(A::MyArray49392) = size(A.data) +Base.getindex(A::MyArray49392, i...) = getindex(A.data, i...) +Base.setindex!(A::MyArray49392, v, i...) = setindex!(A.data, v, i...) +Base.similar(A::MyArray49392, ::Type{T}, dims::Dims{N}) where {T, N} = MyArray49392(similar(A.data, T, dims)) -@testset "Custom matrices (#NNNNN)" begin +@testset "Custom matrices (#49392)" begin x = rand(10, 10) - y = MyArrayNNNNN(copy(x)) + y = MyArray49392(copy(x)) @test all(sort!(y, dims=2) .== sort!(x,dims=2)) end