Skip to content

Commit 87886f5

Browse files
authored
Merge pull request #108 from Tokazama/indexing-tests
Indexing tests
2 parents 82341bf + 0f321be commit 87886f5

File tree

3 files changed

+50
-32
lines changed

3 files changed

+50
-32
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.14.14"
3+
version = "2.14.15"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/indexing.jl

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -322,40 +322,25 @@ end
322322
Reconstruct `A` given the values in `data`. New methods using `unsafe_reconstruct`
323323
should only dispatch on `A`.
324324
"""
325-
function unsafe_reconstruct(A::OneTo, data; kwargs...)
326-
if can_change_size(A)
327-
return typeof(A)(data)
325+
function unsafe_reconstruct(axis::OneTo, data; kwargs...)
326+
if axis === data
327+
return axis
328328
else
329-
if data isa Slice ||
330-
!(known_length(A) === nothing || known_length(A) !== known_length(data))
331-
return A
332-
else
333-
return OneTo(data)
334-
end
329+
return OneTo(data)
335330
end
336331
end
337-
function unsafe_reconstruct(A::UnitRange, data; kwargs...)
338-
if can_change_size(A)
339-
return typeof(A)(data)
332+
function unsafe_reconstruct(axis::UnitRange, data; kwargs...)
333+
if axis === data
334+
return axis
340335
else
341-
if data isa Slice ||
342-
!(known_length(A) === nothing || known_length(A) !== known_length(data))
343-
return A
344-
else
345-
return UnitRange(data)
346-
end
336+
return UnitRange(first(data), last(data))
347337
end
348338
end
349-
function unsafe_reconstruct(A::OptionallyStaticUnitRange, data; kwargs...)
350-
if can_change_size(A)
351-
return typeof(A)(data)
339+
function unsafe_reconstruct(axis::OptionallyStaticUnitRange, data; kwargs...)
340+
if axis === data
341+
return axis
352342
else
353-
if data isa Slice ||
354-
!(known_length(A) === nothing || known_length(A) !== known_length(data))
355-
return A
356-
else
357-
return OptionallyStaticUnitRange(data)
358-
end
343+
return OptionallyStaticUnitRange(static_first(data), static_last(data))
359344
end
360345
end
361346
function unsafe_reconstruct(A::AbstractUnitRange, data; kwargs...)
@@ -420,6 +405,15 @@ previously executed `to_index(old_axis, arg) -> index`. `to_axis` assumes that
420405
return to_axis(IndexStyle(axis), axis, inds)
421406
end
422407
end
408+
409+
# don't need to check size b/c slice means it's the entire axis
410+
@inline function to_axis(axis, inds::Slice)
411+
if can_change_size(axis)
412+
return copy(axis)
413+
else
414+
return axis
415+
end
416+
end
423417
@inline function to_axis(S::IndexStyle, axis, inds)
424418
return unsafe_reconstruct(axis, StaticInt(1):static_length(inds))
425419
end

test/indexing.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
end
99

1010
@testset "UnsafeIndex" begin
11-
@test @inferred(ArrayInterface.UnsafeIndex(ones(2,2,2), typeof((1,[1,2],1)))) == ArrayInterface.UnsafeGetCollection()
11+
@test @inferred(ArrayInterface.UnsafeIndex(ones(2,2,2), typeof((1,[1,2],1)))) == ArrayInterface.UnsafeGetCollection()
1212
@test @inferred(ArrayInterface.UnsafeIndex(ones(2,2,2), typeof((1,1,1)))) == ArrayInterface.UnsafeGetElement()
1313
end
1414

@@ -19,10 +19,24 @@ end
1919
@test @inferred(ArrayInterface.to_index(axis, [1, 2])) == [1, 2]
2020
@test @inferred(ArrayInterface.to_index(axis, [true, false, false])) == [1]
2121

22-
@test_throws BoundsError ArrayInterface.to_index(axis, 4)
23-
@test_throws BoundsError ArrayInterface.to_index(axis, 1:4)
22+
@test_throws BoundsError ArrayInterface.to_index(axis, 4)
23+
@test_throws BoundsError ArrayInterface.to_index(axis, 1:4)
2424
@test_throws BoundsError ArrayInterface.to_index(axis, [1, 2, 5])
25-
@test_throws BoundsError ArrayInterface.to_index(axis, [true, false, false, true])
25+
@test_throws BoundsError ArrayInterface.to_index(axis, [true, false, false, true])
26+
end
27+
28+
@testset "unsafe_reconstruct" begin
29+
one_to = Base.OneTo(10)
30+
opt_ur = StaticInt(1):10
31+
ur = 1:10
32+
@test @inferred(ArrayInterface.unsafe_reconstruct(one_to, opt_ur)) === one_to
33+
@test @inferred(ArrayInterface.unsafe_reconstruct(one_to, one_to)) === one_to
34+
35+
@test @inferred(ArrayInterface.unsafe_reconstruct(opt_ur, opt_ur)) === opt_ur
36+
@test @inferred(ArrayInterface.unsafe_reconstruct(opt_ur, one_to)) === opt_ur
37+
38+
@test @inferred(ArrayInterface.unsafe_reconstruct(ur, ur)) === ur
39+
@test @inferred(ArrayInterface.unsafe_reconstruct(ur, one_to)) === ur
2640
end
2741

2842
@testset "to_indices" begin
@@ -69,6 +83,9 @@ end
6983
@test @inferred(ArrayInterface.to_axes(A, (axis, axis), (inds,))) === (inds,)
7084
# multidim arg
7185
@test @inferred(ArrayInterface.to_axes(A, (axis, axis), (multi_inds,))) === (Base.OneTo(2),)
86+
87+
@test ArrayInterface.to_axis(axis, axis) === axis
88+
@test ArrayInterface.to_axis(axis, ArrayInterface.indices(axis)) === axis
7289
end
7390

7491
@testset "0-dimensional" begin
@@ -96,6 +113,12 @@ end
96113
# TODO should this be implemented in ArrayInterface with vectorization?
97114
#@test_throws ArgumentError Base._sub2ind((1:3,), 2)
98115
#@test_throws ArgumentError Base._ind2sub((1:3,), 2)
116+
x = Array{Int,2}(undef, (2, 2))
117+
ArrayInterface.unsafe_set_element!(x, 1, (2, 2))
118+
@test ArrayInterface.unsafe_get_element(x, (2, 2)) === 1
119+
120+
@test_throws MethodError ArrayInterface.unsafe_set_element!(x, 1, (:x, :x))
121+
@test_throws MethodError ArrayInterface.unsafe_get_element(x, (:x, :x))
99122
end
100123

101124
@testset "2-dimensional" begin
@@ -154,3 +177,4 @@ end
154177
@test @inferred(ArrayInterface.getindex(LinearIndices(A),ArrayInterface.getindex(CartesianIndices(A),i))) == i
155178
end
156179
end
180+

0 commit comments

Comments
 (0)