-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathpartitions_test.jl
307 lines (256 loc) · 7.91 KB
/
partitions_test.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using RecursiveArrayTools, Test, Statistics, ArrayInterface
@test length(ArrayPartition()) == 0
@test isempty(ArrayPartition())
A = (rand(5), rand(5))
p = ArrayPartition(A)
@inferred p[1]
@test (p.x[1][1], p.x[2][1]) == (p[1], p[6])
p = ArrayPartition(A, Val{true})
@test !(p.x[1] === A[1])
p2 = similar(p)
p2[1] = 1
@test p2.x[1] != p.x[1]
C = rand(10)
p3 = similar(p, axes(p))
@test length(p3.x[1]) == length(p3.x[2]) == 5
@test length(p.x) == length(p2.x) == length(p3.x) == 2
A = (rand(5), rand(5))
p = ArrayPartition(A)
B = (rand(5), rand(5))
p2 = ArrayPartition(B)
a = 5
@. p = p * 5
@. p = p * a
@. p = p * p2
K = p .* p2
x = rand(10)
y = p .* x
@test y[1:5] == p.x[1] .* x[1:5]
@test y[6:10] == p.x[2] .* x[6:10]
y = p .* x'
for i in 1:10
@test y[1:5, i] == p.x[1] .* x[i]
@test y[6:10, i] == p.x[2] .* x[i]
end
y = p .* p'
@test y[1:5, 1:5] == p.x[1] .* p.x[1]'
@test y[6:10, 6:10] == p.x[2] .* p.x[2]'
@test y[1:5, 6:10] == p.x[1] .* p.x[2]'
@test y[6:10, 1:5] == p.x[2] .* p.x[1]'
a = ArrayPartition([1], [2])
a .= [10, 20]
b = rand(10)
c = rand(10)
copyto!(b, p)
@test b[1:5] == p.x[1]
@test b[6:10] == p.x[2]
copyto!(p, c)
@test c[1:5] == p.x[1]
@test c[6:10] == p.x[2]
## inference tests
x = ArrayPartition([1, 2], [3.0, 4.0])
y = ArrayPartition(ArrayPartition([1], [2.0]), ArrayPartition([3], [4.0]))
@test x[:, 1] == (1, 3.0)
# similar partitions
@inferred similar(x)
@test similar(x, (4,)) isa ArrayPartition{Float64}
@test (@inferred similar(x, (2, 2))) isa AbstractMatrix{Float64}
@inferred similar(x, Int)
@test similar(x, Int, (4,)) isa ArrayPartition{Int}
@test (@inferred similar(x, Int, (2, 2))) isa AbstractMatrix{Int}
# @inferred similar(x, Int, Float64)
@inferred similar(y)
@test similar(y, (4,)) isa ArrayPartition{Float64}
@test (@inferred similar(y, (2, 2))) isa AbstractMatrix{Float64}
@inferred similar(y, Int)
@test similar(y, Int, (4,)) isa ArrayPartition{Int}
@test (@inferred similar(y, Int, (2, 2))) isa AbstractMatrix{Int}
# Copy
@inferred copy(x)
@inferred copy(ArrayPartition(x, x))
# zero
@inferred zero(x)
@inferred zero(x, (2, 2))
@inferred zero(x)
@inferred zero(ArrayPartition(x, x))
# ones
@inferred ones(x)
@inferred ones(x, (2, 2))
# vector space calculations
@inferred x + 5
@inferred 5 + x
@inferred x - 5
@inferred 5 - x
@inferred x * 5
@inferred 5 * x
@inferred x / 5
@inferred 5 \ x
@inferred x + x
@inferred x - x
@inferred y + 5
@inferred 5 + y
@inferred y - 5
@inferred 5 - y
@inferred y * 5
@inferred 5 * y
@inferred y / 5
@inferred 5 \ y
@inferred y + y
@inferred y - y
# indexing
@inferred first(x)
@inferred last(x)
# recursive
@inferred recursive_mean(x)
@inferred recursive_one(x)
@inferred recursive_bottom_eltype(x)
# mapreduce
@inferred Union{Int, Float64} sum(x)
@inferred sum(ArrayPartition(ArrayPartition(zeros(4, 4))))
@inferred sum(ArrayPartition(ArrayPartition(zeros(4))))
@inferred sum(ArrayPartition(zeros(4, 4)))
@inferred mapreduce(string, *, x)
@test mapreduce(i -> string(i) * "q", *, x) == "1q2q3.0q4.0q"
# any
@test !any(isnan, ArrayPartition([1, 2], [3.0, 4.0]))
@test !any(isnan, ArrayPartition([3.0, 4.0]))
@test any(isnan, ArrayPartition([NaN], [3.0, 4.0]))
@test any(isnan, ArrayPartition([NaN]))
@test any(isnan, ArrayPartition(ArrayPartition([NaN])))
@test any(isnan, ArrayPartition([2], [NaN]))
@test any(isnan, ArrayPartition([2], ArrayPartition([NaN])))
# all
@test !all(isnan, ArrayPartition([1, 2], [3.0, 4.0]))
@test !all(isnan, ArrayPartition([3.0, 4.0]))
@test !all(isnan, ArrayPartition([NaN], [3.0, 4.0]))
@test all(isnan, ArrayPartition([NaN]))
@test all(isnan, ArrayPartition(ArrayPartition([NaN])))
@test !all(isnan, ArrayPartition([2], [NaN]))
@test all(isnan, ArrayPartition([NaN], [NaN]))
@test all(isnan, ArrayPartition([NaN], ArrayPartition([NaN])))
# broadcasting
_scalar_op(y) = y + 1
# Can't do `@inferred(_scalar_op.(x))` so we wrap that in a function:
_broadcast_wrapper(y) = _scalar_op.(y)
# Issue #8
@inferred _broadcast_wrapper(x)
# Testing map
@test map(x -> x^2, x) == ArrayPartition(x.x[1] .^ 2, x.x[2] .^ 2)
# Testing filter
@test filter(x -> iseven(round(Int, x)), x) == ArrayPartition([2], [4.0])
#### testing copyto!
S = [
((1,), (2,)) => ((1,), (2,)),
((3, 2), (2,)) => ((3, 2), (2,)),
((3, 2), (2,)) => ((3,), (3,), (2,))
]
for sizes in S
local x = ArrayPartition(randn.(sizes[1]))
local y = ArrayPartition(zeros.(sizes[2]))
y_array = zeros(length(x))
copyto!(y, x) #testing Base.copyto!(dest::ArrayPartition,A::ArrayPartition)
copyto!(y_array, x) #testing Base.copyto!(dest::Array,A::ArrayPartition)
@test all([x[i] == y[i] for i in eachindex(x)])
@test all([x[i] == y_array[i] for i in eachindex(x)])
end
# Non-allocating broadcast
xce0 = ArrayPartition(zeros(2), [0.0])
xcde0 = copy(xce0)
function foo(y, x)
y .= y .+ x
nothing
end
foo(xcde0, xce0)
#@test 0 == @allocated foo(xcde0, xce0)
function foo2(y, x)
y .= y .+ 2 .* x
nothing
end
foo2(xcde0, xce0)
#@test 0 == @allocated foo(xcde0, xce0)
# Custom AbstractArray types broadcasting
struct MyType{T} <: AbstractVector{T}
data::Vector{T}
end
Base.similar(A::MyType{T}) where {T} = MyType{T}(similar(A.data))
Base.similar(A::MyType{T}, ::Type{S}) where {T, S} = MyType(similar(A.data, S))
Base.size(A::MyType) = size(A.data)
Base.getindex(A::MyType, i::Int) = getindex(A.data, i)
Base.setindex!(A::MyType, v, i::Int) = setindex!(A.data, v, i)
Base.IndexStyle(::MyType) = IndexLinear()
Base.BroadcastStyle(::Type{<:MyType}) = Broadcast.ArrayStyle{MyType}()
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyType}},
::Type{T}) where {T}
similar(find_mt(bc), T)
end
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyType}})
similar(find_mt(bc))
end
find_mt(bc::Base.Broadcast.Broadcasted) = find_mt(bc.args)
find_mt(args::Tuple) = find_mt(find_mt(args[1]), Base.tail(args))
find_mt(x) = x
find_mt(::Tuple{}) = nothing
find_mt(a::MyType, rest) = a
find_mt(::Any, rest) = find_mt(rest)
ap = ArrayPartition(MyType(ones(10)), collect(1:2))
up = ap .+ 1
@test typeof(ap) == typeof(up)
up = 2 .* ap .+ 1
@test typeof(ap) == typeof(up)
# Test that `zeros()` does not get screwed up
ap = ArrayPartition(zeros(), [1.0])
up = ap .+ 1
@test typeof(ap) == typeof(up)
up = 2 .* ap .+ 1
@test typeof(ap) == typeof(up)
@testset "ArrayInterface.ismutable(ArrayPartition($a, $b)) == $r" for (a, b, r) in (
(1,
2,
false),
([
1
],
2,
false),
([
1
],
[
2
],
true))
@test ArrayInterface.ismutable(ArrayPartition(a, b)) == r
end
# Test unary minus
x = ArrayPartition(ArrayPartition([1, 2]), [3, 4])
@test -x == 0 - x
@test typeof(x) === typeof(-x)
# Test conversions
begin
b = [1, 2, 3]
c = [1 2; 3 4]
d = ArrayPartition(view(b, :), c)
new_type = ArrayPartition{Float64, Tuple{Vector{Float64}, Matrix{Float64}}}
@test (@inferred convert(new_type, d)) isa new_type
@test convert(new_type, d) == d
@test_throws MethodError convert(new_type, ArrayPartition(view(b, :), c, c))
end
@testset "Copy and zero with type changing array" begin
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
struct TypeChangingArray{T, N} <: AbstractArray{T, N} end
Base.copy(::TypeChangingArray{T, N}) where {T, N} = Array{T, N}(undef,
ntuple(_ -> 0, N))
Base.zero(::TypeChangingArray{T, N}) where {T, N} = zeros(T, ntuple(_ -> 0, N))
a = ArrayPartition(TypeChangingArray{Int, 2}(), TypeChangingArray{Float32, 2}())
@test copy(a) == ArrayPartition(zeros(Int, 0, 0), zeros(Float32, 0, 0))
@test zero(a) == ArrayPartition(zeros(Int, 0, 0), zeros(Float32, 0, 0))
end
@test !iszero(ArrayPartition([2], [3, 4]))
@testset "Cartesian indexing" begin
@test ArrayPartition([1, 2], [3])[1:3, 1] == [1, 2, 3]
end
@testset "Scalar copyto!" begin
u = [2.0, 1.0]
copyto!(u, ArrayPartition(1.0, -1.2))
@test u == [1.0, -1.2]
end