Skip to content

Commit 4ccfd37

Browse files
authored
upgrade OffsetArrays to v1.3.0 (#37643)
* upgrade to OffsetArrays v1.3.0 * everywhere include OffsetArrays * more tests for #37204 OffsetArrays issues 133 and 100 are fixed by #37204.
1 parent be1f3ab commit 4ccfd37

File tree

5 files changed

+527
-121
lines changed

5 files changed

+527
-121
lines changed

stdlib/Distributed/test/splitrange.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using Distributed: splitrange
2222
@test splitrange(-1, 1, 4) == Array{UnitRange{Int64},1}([-1:-1,0:0,1:1])
2323

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

2828
oa = OffsetArray([123, -345], (-2,))

test/arrayops.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,8 +1428,8 @@ end
14281428

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

14341434
# empty non-1-indexed array
14351435
eoa = OffsetArray([], -5)

test/offsetarray.jl

Lines changed: 136 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ using DelimitedFiles
66
using Random
77
using LinearAlgebra
88
using Statistics
9-
10-
const OAs_name = join(fullname(OffsetArrays), ".")
9+
using Base: IdentityUnitRange
1110

1211
if !isdefined(@__MODULE__, :T24Linear)
1312
include("testhelpers/arrayindexingtypes.jl")
@@ -18,15 +17,15 @@ let
1817
v0 = rand(4)
1918
v = OffsetArray(v0, (-3,))
2019
h = OffsetArray([-1,1,-2,2,0], (-3,))
21-
@test axes(v) == (-2:1,)
20+
@test axes(v) === (OffsetArrays.IdOffsetRange(Base.OneTo(4), -3),)
2221
@test size(v) == (4,)
2322
@test size(v, 1) == 4
2423
@test_throws DimensionMismatch Array(v)
2524

2625
A0 = [1 3; 2 4]
2726
A = OffsetArray(A0, (-1,2)) # IndexLinear
2827
S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # IndexCartesian
29-
@test axes(A) == axes(S) == (0:1, 3:4)
28+
@test axes(A) === axes(S) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -1), OffsetArrays.IdOffsetRange(Base.OneTo(2), 2))
3029
@test size(A) == (2,2)
3130
@test size(A, 1) == 2
3231

@@ -133,13 +132,13 @@ S = view(A, :, 3)
133132
@test S[0] == 1
134133
@test S[1] == 2
135134
@test_throws BoundsError S[2]
136-
@test axes(S) === (Base.IdentityUnitRange(0:1),)
135+
@test axes(S) === (Base.IdentityUnitRange(OffsetArrays.IdOffsetRange(Base.OneTo(2), -1)),)
137136
S = view(A, 0, :)
138137
@test S == OffsetArray([1,3], (A.offsets[2],))
139138
@test S[3] == 1
140139
@test S[4] == 3
141140
@test_throws BoundsError S[1]
142-
@test axes(S) === (Base.IdentityUnitRange(3:4),)
141+
@test axes(S) === (Base.IdentityUnitRange(OffsetArrays.IdOffsetRange(Base.OneTo(2), 2)),)
143142
S = view(A, 0:0, 4)
144143
@test S == [3]
145144
@test S[1] == 3
@@ -158,17 +157,17 @@ S = view(A, :, :)
158157
@test S[0,4] == S[3] == 3
159158
@test S[1,4] == S[4] == 4
160159
@test_throws BoundsError S[1,1]
161-
@test axes(S) === Base.IdentityUnitRange.((0:1, 3:4))
160+
@test axes(S) === Base.IdentityUnitRange.((OffsetArrays.IdOffsetRange(Base.OneTo(2), -1), OffsetArrays.IdOffsetRange(Base.OneTo(2), 2)))
162161
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/27
163162
g = OffsetArray(Vector(-2:3), (-3,))
164163
gv = view(g, -1:2)
165164
@test axes(gv, 1) === Base.OneTo(4)
166165
@test collect(gv) == -1:2
167166
gv = view(g, OffsetArray(-1:2, (-2,)))
168-
@test axes(gv, 1) === Base.IdentityUnitRange(-1:2)
167+
@test axes(gv, 1) === OffsetArrays.IdOffsetRange(Base.OneTo(4), -2)
169168
@test collect(gv) == -1:2
170169
gv = view(g, OffsetArray(-1:2, (-1,)))
171-
@test axes(gv, 1) === Base.IdentityUnitRange(0:3)
170+
@test axes(gv, 1) === OffsetArrays.IdOffsetRange(Base.OneTo(4), -1)
172171
@test collect(gv) == -1:2
173172

