Skip to content

Commit b5d231a

Browse files
committed
Relax setindex argument types
close #17
1 parent 3e9dcad commit b5d231a

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/ArrayInterface.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ ismutable(::Type{<:Array}) = true
1919
ismutable(::Type{<:Number}) = false
2020

2121
# Piracy
22-
function Base.setindex(x::AbstractArray,v,i::Int...)
22+
function Base.setindex(x::AbstractArray,v,i...)
2323
_x = copy(x)
24-
_x[i] = v
24+
_x[i...] = v
2525
_x
2626
end
2727

test/runtests.jl

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using ArrayInterface, Test
2+
using Base: setindex
23
import ArrayInterface: has_sparsestruct, findstructralnz
34
@test ArrayInterface.ismutable(rand(3))
45

@@ -71,4 +72,50 @@ BBB=BandedBlockBandedMatrix(dense, ([4, 4] ,[4, 4]), (1, 1), (1, 1))
7172
rowind,colind=findstructralnz(BBB)
7273
@test [BBB[rowind[i],colind[i]] for i in 1:length(rowind)]==
7374
[1,2,3,1,2,3,4,2,3,4,5,6,7,5,6,7,8,6,7,8,
74-
1,2,3,1,2,3,4,2,3,4,5,6,7,5,6,7,8,6,7,8]
75+
1,2,3,1,2,3,4,2,3,4,5,6,7,5,6,7,8,6,7,8]
76+
77+
@testset "setindex" begin
78+
@testset "$(typeof(x))" for x in [
79+
zeros(3),
80+
falses(3),
81+
spzeros(3),
82+
]
83+
y = setindex(x, true, 1)
84+
@test iszero(x) # x is not mutated
85+
@test y[1] == true
86+
@test iszero(x[CartesianIndices(size(x)) .== [CartesianIndex(1)]])
87+
88+
y2 = setindex(x, one.(x), :)
89+
@test iszero(x)
90+
@test all(isone, y2)
91+
end
92+
93+
@testset "$(typeof(x))" for x in [
94+
zeros(3, 3),
95+
falses(3, 3),
96+
spzeros(3, 3),
97+
]
98+
y = setindex(x, true, 1, 1)
99+
@test iszero(x) # x is not mutated
100+
@test y[1, 1] == true
101+
@test iszero(x[CartesianIndices(size(x)) .== [CartesianIndex(1, 1)]])
102+
103+
y2 = setindex(x, one.(x), :, :)
104+
@test iszero(x)
105+
@test all(isone, y2)
106+
end
107+
108+
@testset "$(typeof(x))" for x in [
109+
zeros(3, 3, 3),
110+
falses(3, 3, 3),
111+
]
112+
y = setindex(x, true, 1, 1, 1)
113+
@test iszero(x) # x is not mutated
114+
@test y[1, 1, 1] == true
115+
@test iszero(x[CartesianIndices(size(x)) .== [CartesianIndex(1, 1, 1)]])
116+
117+
y2 = setindex(x, one.(x), :, :, :)
118+
@test iszero(x)
119+
@test all(isone, y2)
120+
end
121+
end

0 commit comments

Comments
 (0)