From f21ea762bc70b0c0424fec9961d273ac40b59d0f Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Sun, 1 Mar 2020 23:11:48 +0800 Subject: [PATCH 1/2] make ones and zeros return concrete types for Abstract Type and add more tests for zeros and ones --- base/array.jl | 10 ++++++---- test/arrayops.jl | 51 +++++++++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/base/array.jl b/base/array.jl index cdffc3798bf5d..0f981cb88cabc 100644 --- a/base/array.jl +++ b/base/array.jl @@ -501,13 +501,15 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one)) $fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims) $fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims)) function $fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N} - a = Array{T,N}(undef, dims) - fill!(a, $felt(T)) + init = $felt(T) + a = Array{typeof(init),N}(undef, dims) + fill!(a, init) return a end function $fname(::Type{T}, dims::Tuple{}) where {T} - a = Array{T}(undef) - fill!(a, $felt(T)) + init = $felt(T) + a = Array{typeof(init)}(undef) + fill!(a, init) return a end end diff --git a/test/arrayops.jl b/test/arrayops.jl index 4f4f9c53b865b..d371917c908f9 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2477,27 +2477,38 @@ Float64(x::F21666) = Float64(x.x) end @testset "zeros and ones" begin - @test ones(2) == ones(Int, 2) == [1,1] - @test isa(ones(2), Vector{Float64}) - @test isa(ones(Int, 2), Vector{Int}) - - function test_zeros(arr, T, s) - @test all(arr .== 0) - @test isa(arr, T) - @test size(arr) == s + for (fname, felt) in ((zeros, zero), (ones, one)) + for T in (Bool, Int, Float64, ComplexF64) + A0 = fname(T) + @test A0 == fname(Int) == fill(felt(T)) + @test typeof(A0) === Array{T, 0} + @test size(A0) === () + @test isempty(fname(T, 0)) + + A1 = fname(T, 2) + @test A1 == fname(Int, 2) == [felt(Int), felt(Int)] + @test A1 == fname(T, Base.OneTo(2)) + @test A1 == fname(T, (Base.OneTo(2), )) + @test typeof(A1) === Array{T, 1} + @test size(A1) === (2, ) + + A2 = fname(T, 2, 3) + @test A2 == fname(T, (2, 3)) + @test A2 == fname(T, Base.OneTo(2), Base.OneTo(3)) + @test A2 == fname(T, (Base.OneTo(2), Base.OneTo(3))) + @test typeof(A2) === Array{T, 2} + @test size(A2) === (2, 3) + + @test isempty(fname(T, 0, 0)) + @test_throws MethodError fname(T, 1.) + @test_throws MethodError fname(T, [1.]) # issue #19265 + @test_throws MethodError fname(T, [1, 1]) # issue #19265 + end + + for T in (AbstractFloat, Integer, Real, Number) + @test eltype(fname(T)) === typeof(felt(T)) + end end - test_zeros(zeros(), Array{Float64, 0}, ()) - test_zeros(zeros(2), Vector{Float64}, (2,)) - test_zeros(zeros(2,3), Matrix{Float64}, (2,3)) - test_zeros(zeros((2,3)), Matrix{Float64}, (2,3)) - - test_zeros(zeros(Int, 6), Vector{Int}, (6,)) - test_zeros(zeros(Int, 2, 3), Matrix{Int}, (2,3)) - test_zeros(zeros(Int, (2, 3)), Matrix{Int}, (2,3)) - - # #19265" - @test_throws MethodError zeros(Float64, [1.]) - @test_throws MethodError ones(Float64, [0, 0]) end # issue #11053 From eef4f3ce31359b92779132e9d25f4d8c7f25f477 Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Mon, 2 Mar 2020 00:06:34 +0800 Subject: [PATCH 2/2] revert the changes --- base/array.jl | 10 ++++------ test/arrayops.jl | 4 ---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/base/array.jl b/base/array.jl index 0f981cb88cabc..cdffc3798bf5d 100644 --- a/base/array.jl +++ b/base/array.jl @@ -501,15 +501,13 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one)) $fname(dims::Tuple{Vararg{DimOrInd}}) = $fname(Float64, dims) $fname(::Type{T}, dims::NTuple{N, Union{Integer, OneTo}}) where {T,N} = $fname(T, map(to_dim, dims)) function $fname(::Type{T}, dims::NTuple{N, Integer}) where {T,N} - init = $felt(T) - a = Array{typeof(init),N}(undef, dims) - fill!(a, init) + a = Array{T,N}(undef, dims) + fill!(a, $felt(T)) return a end function $fname(::Type{T}, dims::Tuple{}) where {T} - init = $felt(T) - a = Array{typeof(init)}(undef) - fill!(a, init) + a = Array{T}(undef) + fill!(a, $felt(T)) return a end end diff --git a/test/arrayops.jl b/test/arrayops.jl index d371917c908f9..a2f89fa5ce146 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2504,10 +2504,6 @@ end @test_throws MethodError fname(T, [1.]) # issue #19265 @test_throws MethodError fname(T, [1, 1]) # issue #19265 end - - for T in (AbstractFloat, Integer, Real, Number) - @test eltype(fname(T)) === typeof(felt(T)) - end end end