Skip to content

Commit 4b0b691

Browse files
committed
More ReshapedArray tests
1 parent 72af884 commit 4b0b691

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

test/arrayops.jl

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,50 @@ a = reshape(b, (2, 2, 2, 2, 2))
8888
@test a[2,2,2,2,2] == b[end]
8989

9090
# reshaping linearslow arrays
91-
a = zeros(1, 5)
91+
a = collect(reshape(1:5, 1, 5))
9292
s = sub(a, :, [2,3,5])
93-
@test length(reshape(s, length(s))) == 3
93+
r = reshape(s, length(s))
94+
@test length(r) == 3
95+
@test r[1] == 2
96+
@test r[3,1] == 5
97+
@test r[Base.ReshapedIndex(CartesianIndex((1,2)))] == 3
98+
@test parent(reshape(r, (1,3))) === r.parent === s
99+
@test parentindexes(r) == (1:1, 1:3)
100+
@test reshape(r, (3,)) === r
101+
r[2] = -1
102+
@test a[3] == -1
94103
a = zeros(0, 5) # an empty linearslow array
95104
s = sub(a, :, [2,3,5])
96105
@test length(reshape(s, length(s))) == 0
97106

107+
@test reshape(1:5, (5,)) === 1:5
108+
@test reshape(1:5, 5) === 1:5
109+
110+
# setindex! on a reshaped range
111+
a = reshape(1:20, 5, 4)
112+
for idx in ((3,), (2,2), (Base.ReshapedIndex(1),))
113+
try
114+
a[idx...] = 7
115+
catch err
116+
@test err.msg == "indexed assignment fails for a reshaped range; consider calling collect"
117+
end
118+
end
119+
120+
# operations with LinearFast ReshapedArray
121+
b = collect(1:12)
122+
a = Base.ReshapedArray(b, (4,3), ())
123+
@test a[3,2] == 7
124+
@test a[6] == 6
125+
a[3,2] = -2
126+
a[6] = -3
127+
a[Base.ReshapedIndex(5)] = -4
128+
@test b[5] == -4
129+
@test b[6] == -3
130+
@test b[7] == -2
131+
b = reinterpret(Int, a, (3,4))
132+
b[1] = -1
133+
@test vec(b) == vec(a)
134+
98135
a = rand(1, 1, 8, 8, 1)
99136
@test @inferred(squeeze(a, 1)) == @inferred(squeeze(a, (1,))) == reshape(a, (1, 8, 8, 1))
100137
@test @inferred(squeeze(a, (1, 5))) == squeeze(a, (5, 1)) == reshape(a, (1, 8, 8))

0 commit comments

Comments
 (0)