@@ -68,7 +68,6 @@ Base.setindex!{T}(A::TSlow{T,4}, v, i1::Int, i2::Int, i3::Int, i4::Int) =
68
68
Base. setindex! {T} (A:: TSlow{T,5} , v, i1:: Int , i2:: Int , i3:: Int , i4:: Int , i5:: Int ) =
69
69
(A. data[(i1,i2,i3,i4,i5)] = v)
70
70
71
-
72
71
import Base: trailingsize
73
72
const can_inline = Base. JLOptions (). can_inline != 0
74
73
function test_scalar_indexing {T} (:: Type{T} , shape)
@@ -85,6 +84,8 @@ function test_scalar_indexing{T}(::Type{T}, shape)
85
84
for i1 = 1 : size (B, 1 )
86
85
i += 1
87
86
@test A[i1,i2,i3,i4,i5] == B[i1,i2,i3,i4,i5] == i
87
+ @test A[i1,i2,i3,i4,i5] ==
88
+ Base. unsafe_getindex (B, i1, i2, i3, i4, i5) == i
88
89
end
89
90
end
90
91
end
@@ -117,6 +118,9 @@ function test_scalar_indexing{T}(::Type{T}, shape)
117
118
@test A[] == B[] == A[1 ] == B[1 ] == 1
118
119
# Test multidimensional scalar indexed assignment
119
120
C = T (Int, shape)
121
+ D1 = T (Int, shape)
122
+ D2 = T (Int, shape)
123
+ D3 = T (Int, shape)
120
124
i= 0
121
125
for i5 = 1 : trailingsize (B, 5 )
122
126
for i4 = 1 : size (B, 4 )
@@ -125,12 +129,19 @@ function test_scalar_indexing{T}(::Type{T}, shape)
125
129
for i1 = 1 : size (B, 1 )
126
130
i += 1
127
131
C[i1,i2,i3,i4,i5] = i
132
+ # test general unsafe_setindex!
133
+ Base. unsafe_setindex! (D1, i, i1,i2,i3,i4,i5)
134
+ # test for dropping trailing dims
135
+ Base. unsafe_setindex! (D2, i, i1,i2,i3,i4,i5, 1 , 1 , 1 )
136
+ # test for expanding index argument to appropriate dims
137
+ Base. unsafe_setindex! (D3, i, i1,i2,i3,i4)
128
138
end
129
139
end
130
140
end
131
141
end
132
142
end
133
- @test C == B == A
143
+ @test D1 == D2 == C == B == A
144
+ @test D3[:, :, :, :, 1 ] == D2[:, :, :, :, 1 ]
134
145
# Test linear indexing and partial linear indexing
135
146
C = T (Int, shape)
136
147
fill! (C, 0 )
@@ -180,7 +191,278 @@ function test_vector_indexing{T}(::Type{T}, shape)
180
191
@test B[1 : end ,1 : end ] == A[1 : end ,1 : end ] == reshape (1 : N, shape[1 ], prod (shape[2 : end ]))
181
192
end
182
193
194
+ function test_primitives {T} (:: Type{T} , shape)
195
+ N = prod (shape)
196
+ A = reshape (1 : N, shape)
197
+ B = T (A)
198
+
199
+ # elsize{T}(::AbstractArray{T})
200
+ @test Base. elsize (B) == sizeof (T. parameters)
201
+
202
+ # last(a)
203
+ @test last (B) == B[length (B)]
204
+
205
+ # strides(a::AbstractArray)
206
+ strides_B = strides (B)
207
+ for (i, _stride) in enumerate (collect (strides_B))
208
+ @test _stride == stride (B, i)
209
+ end
210
+
211
+ # isassigned(a::AbstractArray, i::Int...)
212
+ j = rand (1 : length (B))
213
+ @test isassigned (B, j) == true
214
+ if T == T24Linear
215
+ @test isassigned (B, length (B) + 1 ) == false
216
+ end
217
+
218
+ # reshape(a::AbstractArray, dims::Dims)
219
+ @test_throws ArgumentError reshape (B, (0 , 1 ))
220
+
221
+ # copy!(dest::AbstractArray, src::AbstractArray)
222
+ @test_throws BoundsError copy! (Array (Int, 10 ), [1 : 11 ... ])
223
+
224
+ # convert{T, N}(::Type{Array}, A::AbstractArray{T, N})
225
+ X = [1 : 10 ... ]
226
+ @test convert (Array, X) == X
227
+ end
228
+
229
+ function test_in_bounds ()
230
+ @test Base. in_bounds ((5 , 5 , 5 ), 5 ) == true
231
+ end
232
+
233
+ type UnimplementedFastArray{T, N} <: AbstractArray{T, N} end
234
+ Base. linearindexing (:: UnimplementedFastArray ) = Base. LinearFast ()
235
+
236
+ type UnimplementedSlowArray{T, N} <: AbstractArray{T, N} end
237
+ Base. linearindexing (:: UnimplementedSlowArray ) = Base. LinearSlow ()
238
+
239
+ type UnimplementedArray{T, N} <: AbstractArray{T, N} end
240
+
241
+ function test_getindex_internals {T} (:: Type{T} , shape)
242
+ N = prod (shape)
243
+ A = reshape (1 : N, shape)
244
+ B = T (A)
245
+
246
+ @test getindex (A) == 1
247
+ @test getindex (B) == 1
248
+ @test Base. unsafe_getindex (A) == 1
249
+ @test Base. unsafe_getindex (B) == 1
250
+ end
251
+
252
+ function test_getindex_internals ()
253
+ U = UnimplementedFastArray {Int, 2} ()
254
+ V = UnimplementedSlowArray {Int, 2} ()
255
+ @test_throws ErrorException getindex (U, 1 )
256
+ @test_throws ErrorException Base. unsafe_getindex (U, 1 )
257
+ @test_throws ErrorException getindex (V, 1 , 1 )
258
+ @test_throws ErrorException Base. unsafe_getindex (V, 1 , 1 )
259
+ end
260
+
261
+ function test_setindex!_internals {T} (:: Type{T} , shape)
262
+ N = prod (shape)
263
+ A = reshape (1 : N, shape)
264
+ B = T (A)
265
+
266
+ Base. unsafe_setindex! (B, 1 )
267
+ @test B[1 ] == 1
268
+ end
269
+
270
+ function test_setindex!_internals ()
271
+ U = UnimplementedFastArray {Int, 2} ()
272
+ V = UnimplementedSlowArray {Int, 2} ()
273
+ @test_throws ErrorException setindex! (U, 1 )
274
+ @test_throws ErrorException Base. unsafe_setindex! (U, 1 )
275
+ @test_throws ErrorException Base. unsafe_setindex! (U, 1 , 1 )
276
+ end
277
+
278
+ function test_get ()
279
+ A = T24Linear ([1 : 24 ... ])
280
+ B = TSlow ([1 : 24 ... ])
281
+
282
+ @test get (A, (), 0 ) == Int[]
283
+ @test get (B, (), 0 ) == TSlow (Int, 0 )
284
+ end
285
+
286
+ function test_cat ()
287
+ A = T24Linear ([1 : 24 ... ])
288
+ b_int = reshape ([1 : 27 ... ], 3 , 3 , 3 )
289
+ b_float = reshape (Float64[1 : 27 ... ], 3 , 3 , 3 )
290
+ b2hcat = Array (Float64, 3 , 6 , 3 )
291
+ b1 = reshape ([1 : 9 ... ], 3 , 3 )
292
+ b2 = reshape ([10 : 18 ... ], 3 , 3 )
293
+ b3 = reshape ([19 : 27 ... ], 3 , 3 )
294
+ b2hcat[:, :, 1 ] = hcat (b1, b1)
295
+ b2hcat[:, :, 2 ] = hcat (b2, b2)
296
+ b2hcat[:, :, 3 ] = hcat (b3, b3)
297
+ b3hcat = Array (Float64, 3 , 9 , 3 )
298
+ b3hcat[:, :, 1 ] = hcat (b1, b1, b1)
299
+ b3hcat[:, :, 2 ] = hcat (b2, b2, b2)
300
+ b3hcat[:, :, 3 ] = hcat (b3, b3, b3)
301
+ B = TSlow (b_int)
302
+ B1 = TSlow ([1 : 24 ... ])
303
+ B2 = TSlow ([1 : 25 ... ])
304
+ C1 = TSlow ([1 2 ; 3 4 ])
305
+ C2 = TSlow ([1 2 3 ; 4 5 6 ])
306
+ C3 = TSlow ([1 2 ; 3 4 ; 5 6 ])
307
+ D = [1 : 24 ... ]
308
+ i = rand (1 : 10 )
309
+
310
+ @test cat (i) == Any[]
311
+ @test vcat () == Any[]
312
+ @test hcat () == Any[]
313
+ @test hcat (1 , 1.0 , 3 , 3.0 ) == [1.0 1.0 3.0 3.0 ]
314
+ @test_throws ArgumentError hcat (B1, B2)
315
+ @test_throws ArgumentError vcat (C1, C2)
316
+
317
+ @test vcat (B) == B
318
+ @test hcat (B) == B
319
+ @test Base. typed_hcat (Float64, B) == TSlow (b_float)
320
+ @test Base. typed_hcat (Float64, B, B) == TSlow (b2hcat)
321
+ @test Base. typed_hcat (Float64, B, B, B) == TSlow (b3hcat)
322
+
323
+ @test vcat (B1, B2) == TSlow (vcat ([1 : 24 ... ], [1 : 25 ... ]))
324
+ @test hcat (C1, C2) == TSlow ([1 2 1 2 3 ; 3 4 4 5 6 ])
325
+ @test hcat (C1, C2, C1) == TSlow ([1 2 1 2 3 1 2 ; 3 4 4 5 6 3 4 ])
326
+
327
+ # hvcat
328
+ for nbc in (1 , 2 , 3 , 4 , 5 , 6 )
329
+ @test hvcat (nbc, 1 : 120 ... ) ==
330
+ transpose (reshape ([1 : 120 ... ], nbc, round (Int, 120 / nbc)))
331
+ end
332
+
333
+ @test_throws ArgumentError hvcat (7 , 1 : 20 ... )
334
+ @test_throws ArgumentError hvcat ((2 ), C1, C3)
335
+ @test_throws ArgumentError hvcat ((1 ), C1, C2)
336
+ @test_throws ArgumentError hvcat ((1 ), C2, C3)
337
+
338
+ tup = tuple (rand (1 : 10 , i)... )
339
+ @test hvcat (tup) == []
340
+
341
+ # check for shape mismatch
342
+ @test_throws ArgumentError hvcat ((2 , 2 ), 1 , 2 , 3 , 4 , 5 )
343
+ @test_throws ArgumentError Base. typed_hvcat (Int, (2 , 2 ), 1 , 2 , 3 , 4 , 5 )
344
+ # check for # of columns mismatch b/w rows
345
+ @test_throws ArgumentError hvcat ((3 , 2 ), 1 , 2 , 3 , 4 , 5 , 6 )
346
+ @test_throws ArgumentError Base. typed_hvcat (Int, (3 , 2 ), 1 , 2 , 3 , 4 , 5 , 6 )
347
+ end
348
+
349
+ function test_ind2sub ()
350
+ n = rand (2 : 5 )
351
+ dims = tuple (rand (1 : 5 , n)... )
352
+ len = prod (dims)
353
+ A = reshape (1 : len, dims... )
354
+ I = ind2sub (dims, [1 : len... ])
355
+ for i in 1 : len
356
+ idx = [ I[j][i] for j in 1 : n ]
357
+ @test A[idx... ] == A[i]
358
+ end
359
+ end
360
+
361
+ function test_cartesianmap ()
362
+ f (x, y, z, B:: Array ) = push! (B, (x, y, z))
363
+ B = NTuple{3 , Int}[]
364
+ cartesianmap (f, (3 , 3 , 3 , 1 ), B)
365
+ @test B == NTuple{3 , Int}[(1 ,1 ,1 ), (2 ,1 ,1 ), (3 ,1 ,1 ), (1 ,2 ,1 ), (2 ,2 ,1 ), (3 ,2 ,1 ),
366
+ (1 ,3 ,1 ), (2 ,3 ,1 ), (3 ,3 ,1 ), (1 ,1 ,2 ), (2 ,1 ,2 ), (3 ,1 ,2 ), (1 ,2 ,2 ), (2 ,2 ,2 ),
367
+ (3 ,2 ,2 ), (1 ,3 ,2 ), (2 ,3 ,2 ), (3 ,3 ,2 ), (1 ,1 ,3 ), (2 ,1 ,3 ), (3 ,1 ,3 ), (1 ,2 ,3 ),
368
+ (2 ,2 ,3 ), (3 ,2 ,3 ), (1 ,3 ,3 ), (2 ,3 ,3 ), (3 ,3 ,3 )]
369
+ @test_throws ArgumentError cartesianmap (f, (1 ,), B)
370
+ end
371
+
372
+ type GenericIterator{N} end
373
+ Base. start {N} (:: GenericIterator{N} ) = 1
374
+ Base. next {N} (:: GenericIterator{N} , i) = (i, i + 1 )
375
+ Base. done {N} (:: GenericIterator{N} , i) = i > N ? true : false
376
+
377
+ function test_map ()
378
+
379
+ for typ in (Float16, Float32, Float64,
380
+ Int8, Int16, Int32, Int64, Int128,
381
+ UInt8, UInt16, UInt32, UInt64, UInt128
382
+ ),
383
+ arg_typ in (Integer,
384
+ Signed,
385
+ Unsigned
386
+ )
387
+ X = typ[1 : 10 ... ]
388
+ _typ = typeof (arg_typ (one (typ)))
389
+ @test map (arg_typ, X) == _typ[1 : 10 ... ]
390
+ end
391
+
392
+ # generic map
393
+ f (x) = x + 1
394
+ I = GenericIterator {10} ()
395
+ @test map (f, I) == Any[2 : 11 ... ]
396
+
397
+ # AbstractArray map for 2 arg case
398
+ f (x, y) = x + y
399
+ A = Array (Int, 10 )
400
+ B = Float64[1 : 10 ... ]
401
+ C = Float64[1 : 10 ... ]
402
+ @test Base. map_to! (f, 1 , A, B, C) == Real[ 2 * i for i in 1 : 10 ]
403
+ @test map (f, Int[], Float64[]) == Float64[]
404
+
405
+ # AbstractArray map for N-arg case
406
+ f (x, y, z) = x + y + z
407
+ D = Float64[1 : 10 ... ]
408
+
409
+ @test map! (f, A, B, C, D) == Int[ 3 * i for i in 1 : 10 ]
410
+ @test Base. map_to_n! (f, 1 , A, (B, C, D)) == Real[ 3 * i for i in 1 : 10 ]
411
+ @test map (f, B, C, D) == Float64[ 3 * i for i in 1 : 10 ]
412
+ @test map (f, Int[], Int[], Complex{Int}[]) == Number[]
413
+ end
414
+
415
+ function test_map_promote ()
416
+ A = [1 : 10 ... ]
417
+ f (x) = iseven (x) ? 1.0 : 1
418
+ @test Base. map_promote (f, A) == fill (1.0 , 10 )
419
+ end
420
+
421
+ function test_UInt_indexing ()
422
+ A = [1 : 100 ... ]
423
+ _A = Expr (:quote , A)
424
+ for i in 1 : 100
425
+ _i8 = convert (UInt8, i)
426
+ _i16 = convert (UInt16, i)
427
+ _i32 = convert (UInt32, i)
428
+ for _i in (_i8, _i16, _i32)
429
+ @eval begin
430
+ @test $ _A[$ _i] == $ i
431
+ end
432
+ end
433
+ end
434
+ end
435
+
436
+ function test_vcat_depwarn ()
437
+ if (Base. JLOptions ()). depwarn > 1
438
+ @test_throws ErrorException [1 : 10 ]
439
+ @test_throws ErrorException [[1 , 2 ], [3 , 4 ]]
440
+ @test_throws ErrorException [[1 , 2 ], [3 , 4 ], [5 , 6 ]]
441
+ else
442
+ [1 : 10 ]
443
+ [[1 , 2 ], [3 , 4 ]]
444
+ [[1 , 2 ], [3 , 4 ], [5 , 6 ]]
445
+ nothing
446
+ end
447
+ end
448
+
449
+ # ----- run tests -------------------------------------------------------------#
450
+
183
451
for T in (T24Linear, TSlow), shape in ((24 ,), (2 , 12 ), (2 ,3 ,4 ), (1 ,2 ,3 ,4 ), (4 ,3 ,2 ,1 ))
184
452
test_scalar_indexing (T, shape)
185
453
test_vector_indexing (T, shape)
454
+ test_primitives (T, shape)
455
+ test_getindex_internals (T, shape)
456
+ test_setindex!_internals (T, shape)
186
457
end
458
+ test_in_bounds ()
459
+ test_getindex_internals ()
460
+ test_setindex!_internals ()
461
+ test_get ()
462
+ test_cat ()
463
+ test_ind2sub ()
464
+ test_cartesianmap ()
465
+ test_map ()
466
+ test_map_promote ()
467
+ test_UInt_indexing ()
468
+ test_vcat_depwarn ()
0 commit comments