Skip to content

Commit

Permalink
upgrade OffsetArrays to v1.3.0 (JuliaLang#37643)
Browse files Browse the repository at this point in the history
* upgrade to OffsetArrays v1.3.0

* everywhere include OffsetArrays

* more tests for JuliaLang#37204

OffsetArrays issues 133 and 100 are fixed by JuliaLang#37204.
  • Loading branch information
johnnychen94 committed Oct 6, 2020
1 parent be1f3ab commit 4ccfd37
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 121 deletions.
2 changes: 1 addition & 1 deletion stdlib/Distributed/test/splitrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using Distributed: splitrange
@test splitrange(-1, 1, 4) == Array{UnitRange{Int64},1}([-1:-1,0:0,1:1])

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :OffsetArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
isdefined(Main, :OffsetArrays) || @eval Main @everywhere include(joinpath($(BASE_TEST_PATH), "testhelpers", "OffsetArrays.jl"))
using .Main.OffsetArrays

oa = OffsetArray([123, -345], (-2,))
Expand Down
4 changes: 2 additions & 2 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,8 @@ end

# non-1-indexed array
oa = OffsetArray(Vector(1:10), -5)
filter!(x -> x > 5, oa)
@test oa == OffsetArray(Vector(6:10), -5)
oa = oa[oa.>5] # deleteat! is not supported for OffsetArrays
@test oa == Vector(6:10)

# empty non-1-indexed array
eoa = OffsetArray([], -5)
Expand Down
163 changes: 136 additions & 27 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ using DelimitedFiles
using Random
using LinearAlgebra
using Statistics

const OAs_name = join(fullname(OffsetArrays), ".")
using Base: IdentityUnitRange

if !isdefined(@__MODULE__, :T24Linear)
include("testhelpers/arrayindexingtypes.jl")
Expand All @@ -18,15 +17,15 @@ let
v0 = rand(4)
v = OffsetArray(v0, (-3,))
h = OffsetArray([-1,1,-2,2,0], (-3,))
@test axes(v) == (-2:1,)
@test axes(v) === (OffsetArrays.IdOffsetRange(Base.OneTo(4), -3),)
@test size(v) == (4,)
@test size(v, 1) == 4
@test_throws DimensionMismatch Array(v)

A0 = [1 3; 2 4]
A = OffsetArray(A0, (-1,2)) # IndexLinear
S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # IndexCartesian
@test axes(A) == axes(S) == (0:1, 3:4)
@test axes(A) === axes(S) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -1), OffsetArrays.IdOffsetRange(Base.OneTo(2), 2))
@test size(A) == (2,2)
@test size(A, 1) == 2

Expand Down Expand Up @@ -133,13 +132,13 @@ S = view(A, :, 3)
@test S[0] == 1
@test S[1] == 2
@test_throws BoundsError S[2]
@test axes(S) === (Base.IdentityUnitRange(0:1),)
@test axes(S) === (Base.IdentityUnitRange(OffsetArrays.IdOffsetRange(Base.OneTo(2), -1)),)
S = view(A, 0, :)
@test S == OffsetArray([1,3], (A.offsets[2],))
@test S[3] == 1
@test S[4] == 3
@test_throws BoundsError S[1]
@test axes(S) === (Base.IdentityUnitRange(3:4),)
@test axes(S) === (Base.IdentityUnitRange(OffsetArrays.IdOffsetRange(Base.OneTo(2), 2)),)
S = view(A, 0:0, 4)
@test S == [3]
@test S[1] == 3
Expand All @@ -158,17 +157,17 @@ S = view(A, :, :)
@test S[0,4] == S[3] == 3
@test S[1,4] == S[4] == 4
@test_throws BoundsError S[1,1]
@test axes(S) === Base.IdentityUnitRange.((0:1, 3:4))
@test axes(S) === Base.IdentityUnitRange.((OffsetArrays.IdOffsetRange(Base.OneTo(2), -1), OffsetArrays.IdOffsetRange(Base.OneTo(2), 2)))
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/27
g = OffsetArray(Vector(-2:3), (-3,))
gv = view(g, -1:2)
@test axes(gv, 1) === Base.OneTo(4)
@test collect(gv) == -1:2
gv = view(g, OffsetArray(-1:2, (-2,)))
@test axes(gv, 1) === Base.IdentityUnitRange(-1:2)
@test axes(gv, 1) === OffsetArrays.IdOffsetRange(Base.OneTo(4), -2)
@test collect(gv) == -1:2
gv = view(g, OffsetArray(-1:2, (-1,)))
@test axes(gv, 1) === Base.IdentityUnitRange(0:3)
@test axes(gv, 1) === OffsetArrays.IdOffsetRange(Base.OneTo(4), -1)
@test collect(gv) == -1:2