174173
# iteration
@@ -199,7 +198,7 @@ str = String(take!(io))
199198
show(io, parent(v))
200199
@test str == String(take!(io))
201200
smry = summary(v)
202-
@test occursin("OffsetArray{Float64, 1", smry)
201+
@test occursin("OffsetArray(::Vector{Float64", smry)
203202
@test occursin("with indices -1:1", smry)
204203
function cmp_showf(printfunc, io, A; options = ())
205204
ioc = IOContext(io, :limit => true, :compact => true, options...)
@@ -216,11 +215,11 @@ cmp_showf(Base.print_matrix, io, OffsetArray(rand(10^3,10^3), (10,-9))) # neithe
216215
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),))
217216
cmp_showf(show, io, OffsetArray(collect(1:100), (100,))) # issue #31641
218217

219-
targets1 = ["0-dimensional $OAs_name.OffsetArray{Float64, 0, Array{Float64, 0}}:\n1.0",
220-
"1-element $OAs_name.OffsetArray{Float64, 1, Vector{Float64}} with indices 2:2:\n 1.0",
221-
"1×1 $OAs_name.OffsetArray{Float64, 2, Matrix{Float64}} with indices 2:2×3:3:\n 1.0",
222-
"1×1×1 $OAs_name.OffsetArray{Float64, 3, Array{Float64, 3}} with indices 2:2×3:3×4:4:\n[:, :, 4] =\n 1.0",
223-
"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"]
218+
targets1 = ["0-dimensional OffsetArray(::Array{Float64, 0}) with eltype Float64:\n1.0",
219+
"1-element OffsetArray(::Vector{Float64}, 2:2) with eltype Float64 with indices 2:2:\n 1.0",
220+
"1×1 OffsetArray(::Matrix{Float64}, 2:2, 3:3) with eltype Float64 with indices 2:2×3:3:\n 1.0",
221+
"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",
222+
"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"]
224223
targets2 = ["(fill(1.0), fill(1.0))",
225224
"([1.0], [1.0])",
226225
"([1.0], [1.0])",
@@ -235,7 +234,7 @@ targets2 = ["(fill(1.0), fill(1.0))",
235234
end
236235
P = OffsetArray(rand(8,8), (1,1))
237236
PV = view(P, 2:3, :)
238-
@test endswith(summary(PV), "with indices Base.OneTo(2)×2:9")
237+
@test endswith(summary(PV), "with indices Base.OneTo(2)×OffsetArrays.IdOffsetRange(2:9)")
239238

240239
# Similar
241240
B = similar(A, Float32)
@@ -247,26 +246,26 @@ B = similar(A, (3,4))
247246
@test axes(B) === (Base.OneTo(3), Base.OneTo(4))
248247
B = similar(A, (-3:3,1:4))
249248
@test isa(B, OffsetArray{Int,2})
250-
@test axes(B) === Base.IdentityUnitRange.((-3:3, 1:4))
249+
@test axes(B) === (OffsetArrays.IdOffsetRange(Base.OneTo(7), -4), OffsetArrays.IdOffsetRange(Base.OneTo(4)))
251250
B = similar(parent(A), (-3:3,1:4))
252251
@test isa(B, OffsetArray{Int,2})
253-
@test axes(B) === Base.IdentityUnitRange.((-3:3, 1:4))
252+
@test axes(B) === (OffsetArrays.IdOffsetRange(Base.OneTo(7), -4), OffsetArrays.IdOffsetRange(Base.OneTo(4)))
254253

255254
# Indexing with OffsetArray indices
256255
i1 = OffsetArray([2,1], (-5,))
257256
i1 = OffsetArray([2,1], -5)
258257
b = A0[i1, 1]
259-
@test axes(b) === (Base.IdentityUnitRange(-4:-3),)
258+
@test axes(b) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
260259
@test b[-4] == 2
261260
@test b[-3] == 1
262261
b = A0[1,i1]
263-
@test axes(b) === (Base.IdentityUnitRange(-4:-3),)
262+
@test axes(b) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
264263
@test b[-4] == 3
265264
@test b[-3] == 1
266265
v = view(A0, i1, 1)
267-
@test axes(v) === (Base.IdentityUnitRange(-4:-3),)
266+
@test axes(v) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), -5),)
268267
v = view(A0, 1:1, i1)
269-
@test axes(v) === (Base.OneTo(1), Base.IdentityUnitRange(-4:-3))
268+
@test axes(v) === (Base.OneTo(1), OffsetArrays.IdOffsetRange(Base.OneTo(2), -5))
270269

