@@ -66,18 +66,19 @@ for (_A, Ar, _B) in ((A, Ars, B), (As, Arss, Bs))
66
66
@test Arsc == [1 - 1 ; 2 - 2 ]
67
67
reinterpret (NTuple{3 , Int64}, Bc)[2 ] = (4 ,5 ,6 )
68
68
@test Bc == Complex{Int64}[5 + 6im , 7 + 4im , 5 + 6im ]
69
- reinterpret (NTuple{3 , Int64}, Bc)[1 ] = (1 ,2 ,3 )
69
+ B2 = reinterpret (NTuple{3 , Int64}, Bc)
70
+ @test setindex! (B2, (1 ,2 ,3 ), 1 ) == B2
70
71
@test Bc == Complex{Int64}[1 + 2im , 3 + 4im , 5 + 6im ]
71
72
Bc = copy (_B)
72
73
Brrs = reinterpret (reshape, Int64, Bc)
73
- Brrs[ 2 , 3 ] = - 5
74
+ @test setindex! ( Brrs, - 5 , 2 , 3 ) == Brrs
74
75
@test Bc == Complex{Int64}[5 + 6im , 7 + 8im , 9 - 5im ]
75
76
Brrs[last (eachindex (Brrs))] = 22
76
77
@test Bc == Complex{Int64}[5 + 6im , 7 + 8im , 9 + 22im ]
77
78
78
79
A1 = reinterpret (Float64, _A)
79
80
A2 = reinterpret (ComplexF64, _A)
80
- A1[ 1 ] = 1.0
81
+ @test setindex! (A1, 1.0 , 1 ) == A1
81
82
@test real (A2[1 ]) == 1.0
82
83
A1 = reinterpret (reshape, Float64, _A)
83
84
A1[1 ] = 2.5
@@ -88,7 +89,7 @@ for (_A, Ar, _B) in ((A, Ars, B), (As, Arss, Bs))
88
89
@test real (A2rs[1 ]) == 1.0
89
90
A1rs = reinterpret (reshape, Float64, Ar)
90
91
A2rs = reinterpret (reshape, ComplexF64, Ar)
91
- A1rs[ 1 , 1 ] = 2.5
92
+ @test setindex! ( A1rs, 2.5 , 1 , 1 ) == A1rs
92
93
@test real (A2rs[1 ]) == 2.5
93
94
end
94
95
end
376
377
a = reinterpret (reshape, NTuple{4 ,Float64}, rand (Float64, 4 , 4 ))
377
378
@test typeof (Base. unaliascopy (a)) === typeof (a)
378
379
end
380
+
381
+
382
+ @testset " singleton types" begin
383
+ mutable struct NotASingleton end # not a singleton because it is mutable
384
+ struct SomeSingleton
385
+ # A singleton type that does not have the internal constructor SomeSingleton()
386
+ SomeSingleton (x) = new ()
387
+ end
388
+
389
+ @test_throws ErrorException reinterpret (Int, nothing )
390
+ @test_throws ErrorException reinterpret (Missing, 3 )
391
+ @test_throws ErrorException reinterpret (Missing, NotASingleton ())
392
+ @test_throws ErrorException reinterpret (NotASingleton, ())
393
+
394
+ @test_throws ArgumentError reinterpret (NotASingleton, fill (nothing , ()))
395
+ @test_throws ArgumentError reinterpret (reshape, NotASingleton, fill (missing , 3 ))
396
+ @test_throws ArgumentError reinterpret (Tuple{}, fill (NotASingleton (), 2 ))
397
+ @test_throws ArgumentError reinterpret (reshape, Nothing, fill (NotASingleton (), ()))
398
+
399
+ t = fill (nothing , 3 , 5 )
400
+ @test reinterpret (SomeSingleton, t) == reinterpret (reshape, SomeSingleton, t)
401
+ @test reinterpret (SomeSingleton, t) == [SomeSingleton (i* j) for i in 1 : 3 , j in 1 : 5 ]
402
+ @test reinterpret (Int, t) == fill (17 , 0 , 5 )
403
+ @test_throws ArgumentError reinterpret (reshape, Float64, t)
404
+ @test_throws ArgumentError reinterpret (Nothing, 1 : 6 )
405
+ @test_throws ArgumentError reinterpret (reshape, Missing, [0.0 ])
406
+
407
+ # reintepret of empty array with reshape
408
+ @test reinterpret (reshape, Nothing, fill (missing , (0 ,0 ,0 ))) == fill (nothing , (0 ,0 ,0 ))
409
+ @test_throws ArgumentError reinterpret (reshape, Nothing, fill (3.2 , (0 ,0 )))
410
+ @test_throws ArgumentError reinterpret (reshape, Float64, fill (nothing , 0 ))
411
+
412
+ # reinterpret of 0-dimensional array
413
+ z = reinterpret (Tuple{}, fill (missing , ()))
414
+ @test z == fill ((), ())
415
+ @test z == reinterpret (reshape, Tuple{}, fill (nothing , ()))
416
+ @test_throws BoundsError z[2 ]
417
+ @test_throws BoundsError z[3 ] = ()
418
+ @test_throws ArgumentError reinterpret (UInt8, fill (nothing , ()))
419
+ @test_throws ArgumentError reinterpret (Missing, fill (1f0 , ()))
420
+ @test_throws ArgumentError reinterpret (reshape, Float64, fill (nothing , ()))
421
+ @test_throws ArgumentError reinterpret (reshape, Nothing, fill (17 , ()))
422
+
423
+
424
+ @test @inferred (ndims (reinterpret (reshape, SomeSingleton, t))) == 2
425
+ @test @inferred (axes (reinterpret (reshape, Tuple{}, t))) == (Base. OneTo (3 ),Base. OneTo (5 ))
426
+ @test @inferred (size (reinterpret (reshape, Missing, t))) == (3 ,5 )
427
+
428
+ x = reinterpret (Tuple{}, t)
429
+ @test x == reinterpret (reshape, Tuple{}, t)
430
+ @test x[3 ,5 ] === ()
431
+ x1 = fill ((), 3 , 5 )
432
+ @test setindex! (x, (), 1 , 1 ) == x1
433
+ @test_throws BoundsError x[17 ]
434
+ @test_throws BoundsError x[4 ,2 ]
435
+ @test_throws BoundsError x[1 ,2 ,3 ]
436
+ @test_throws BoundsError x[18 ] = ()
437
+ @test_throws MethodError x[1 ,3 ] = missing
438
+ @test x == fill ((), (3 , 5 ))
439
+ x = reinterpret (reshape, SomeSingleton, t)
440
+ @test_throws BoundsError x[19 ]
441
+ @test_throws BoundsError x[2 ,6 ] = SomeSingleton (0xa )
442
+ @test x[2 ,3 ] === SomeSingleton (:x )
443
+ x2 = fill (SomeSingleton (0.7 ), 3 , 5 )
444
+ @test x == x2
445
+ @test setindex! (x, SomeSingleton (:), 3 , 5 ) == x2
446
+ @test_throws MethodError x[2 ,4 ] = nothing
447
+ end
0 commit comments