forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranges.jl
1502 lines (1351 loc) · 54.1 KB
/
ranges.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Dates, Random
isdefined(Main, :PhysQuantities) || @eval Main include("testhelpers/PhysQuantities.jl")
using .Main.PhysQuantities
# Compare precision in a manner sensitive to subnormals, which lose
# precision compared to widening.
function cmp_sn(w, hi, lo, slopbits=0)
if !isfinite(hi)
if abs(w) > floatmax(typeof(hi))
return isinf(hi) && sign(w) == sign(hi)
end
if isnan(w) && isnan(hi)
return true
end
return w == hi
end
if abs(w) < subnormalmin(typeof(hi))
return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
end
# Compare w == hi + lo unless `lo` issubnormal
z = widen(hi) + widen(lo)
if !issubnormal(lo) && lo != 0
if slopbits == 0
return z == w
end
wr, zr = roundshift(w, slopbits), roundshift(z, slopbits)
return max(wr-1, zero(wr)) <= zr <= wr+1
end
# round w to the same number of bits as z
zu = asbits(z)
wu = asbits(w)
lastbit = false
while zu > 0 && !isodd(zu)
lastbit = isodd(wu)
zu = zu >> 1
wu = wu >> 1
end
return wu <= zu <= wu + lastbit
end
asbits(x) = reinterpret(Base.uinttype(typeof(x)), x)
function roundshift(x, n)
xu = asbits(x)
lastbit = false
for i = 1:n
lastbit = isodd(xu)
xu = xu >> 1
end
xu + lastbit
end
subnormalmin(::Type{T}) where T = reinterpret(T, Base.uinttype(T)(1))
function highprec_pair(x, y)
slopbits = (Base.Math.significand_bits(typeof(widen(x))) + 1) -
2*(Base.Math.significand_bits(typeof(x)) + 1)
hi, lo = Base.add12(x, y)
@test cmp_sn(widen(x) + widen(y), hi, lo)
hi, lo = Base.mul12(x, y)
@test cmp_sn(widen(x) * widen(y), hi, lo)
y == 0 && return nothing
hi, lo = Base.div12(x, y)
@test cmp_sn(widen(x) / widen(y), hi, lo, slopbits)
nothing
end
@testset "high precision" begin
# Because ranges rely on high precision arithmetic, test those utilities first
for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64)), i = 1:10^3
i = rand(I) >> 1 # test large values below
hi, lo = Base.splitprec(T, i)
@test widen(hi) + widen(lo) == i
@test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
end
for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64))
x = T(typemax(I))
Δi = ceil(I, eps(x))
for i = typemax(I)-2Δi:typemax(I)-Δi
hi, lo = Base.splitprec(T, i)
@test widen(hi) + widen(lo) == i
@test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
end
for i = typemin(I):typemin(I)+Δi
hi, lo = Base.splitprec(T, i)
@test widen(hi) + widen(lo) == i
@test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
end
end
# # This tests every possible pair of Float16s. It takes too long for
# # ordinary use, which is why it's commented out.
# function pair16()
# for yu in 0x0000:0xffff
# for xu in 0x0000:0xffff
# x, y = reinterpret(Float16, xu), reinterpret(Float16, yu)
# highprec_pair(x, y)
# end
# end
# end
for T in (Float16, Float32) # skip Float64 (bit representation of BigFloat is not available)
for i = 1:10^5
x, y = rand(T), rand(T)
highprec_pair(x, y)
highprec_pair(-x, y)
highprec_pair(x, -y)
highprec_pair(-x, -y)
end
# Make sure we test dynamic range too
for i = 1:10^5
x, y = rand(T), rand(T)
x == 0 || y == 0 && continue
x, y = log(x), log(y)
highprec_pair(x, y)
end
end
end
asww(x) = widen(widen(x.hi)) + widen(widen(x.lo))
astuple(x) = (x.hi, x.lo)
function cmp_sn2(w, hi, lo, slopbits=0)
if !isfinite(hi)
if abs(w) > floatmax(typeof(hi))
return isinf(hi) && sign(w) == sign(hi)
end
if isnan(w) && isnan(hi)
return true
end
return w == hi
end
if abs(w) < subnormalmin(typeof(hi))
return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
end
z = widen(hi) + widen(lo)
w == z && return true
zu, wu = asbits(z), asbits(w)
while zu > 0 && !isodd(zu)
zu = zu >> 1
wu = wu >> 1
end
zu = zu >> slopbits
wu = wu >> slopbits
return wu - 1 <= zu <= wu + 1
end
@testset "TwicePrecision" begin
# TwicePrecision test. These routines lose accuracy if you form
# intermediate subnormals; with Float16, this happens so frequently,
# let's only test Float32.
let T = Float32
Tw = widen(T)
slopbits = (Base.Math.significand_bits(Tw) + 1) -
2*(Base.Math.significand_bits(T) + 1)
for i = 1:10^5
x = Base.TwicePrecision{T}(rand())
y = Base.TwicePrecision{T}(rand())
xw, yw = asww(x), asww(y)
@test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
@test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
@test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
@test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
y = rand(T)
yw = widen(widen(y))
@test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
@test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
@test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
@test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
end
end
x1 = Base.TwicePrecision{Float64}(1)
x0 = Base.TwicePrecision{Float64}(0)
xinf = Base.TwicePrecision{Float64}(Inf)
@test Float64(x1+x0) == 1
@test Float64(x1+0) == 1
@test Float64(x1+0.0) == 1
@test Float64(x1*x0) == 0
@test Float64(x1*0) == 0
@test Float64(x1*0.0) == 0
@test Float64(x1/x0) == Inf
@test Float64(x1/0) == Inf
@test Float64(xinf*x1) == Inf
@test isnan(Float64(xinf*x0))
@test isnan(Float64(xinf*0))
@test isnan(Float64(xinf*0.0))
@test isnan(Float64(x0/x0))
@test isnan(Float64(x0/0))
@test isnan(Float64(x0/0.0))
x = Base.TwicePrecision(PhysQuantity{1}(4.0))
@test x.hi*2 === PhysQuantity{1}(8.0)
@test_throws ErrorException("Int is incommensurate with PhysQuantity") x*2 # not a MethodError for convert
@test x.hi/2 === PhysQuantity{1}(2.0)
@test_throws ErrorException("Int is incommensurate with PhysQuantity") x/2
end
@testset "ranges" begin
@test size(10:1:0) == (0,)
@testset "colon" begin
@inferred((:)(10, 1, 0))
@inferred((:)(1, .2, 2))
@inferred((:)(1., .2, 2.))
@inferred((:)(2, -.2, 1))
@inferred((:)(1, 0))
@inferred((:)(0.0, -0.5))
end
@testset "indexing" begin
L32 = @inferred(range(Int32(1), stop=Int32(4), length=4))
L64 = @inferred(range(Int64(1), stop=Int64(4), length=4))
@test @inferred(L32[1]) === 1.0 && @inferred(L64[1]) === 1.0
@test L32[2] == 2 && L64[2] == 2
@test L32[3] == 3 && L64[3] == 3
@test L32[4] == 4 && L64[4] == 4
@test @inferred(range(1.0, stop=2.0, length=2))[1] === 1.0
@test @inferred(range(1.0f0, stop=2.0f0, length=2))[1] === 1.0f0
@test @inferred(range(Float16(1.0), stop=Float16(2.0), length=2))[1] === Float16(1.0)
let r = 5:-1:1
@test r[1]==5
@test r[2]==4
@test r[3]==3
@test r[4]==2
@test r[5]==1
end
@test @inferred((0.1:0.1:0.3)[2]) === 0.2
@test @inferred((0.1f0:0.1f0:0.3f0)[2]) === 0.2f0
@test @inferred((1:5)[1:4]) === 1:4
@test @inferred((1.0:5)[1:4]) === 1.0:4
@test (2:6)[1:4] == 2:5
@test (1:6)[2:5] === 2:5
@test (1:6)[2:2:5] === 2:2:4
@test (1:2:13)[2:6] === 3:2:11
@test (1:2:13)[2:3:7] === 3:6:13
@test isempty((1:4)[5:4])
@test_throws BoundsError (1:10)[8:-1:-2]
let r = typemax(Int)-5:typemax(Int)-1
@test_throws BoundsError r[7]
end
end
@testset "length" begin
@test length(.1:.1:.3) == 3
@test length(1.1:1.1:3.3) == 3
@test length(1.1:1.3:3) == 2
@test length(1:1:1.8) == 1
@test length(1:.2:2) == 6
@test length(1.:.2:2.) == 6
@test length(2:-.2:1) == 6
@test length(2.:-.2:1.) == 6
@test length(2:.2:1) == 0
@test length(2.:.2:1.) == 0
@test length(1:0) == 0
@test length(0.0:-0.5) == 0
@test length(1:2:0) == 0
@test length(Char(0):Char(0x001fffff)) == 2097152
@test length(typemax(UInt64)//one(UInt64):1:typemax(UInt64)//one(UInt64)) == 1
end
@testset "keys/values" begin
keytype_is_correct(r) = keytype(r) == eltype(keys(r))
valtype_is_correct(r) = valtype(r) == eltype(values(r))
@test keytype_is_correct(1:3)
@test keytype_is_correct(1:.3:4)
@test keytype_is_correct(.1:.1:.3)
@test keytype_is_correct(Int8(1):Int8(5))
@test keytype_is_correct(Int16(1):Int8(5))
@test keytype_is_correct(Int16(1):Int8(3):Int8(5))
@test keytype_is_correct(Int8(1):Int16(3):Int8(5))
@test keytype_is_correct(Int8(1):Int8(3):Int16(5))
@test keytype_is_correct(Int64(1):Int64(5))
@test keytype_is_correct(Int64(1):Int64(5))
@test keytype_is_correct(Int128(1):Int128(5))
@test keytype_is_correct(Base.OneTo(4))
@test keytype_is_correct(Base.OneTo(Int32(4)))
@test valtype_is_correct(1:3)
@test valtype_is_correct(1:.3:4)
@test valtype_is_correct(.1:.1:.3)
@test valtype_is_correct(Int8(1):Int8(5))
@test valtype_is_correct(Int16(1):Int8(5))
@test valtype_is_correct(Int16(1):Int8(3):Int8(5))
@test valtype_is_correct(Int8(1):Int16(3):Int8(5))
@test valtype_is_correct(Int8(1):Int8(3):Int16(5))
@test valtype_is_correct(Int64(1):Int64(5))
@test valtype_is_correct(Int64(1):Int64(5))
@test valtype_is_correct(Int128(1):Int128(5))
@test valtype_is_correct(Base.OneTo(4))
@test valtype_is_correct(Base.OneTo(Int32(4)))
end
@testset "findall(::Base.Fix2{typeof(in)}, ::Array)" begin
@test findall(in(3:20), [5.2, 3.3]) == findall(in(Vector(3:20)), [5.2, 3.3])
let span = 5:20,
r = -7:3:42
@test findall(in(span), r) == 5:10
r = 15:-2:-38
@test findall(in(span), r) == 1:6
end
end
@testset "findfirst" begin
@test findfirst(isequal(7), 1:2:10) == 4
@test findfirst(==(7), 1:2:10) == 4
@test findfirst(==(10), 1:2:10) == nothing
@test findfirst(==(11), 1:2:10) == nothing
end
@testset "reverse" begin
@test reverse(reverse(1:10)) == 1:10
@test reverse(reverse(typemin(Int):typemax(Int))) == typemin(Int):typemax(Int)
@test reverse(reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)
end
@testset "intersect" begin
@test intersect(1:5, 2:3) == 2:3
@test intersect(-3:5, 2:8) == 2:5
@test intersect(-8:-3, -8:-3) == -8:-3
@test intersect(1:5, 5:13) == 5:5
@test isempty(intersect(-8:-3, -2:2))
@test isempty(intersect(-3:7, 2:1))
@test intersect(1:11, -2:3:15) == 1:3:10
@test intersect(1:11, -2:2:15) == 2:2:10
@test intersect(1:11, -2:1:15) == 1:11
@test intersect(1:11, 15:-1:-2) == 1:11
@test intersect(1:11, 15:-4:-2) == 3:4:11
@test intersect(-20:-5, -10:3:-2) == -10:3:-7
@test isempty(intersect(-5:5, -6:13:20))
@test isempty(intersect(1:11, 15:4:-2))
@test isempty(intersect(11:1, 15:-4:-2))
#@test intersect(-5:5, 1+0*(1:3)) == 1:1
#@test isempty(intersect(-5:5, 6+0*(1:3)))
@test intersect(-15:4:7, -10:-2) == -7:4:-3
@test intersect(13:-2:1, -2:8) == 7:-2:1
@test isempty(intersect(13:2:1, -2:8))
@test isempty(intersect(13:-2:1, 8:-2))
#@test intersect(5+0*(1:4), 2:8) == 5+0*(1:4)
#@test isempty(intersect(5+0*(1:4), -7:3))
@test intersect(0:3:24, 0:4:24) == 0:12:24
@test intersect(0:4:24, 0:3:24) == 0:12:24
@test intersect(0:3:24, 24:-4:0) == 0:12:24
@test intersect(24:-3:0, 0:4:24) == 24:-12:0
@test intersect(24:-3:0, 24:-4:0) == 24:-12:0
@test intersect(1:3:24, 0:4:24) == 4:12:16
@test intersect(0:6:24, 0:4:24) == 0:12:24
@test isempty(intersect(1:6:2400, 0:4:2400))
@test intersect(-51:5:100, -33:7:125) == -26:35:79
@test intersect(-51:5:100, -32:7:125) == -11:35:94
#@test intersect(0:6:24, 6+0*(0:4:24)) == 6:6:6
#@test intersect(12+0*(0:6:24), 0:4:24) == AbstractRange(12, 0, 5)
#@test isempty(intersect(6+0*(0:6:24), 0:4:24))
@test intersect(-10:3:24, -10:3:24) == -10:3:23
@test isempty(intersect(-11:3:24, -10:3:24))
@test intersect(typemin(Int):2:typemax(Int),1:10) == 2:2:10
@test intersect(1:10,typemin(Int):2:typemax(Int)) == 2:2:10
@test intersect(reverse(typemin(Int):2:typemax(Int)),typemin(Int):2:typemax(Int)) == reverse(typemin(Int):2:typemax(Int))
@test intersect(typemin(Int):2:typemax(Int),reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)
@test intersect(UnitRange(1,2),3) == UnitRange(3,2)
@test intersect(UnitRange(1,2), UnitRange(1,5), UnitRange(3,7), UnitRange(4,6)) == UnitRange(4,3)
@test intersect(1:3, 2) === intersect(2, 1:3) === 2:2
@test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0]
end
@testset "sort/sort!/partialsort" begin
@test sort(UnitRange(1,2)) == UnitRange(1,2)
@test sort!(UnitRange(1,2)) == UnitRange(1,2)
@test sort(1:10, rev=true) == 10:-1:1
@test sort(-3:3, by=abs) == [0,-1,1,-2,2,-3,3]
@test partialsort(1:10, 4) == 4
end
@testset "in" begin
@test 0 in UInt(0):100:typemax(UInt)
@test last(UInt(0):100:typemax(UInt)) in UInt(0):100:typemax(UInt)
@test -9223372036854775790 in -9223372036854775790:100:9223372036854775710
@test -9223372036854775690 in -9223372036854775790:100:9223372036854775710
@test -90 in -9223372036854775790:100:9223372036854775710
@test 10 in -9223372036854775790:100:9223372036854775710
@test 110 in -9223372036854775790:100:9223372036854775710
@test 9223372036854775610 in -9223372036854775790:100:9223372036854775710
@test 9223372036854775710 in -9223372036854775790:100:9223372036854775710
@test !(3.5 in 1:5)
@test (3 in 1:5)
@test (3 in 5:-1:1)
#@test (3 in 3+0*(1:5))
#@test !(4 in 3+0*(1:5))
let r = 0.0:0.01:1.0
@test (r[30] in r)
end
let r = (-4*Int64(maxintfloat(Int === Int32 ? Float32 : Float64))):5
@test (3 in r)
@test (3.0 in r)
end
@test !(1 in 1:0)
@test !(1.0 in 1.0:0.0)
end
@testset "in() works across types, including non-numeric types (#21728)" begin
@test 1//1 in 1:3
@test 1//1 in 1.0:3.0
@test !(5//1 in 1:3)
@test !(5//1 in 1.0:3.0)
@test Complex(1, 0) in 1:3
@test Complex(1, 0) in 1.0:3.0
@test Complex(1.0, 0.0) in 1:3
@test Complex(1.0, 0.0) in 1.0:3.0
@test !(Complex(1, 1) in 1:3)
@test !(Complex(1, 1) in 1.0:3.0)
@test !(Complex(1.0, 1.0) in 1:3)
@test !(Complex(1.0, 1.0) in 1.0:3.0)
@test !(π in 1:3)
@test !(π in 1.0:3.0)
@test !("a" in 1:3)
@test !("a" in 1.0:3.0)
@test !(1 in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
@test !(Complex(1, 0) in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
@test !(π in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
@test !("a" in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
end
end
@testset "indexing range with empty range (#4309)" begin
@test (3:6)[5:4] == 7:6
@test_throws BoundsError (3:6)[5:5]
@test_throws BoundsError (3:6)[5]
@test (0:2:10)[7:6] == 12:2:10
@test_throws BoundsError (0:2:10)[7:7]
end
# indexing with negative ranges (#8351)
for a=AbstractRange[3:6, 0:2:10], b=AbstractRange[0:1, 2:-1:0]
@test_throws BoundsError a[b]
end
# avoiding intermediate overflow (#5065)
@test length(1:4:typemax(Int)) == div(typemax(Int),4) + 1
@testset "overflow in length" begin
Tset = Int === Int64 ? (Int,UInt,Int128,UInt128) :
(Int,UInt,Int64,UInt64,Int128, UInt128)
for T in Tset
@test_throws OverflowError length(zero(T):typemax(T))
@test_throws OverflowError length(typemin(T):typemax(T))
@test_throws OverflowError length(zero(T):one(T):typemax(T))
@test_throws OverflowError length(typemin(T):one(T):typemax(T))
if T <: Signed
@test_throws OverflowError length(-one(T):typemax(T)-one(T))
@test_throws OverflowError length(-one(T):one(T):typemax(T)-one(T))
end
end
end
@testset "loops involving typemin/typemax" begin
n = 0
s = 0
# loops ending at typemax(Int)
for i = (typemax(Int)-1):typemax(Int)
s += 1
@test s <= 2
end
@test s == 2
s = 0
for i = (typemax(Int)-2):(typemax(Int)-1)
s += 1
@test s <= 2
end
@test s == 2
s = 0
for i = typemin(Int):(typemin(Int)+1)
s += 1
@test s <= 2
end
@test s == 2
# loops covering the full range of integers
s = 0
for i = typemin(UInt8):typemax(UInt8)
s += 1
end
@test s == 256
s = 0
for i = typemin(UInt):typemax(UInt)
i == 10 && break
s += 1
end
@test s == 10
s = 0
for i = typemin(UInt8):one(UInt8):typemax(UInt8)
s += 1
end
@test s == 256
s = 0
for i = typemin(UInt):1:typemax(UInt)
i == 10 && break
s += 1
end
@test s == 10
# loops past typemax(Int)
n = 0
s = Int128(0)
for i = typemax(UInt64)-2:typemax(UInt64)
n += 1
s += i
end
@test n == 3
@test s == 3*Int128(typemax(UInt64)) - 3
# loops over empty ranges
s = 0
for i = 0xff:0x00
s += 1
end
@test s == 0
s = 0
for i = Int128(typemax(Int128)):Int128(typemin(Int128))
s += 1
end
@test s == 0
end
@testset "sums of ranges" begin
@test sum(1:100) == 5050
@test sum(0:100) == 5050
@test sum(-100:100) == 0
@test sum(0:2:100) == 2550
end
@testset "overflowing sums (see #5798)" begin
if Sys.WORD_SIZE == 64
@test sum(Int128(1):10^18) == div(10^18 * (Int128(10^18)+1), 2)
@test sum(Int128(1):10^18-1) == div(10^18 * (Int128(10^18)-1), 2)
else
@test sum(Int64(1):10^9) == div(10^9 * (Int64(10^9)+1), 2)
@test sum(Int64(1):10^9-1) == div(10^9 * (Int64(10^9)-1), 2)
end
end
@testset "Tricky sums of StepRangeLen #8272" begin
@test sum(10000.:-0.0001:0) == 5.00000005e11
@test sum(0:0.001:1) == 500.5
@test sum(0:0.000001:1) == 500000.5
@test sum(0:0.1:10) == 505.
end
@testset "broadcasted operations with scalars" begin
@test broadcast(-, 1:3) === -1:-1:-3
@test broadcast(-, 1:3, 2) === -1:1
@test broadcast(-, 1:3, 0.25) === 1-0.25:3-0.25
@test broadcast(+, 1:3) === 1:3
@test broadcast(+, 1:3, 2) === 3:5
@test broadcast(+, 1:3, 0.25) === 1+0.25:3+0.25
@test broadcast(+, 1:2:6, 1) === 2:2:6
@test broadcast(+, 1:2:6, 0.3) === 1+0.3:2:5+0.3
@test broadcast(-, 1:2:6, 1) === 0:2:4
@test broadcast(-, 1:2:6, 0.3) === 1-0.3:2:5-0.3
@test broadcast(-, 2, 1:3) === 1:-1:-1
end
@testset "operations between ranges and arrays" begin
@test all(([1:5;] + (5:-1:1)) .== 6)
@test all(((5:-1:1) + [1:5;]) .== 6)
@test all(([1:5;] - (1:5)) .== 0)
@test all(((1:5) - [1:5;]) .== 0)
end
@testset "tricky floating-point ranges" begin
for (start, step, stop, len) in ((1, 1, 3, 3), (0, 1, 3, 4),
(3, -1, -1, 5), (1, -1, -3, 5),
(0, 1, 10, 11), (0, 7, 21, 4),
(0, 11, 33, 4), (1, 11, 34, 4),
(0, 13, 39, 4), (1, 13, 40, 4),
(11, 11, 33, 3), (3, 1, 11, 9),
(0, 10, 55, 0), (0, -1, 5, 0), (0, 10, 5, 0),
(0, 1, 5, 0), (0, -10, 5, 0), (0, -10, 0, 1),
(0, -1, 1, 0), (0, 1, -1, 0), (0, -1, -10, 11))
r = start/10:step/10:stop/10
a = Vector(start:step:stop)./10
ra = Vector(r)
@test r == a
@test isequal(r, a)
@test r == ra
@test isequal(r, ra)
@test hash(r) == hash(a)
@test hash(r) == hash(ra)
if len > 0
l = range(start/10, stop=stop/10, length=len)
la = Vector(l)
@test a == l
@test r == l
@test isequal(a, l)
@test isequal(r, l)
@test l == la
@test isequal(l, la)
@test hash(l) == hash(a)
@test hash(l) == hash(la)
end
end
@test 1.0:1/49:27.0 == range(1.0, stop=27.0, length=1275) == [49:1323;]./49
@test isequal(1.0:1/49:27.0, range(1.0, stop=27.0, length=1275))
@test isequal(1.0:1/49:27.0, Vector(49:1323)./49)
@test hash(1.0:1/49:27.0) == hash(range(1.0, stop=27.0, length=1275)) == hash(Vector(49:1323)./49)
@test [prevfloat(0.1):0.1:0.3;] == [prevfloat(0.1), 0.2, 0.3]
@test [nextfloat(0.1):0.1:0.3;] == [nextfloat(0.1), 0.2]
@test [prevfloat(0.0):0.1:0.3;] == [prevfloat(0.0), 0.1, 0.2]
@test [nextfloat(0.0):0.1:0.3;] == [nextfloat(0.0), 0.1, 0.2]
@test [0.1:0.1:prevfloat(0.3);] == [0.1, 0.2]
@test [0.1:0.1:nextfloat(0.3);] == [0.1, 0.2, nextfloat(0.3)]
@test [0.0:0.1:prevfloat(0.3);] == [0.0, 0.1, 0.2]
@test [0.0:0.1:nextfloat(0.3);] == [0.0, 0.1, 0.2, nextfloat(0.3)]
@test [0.1:prevfloat(0.1):0.3;] == [0.1, 0.2, 0.3]
@test [0.1:nextfloat(0.1):0.3;] == [0.1, 0.2]
@test [0.0:prevfloat(0.1):0.3;] == [0.0, prevfloat(0.1), prevfloat(0.2), 0.3]
@test [0.0:nextfloat(0.1):0.3;] == [0.0, nextfloat(0.1), nextfloat(0.2)]
end
function loop_range_values(::Type{T}) where T
for a = -5:25,
s = [-5:-1; 1:25; ],
d = 1:25,
n = -1:15
denom = convert(T, d)
strt = convert(T, a)/denom
Δ = convert(T, s)/denom
stop = convert(T, (a + (n - 1) * s)) / denom
vals = T[a:s:(a + (n - 1) * s); ] ./ denom
r = strt:Δ:stop
@test [r;] == vals
@test [range(strt, stop=stop, length=length(r));] == vals
n = length(r)
@test [r[1:n];] == [r;]
@test [r[2:n];] == [r;][2:end]
@test [r[1:3:n];] == [r;][1:3:n]
@test [r[2:2:n];] == [r;][2:2:n]
@test [r[n:-1:2];] == [r;][n:-1:2]
@test [r[n:-2:1];] == [r;][n:-2:1]
end
end
@testset "issue #7420 for type $T" for T = (Float32, Float64,) # BigFloat),
loop_range_values(T)
end
@testset "issue #20373 (unliftable ranges with exact end points)" begin
@test [3*0.05:0.05:0.2;] == [range(3*0.05, stop=0.2, length=2);] == [3*0.05,0.2]
@test [0.2:-0.05:3*0.05;] == [range(0.2, stop=3*0.05, length=2);] == [0.2,3*0.05]
@test [-3*0.05:-0.05:-0.2;] == [range(-3*0.05, stop=-0.2, length=2);] == [-3*0.05,-0.2]
@test [-0.2:0.05:-3*0.05;] == [range(-0.2, stop=-3*0.05, length=2);] == [-0.2,-3*0.05]
end
function range_fuzztests(::Type{T}, niter, nrange) where {T}
for i = 1:niter, n in nrange
strt, Δ = randn(T), randn(T)
Δ == 0 && continue
stop = strt + (n-1)*Δ
# `n` is not necessarily unique s.t. `strt + (n-1)*Δ == stop`
# so test that `length(strt:Δ:stop)` satisfies this identity
# and is the closest value to `(stop-strt)/Δ` to do so
lo = hi = n
while strt + (lo-1)*Δ == stop; lo -= 1; end
while strt + (hi-1)*Δ == stop; hi += 1; end
m = clamp(round(Int, (stop-strt)/Δ) + 1, lo+1, hi-1)
r = strt:Δ:stop
@test m == length(r)
@test strt == first(r)
@test Δ == step(r)
@test_skip stop == last(r)
l = range(strt, stop=stop, length=n)
@test n == length(l)
@test strt == first(l)
@test stop == last(l)
end
end
@testset "range fuzztests for $T" for T = (Float32, Float64,)
range_fuzztests(T, 2^15, 1:5)
end
@testset "Inexact errors on 32 bit architectures. #22613" begin
@test first(range(log(0.2), stop=log(10.0), length=10)) == log(0.2)
@test last(range(log(0.2), stop=log(10.0), length=10)) == log(10.0)
@test length(Base.floatrange(-3e9, 1.0, 1, 1.0)) == 1
end
@testset "ranges with very small endpoints for type $T" for T = (Float32, Float64)
z = zero(T)
u = eps(z)
@test first(range(u, stop=u, length=0)) == u
@test last(range(u, stop=u, length=0)) == u
@test first(range(-u, stop=u, length=0)) == -u
@test last(range(-u, stop=u, length=0)) == u
@test [range(-u, stop=u, length=0);] == []
@test [range(-u, stop=-u, length=1);] == [-u]
@test [range(-u, stop=u, length=2);] == [-u,u]
@test [range(-u, stop=u, length=3);] == [-u,0,u]
@test first(range(-u, stop=-u, length=0)) == -u
@test last(range(-u, stop=-u, length=0)) == -u
@test first(range(u, stop=-u, length=0)) == u
@test last(range(u, stop=-u, length=0)) == -u
@test [range(u, stop=-u, length=0);] == []
@test [range(u, stop=u, length=1);] == [u]
@test [range(u, stop=-u, length=2);] == [u,-u]
@test [range(u, stop=-u, length=3);] == [u,0,-u]
v = range(-u, stop=u, length=12)
@test length(v) == 12
@test [-3u:u:3u;] == [range(-3u, stop=3u, length=7);] == [-3:3;].*u
@test [3u:-u:-3u;] == [range(3u, stop=-3u, length=7);] == [3:-1:-3;].*u
end
@testset "range with very large endpoints for type $T" for T = (Float32, Float64)
largeint = Int(min(maxintfloat(T), typemax(Int)))
a = floatmax()
for i = 1:5
@test [range(a, stop=a, length=1);] == [a]
@test [range(-a, stop=-a, length=1);] == [-a]
b = floatmax()
for j = 1:5
@test [range(-a, stop=b, length=0);] == []
@test [range(-a, stop=b, length=2);] == [-a,b]
@test [range(-a, stop=b, length=3);] == [-a,(b-a)/2,b]
@test [range(a, stop=-b, length=0);] == []
@test [range(a, stop=-b, length=2);] == [a,-b]
@test [range(a, stop=-b, length=3);] == [a,(a-b)/2,-b]
for c = largeint-3:largeint
s = range(-a, stop=b, length=c)
@test first(s) == -a
@test last(s) == b
@test length(s) == c
s = range(a, stop=-b, length=c)
@test first(s) == a
@test last(s) == -b
@test length(s) == c
end
b = prevfloat(b)
end
a = prevfloat(a)
end
end
# issue #20380
let r = LinRange(1,4,4)
@test isa(r[1:4], LinRange)
end
@testset "range with 1 or 0 elements (whose step length is NaN)" begin
@test issorted(range(1, stop=1, length=0))
@test issorted(range(1, stop=1, length=1))
end
# near-equal ranges
@test 0.0:0.1:1.0 != 0.0f0:0.1f0:1.0f0
# comparing and hashing ranges
@testset "comparing and hashing ranges" begin
Rs = AbstractRange[1:1, 1:1:1, 1:2, 1:1:2,
map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0,
0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)),
1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20)),
3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0)]
for r in Rs
local r
ar = Vector(r)
@test r == ar
@test isequal(r,ar)
@test hash(r) == hash(ar)
for s in Rs
as = Vector(s)
@test isequal(r,s) == (hash(r)==hash(s))
@test (r==s) == (ar==as)
end
end
end
@testset "comparing UnitRanges and OneTo" begin
@test 1:2:10 == 1:2:10 != 1:3:10 != 1:3:13 != 2:3:13 == 2:3:11 != 2:11
@test 1:1:10 == 1:10 == 1:10 == Base.OneTo(10) == Base.OneTo(10)
@test 1:10 != 2:10 != 2:11 != Base.OneTo(11)
@test Base.OneTo(10) != Base.OneTo(11) != 1:10
@test Base.OneTo(0) == 5:4
end
# issue #2959
@test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0
#@test 1.0:(.3-.1)/.1 == 1.0:2.0
@testset "length with typemin/typemax" begin
let r = typemin(Int64):2:typemax(Int64), s = typemax(Int64):-2:typemin(Int64)
@test first(r) == typemin(Int64)
@test last(r) == (typemax(Int64)-1)
@test_throws OverflowError length(r)
@test first(s) == typemax(Int64)
@test last(s) == (typemin(Int64)+1)
@test_throws OverflowError length(s)
end
@test length(typemin(Int64):3:typemax(Int64)) == 6148914691236517206
@test length(typemax(Int64):-3:typemin(Int64)) == 6148914691236517206
for s in 3:100
@test length(typemin(Int):s:typemax(Int)) == length(big(typemin(Int)):big(s):big(typemax(Int)))
@test length(typemax(Int):-s:typemin(Int)) == length(big(typemax(Int)):big(-s):big(typemin(Int)))
end
@test length(UInt(1):UInt(1):UInt(0)) == 0
@test length(typemax(UInt):UInt(1):(typemax(UInt)-1)) == 0
@test length(typemax(UInt):UInt(2):(typemax(UInt)-1)) == 0
@test length((typemin(Int)+3):5:(typemin(Int)+1)) == 0
end
# issue #6364
@test length((1:64)*(pi/5)) == 64
@testset "issue #6973" begin
r1 = 1.0:0.1:2.0
r2 = 1.0f0:0.2f0:3.0f0
r3 = 1:2:21
@test r1 + r1 == 2*r1
@test r1 + r2 == 2.0:0.3:5.0
@test (r1 + r2) - r2 == r1
@test r1 + r3 == convert(StepRangeLen{Float64}, r3) + r1
@test r3 + r3 == 2 * r3
end
@testset "issue #7114" begin
let r = -0.004532318104333742:1.2597349521122731e-5:0.008065031416788989
@test length(r[1:end-1]) == length(r) - 1
@test isa(r[1:2:end],AbstractRange) && length(r[1:2:end]) == div(length(r)+1, 2)
@test r[3:5][2] ≈ r[4]
@test r[5:-2:1][2] ≈ r[3]
@test_throws BoundsError r[0:10]
@test_throws BoundsError r[1:10000]
end
let r = range(1/3, stop=5/7, length=6)
@test length(r) == 6
@test r[1] == 1/3
@test abs(r[end] - 5/7) <= eps(5/7)
end
let r = range(0.25, stop=0.25, length=1)
@test length(r) == 1
@test_throws ArgumentError range(0.25, stop=0.5, length=1)
end
end
# issue #7426
@test [typemax(Int):1:typemax(Int);] == [typemax(Int)]
#issue #7484
let r7484 = 0.1:0.1:1
@test [reverse(r7484);] == reverse([r7484;])
end
@testset "issue #7387" begin
for r in (0:1, 0.0:1.0)
local r
@test [r .+ im;] == [r;] .+ im
@test [r .- im;] == [r;] .- im
@test [r * im;] == [r;] * im
@test [r / im;] == [r;] / im
end
end
# Preservation of high precision upon addition
let r = (-0.1:0.1:0.3) + broadcast(+, -0.3:0.1:0.1, 1e-12)
@test r[3] == 1e-12
end
@testset "issue #7709" begin
@test length(map(identity, 0x01:0x05)) == 5
@test length(map(identity, 0x0001:0x0005)) == 5
@test length(map(identity, UInt64(1):UInt64(5))) == 5
@test length(map(identity, UInt128(1):UInt128(5))) == 5
end
@testset "issue #8531" begin
smallint = (Int === Int64 ?
(Int8,UInt8,Int16,UInt16,Int32,UInt32) :
(Int8,UInt8,Int16,UInt16))
for T in smallint
@test length(typemin(T):typemax(T)) == 2^(8*sizeof(T))
end
end
# issue #8584
@test (0:1//2:2)[1:2:3] == 0:1//1:1
# issue #12278
@test length(1:UInt(0)) == 0
@testset "zip" begin
i = 0
x = 1:2:8
y = 2:2:8
xy = 1:8
for (thisx, thisy) in zip(x, y)
@test thisx == xy[i+=1]
@test thisy == xy[i+=1]
end
end
@testset "issue #9962" begin
@test eltype(0:1//3:10) <: Rational
@test (0:1//3:10)[1] == 0
@test (0:1//3:10)[2] == 1//3
end
@testset "converting ranges (issue #10965)" begin
@test promote(0:1, UInt8(2):UInt8(5)) === (0:1, 2:5)
@test convert(UnitRange{Int}, 0:5) === 0:5
@test convert(UnitRange{Int128}, 0:5) === Int128(0):Int128(5)
@test promote(0:1:1, UInt8(2):UInt8(1):UInt8(5)) === (0:1:1, 2:1:5)
@test convert(StepRange{Int,Int}, 0:1:1) === 0:1:1
@test convert(StepRange{Int128,Int128}, 0:1:1) === Int128(0):Int128(1):Int128(1)
@test promote(0:1:1, 2:5) === (0:1:1, 2:1:5)
@test convert(StepRange{Int128,Int128}, 0:5) === Int128(0):Int128(1):Int128(5)
@test convert(StepRange, 0:5) === 0:1:5
@test convert(StepRange{Int128,Int128}, 0.:5) === Int128(0):Int128(1):Int128(5)
@test_throws ArgumentError StepRange(1.1,1,5.1)
@test promote(0f0:inv(3f0):1f0, 0.:2.:5.) === (0:1/3:1, 0.:2.:5.)
@test convert(StepRangeLen{Float64}, 0:1/3:1) === 0:1/3:1
@test convert(StepRangeLen{Float64}, 0f0:inv(3f0):1f0) === 0:1/3:1
@test promote(0:1/3:1, 0:5) === (0:1/3:1, 0.:1.:5.)
@test convert(StepRangeLen{Float64}, 0:5) === 0.:1.:5.
@test convert(StepRangeLen{Float64}, 0:1:5) === 0.:1.:5.
@test convert(StepRangeLen, 0:5) == 0:5
@test convert(StepRangeLen, 0:1:5) == 0:1:5
@test convert(LinRange{Float64}, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4)
@test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4)
@test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4)
@test promote('a':'z', 1:2) === ('a':'z', 1:1:2)
@test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T)
end
@testset "LinRange ops" begin
@test 2*LinRange(0,3,4) == LinRange(0,6,4)
@test LinRange(0,3,4)*2 == LinRange(0,6,4)
@test LinRange(0,3,4)/3 == LinRange(0,1,4)
@test broadcast(-, 2, LinRange(0,3,4)) == LinRange(2,-1,4)
@test broadcast(+, 2, LinRange(0,3,4)) == LinRange(2,5,4)
@test -LinRange(0,3,4) == LinRange(0,-3,4)
@test reverse(LinRange(0,3,4)) == LinRange(3,0,4)
end
@testset "Issue #11245" begin
io = IOBuffer()
show(io, range(1, stop=2, length=3))
str = String(take!(io))
# @test str == "range(1.0, stop=2.0, length=3)"
@test str == "1.0:0.5:2.0"
end
@testset "issue 10950" begin
r = 1//2:3
@test length(r) == 3
i = 1
for x in r
@test x == i//2
i += 2
end
@test i == 7
end
@testset "repr" begin
# repr/show should display the range nicely
# to test print_range in range.jl
replrepr(x) = repr("text/plain", x; context=IOContext(stdout, :limit=>true, :displaysize=>(24, 80)))
@test replrepr(1:4) == "1:4"
@test repr("text/plain", 1:4) == "1:4"
@test repr("text/plain", range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0"
@test repr("text/plain", LinRange{Float64}(1,5,7)) == "7-element LinRange{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0"
@test repr(range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0"
@test repr(LinRange{Float64}(1,5,7)) == "range(1.0, stop=5.0, length=7)"
@test replrepr(0:100.) == "0.0:1.0:100.0"
# next is to test a very large range, which should be fast because print_range
# only examines spacing of the left and right edges of the range, sufficient
# to cover the designated screen size.
@test replrepr(range(0, stop=100, length=10000)) == "0.0:0.010001000100010001:100.0"
@test replrepr(LinRange{Float64}(0,100, 10000)) == "10000-element LinRange{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0"
@test sprint(show, UnitRange(1, 2)) == "1:2"
@test sprint(show, StepRange(1, 2, 5)) == "1:2:5"
end
@testset "Issue 11049 and related" begin
@test promote(range(0f0, stop=1f0, length=3), range(0., stop=5., length=2)) ===
(range(0., stop=1., length=3), range(0., stop=5., length=2))