-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathruntests.jl
3050 lines (2625 loc) · 122 KB
/
runtests.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using FillArrays, LinearAlgebra, PDMats, SparseArrays, StaticArrays, ReverseDiff, Random, Test, Statistics, Quaternions
import FillArrays: AbstractFill, RectDiagonal, SquareEye
using Documenter
DocMeta.setdocmeta!(FillArrays, :DocTestSetup, :(using FillArrays))
doctest(FillArrays; manual = false)
include("aqua.jl")
include("infinitearrays.jl")
import .InfiniteArrays
# we may use this instead of rand(n) to generate deterministic arrays
oneton(T::Type, sz...) = reshape(T.(1:prod(sz)), sz)
oneton(sz...) = oneton(Float64, sz...)
stringmime(args...) = sprint(show, args...)
@testset "fill array constructors and convert" begin
for (Typ, funcs) in ((Zeros, zeros), (Ones, ones))
@test Typ((-1,5)) == Typ((0,5))
@test Typ(5) isa AbstractVector{Float64}
@test Typ(5,5) isa AbstractMatrix{Float64}
@test Typ(5) == Typ((5,))
@test Typ(5,5) == Typ((5,5))
@test eltype(Typ(5,5)) == Float64
for T in (Int, Float64)
Z = Typ{T}(5)
@test Typ(T, 5) ≡ Z
@test eltype(Z) == T
@test Array(Z) == funcs(T,5)
@test Array{T}(Z) == funcs(T,5)
@test Array{T,1}(Z) == funcs(T,5)
@test convert(AbstractArray,Z) ≡ Z
@test convert(AbstractArray{T},Z) ≡ AbstractArray{T}(Z) ≡ Z
@test convert(AbstractVector{T},Z) ≡ AbstractVector{T}(Z) ≡ Z
@test convert(AbstractFill{T},Z) ≡ AbstractFill{T}(Z) ≡ Z
@test Typ{T,1}(2ones(T,5)) == Z
@test Typ{T}(2ones(T,5)) == Z
@test Typ(2ones(T,5)) == Z
Z = Typ{T}(5, 5)
@test Typ(T, 5, 5) ≡ Z
@test eltype(Z) == T
@test Array(Z) == funcs(T,5,5)
@test Array{T}(Z) == funcs(T,5,5)
@test Array{T,2}(Z) == funcs(T,5,5)
@test convert(AbstractArray,Z) ≡ convert(AbstractFill,Z) ≡ Z
@test convert(AbstractArray{T},Z) ≡ convert(AbstractFill{T},Z) ≡ AbstractArray{T}(Z) ≡ Z
@test convert(AbstractMatrix{T},Z) ≡ convert(AbstractFill{T,2},Z) ≡ AbstractMatrix{T}(Z) ≡ Z
@test_throws Exception convert(Fill{Float64}, [1,1,2])
@test_throws Exception convert(Fill, [])
@test convert(Fill{Float64}, [1,1,1]) ≡ Fill(1.0, 3)
@test convert(Fill, Float64[1,1,1]) ≡ Fill(1.0, 3)
@test convert(Fill{Float64}, Fill(1.0,2)) ≡ Fill(1.0, 2) # was ambiguous
@test convert(Fill{Int}, Ones(20)) ≡ Fill(1, 20)
@test convert(Fill{Int,1}, Ones(20)) ≡ Fill(1, 20)
@test convert(Fill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) ≡ Fill(1, 20)
@test convert(AbstractFill{Int}, Ones(20)) ≡ AbstractFill{Int}(Ones(20)) ≡ Ones{Int}(20)
@test convert(AbstractFill{Int,1}, Ones(20)) ≡ AbstractFill{Int,1}(Ones(20)) ≡ Ones{Int}(20)
@test convert(AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}, Ones(20)) ≡ AbstractFill{Int,1,Tuple{Base.OneTo{Int}}}(Ones(20)) ≡ Ones{Int}(20)
@test Typ{T,2}(2ones(T,5,5)) ≡ Typ{T}(5,5)
@test Typ{T}(2ones(T,5,5)) ≡ Typ{T}(5,5)
@test Typ(2ones(T,5,5)) ≡ Typ{T}(5,5)
z = Typ{T}()[]
@test convert(Typ, Fill(z,2)) === Typ{T}(2)
z = Typ{Int8}()[]
@test convert(Typ{T}, Fill(z,2)) === Typ{T}(2)
@test convert(Typ{T,1}, Fill(z,2)) === Typ{T}(2)
@test convert(Typ{T,1,Tuple{Base.OneTo{Int}}}, Fill(z,2)) === Typ{T}(2)
@test_throws ArgumentError convert(Typ, Fill(2,2))
@test Typ(Z) ≡ Typ{T}(Z) ≡ Typ{T,2}(Z) ≡ typeof(Z)(Z) ≡ Z
@test AbstractArray{Float32}(Z) ≡ Typ{Float32}(5,5)
@test AbstractArray{Float32,2}(Z) ≡ Typ{Float32}(5,5)
end
end
@test Fill(1) ≡ Fill{Int}(1) ≡ Fill{Int,0}(1) ≡ Fill{Int,0,Tuple{}}(1,())
@test Fill(1,(-1,5)) ≡ Fill(1,(0,5))
@test Fill(1.0,5) isa AbstractVector{Float64}
@test Fill(1.0,5,5) isa AbstractMatrix{Float64}
@test Fill(1,5) ≡ Fill(1,(5,))
@test Fill(1,5,5) ≡ Fill(1,(5,5))
@test eltype(Fill(1.0,5,5)) == Float64
@test Matrix{Float64}(Zeros{ComplexF64}(10,10)) == zeros(10,10)
@test_throws InexactError Matrix{Float64}(Fill(1.0+1.0im,10,10))
for T in (Int, Float64)
F = Fill{T, 0}(2)
@test size(F) == ()
@test F[] === T(2)
F = Fill{T}(1, 5)
@test eltype(F) == T
@test Array(F) == fill(one(T),5)
@test Array{T}(F) == fill(one(T),5)
@test Array{T,1}(F) == fill(one(T),5)
F = Fill{T}(1, 5, 5)
@test eltype(F) == T
@test Array(F) == fill(one(T),5,5)
@test Array{T}(F) == fill(one(T),5,5)
@test Array{T,2}(F) == fill(one(T),5,5)
@test convert(AbstractArray,F) ≡ F
@test convert(AbstractArray{T},F) ≡ AbstractArray{T}(F) ≡ F
@test convert(AbstractMatrix{T},F) ≡ AbstractMatrix{T}(F) ≡ F
@test convert(AbstractArray{Float32},F) ≡ AbstractArray{Float32}(F) ≡
Fill{Float32}(one(Float32),5,5)
@test convert(AbstractMatrix{Float32},F) ≡ AbstractMatrix{Float32}(F) ≡
Fill{Float32}(one(Float32),5,5)
@test Fill{T}(F) ≡ Fill{T,2}(F) ≡ typeof(F)(F) ≡ F
show(devnull, MIME("text/plain"), F) # for codecov
end
@test Eye(5) isa Diagonal{Float64}
@test SquareEye(5) isa Diagonal{Float64}
@test Eye(5) == Eye{Float64}(5) == SquareEye(5) == SquareEye{Float64}(5)
@test Eye(5,6) == Eye{Float64}(5,6)
@test Eye(Ones(5,6)) == Eye{Float64}(5,6)
@test eltype(Eye(5)) == Float64
@test eltype(Eye(5,6)) == Float64
@test Eye((Base.OneTo(5),)) ≡ SquareEye((Base.OneTo(5),)) ≡ Eye(5)
@test Eye((Base.OneTo(5),Base.OneTo(6))) ≡ Eye(5,6)
for T in (Int, Float64)
E = Eye{T}(5)
M = Matrix{T}(I, 5, 5)
@test eltype(E) == T
@test Array(E) == M
@test Array{T}(E) == M
@test Array{T,2}(E) == M
@test convert(AbstractArray,E) === E
@test convert(AbstractArray{T},E) === E
@test convert(AbstractMatrix{T},E) === E
@test AbstractArray{Float32}(E) == Eye{Float32}(5)
@test AbstractArray{Float32}(E) == Eye{Float32}(5, 5)
@test Eye{T}(randn(4,5)) ≡ Eye{T}(4,5) ≡ Eye{T}((Base.OneTo(4),Base.OneTo(5)))
@test Eye{T}((Base.OneTo(5),)) ≡ SquareEye{T}((Base.OneTo(5),)) ≡ Eye{T}(5)
end
@testset "Bool should change type" begin
x = Fill(true,5)
y = x + x
@test y isa Fill{Int,1}
@test y[1] == 2
x = Ones{Bool}(5)
y = x + x
@test y isa Fill{Int,1}
@test y[1] == 2
@test x + Zeros{Bool}(5) ≡ Ones{Int}(5)
@test x - Zeros{Bool}(5) ≡ Ones{Int}(5)
@test Zeros{Bool}(5) + x ≡ Ones{Int}(5)
@test -x ≡ Fill(-1,5)
end
@testset "copy should return Fill" begin
x = Fill(1.0,10)
@test copy(x) ≡ x
x = Zeros(10)
@test copy(x) ≡ x
x = Fill([1.,2.],10)
@test copy(x) == x
@test copy(x) === x # because isbits(x)
@test copy(x) isa Fill
@test copy(Fill(:a, 4)) == fill(:a, 4) # FillArrays#63
end
@testset "vec" begin
@test vec(Ones{Int}(5,10)) ≡ Ones{Int}(50)
@test vec(Zeros{Int}(5,10)) ≡ Zeros{Int}(50)
@test vec(Zeros{Int}(5,10,20)) ≡ Zeros{Int}(1000)
@test vec(Fill(1,5,10)) ≡ Fill(1,50)
end
@testset "in" begin
for T in [Zeros, Ones, Fill, Trues, Falses]
A = T(4, 4)
@test FillArrays.getindex_value(A) in A
@test !(FillArrays.getindex_value(A) + 1 in A)
end
A = FillArrays.RectDiagonal([1, 2, 3])
@test 3 in A
@test 0 in A
@test !(4 in A)
A = FillArrays.RectDiagonal([1])
@test 1 in A
@test !(0 in A)
A = FillArrays.RectDiagonal([2], (1:1, 1:4))
@test 2 in A
@test 0 in A
@test !(1 in A)
@test !(Zeros(1,1) in A)
A = FillArrays.RectDiagonal(Int[])
@test !(0 in A)
A = FillArrays.RectDiagonal(Int[], (1:0, 1:4))
@test !(0 in A)
end
@testset "promotion" begin
Z = Zeros{Int}(5)
Zf = Zeros(5)
@test promote_type(typeof(Z), typeof(Zf)) == typeof(Zf)
O = Ones{Int}(5)
Of = Ones{Float64}(5)
@test promote_type(typeof(O), typeof(Of)) == typeof(Of)
@test [Z,O] isa Vector{Fill{Int,1,Tuple{Base.OneTo{Int}}}}
@test [Z,Of] isa Vector{Fill{Float64,1,Tuple{Base.OneTo{Int}}}}
@test [O,O] isa Vector{Ones{Int,1,Tuple{Base.OneTo{Int}}}}
@test [O,Of] isa Vector{Ones{Float64,1,Tuple{Base.OneTo{Int}}}}
@test [Z,Zf] isa Vector{Zeros{Float64,1,Tuple{Base.OneTo{Int}}}}
@test convert(Ones{Int}, Of) ≡ convert(Ones{Int,1}, Of) ≡ convert(typeof(O), Of) ≡ O
@test convert(Zeros{Int}, Zf) ≡ convert(Zeros{Int,1}, Zf) ≡ convert(typeof(Z), Zf) ≡ Z
F = Fill(1, 2)
Ff = Fill(1.0, 2)
@test promote_type(typeof(F), typeof(Ff)) == typeof(Ff)
@test_throws MethodError convert(Zeros{SVector{2,Int}}, Zf)
end
end
@testset "interface" begin
struct Twos{T,N} <: FillArrays.AbstractFill{T,N,NTuple{N,Base.OneTo{Int}}}
sz :: NTuple{N,Int}
end
Twos{T}(sz::NTuple{N,Int}) where {T,N} = Twos{T,N}(sz)
Twos{T}(sz::Vararg{Int,N}) where {T,N} = Twos{T,N}(sz)
Base.size(A::Twos) = A.sz
FillArrays.getindex_value(A::Twos{T}) where {T} = oneunit(T) + oneunit(T)
@testset "broadcasting ambiguities" begin
A = Twos{Int}(3)
B = Zeros{Int}(size(A))
@test A .* B === B
@test B .* A === B
@test B ./ A === Zeros{Float64}(size(A))
@test A .\ B === Zeros{Float64}(size(A))
@test A ./ B === Fill(Inf, size(A))
end
end
@testset "isassigned" begin
for f in (Fill("", 3, 4), Zeros(3,4), Ones(3,4))
@test !isassigned(f, 0, 0)
@test isassigned(f, 2, 2)
@test !isassigned(f, 10, 10)
@test_throws ArgumentError isassigned(f, true)
end
end
@testset "indexing" begin
A = Fill(3.0,5)
@test A[1:3] ≡ Fill(3.0,3)
@test A[1:3,1:1] ≡ Fill(3.0,3,1)
@test_throws BoundsError A[1:3,2]
@test_throws BoundsError A[1:26]
@test A[[true, false, true, false, false]] ≡ Fill(3.0, 2)
A = Fill(3.0, 2, 2)
@test A[[true true; true false]] ≡ Fill(3.0, 3)
@test_throws BoundsError A[[true, false]]
A = Ones{Int}(5,5)
@test A[1:3] ≡ Ones{Int}(3)
@test A[1:3,1:2] ≡ Ones{Int}(3,2)
@test A[1:3,2] ≡ Ones{Int}(3)
@test_throws BoundsError A[1:26]
A = Ones{Int}(2,2)
@test A[[true false; true false]] ≡ Ones{Int}(2)
@test A[[true, false, true, false]] ≡ Ones{Int}(2)
@test_throws BoundsError A[[true false false; true false false]]
A = Zeros{Int}(5,5)
@test A[1:3] ≡ Zeros{Int}(3)
@test A[1:3,1:2] ≡ Zeros{Int}(3,2)
@test A[1:3,2] ≡ Zeros{Int}(3)
@test_throws BoundsError A[1:26]
A = Zeros{Int}(2,2)
@test A[[true false; true false]] ≡ Zeros{Int}(2)
@test A[[true, false, true, false]] ≡ Zeros{Int}(2)
@test_throws BoundsError A[[true false false; true false false]]
@testset "colon" begin
@test Ones(2)[:] ≡ Ones(2)[Base.Slice(Base.OneTo(2))] ≡ Ones(2)
@test Zeros(2)[:] ≡ Zeros(2)[Base.Slice(Base.OneTo(2))] ≡ Zeros(2)
@test Fill(3.0,2)[:] ≡ Fill(3.0,2)[Base.Slice(Base.OneTo(2))] ≡ Fill(3.0,2)
@test Ones(2,2)[:,:] ≡ Ones(2,2)[Base.Slice(Base.OneTo(2)),Base.Slice(Base.OneTo(2))] ≡ Ones(2,2)
@test Zeros(2,2)[:,:] ≡ Zeros(2)[Base.Slice(Base.OneTo(2)),Base.Slice(Base.OneTo(2))] ≡ Zeros(2,2)
@test Fill(3.0,2,2)[:,:] ≡ Fill(3.0,2,2)[Base.Slice(Base.OneTo(2)),Base.Slice(Base.OneTo(2))] ≡ Fill(3.0,2,2)
end
@testset "mixed integer / vector /colon" begin
a = Fill(2.0,5)
z = Zeros(5)
@test a[1:5] ≡ a[:] ≡ a
@test z[1:5] ≡ z[:] ≡ z
A = Fill(2.0,5,6)
Z = Zeros(5,6)
@test A[:,1] ≡ A[1:5,1] ≡ Fill(2.0,5)
@test A[1,:] ≡ A[1,1:6] ≡ Fill(2.0,6)
@test A[:,:] ≡ A[1:5,1:6] ≡ A[1:5,:] ≡ A[:,1:6] ≡ A
@test Z[:,1] ≡ Z[1:5,1] ≡ Zeros(5)
@test Z[1,:] ≡ Z[1,1:6] ≡ Zeros(6)
@test Z[:,:] ≡ Z[1:5,1:6] ≡ Z[1:5,:] ≡ Z[:,1:6] ≡ Z
A = Fill(2.0,5,6,7)
Z = Zeros(5,6,7)
@test A[:,1,1] ≡ A[1:5,1,1] ≡ Fill(2.0,5)
@test A[1,:,1] ≡ A[1,1:6,1] ≡ Fill(2.0,6)
@test A[:,:,:] ≡ A[1:5,1:6,1:7] ≡ A[1:5,:,1:7] ≡ A[:,1:6,1:7] ≡ A
end
@testset "StepRangeLen convert" begin
for (z,s) in ((Zeros{Int}(5), StepRangeLen(0, 0, 5)), (Ones{Int}(5), StepRangeLen(1, 0, 5)), (Fill(2,5), StepRangeLen(2, 0, 5)))
@test s == z
@test StepRangeLen(z) ≡ convert(StepRangeLen, z) ≡ convert(StepRangeLen{Int}, z) ≡ convert(typeof(s), z) ≡ convert(AbstractRange, z) ≡ s
end
end
end
@testset "RectDiagonal" begin
data = 1:3
expected_size = (5, 3)
expected_axes = Base.OneTo.(expected_size)
expected_matrix = [1 0 0; 0 2 0; 0 0 3; 0 0 0; 0 0 0]
expected = RectDiagonal{Int, UnitRange{Int}}(data, expected_axes)
@test axes(expected) == expected_axes
@test size(expected) == expected_size
@test (axes(expected, 1), axes(expected, 2)) == expected_axes
@test (size(expected, 1), size(expected, 2)) == expected_size
@test expected == expected_matrix
@test Matrix(expected) == expected_matrix
@test expected[:, 2] == expected_matrix[:, 2]
@test expected[2, :] == expected_matrix[2, :]
@test expected[5, :] == expected_matrix[5, :]
for Typ in (RectDiagonal, RectDiagonal{Int}, RectDiagonal{Int, UnitRange{Int}})
@test Typ(data) == expected[1:3, 1:3]
@test Typ(data, expected_axes) == expected
@test Typ(data, expected_axes...) == expected
@test Typ(data, expected_size) == expected
@test Typ(data, expected_size...) == expected
end
@test diag(expected) === expected.diag
mut = RectDiagonal(collect(data), expected_axes)
@test mut == expected
@test mut == expected_matrix
mut[1, 1] = 5
@test mut[1] == 5
@test diag(mut) == [5, 2, 3]
mut[2, 1] = 0
@test_throws ArgumentError mut[2, 1] = 9
D = RectDiagonal([1.,2.], (Base.OneTo(3),Base.OneTo(2)))
@test stringmime("text/plain", D) == "3×2 RectDiagonal{Float64, Vector{Float64}, Tuple{Base.OneTo{$Int}, Base.OneTo{$Int}}}:\n 1.0 ⋅ \n ⋅ 2.0\n ⋅ ⋅ "
end
# Check that all pair-wise combinations of + / - elements of As and Bs yield the correct
# type, and produce numerically correct results.
as_array(x::AbstractArray) = Array(x)
as_array(x::UniformScaling) = x
isapprox_or_undef(a::Number, b::Number) = (a ≈ b) || isequal(a, b)
isapprox_or_undef(a, b) = all(((x,y),) -> isapprox_or_undef(x,y), zip(a, b))
function test_addition_subtraction_dot(As, Bs, Tout::Type)
for A in As, B in Bs
@testset "$(typeof(A)) and $(typeof(B))" begin
@test @inferred(A + B) isa Tout{promote_type(eltype(A), eltype(B))}
@test isapprox_or_undef(as_array(A + B), as_array(A) + as_array(B))
@test @inferred(A - B) isa Tout{promote_type(eltype(A), eltype(B))}
@test isapprox_or_undef(as_array(A - B), as_array(A) - as_array(B))
@test @inferred(B + A) isa Tout{promote_type(eltype(B), eltype(A))}
@test isapprox_or_undef(as_array(B + A), as_array(B) + as_array(A))
@test @inferred(B - A) isa Tout{promote_type(eltype(B), eltype(A))}
@test isapprox_or_undef(as_array(B - A), as_array(B) - as_array(A))
# Julia 1.6 doesn't support dot(UniformScaling)
if VERSION < v"1.6.0" || VERSION >= v"1.8.0"
d1 = dot(A, B)
d2 = dot(as_array(A), as_array(B))
d3 = dot(B, A)
d4 = dot(as_array(B), as_array(A))
@test d1 ≈ d2 || d1 ≡ d2
@test d3 ≈ d4 || d3 ≡ d4
end
end
end
end
# Check that all permutations of + / - throw a `DimensionMismatch` exception.
function test_addition_and_subtraction_dim_mismatch(a, b)
@testset "$(typeof(a)) ± $(typeof(b))" begin
@test_throws DimensionMismatch a + b
@test_throws DimensionMismatch a - b
@test_throws DimensionMismatch b + a
@test_throws DimensionMismatch b - a
end
end
@testset "FillArray addition and subtraction" begin
test_addition_and_subtraction_dim_mismatch(Zeros(5), Zeros(6))
test_addition_and_subtraction_dim_mismatch(Zeros(5), Zeros{Int}(6))
test_addition_and_subtraction_dim_mismatch(Zeros(5), Zeros(6,6))
test_addition_and_subtraction_dim_mismatch(Zeros(5), Zeros{Int}(6,5))
# Construct FillArray for repeated use.
rng = MersenneTwister(123456)
A_fill, B_fill = Fill(randn(rng, Float64), 5), Fill(4, 5)
# Unary +/- constructs a new FillArray.
@test +A_fill === A_fill
@test -A_fill === Fill(-A_fill.value, 5)
# FillArray +/- FillArray should construct a new FillArray.
test_addition_subtraction_dot((A_fill, B_fill), (A_fill, B_fill), Fill)
test_addition_and_subtraction_dim_mismatch(A_fill, Fill(randn(rng), 5, 2))
# FillArray + Array (etc) should construct a new Array using `getindex`.
B_dense = (randn(rng, 5), [5, 4, 3, 2, 1], fill(Inf, 5), fill(NaN, 5))
test_addition_subtraction_dot((A_fill, B_fill), B_dense, Array)
test_addition_and_subtraction_dim_mismatch(A_fill, randn(rng, 5, 2))
# FillArray + StepLenRange / UnitRange (etc) should yield an AbstractRange.
A_ur, B_ur = 1.0:5.0, 6:10
test_addition_subtraction_dot((A_fill, B_fill), (A_ur, B_ur), AbstractRange)
test_addition_and_subtraction_dim_mismatch(A_fill, 1.0:6.0)
test_addition_and_subtraction_dim_mismatch(A_fill, 5:10)
# FillArray + UniformScaling should yield a Matrix in general
As_fill_square = (Fill(randn(rng, Float64), 3, 3), Fill(5, 4, 4))
Bs_us = (UniformScaling(2.3), UniformScaling(3))
test_addition_subtraction_dot(As_fill_square, Bs_us, Matrix)
As_fill_nonsquare = (Fill(randn(rng, Float64), 3, 2), Fill(5, 3, 4))
for A in As_fill_nonsquare, B in Bs_us
test_addition_and_subtraction_dim_mismatch(A, B)
end
# FillArray + StaticArray should not have ambiguities
A_svec, B_svec = SVector{5}(rand(5)), SVector(1, 2, 3, 4, 5)
test_addition_subtraction_dot((A_fill, B_fill, Zeros(5)), (A_svec, B_svec), SVector{5})
# Issue #224
A_matmat, B_matmat = Fill(rand(3,3),5), [rand(3,3) for n=1:5]
test_addition_subtraction_dot((A_matmat,), (A_matmat,), Fill)
test_addition_subtraction_dot((B_matmat,), (A_matmat,), Vector)
# Optimizations for Zeros and RectOrDiagonal{<:Any, <:AbstractFill}
As_special_square = (
Zeros(3, 3), Zeros{Int}(4, 4),
Eye(3), Eye{Int}(4), Eye(3, 3), Eye{Int}(4, 4),
Diagonal(Fill(randn(rng, Float64), 3)), Diagonal(Fill(3, 4)),
RectDiagonal(Fill(randn(rng, Float64), 3), 3, 3), RectDiagonal(Fill(3, 4), 4, 4)
)
DiagonalAbstractFill{T} = Diagonal{T, <:AbstractFill{T, 1}}
test_addition_subtraction_dot(As_special_square, Bs_us, DiagonalAbstractFill)
As_special_nonsquare = (
Zeros(3, 2), Zeros{Int}(3, 4),
Eye(3, 2), Eye{Int}(3, 4),
RectDiagonal(Fill(randn(rng, Float64), 2), 3, 2), RectDiagonal(Fill(3, 3), 3, 4)
)
for A in As_special_nonsquare, B in Bs_us
test_addition_and_subtraction_dim_mismatch(A, B)
end
@testset "Zeros" begin
As = ([1,2], Float64[1,2], Int8[1,2], ComplexF16[2,4])
Zs = (TZ -> Zeros{TZ}(2)).((Int, Float64, Int8, ComplexF64))
test_addition_subtraction_dot(As, Zs, Vector)
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
test_addition_and_subtraction_dim_mismatch(A, Z)
end
As = (@SArray([1,2]), @SArray(Float64[1,2]), @SArray(Int8[1,2]), @SArray(ComplexF16[2,4]))
test_addition_subtraction_dot(As, Zs, SVector{2})
for A in As, Z in (TZ -> Zeros{TZ}(3)).((Int, Float64, Int8, ComplexF64))
test_addition_and_subtraction_dim_mismatch(A, Z)
end
end
end
@testset "Other matrix types" begin
@test Diagonal(Zeros(5)) == Diagonal(zeros(5))
@test Diagonal(Zeros(8,5)) == Diagonal(zeros(5))
@test convert(Diagonal, Zeros(5,5)) == Diagonal(zeros(5))
@test_throws DimensionMismatch convert(Diagonal, Zeros(8,5))
@test convert(Diagonal{Int}, Zeros(5,5)) == Diagonal(zeros(Int,5))
@test_throws DimensionMismatch convert(Diagonal{Int}, Zeros(8,5))
@test Diagonal(Eye(8,5)) == Diagonal(ones(5))
@test convert(Diagonal, Eye(5)) == Diagonal(ones(5))
@test convert(Diagonal{Int}, Eye(5)) == Diagonal(ones(Int,5))
for E in (Eye(2,4), Eye(3))
M = collect(E)
for i in -5:5
@test diag(E, i) == diag(M, i)
end
end
end
@testset "one" begin
@testset for A in Any[Eye(4), Zeros(4,4), Ones(4,4), Fill(3,4,4)]
B = one(A)
@test B * A == A * B == A
end
@test_throws ArgumentError one(Ones(3,4))
@test_throws ArgumentError one(Ones((3:5,4:5)))
end
@testset "Sparse vectors and matrices" begin
@test SparseVector(Zeros(5)) ==
SparseVector{Int}(Zeros(5)) ==
SparseVector{Float64}(Zeros(5)) ==
SparseVector{Float64,Int}(Zeros(5)) ==
convert(AbstractSparseArray,Zeros(5)) ==
convert(AbstractSparseVector,Zeros(5)) ==
convert(AbstractSparseArray{Float64},Zeros(5)) ==
convert(AbstractSparseVector{Float64},Zeros(5)) ==
convert(AbstractSparseVector{Float64,Int},Zeros(5)) ==
spzeros(5)
for (Mat, SMat) in ((Zeros(5,5), spzeros(5,5)), (Zeros(6,5), spzeros(6,5)),
(Eye(5), sparse(I,5,5)), (Eye(6,5), sparse(I,6,5)))
@test SparseMatrixCSC(Mat) ==
SparseMatrixCSC{Int}(Mat) ==
SparseMatrixCSC{Float64}(Mat) ==
SparseMatrixCSC{Float64,Int}(Mat) ==
convert(AbstractSparseArray,Mat) ==
convert(AbstractSparseMatrix,Mat) ==
convert(AbstractSparseArray{Float64},Mat) ==
convert(AbstractSparseArray{Float64,Int},Mat) ==
convert(AbstractSparseMatrix{Float64},Mat) ==
convert(AbstractSparseMatrix{Float64,Int},Mat) ==
SMat
end
function testsparsediag(E)
S = @inferred SparseMatrixCSC(E)
@test S == E
S = @inferred SparseMatrixCSC{Float64}(E)
@test S == E
@test S isa SparseMatrixCSC{Float64}
@test convert(SparseMatrixCSC{Float64}, E) == S
S = @inferred SparseMatrixCSC{Float64,Int32}(E)
@test S == E
@test S isa SparseMatrixCSC{Float64,Int32}
@test convert(SparseMatrixCSC{Float64,Int32}, E) == S
end
for f in (Fill(Int8(4),3), Ones{Int8}(3), Zeros{Int8}(3))
E = Diagonal(f)
testsparsediag(E)
for sz in ((3,6), (6,3), (3,3))
E = RectDiagonal(f, sz)
testsparsediag(E)
end
end
end
@testset "==" begin
@test Zeros(5,4) == Fill(0,5,4)
@test Zeros(5,4) ≠ Zeros(3)
@test Ones(5,4) == Fill(1,5,4)
end
@testset "Rank" begin
@test rank(Zeros(5,4)) == 0
@test rank(Ones(5,4)) == 1
@test rank(Fill(2,5,4)) == 1
@test rank(Fill(0,5,4)) == 0
@test rank(Eye(2)) == 2
end
@testset "ishermitian" begin
@testset for el in (2, 3+0im, 4+5im, [1 2; 3 4], fill(2, 2, 2)), size in [(3,3), (3,4), (0,0), (0,1)]
@test issymmetric(Fill(el, size...)) == issymmetric(fill(el, size...))
@test ishermitian(Fill(el, size...)) == ishermitian(fill(el, size...))
end
end
@testset "BigInt indices" begin
for A in (Zeros(BigInt(100)), Ones(BigInt(100)), Fill(2, BigInt(100)))
@test length(A) isa BigInt
@test axes(A) == tuple(Base.OneTo{BigInt}(BigInt(100)))
@test size(A) isa Tuple{BigInt}
end
for A in (Eye(BigInt(100)), Eye(BigInt(100), BigInt(100)))
@test length(A) isa BigInt
@test axes(A) == tuple(Base.OneTo{BigInt}(BigInt(100)),Base.OneTo{BigInt}(BigInt(100)))
@test size(A) isa Tuple{BigInt,BigInt}
end
for A in (Zeros(BigInt(10), 10), Ones(BigInt(10), 10), Fill(2.0, (BigInt(10), 10)), Eye(BigInt(10), 8))
@test size(A) isa Tuple{BigInt,Int}
end
end
@testset "IndexStyle" begin
@test IndexStyle(Zeros(5,5)) == IndexStyle(typeof(Zeros(5,5))) == IndexLinear()
end
@testset "Identities" begin
@test Zeros(3,4) * randn(4,5) === randn(3,4) * Zeros(4,5) === Zeros(3, 5)
@test_throws DimensionMismatch randn(3,4) * Zeros(3, 3)
@test eltype(Zeros{Int}(3,4) * fill(1, 4, 5)) == Int
@test eltype(Zeros{Int}(3,4) * fill(3.4, 4, 5)) == Float64
@test Zeros(3, 4) * randn(4) == Zeros(3, 4) * Zeros(4) == Zeros(3)
@test Zeros(3, 4) * Zeros(4, 5) === Zeros(3, 5)
@test_throws MethodError [1,2,3]*Zeros(1) # Not defined for [1,2,3]*[0] either
@test [1,2,3]*Zeros(1,3) ≡ Zeros(3,3)
@test_throws MethodError [1,2,3]*Zeros(3) # Not defined for [1,2,3]*[0,0,0] either
@testset "Matrix multiplication with array elements" begin
x = [1 2; 3 4]
z = zero(SVector{2,Int})
ZV = Zeros{typeof(z)}(2)
A = Fill(x, 3, 2) * ZV
@test A isa Fill
@test size(A) == (3,)
@test A[1] == x * z
@test_throws DimensionMismatch Fill(x, 1, 1) * ZV
@test_throws DimensionMismatch Fill(oneton(1,1), 1, length(ZV)) * ZV
@test_throws DimensionMismatch Ones(SMatrix{3,3,Int,9},2) * Ones(SMatrix{2,2,Int,4},1,2)
end
@testset "Check multiplication by Adjoint vectors works as expected." begin
@test @inferred(oneton(4, 3)' * Zeros(4)) ≡ Zeros(3)
@test @inferred(oneton(4)' * Zeros(4)) ≡ @inferred(transpose(oneton(4)) * Zeros(4)) == 0.0
@test [1, 2, 3]' * Zeros{Int}(3) ≡ zero(Int)
@test [SVector(1,2)', SVector(2,3)', SVector(3,4)']' * Zeros{Int}(3) === SVector(0,0)
@test_throws DimensionMismatch oneton(4)' * Zeros(3)
@test Zeros(5)' * oneton(5,3) ≡ Zeros(5)'*Zeros(5,3) ≡ Zeros(5)'*Ones(5,3) ≡ Zeros(3)'
@test abs(Zeros(5)' * oneton(5)) == abs(Zeros(5)' * Zeros(5)) ≡ abs(Zeros(5)' * Ones(5)) == 0.0
@test Zeros(5) * Zeros(6)' ≡ Zeros(5,1) * Zeros(6)' ≡ Zeros(5,6)
@test oneton(5) * Zeros(6)' ≡ oneton(5,1) * Zeros(6)' ≡ Zeros(5,6)
@test Zeros(5) * oneton(6)' ≡ Zeros(5,6)
@test @inferred(Zeros{Int}(0)' * Zeros{Int}(0)) === zero(Int)
@test Any[1,2.0]' * Zeros{Int}(2) == 0
@test Real[1,2.0]' * Zeros{Int}(2) == 0
@test @inferred(([[1,2]])' * Zeros{SVector{2,Int}}(1)) ≡ 0
@test ([[1,2], [1,2]])' * Zeros{SVector{2,Int}}(2) ≡ 0
@test_throws DimensionMismatch ([[1,2,3]])' * Zeros{SVector{2,Int}}(1)
@test_throws DimensionMismatch ([[1,2,3], [1,2]])' * Zeros{SVector{2,Int}}(2)
A = SMatrix{2,1,Int,2}[]'
B = Zeros(SVector{2,Int},0)
C = collect(B)
@test @inferred(A * B) == @inferred(A * C)
end
@testset "Check multiplication by Transpose-d vectors works as expected." begin
@test transpose(oneton(4, 3)) * Zeros(4) === Zeros(3)
@test transpose(oneton(4)) * Zeros(4) == 0.0
@test transpose([1, 2, 3]) * Zeros{Int}(3) === zero(Int)
@test_throws DimensionMismatch transpose(oneton(4)) * Zeros(3)
@test transpose(Zeros(5)) * oneton(5,3) ≡ transpose(Zeros(5))*Zeros(5,3) ≡ transpose(Zeros(5))*Ones(5,3) ≡ transpose(Zeros(3))
@test abs(transpose(Zeros(5)) * oneton(5)) ≡ abs(transpose(Zeros(5)) * Zeros(5)) ≡ abs(transpose(Zeros(5)) * Ones(5)) ≡ 0.0
@test oneton(5) * transpose(Zeros(6)) ≡ oneton(5,1) * transpose(Zeros(6)) ≡ Zeros(5,6)
@test Zeros(5) * transpose(oneton(6)) ≡ Zeros(5,6)
@test transpose(oneton(5)) * Zeros(5) == 0.0
@test transpose(oneton(5) .+ im) * Zeros(5) == 0.0 + 0im
@test @inferred(transpose(Zeros{Int}(0)) * Zeros{Int}(0)) === zero(Int)
@test transpose(Any[1,2.0]) * Zeros{Int}(2) == 0
@test transpose(Real[1,2.0]) * Zeros{Int}(2) == 0
@test @inferred(transpose([[1,2]]) * Zeros{SVector{2,Int}}(1)) ≡ 0
@test transpose([[1,2], [1,2]]) * Zeros{SVector{2,Int}}(2) ≡ 0
@test_throws DimensionMismatch transpose([[1,2,3]]) * Zeros{SVector{2,Int}}(1)
@test_throws DimensionMismatch transpose([[1,2,3], [1,2]]) * Zeros{SVector{2,Int}}(2)
A = transpose(SMatrix{2,1,Int,2}[])
B = Zeros(SVector{2,Int},0)
C = collect(B)
@test @inferred(A * B) == @inferred(A * C)
@testset "Diagonal mul introduced in v1.9" begin
@test Zeros(5)'*Diagonal(1:5) ≡ Zeros(5)'
@test transpose(Zeros(5))*Diagonal(1:5) ≡ transpose(Zeros(5))
@test Zeros(5)'*Diagonal(1:5)*(1:5) ==
(1:5)'*Diagonal(1:5)*Zeros(5) ==
transpose(1:5)*Diagonal(1:5)*Zeros(5) ==
Zeros(5)'*Diagonal(1:5)*Zeros(5) ==
transpose(Zeros(5))*Diagonal(1:5)*Zeros(5) ==
transpose(Zeros(5))*Diagonal(1:5)*(1:5)
@test_throws DimensionMismatch Zeros(6)'*Diagonal(1:5)*Zeros(5)
@test_throws DimensionMismatch Zeros(5)'*Diagonal(1:6)*Zeros(5)
@test_throws DimensionMismatch Zeros(5)'*Diagonal(1:5)*Zeros(6)
end
end
z1, z2 = Zeros{Float64}(4), Zeros{Int}(4)
@testset "`Zeros` are closed under addition and subtraction (both unary and binary)." begin
@test +(Zeros{Float64}(3, 5)) === Zeros{Float64}(3, 5)
@test -(Zeros{Float32}(5, 2)) === Zeros{Float32}(5, 2)
@test +(z1) === z1
@test -(z1) === z1
test_addition_subtraction_dot((z1, z2), (z1, z2), Zeros)
test_addition_and_subtraction_dim_mismatch(z1, Zeros{Float64}(4, 2))
end
# `Zeros` +/- `Fill`s should yield `Fills`.
fill1, fill2 = Fill(5.0, 4), Fill(5, 4)
test_addition_subtraction_dot((z1, z2), (fill1, fill2), Fill)
test_addition_and_subtraction_dim_mismatch(z1, Fill(5, 5))
X = randn(3, 5)
for op in [+, -]
# Addition / subtraction with same eltypes.
@test op(Zeros(6, 4), Zeros(6, 4)) === Zeros(6, 4)
@test_throws DimensionMismatch op(X, Zeros(4, 6))
@test eltype(op(Zeros(3, 5), X)) == Float64
# Different eltypes, the other way around.
@test op(X, Zeros{Float32}(3, 5)) isa Matrix{Float64}
@test !(op(X, Zeros{Float32}(3, 5)) === X)
@test op(X, Zeros{Float32}(3, 5)) == X
@test !(op(X, Zeros{ComplexF64}(3, 5)) === X)
@test op(X, Zeros{ComplexF64}(3, 5)) == X
# Addition / subtraction of Zeros.
@test eltype(op(Zeros{Float64}(4, 5), Zeros{Int}(4, 5))) == Float64
@test eltype(op(Zeros{Int}(5, 4), Zeros{Float32}(5, 4))) == Float32
@test op(Zeros{Float64}(4, 5), Zeros{Int}(4, 5)) isa Zeros{Float64}
@test op(Zeros{Float64}(4, 5), Zeros{Int}(4, 5)) === Zeros{Float64}(4, 5)
end
@testset "Zeros +/- dense where + / - have different results." begin
@test +(Zeros(3, 5), X) == X && +(X, Zeros(3, 5)) == X
@test !(Zeros(3, 5) + X === X) && !(X + Zeros(3, 5) === X)
@test -(Zeros(3, 5), X) == -X
end
@testset "Addition with different eltypes." begin
@test +(Zeros{Float32}(3, 5), X) isa Matrix{Float64}
@test !(+(Zeros{Float32}(3, 5), X) === X)
@test +(Zeros{Float32}(3, 5), X) == X
@test !(+(Zeros{ComplexF64}(3, 5), X) === X)
@test +(Zeros{ComplexF64}(3, 5), X) == X
end
@testset "Subtraction with different eltypes." begin
@test -(Zeros{Float32}(3, 5), X) isa Matrix{Float64}
@test -(Zeros{Float32}(3, 5), X) == -X
@test -(Zeros{ComplexF64}(3, 5), X) == -X
end
@testset "Tests for ranges." begin
X = randn(5)
@test !(Zeros(5) + X ≡ X)
@test Zeros{Int}(5) + (1:5) ≡ (1:5) + Zeros{Int}(5) ≡ (1:5)
@test Zeros(5) + (1:5) ≡ (1:5) + Zeros(5) ≡ (1.0:1.0:5.0)
@test (1:5) - Zeros{Int}(5) ≡ (1:5)
@test Zeros{Int}(5) - (1:5) ≡ -1:-1:-5
@test Zeros(5) - (1:5) ≡ -1.0:-1.0:-5.0
@test Zeros{Int}(5) + (1.0:5) ≡ (1.0:5) + Zeros{Int}(5) ≡ 1.0:5
end
@testset "test Base.zero" begin
@test zero(Zeros(10)) == Zeros(10)
@test zero(Ones(10,10)) == Zeros(10,10)
@test zero(Fill(0.5, 10, 10)) == Zeros(10,10)
end
@testset "Matrix ±" begin
x = Fill([1,2], 5)
z = Zeros{SVector{2,Int}}(5)
@test +(z) ≡ -(z) ≡ z
@test +(x) == x
@test -(x) == Fill(-[1,2], 5)
end
end
@testset "maximum/minimum/svd/sort" begin
@test maximum(Fill(1, 1_000_000_000)) == minimum(Fill(1, 1_000_000_000)) == 1
@test svdvals(fill(2,5,6)) ≈ svdvals(Fill(2,5,6))
@test svdvals(Eye(5)) === Fill(1.0,5)
@test sort(Ones(5)) == sort!(Ones(5))
@test_throws MethodError issorted(Fill(im, 2))
@test_throws MethodError sort(Fill(im, 2))
@test_throws MethodError sort!(Fill(im, 2))
end
@testset "Cumsum, accumulate and diff" begin
@test @inferred(sum(Fill(3,10))) ≡ 30
@test @inferred(reduce(+, Fill(3,10))) ≡ 30
@test @inferred(sum(x -> x + 1, Fill(3,10))) ≡ 40
@test @inferred(cumsum(Fill(3,10))) ≡ @inferred(accumulate(+, Fill(3,10))) ≡ StepRangeLen(3,3,10)
@test @inferred(accumulate(-, Fill(3,10))) ≡ StepRangeLen(3,-3,10)
@test @inferred(sum(Ones(10))) ≡ 10.0
@test @inferred(sum(x -> x + 1, Ones(10))) ≡ 20.0
@test @inferred(cumsum(Ones(10))) ≡ @inferred(accumulate(+, Ones(10))) ≡ StepRangeLen(1.0, 1.0, 10)
@test @inferred(accumulate(-, Ones(10))) ≡ StepRangeLen(1.0,-1.0,10)
@test sum(Ones{Int}(10)) ≡ 10
@test sum(x -> x + 1, Ones{Int}(10)) ≡ 20
@test cumsum(Ones{Int}(10)) ≡ accumulate(+,Ones{Int}(10)) ≡ Base.OneTo(10)
@test accumulate(-, Ones{Int}(10)) ≡ StepRangeLen(1,-1,10)
@test sum(Zeros(10)) ≡ 0.0
@test sum(x -> x + 1, Zeros(10)) ≡ 10.0
@test cumsum(Zeros(10)) ≡ accumulate(+,Zeros(10)) ≡ accumulate(-,Zeros(10)) ≡ Zeros(10)
@test sum(Zeros{Int}(10)) ≡ 0
@test sum(x -> x + 1, Zeros{Int}(10)) ≡ 10
@test cumsum(Zeros{Int}(10)) ≡ accumulate(+,Zeros{Int}(10)) ≡ accumulate(-,Zeros{Int}(10)) ≡ Zeros{Int}(10)
# we want cumsum of fills to match the types of the standard cusum
@test all(cumsum(Zeros{Bool}(10)) .≡ cumsum(zeros(Bool,10)))
@test all(accumulate(+, Zeros{Bool}(10)) .≡ accumulate(+, zeros(Bool,10)) .≡ accumulate(-, zeros(Bool,10)))
@test cumsum(Zeros{Bool}(10)) ≡ accumulate(+, Zeros{Bool}(10)) ≡ accumulate(-, Zeros{Bool}(10)) ≡ Zeros{Int}(10)
@test cumsum(Ones{Bool}(10)) ≡ accumulate(+, Ones{Bool}(10)) ≡ Base.OneTo{Int}(10)
@test all(cumsum(Fill(true,10)) .≡ cumsum(fill(true,10)))
@test cumsum(Fill(true,10)) ≡ StepRangeLen(1, true, 10)
@test all(cumsum(Zeros{UInt8}(10)) .≡ cumsum(zeros(UInt8,10)))
@test all(accumulate(+, Zeros{UInt8}(10)) .≡ accumulate(+, zeros(UInt8,10)))
@test cumsum(Zeros{UInt8}(10)) ≡ Zeros{UInt64}(10)
@test accumulate(+, Zeros{UInt8}(10)) ≡ accumulate(-, Zeros{UInt8}(10)) ≡ Zeros{UInt8}(10)
@test all(cumsum(Ones{UInt8}(10)) .≡ cumsum(ones(UInt8,10)))
@test all(accumulate(+, Ones{UInt8}(10)) .≡ accumulate(+, ones(UInt8,10)))
@test cumsum(Ones{UInt8}(10)) ≡ Base.OneTo(UInt64(10))
@test accumulate(+, Ones{UInt8}(10)) ≡ Base.OneTo(UInt8(10))
@test all(cumsum(Fill(UInt8(2),10)) .≡ cumsum(fill(UInt8(2),10)))
@test all(accumulate(+, Fill(UInt8(2))) .≡ accumulate(+, fill(UInt8(2))))
@test cumsum(Fill(UInt8(2),10)) ≡ StepRangeLen(UInt64(2), UInt8(2), 10)
@test accumulate(+, Fill(UInt8(2),10)) ≡ StepRangeLen(UInt8(2), UInt8(2), 10)
@test diff(Fill(1,10)) ≡ Zeros{Int}(9)
@test diff(Ones{Float64}(10)) ≡ Zeros{Float64}(9)
@test_throws UndefKeywordError cumsum(Fill(1,1,5))
@test @inferred(sum([Ones(4)])) ≡ Fill(1.0, 4)
@test @inferred(sum([Trues(4)])) ≡ Fill(1, 4)
@testset "infinite arrays" begin
r = InfiniteArrays.OneToInf()
A = Ones{Int}((r,))
@test isinf(sum(A))
@test sum(A) == length(A)
@test sum(x->x^2, A) == sum(A.^2)
@testset "IteratorSize" begin
@test (@inferred Base.IteratorSize(Ones())) == Base.IteratorSize(ones())
@test (@inferred Base.IteratorSize(Ones(2))) == Base.IteratorSize(ones(2))
@test (@inferred Base.IteratorSize(Ones(r))) == Base.IsInfinite()
@test (@inferred Base.IteratorSize(Fill(2, (1:2, 1:2)))) == Base.HasShape{2}()
@test (@inferred Base.IteratorSize(Fill(2, (1:2, r)))) == Base.IsInfinite()
@test (@inferred Base.IteratorSize(Fill(2, (r, 1:2)))) == Base.IsInfinite()
@test (@inferred Base.IteratorSize(Fill(2, (r, r)))) == Base.IsInfinite()
end
@test issorted(Fill(2, (InfiniteArrays.OneToInf(),)))
end
end
@testset "iterators" begin
@testset "invalid state" begin
@test isnothing(iterate(Ones(4), (1,-3)))
@test isempty(Iterators.rest(Ones(4), (1,-3)))
end
@testset "Iterators.rest" begin
@test Iterators.rest(Fill(4, 10), (4, 3)) === Fill(4, 7)
# Base.rest
a, b... = Fill(3, 4)
@test a === 3
@test b === Fill(3, 3)
a, b... = Ones(3, 4)
@test a === 1.0
@test b === Ones(11)
end
@testset "Iterators.drop/take" begin
@test Iterators.drop(Fill(4, 10), 3) === Fill(4, 7)
@test Iterators.take(Fill(4, 10), 3) === Fill(4, 3)
@test Iterators.drop(Fill(4, 10), 0) === Fill(4, 10)
@test Iterators.take(Fill(4, 10), 0) === Fill(4, 0)
@test Iterators.drop(Fill(4, 10), 11) === Fill(4, 0)
@test Iterators.take(Fill(4, 10), 11) === Fill(4, 10)
@test_throws ArgumentError Iterators.drop(Fill(4, 10), -11)
@test_throws ArgumentError Iterators.take(Fill(4, 10), -11)
@test Iterators.drop(Ones(4, 10), 3) === Ones(37)
@test Iterators.take(Ones(4, 10), 3) === Ones(3)
end
end
@testset "Broadcast" begin
x = Fill(5,5)
@test (.+)(x) ≡ x
@test (.-)(x) ≡ -x
@test exp.(x) ≡ Fill(exp(5),5)
@test x .+ 1 ≡ Fill(6,5)
@test 1 .+ x ≡ Fill(6,5)
@test x .+ x ≡ Fill(10,5)
@test x .+ Ones(5) ≡ Fill(6.0,5)
f = (x,y) -> cos(x*y)
@test f.(x, Ones(5)) ≡ Fill(f(5,1.0),5)
@test x .^ 1 ≡ Fill(5,5)
y = Ones(5,5)
@test (.+)(y) ≡ Ones(5,5)
@test (.-)(y) ≡ Fill(-1.0,5,5)
@test exp.(y) ≡ Fill(exp(1),5,5)
@test y .+ 1 ≡ Fill(2.0,5,5)
@test y .+ y ≡ Fill(2.0,5,5)
@test y .* y ≡ y ./ y ≡ y .\ y ≡ y
@test y .^ 1 ≡ y .^ 0 ≡ Ones(5,5)
rng = MersenneTwister(123456)
sizes = [(5, 4), (5, 1), (1, 4), (1, 1), (5,)]
for sx in sizes, sy in sizes
x, y = Fill(randn(rng), sx), Fill(randn(rng), sy)
x_one, y_one = Ones(sx), Ones(sy)
x_zero, y_zero = Zeros(sx), Zeros(sy)
x_dense, y_dense = randn(rng, sx), randn(rng, sy)
for x in [x, x_one, x_zero, x_dense], y in [y, y_one, y_zero, y_dense]
@test x .+ y == collect(x) .+ collect(y)
end
@test x_zero .+ y_zero isa Zeros
@test x_zero .+ y_one isa Ones
@test x_one .+ y_zero isa Ones
for x in [x, x_one, x_zero, x_dense], y in [y, y_one, y_zero, y_dense]
@test x .* y == collect(x) .* collect(y)
end
for x in [x, x_one, x_zero, x_dense]
@test x .* y_zero isa Zeros
end
for y in [y, y_one, y_zero, y_dense]
@test x_zero .* y isa Zeros
end
end
@test Zeros{Int}(5) .^ 0 ≡ Ones{Int}(5)
@test Zeros{Int}(5) .^ 1 ≡ Zeros{Int}(5)
@test Zeros{Int}(5) .+ Zeros(5) isa Zeros{Float64}
# Test for conj, real and imag with complex element types
@test conj(Zeros{ComplexF64}(10)) isa Zeros{ComplexF64}
@test conj(Zeros{ComplexF64}(10,10)) isa Zeros{ComplexF64}
@test conj(Ones{ComplexF64}(10)) isa Ones{ComplexF64}
@test conj(Ones{ComplexF64}(10,10)) isa Ones{ComplexF64}
@test real(Zeros{Float64}(10)) isa Zeros{Float64}
@test real(Zeros{Float64}(10,10)) isa Zeros{Float64}
@test real(Zeros{ComplexF64}(10)) isa Zeros{Float64}
@test real(Zeros{ComplexF64}(10,10)) isa Zeros{Float64}
@test real(Ones{Float64}(10)) isa Ones{Float64}
@test real(Ones{Float64}(10,10)) isa Ones{Float64}
@test real(Ones{ComplexF64}(10)) isa Ones{Float64}
@test real(Ones{ComplexF64}(10,10)) isa Ones{Float64}