-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
test_SparseAxisArray.jl
346 lines (327 loc) · 11.6 KB
/
test_SparseAxisArray.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See https://github.com/jump-dev/JuMP.jl
#############################################################################
module TestContainersSparseAxisArray
using JuMP.Containers
using Test
function _util_sparse_test(d, sum_d, d2, d3, dsqr, d_bads)
sqr(x) = x^2
# map
@test d == @inferred map(identity, d)
@test dsqr == @inferred map(sqr, d)
@test d3 == @inferred map(x -> x * 3, d)
@test d3 == @inferred map(x -> 3 * x, d)
# reduce
@test sum_d == @inferred sum(d)
# bbroadcasting
@test dsqr == @inferred d .* d
@test d2 == @inferred d .+ d
@test d3 == @inferred d .* 3
@test d3 == @inferred 3 .* d
@test d == identity.(d)
@test dsqr == sqr.(d)
err = ArgumentError(
"Cannot broadcast" *
" Containers.SparseAxisArray with" *
" another array of different type",
)
@test_throws err [1, 2] .+ d
@test_throws err d .* [1, 2]
err = ArgumentError(
"Cannot broadcast" *
" Containers.SparseAxisArray with " *
"different indices",
)
for d_bad in d_bads
@test_throws err d_bad .+ d
@test_throws err d .+ d_bad
end
return
end
function test_1_dimensional()
d = @inferred SparseAxisArray(Dict((:a,) => 1, (:b,) => 2))
@test sprint(summary, d) == """
$(SparseAxisArray{Int,1,Tuple{Symbol}}) with 2 entries"""
@test sprint(show, "text/plain", d) == """
$(SparseAxisArray{Int,1,Tuple{Symbol}}) with 2 entries:
[a] = 1
[b] = 2"""
@test d isa SparseAxisArray{Int,1,Tuple{Symbol}}
d2 = @inferred SparseAxisArray(Dict((:a,) => 2, (:b,) => 4))
d3 = @inferred SparseAxisArray(Dict((:a,) => 3, (:b,) => 6))
dsqr = @inferred SparseAxisArray(Dict((:a,) => 1, (:b,) => 4))
da = @inferred SparseAxisArray(Dict((:b,) => 2))
dc = @inferred SparseAxisArray(Dict((:a,) => 1, (:b,) => 2, (:c,) => 3))
_util_sparse_test(d, 3, d2, d3, dsqr, [da, dc])
# f(::Int) has return type Union{Int, Float64}
f(x) = x == 1 ? 1 : 1 / x
fd = f.(d)
@test fd isa SparseAxisArray{Real,1,Tuple{Symbol}}
@test fd == SparseAxisArray(Dict((:a,) => 1, (:b,) => 0.5))
g(x, y) = f(x) + f(y)
fd = g.(d, 1)
@test fd isa SparseAxisArray{Real,1,Tuple{Symbol}}
@test fd == SparseAxisArray(Dict((:a,) => 2, (:b,) => 1.5))
fd = g.(1, d)
@test fd isa SparseAxisArray{Real,1,Tuple{Symbol}}
@test fd == SparseAxisArray(Dict((:a,) => 2, (:b,) => 1.5))
@test 3 * (d2 / 2) == d3
@test (d2 / 2) * 3 == d3
return
end
function test_2_dimensional()
d = @inferred SparseAxisArray(Dict((:a, 'u') => 2.0, (:b, 'v') => 0.5))
@test d isa SparseAxisArray{Float64,2,Tuple{Symbol,Char}}
@test_throws BoundsError(d, (:a,)) d[:a]
@test sprint(summary, d) == """
$(SparseAxisArray{Float64,2,Tuple{Symbol,Char}}) with 2 entries"""
@test sprint(show, "text/plain", d) == """
$(SparseAxisArray{Float64,2,Tuple{Symbol,Char}}) with 2 entries:
[a, u] = 2.0
[b, v] = 0.5"""
d2 = @inferred SparseAxisArray(Dict((:b, 'v') => 1.0, (:a, 'u') => 4.0))
d3 = @inferred SparseAxisArray(Dict((:a, 'u') => 6.0, (:b, 'v') => 1.5))
dsqr = @inferred SparseAxisArray(Dict((:a, 'u') => 4.0, (:b, 'v') => 0.25))
da = @inferred SparseAxisArray(Dict((:b, 'v') => 2.0))
db = @inferred SparseAxisArray(Dict((:a, 'u') => 1.0, (:b, 'u') => 2.0))
dc = @inferred SparseAxisArray(
Dict((:a, 'u') => 1.0, (:b, 'v') => 2.0, (:c, 'w') => 3.0),
)
_util_sparse_test(d, 2.5, d2, d3, dsqr, [da, db, dc])
return
end
function test_3_dimensional()
d = @inferred SparseAxisArray(
Dict((:a, 'u', 2) => 2.0, (:b, 'v', 3) => 0.5),
)
@test d isa SparseAxisArray{Float64,3,Tuple{Symbol,Char,Int}}
@test_throws BoundsError(d, (:a,)) d[:a]
@test_throws BoundsError(d, (:a, 'u')) d[:a, 'u']
d2 = @inferred SparseAxisArray(
Dict((:b, 'v', 3) => 1.0, (:a, 'u', 2) => 4.0),
)
d3 = @inferred SparseAxisArray(
Dict((:a, 'u', 2) => 6.0, (:b, 'v', 3) => 1.5),
)
dsqr = @inferred SparseAxisArray(
Dict((:a, 'u', 2) => 4.0, (:b, 'v', 3) => 0.25),
)
da = @inferred SparseAxisArray(Dict((:b, 'v', 3) => 2.0))
db = @inferred SparseAxisArray(
Dict((:a, 'u', 3) => 1.0, (:b, 'u', 2) => 2.0),
)
dc = @inferred SparseAxisArray(
Dict((:a, 'u', 2) => 1.0, (:b, 'v', 3) => 2.0, (:c, 'w', 4) => 3.0),
)
_util_sparse_test(d, 2.5, d2, d3, dsqr, [da, db, dc])
return
end
function test_empty_array()
a = Containers.@container([i = 1:3; i > 5], sqrt(i))
@test a isa SparseAxisArray{Float64,1,Tuple{Int}}
@test length(a) == 0
S = [["a"], [:b]]
b = Containers.@container([i = 1:2, j = S[i]; i > 3], fill(i, j))
# The compiler doesn't always return the same thing for
# `@default_eltype`. It gets Tuple{Int,Any} if run from the REPL, but
# `Tuple` if run from within this testset. Just test for either.
@test(
b isa SparseAxisArray{Any,2,Tuple{Int,Any}} ||
b isa SparseAxisArray{Any,2,Tuple{Any,Any}}
)
@test length(b) == 0
c = Containers.@container(
[i = 1:0, j = Any[]],
i,
container = SparseAxisArray
)
@test c isa SparseAxisArray{Int,2,Tuple{Int,Any}}
@test length(c) == 0
d = Containers.@container([i = Any[], j = Any[]; isodd(i)], i)
@test d isa SparseAxisArray{Any,2,Tuple{Any,Any}}
@test length(d) == 0
return
end
function test_half_screen_printing()
d = SparseAxisArray(Dict((i,) => 2 * i for i in 1:100))
io = IOBuffer()
show(IOContext(io, :limit => true, :compact => true), "text/plain", d)
seekstart(io)
@test occursin("\u22ee", read(io, String))
return
end
function test_hash()
a = Containers.@container([i = 1:3; i > 5], sqrt(i))
@test hash(a) isa UInt
s = Set{Any}()
push!(s, a)
@test length(s) == 1
return
end
function test_size()
err = ErrorException(
"`Base.size` is not implemented for `SparseAxisArray` because " *
"although it is a subtype of `AbstractArray`, it is conceptually " *
"closer to a dictionary with `N`-dimensional keys. If you encounter " *
"this error and you didn't call `size` explicitly, it is because " *
"you called a method that is unsupported for `SparseAxisArray`s. " *
"Consult the JuMP documentation for a list of supported operations.",
)
x = Containers.@container([i = 1:3, j = i:3], i + j)
@test_throws err size(x)
return
end
function test_empty_broadcasting()
S = Any[]
x = Containers.@container([S, 1:2], 0, container = SparseAxisArray)
f(x) = 2x
y = f.(x)
@test y isa SparseAxisArray{Any,2,Tuple{Any,Int}}
@test isempty(y)
return
end
function test_slicing()
Containers.@container(x[i = 1:4, j = 1:2; isodd(i + j)], i + j)
@test x[:, :] == x
@test x[1, :] == Containers.@container(y[j = 1:2; isodd(1 + j)], 1 + j)
@test x[:, 1] == Containers.@container(z[i = 1:4; isodd(i + 1)], i + 1)
@test isempty(x[[1, 3], [1, 3]])
@test typeof(x[[1, 3], [1, 3]]) == typeof(x)
@test typeof(x[[1, 3], 1]) == Containers.SparseAxisArray{Int,1,Tuple{Int}}
@test isempty(x[[1, 3], 1])
Containers.@container(y[i = 1:4; isodd(i)], i)
@test y[:] == y
Containers.@container(y[i = 1:4; isodd(i)], i)
@test y[[1, 3]] == y
z = Containers.@container([i = 1:3, j = [:A, :B]; i > 1], (i, j))
@test z[2, :] == Containers.@container([j = [:A, :B]; true], (2, j))
@test z[:, :A] == Containers.@container([i = 2:3; true], (i, :A))
@test z[:, :] == z
@test z[1:2, :A] == Containers.@container([i = 2:2; true], (i, :A))
@test z[2, [:A, :B]] == Containers.@container([j = [:A, :B]; true], (2, j))
@test z[1:2, [:A, :B]] ==
Containers.@container([i = 2:2, j = [:A, :B]; true], (i, j))
return
end
function test_slicing_on_set()
Containers.@container(x[i = 1:4, j = 1:2; isodd(i + j)], i + j)
err = ArgumentError(
"Slicing is not supported when calling `setindex!` on a" *
" SparseAxisArray",
)
@test_throws(err, x[:, :] = 1)
@test_throws(err, x[1, :] = 1)
@test_throws(err, x[1, 1:2] = 1)
return
end
function test_ambiguity_broadcast_preserving_zero_d()
Containers.@container(x[i = 1:2, j = i:3], i + j)
@test Broadcast.broadcast_preserving_zero_d(*, x, x) == x .* x
return
end
function test_ambuguity_BroadcastStyleUnknown()
Containers.@container(x[i = 1:2, j = i:3], i + j)
style = Base.BroadcastStyle(typeof(x))
@test_throws ArgumentError Base.BroadcastStyle(style, Broadcast.Unknown())
return
end
function test_containers_sparseaxisarray_kwarg_indexing()
Containers.@container(
x[i = 2:3, j = 1:2],
i + j,
container = SparseAxisArray,
)
for i in (2, 3, 2:2, 2:3, :), j in (1, 2, 1:2, 1:1, 2:2, :)
@test x[i = i, j = j] == x[i, j]
@test_throws ErrorException x[j = j, i = i]
end
@test_throws(
ErrorException(
"Invalid index j in position 1. When using keyword indexing, the " *
"indices must match the exact name and order used when creating " *
"the container.",
),
x[j = 1, i = 2],
)
@test_throws(
ErrorException(
"Invalid index k in position 2. When using keyword indexing, the " *
"indices must match the exact name and order used when creating " *
"the container.",
),
x[i = 2, k = 2],
)
@test_throws(
ErrorException(
"Cannot index with mix of positional and keyword arguments",
),
x[i = 2, 2],
)
Containers.@container(y[i = 2:3, 1:2], i, container = SparseAxisArray,)
@test_throws(
ErrorException(
"Cannot index with mix of positional and keyword arguments",
),
y[i = 2, 2],
)
@test_throws(BoundsError, y[i = 2] = 1)
@test_throws(BoundsError, y[2] = 1)
return
end
function test_containers_sparseaxisarray_kwarg_indexing_slicing()
Containers.@container(
x[i = 2:3, j = 1:2],
i + j,
container = SparseAxisArray,
)
y = x[i = 2, j = :]
@test y[j = 2] == 4
y = x[i = :, j = 1]
@test y[i = 3] == 4
y = x[i = :, j = :]
@test y[i = 3, j = 1] == 4
return
end
function test_containers_sparseaxisarray_kwarg_setindex()
Containers.@container(
x[i = 2:3, j = 1:2],
i + j,
container = SparseAxisArray,
)
for i in 2:3, j in 1:2
@test x[i = i, j = j] == i + j
x[i = i, j = j] = i + j + 2
@test x[i = i, j = j] == i + j + 2
end
@test_throws(
ErrorException(
"Invalid index j in position 1. When using keyword indexing, the " *
"indices must match the exact name and order used when creating " *
"the container.",
),
x[j = 1, i = 2] = 2,
)
@test_throws(
ErrorException(
"Invalid index k in position 2. When using keyword indexing, the " *
"indices must match the exact name and order used when creating " *
"the container.",
),
x[i = 2, k = 2] = 2,
)
@test_throws(
ErrorException(
"Cannot index with mix of positional and keyword arguments",
),
x[i = 2, 2] = 3,
)
@test_throws(BoundsError, x[i = 2] = 3)
return
end
end # module