# iteration
Expand Down Expand Up @@ -199,7 +198,7 @@ str = String(take!(io))
show(io, parent(v))
@test str == String(take!(io))
smry = summary(v)
@test occursin("OffsetArray{Float64, 1", smry)
@test occursin("OffsetArray(::Vector{Float64", smry)
@test occursin("with indices -1:1", smry)
function cmp_showf(printfunc, io, A; options = ())
ioc = IOContext(io, :limit => true, :compact => true, options...)
Expand All @@ -216,11 +215,11 @@ cmp_showf(Base.print_matrix, io, OffsetArray(rand(10^3,10^3), (10,-9))) # neithe
cmp_showf(Base.print_matrix, io, OffsetArray(reshape(range(-0.212121212121, stop=2/11, length=3*29), 3, 29), (-2, -15)); options=(:displaysize=>(53,210),))
cmp_showf(show, io, OffsetArray(collect(1:100), (100,))) # issue #31641

targets1 = ["0-dimensional $OAs_name.OffsetArray{Float64, 0, Array{Float64, 0}}:\n1.0",
"1-element $OAs_name.OffsetArray{Float64, 1, Vector{Float64}} with indices 2:2:\n 1.0",
"1×1 $OAs_name.OffsetArray{Float64, 2, Matrix{Float64}} with indices 2:2×3:3:\n 1.0",
"1×1×1 $OAs_name.OffsetArray{Float64, 3, Array{Float64, 3}} with indices 2:2×3:3×4:4:\n[:, :, 4] =\n 1.0",
"1×1×1×1 $OAs_name.OffsetArray{Float64, 4, Array{Float64, 4}} with indices 2:2×3:3×4:4×5:5:\n[:, :, 4, 5] =\n 1.0"]
targets1 = ["0-dimensional OffsetArray(::Array{Float64, 0}) with eltype Float64:\n1.0",
"1-element OffsetArray(::Vector{Float64}, 2:2) with eltype Float64 with indices 2:2:\n 1.0",
"1×1 OffsetArray(::Matrix{Float64}, 2:2, 3:3) with eltype Float64 with indices 2:2×3:3:\n 1.0",
"1×1×1 OffsetArray(::Array{Float64, 3}, 2:2, 3:3, 4:4) with eltype Float64 with indices 2:2×3:3×4:4:\n[:, :, 4] =\n 1.0",
"1×1×1×1 OffsetArray(::Array{Float64, 4}, 2:2, 3:3, 4:4, 5:5) with eltype Float64 with indices 2:2×3:3×4:4×5:5:\n[:, :, 4, 5] =\n 1.0"]
targets2 = ["(fill(1.0), fill(1.0))",
"([1.0], [1.0])",
"([1.0], [1.0])",
Expand All @@ -235,7 +234,7 @@ targets2 = ["(fill(1.0), fill(1.0))",
end
P = OffsetArray(rand(8,8), (1,1))
PV = view(P, 2:3, :)
@test endswith(summary(PV), "with indices Base.OneTo(2)×2:9")
@test endswith(summary(PV), "with indices Base.OneTo(2)×OffsetArrays.IdOffsetRange(2:9)")

# Similar
B = similar(A, Float32)
Expand All @@ -247,26 +246,26 @@ B = similar(A, (3,4))
@test axes(B) === (Base.OneTo(3), Base.OneTo(4))
B = similar(A, (-3:3,1:4))
@test isa(B, OffsetArray{Int,2})
@test axes(B) === Base.IdentityUnitRange.((-3:3, 1:4))
@test axes(B) === (OffsetArrays.IdOffsetRange(Base.OneTo(7), -4), OffsetArrays.IdOffsetRange(Base.OneTo(4)))
B = similar(parent(A), (-3:3,1:4))
@test isa(B, OffsetArray{Int,2})
@test axes(B) === Base.IdentityUnitRange.((-3:3, 1:4))
@test axes(B) === (OffsetArrays.IdOffsetRange(Base.OneTo(7), -4), OffsetArrays.IdOffsetRange(Base.OneTo(4)))

# Indexing with OffsetArray indices
i1 = OffsetArray([2,1], (-5,))
i1 = OffsetArray([2,1], -5)
b = A0[i1, 1]
@test axes(b) === (Base.IdentityUnitRange(-4:-3),)
@test axes(b) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
@test b[-4] == 2
@test b[-3] == 1
b = A0[1,i1]
@test axes(b) === (Base.IdentityUnitRange(-4:-3),)
@test axes(b) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
@test b[-4] == 3
@test b[-3] == 1
v = view(A0, i1, 1)
@test axes(v) === (Base.IdentityUnitRange(-4:-3),)
@test axes(v) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
v = view(A0, 1:1, i1)
@test axes(v) === (Base.OneTo(1), Base.IdentityUnitRange(-4:-3))
@test axes(v) === (Base.OneTo(1), OffsetArrays.IdOffsetRange(Base.OneTo(2), -5))

# copyto! and fill!
a = OffsetArray{Int}(undef, (-3:-1,))
Expand Down Expand Up @@ -395,7 +394,7 @@ v2 = copy(v)
v = OffsetArray(v0, (-3,))
@test lastindex(v) == 1
@test v v
@test axes(v') === (Base.OneTo(1),Base.IdentityUnitRange(-2:1))
@test axes(v') === (Base.OneTo(1), OffsetArrays.IdOffsetRange(Base.OneTo(4), -3))
@test parent(v) == collect(v)
rv = reverse(v)
@test axes(rv) == axes(v)
Expand All @@ -411,7 +410,7 @@ A = OffsetArray(rand(4,4), (-3,5))
@test lastindex(A, 1) == 1
@test lastindex(A, 2) == 9
@test A A
@test axes(A') === Base.IdentityUnitRange.((6:9, -2:1))
@test axes(A') === (OffsetArrays.IdOffsetRange(Base.OneTo(4), 5), OffsetArrays.IdOffsetRange(Base.OneTo(4), -3))
@test parent(copy(A')) == copy(parent(A)')
@test collect(A) == parent(A)
@test maximum(A) == maximum(parent(A))
Expand Down Expand Up @@ -583,9 +582,21 @@ module SimilarUR
end
ur = MyURange(1,3)
a = Vector{Int}(undef, 2)
@test_throws MethodError similar(a, ur)
@test_throws MethodError similar(a, Float64, ur)
@test_throws MethodError similar(a, Float64, (ur,))

function catch_exception(f, args...)
try
f(args...)
catch err
return err
end
end
# type-piracy https://github.com/JuliaArrays/OffsetArrays.jl/issues/87
@test_broken (catch_exception(similar, a, ur) isa MethodError)
@test_broken (catch_exception(similar, a, Float64, ur) isa MethodError)
@test_broken (catch_exception(similar, a, Float64, (ur,)) isa MethodError)
# @test_throws MethodError similar(a, ur)
# @test_throws MethodError similar(a, Float64, ur)
# @test_throws MethodError similar(a, Float64, (ur,))
@test_throws MethodError similar(a, (2.0,3.0))
end

Expand Down Expand Up @@ -630,3 +641,101 @@ end
@test last(v, 100) !== v
@test last(v, 1) == [v[end]]
end

@testset "Resizing OffsetVectors" begin
local a = OffsetVector(rand(5),-3)
axes(a,1) == -2:2
length(a) == 5
resize!(a,3)
length(a) == 3
axes(a,1) == -2:0
@test_throws ArgumentError resize!(a,-3)
end

@testset "issue #37199: offset range indices" begin
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/133
A0 = [1 3; 2 4]
A = OffsetArray(A0, (-1,2))

r = OffsetArrays.IdOffsetRange(1:2, -1)
v1 = view(A, r, 3)
@test v1[0] == 1
@test v1[1] == 2
@test axes(v1, 1) == axes(r, 1)
v2 = view(A, UnitRange(r), 3)
for (indflat, indoffset) in enumerate(r)
@test v1[indoffset] == v2[indflat]
end

r = OffsetArrays.IdOffsetRange(1:2, 2)
v1 = view(A, 1, r)
@test v1[3] == 2
@test v1[4] == 4
@test axes(v1, 1) == axes(r, 1)
v2 = view(A, 1, UnitRange(r))
for (indflat, indoffset) in enumerate(r)
@test v1[indoffset] == v2[indflat]
end

a12 = zeros(3:8, 3:4)
r = OffsetArrays.IdOffsetRange(Base.OneTo(3), 5)
a12[r, 4] .= 3
@test all(a12[r, 4] .== 3)
@test all(a12[UnitRange(r), 4] .== 3)

# https://github.com/JuliaArrays/OffsetArrays.jl/issues/100
S = view(A, axes(A)...)
@test S == A
@test S[0,3] == S[1] == 1
@test S[1,3] == S[2] == 2
@test S[0,4] == S[3] == 3
@test S[1,4] == S[4] == 4
@test_throws BoundsError S[1,1]
@test axes(S) == OffsetArrays.IdOffsetRange.((0:1, 3:4))
S = view(A, axes(A, 1), 3)
@test S == A[:, 3]
@test S[0] == 1
@test S[1] == 2
@test_throws BoundsError S[length(S)]
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), )
S = view(A, 1, axes(A, 2))
@test S == A[1, :]
@test S[3] == 2
@test S[4] == 4
@test_throws BoundsError S[1]
@test axes(S) == (OffsetArrays.IdOffsetRange(3:4), )

A0 = collect(reshape(1:24, 2, 3, 4))
A = OffsetArray(A0, (-1,2,1))
S = view(A, axes(A, 1), 3:4, axes(A, 3))
@test S == A[:, 3:4, :]
@test S[0, 1, 2] == A[0, 3, 2]
@test S[0, 2, 2] == A[0, 4, 2]
@test S[1, 1, 2] == A[1, 3, 2]
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), Base.OneTo(2), OffsetArrays.IdOffsetRange(2:5))
end

@testset "IdentityUnitRange indexing" begin
a = OffsetVector(3:4, 2:3)
ax = IdentityUnitRange(2:3)
@test a[ax[2]] == a[ax][2]

s = -2:2:4
r = 5:8
y = OffsetArray(s, r)
@test axes(y) == (r,)
@test step(y) == step(s)

a = OffsetVector(3:4, 10:11)
ax = OffsetArrays.IdOffsetRange(5:6, 5)
@test axes(a[ax]) == axes(ax)
for i in axes(ax,1)
@test a[ax[i]] == a[ax][i]
end

ax = IdentityUnitRange(10:11)
@test axes(a[ax]) == axes(ax)
for i in axes(ax,1)
@test a[ax[i]] == a[ax][i]
end
end
8 changes: 4 additions & 4 deletions test/reinterpretarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ let a = [0.1 0.2; 0.3 0.4], at = reshape([(i,i+1) for i = 1:2:8], 2, 2)
@test r[1,2] === reinterpret(Int64, v[1,2])
@test r[0,3] === reinterpret(Int64, v[0,3])
@test r[1,3] === reinterpret(Int64, v[1,3])
@test_throws ArgumentError("cannot reinterpret a `Float64` array to `UInt32` when the first axis is Base.IdentityUnitRange(0:1). Try reshaping first.") reinterpret(UInt32, v)
@test_throws ArgumentError("`reinterpret(reshape, Tuple{Float64, Float64}, a)` where `eltype(a)` is Float64 requires that `axes(a, 1)` (got Base.IdentityUnitRange(0:1)) be equal to 1:2 (from the ratio of element sizes)") reinterpret(reshape, Tuple{Float64,Float64}, v)
@test_throws ArgumentError("cannot reinterpret a `Float64` array to `UInt32` when the first axis is OffsetArrays.IdOffsetRange(0:1). Try reshaping first.") reinterpret(UInt32, v)
@test_throws ArgumentError("`reinterpret(reshape, Tuple{Float64, Float64}, a)` where `eltype(a)` is Float64 requires that `axes(a, 1)` (got OffsetArrays.IdOffsetRange(0:1)) be equal to 1:2 (from the ratio of element sizes)") reinterpret(reshape, Tuple{Float64,Float64}, v)
v = OffsetArray(a, (0, 1))
@test axes(reinterpret(reshape, Tuple{Float64,Float64}, v)) === (Base.IdentityUnitRange(2:3),)
@test axes(reinterpret(reshape, Tuple{Float64,Float64}, v)) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), 1),)
r = reinterpret(UInt32, v)
axsv = axes(v)
@test axes(r) === (oftype(axsv[1], 1:4), axsv[2])
Expand All @@ -283,7 +283,7 @@ let a = [0.1 0.2; 0.3 0.4], at = reshape([(i,i+1) for i = 1:2:8], 2, 2)
offsetvt = (-2, 4)
vt = OffsetArray(at, offsetvt)
istr = string(Int)
@test_throws ArgumentError("cannot reinterpret a `Tuple{$istr, $istr}` array to `$istr` when the first axis is Base.IdentityUnitRange(-1:0). Try reshaping first.") reinterpret(Int, vt)
@test_throws ArgumentError("cannot reinterpret a `Tuple{$istr, $istr}` array to `$istr` when the first axis is OffsetArrays.IdOffsetRange(-1:0). Try reshaping first.") reinterpret(Int, vt)
vt = reshape(vt, 1:1, axes(vt)...)
r = reinterpret(Int, vt)
@test r == OffsetArray(reshape(1:8, 2, 2, 2), (0, offsetvt...))
Expand Down
Loading

0 comments on commit 4ccfd37

Please sign in to comment.