271270
# copyto! and fill!
272271
a = OffsetArray{Int}(undef, (-3:-1,))
@@ -395,7 +394,7 @@ v2 = copy(v)
395394
v = OffsetArray(v0, (-3,))
396395
@test lastindex(v) == 1
397396
@test v v
398-
@test axes(v') === (Base.OneTo(1),Base.IdentityUnitRange(-2:1))
397+
@test axes(v') === (Base.OneTo(1), OffsetArrays.IdOffsetRange(Base.OneTo(4), -3))
399398
@test parent(v) == collect(v)
400399
rv = reverse(v)
401400
@test axes(rv) == axes(v)
@@ -411,7 +410,7 @@ A = OffsetArray(rand(4,4), (-3,5))
411410
@test lastindex(A, 1) == 1
412411
@test lastindex(A, 2) == 9
413412
@test A A
414-
@test axes(A') === Base.IdentityUnitRange.((6:9, -2:1))
413+
@test axes(A') === (OffsetArrays.IdOffsetRange(Base.OneTo(4), 5), OffsetArrays.IdOffsetRange(Base.OneTo(4), -3))
415414
@test parent(copy(A')) == copy(parent(A)')
416415
@test collect(A) == parent(A)
417416
@test maximum(A) == maximum(parent(A))
@@ -583,9 +582,21 @@ module SimilarUR
583582
end
584583
ur = MyURange(1,3)
585584
a = Vector{Int}(undef, 2)
586-
@test_throws MethodError similar(a, ur)
587-
@test_throws MethodError similar(a, Float64, ur)
588-
@test_throws MethodError similar(a, Float64, (ur,))
585+
586+
function catch_exception(f, args...)
587+
try
588+
f(args...)
589+
catch err
590+
return err
591+
end
592+
end
593+
# type-piracy https://github.com/JuliaArrays/OffsetArrays.jl/issues/87
594+
@test_broken (catch_exception(similar, a, ur) isa MethodError)
595+
@test_broken (catch_exception(similar, a, Float64, ur) isa MethodError)
596+
@test_broken (catch_exception(similar, a, Float64, (ur,)) isa MethodError)
597+
# @test_throws MethodError similar(a, ur)
598+
# @test_throws MethodError similar(a, Float64, ur)
599+
# @test_throws MethodError similar(a, Float64, (ur,))
589600
@test_throws MethodError similar(a, (2.0,3.0))
590601
end
591602

@@ -630,3 +641,101 @@ end
630641
@test last(v, 100) !== v
631642
@test last(v, 1) == [v[end]]
632643
end
644+
645+
@testset "Resizing OffsetVectors" begin
646+
local a = OffsetVector(rand(5),-3)
647+
axes(a,1) == -2:2
648+
length(a) == 5
649+
resize!(a,3)
650+
length(a) == 3
651+
axes(a,1) == -2:0
652+
@test_throws ArgumentError resize!(a,-3)
653+
end
654+
655+
@testset "issue #37199: offset range indices" begin
656+
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/133
657+
A0 = [1 3; 2 4]
658+
A = OffsetArray(A0, (-1,2))
659+
660+
r = OffsetArrays.IdOffsetRange(1:2, -1)
661+
v1 = view(A, r, 3)
662+
@test v1[0] == 1
663+
@test v1[1] == 2
664+
@test axes(v1, 1) == axes(r, 1)
665+
v2 = view(A, UnitRange(r), 3)
666+
for (indflat, indoffset) in enumerate(r)
667+
@test v1[indoffset] == v2[indflat]
668+
end
669+
670+
r = OffsetArrays.IdOffsetRange(1:2, 2)
671+
v1 = view(A, 1, r)
672+
@test v1[3] == 2
673+
@test v1[4] == 4
674+
@test axes(v1, 1) == axes(r, 1)
675+
v2 = view(A, 1, UnitRange(r))
676+
for (indflat, indoffset) in enumerate(r)
677+
@test v1[indoffset] == v2[indflat]
678+
end
679+
680+
a12 = zeros(3:8, 3:4)
681+
r = OffsetArrays.IdOffsetRange(Base.OneTo(3), 5)
682+
a12[r, 4] .= 3
683+
@test all(a12[r, 4] .== 3)
684+
@test all(a12[UnitRange(r), 4] .== 3)
685+
686+
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/100
687+
S = view(A, axes(A)...)
688+
@test S == A
689+
@test S[0,3] == S[1] == 1
690+
@test S[1,3] == S[2] == 2
691+
@test S[0,4] == S[3] == 3
692+
@test S[1,4] == S[4] == 4
693+
@test_throws BoundsError S[1,1]
694+
@test axes(S) == OffsetArrays.IdOffsetRange.((0:1, 3:4))
695+
S = view(A, axes(A, 1), 3)
696+
@test S == A[:, 3]
697+
@test S[0] == 1
698+
@test S[1] == 2
699+
@test_throws BoundsError S[length(S)]
700+
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), )
701+
S = view(A, 1, axes(A, 2))
702+
@test S == A[1, :]
703+
@test S[3] == 2
704+
@test S[4] == 4
705+
@test_throws BoundsError S[1]
706+
@test axes(S) == (OffsetArrays.IdOffsetRange(3:4), )
707+
708+
A0 = collect(reshape(1:24, 2, 3, 4))
709+
A = OffsetArray(A0, (-1,2,1))
710+
S = view(A, axes(A, 1), 3:4, axes(A, 3))
711+
@test S == A[:, 3:4, :]
712+
@test S[0, 1, 2] == A[0, 3, 2]
713+
@test S[0, 2, 2] == A[0, 4, 2]
714+
@test S[1, 1, 2] == A[1, 3, 2]
715+
@test axes(S) == (OffsetArrays.IdOffsetRange(0:1), Base.OneTo(2), OffsetArrays.IdOffsetRange(2:5))
716+
end
717+
718+
@testset "IdentityUnitRange indexing" begin
719+
a = OffsetVector(3:4, 2:3)
720+
ax = IdentityUnitRange(2:3)
721+
@test a[ax[2]] == a[ax][2]
722+
723+
s = -2:2:4
724+
r = 5:8
725+
y = OffsetArray(s, r)
726+
@test axes(y) == (r,)
727+
@test step(y) == step(s)
728+
729+
a = OffsetVector(3:4, 10:11)
730+
ax = OffsetArrays.IdOffsetRange(5:6, 5)
731+
@test axes(a[ax]) == axes(ax)
732+
for i in axes(ax,1)
733+
@test a[ax[i]] == a[ax][i]
734+
end
735+
736+
ax = IdentityUnitRange(10:11)
737+
@test axes(a[ax]) == axes(ax)
738+
for i in axes(ax,1)
739+
@test a[ax[i]] == a[ax][i]
740+
end
741+
end

test/reinterpretarray.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
262262
@test r[1,2] === reinterpret(Int64, v[1,2])
263263
@test r[0,3] === reinterpret(Int64, v[0,3])
264264
@test r[1,3] === reinterpret(Int64, v[1,3])
265-
@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)
266-
@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)
265+
@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)
266+
@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)
267267
v = OffsetArray(a, (0, 1))
268-
@test axes(reinterpret(reshape, Tuple{Float64,Float64}, v)) === (Base.IdentityUnitRange(2:3),)
268+
@test axes(reinterpret(reshape, Tuple{Float64,Float64}, v)) === (OffsetArrays.IdOffsetRange(Base.OneTo(2), 1),)
269269
r = reinterpret(UInt32, v)
270270
axsv = axes(v)
271271
@test axes(r) === (oftype(axsv[1], 1:4), axsv[2])
@@ -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)
283283
offsetvt = (-2, 4)
284284
vt = OffsetArray(at, offsetvt)
285285
istr = string(Int)
286-
@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)
286+
@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)
287287
vt = reshape(vt, 1:1, axes(vt)...)
288288
r = reinterpret(Int, vt)
289289
@test r == OffsetArray(reshape(1:8, 2, 2, 2), (0, offsetvt...))

0 commit comments

Comments
 (0)