## "Fossies" - the Fresh Open Source Software Archive

### Member "julia-1.1.1/share/julia/test/ranges.jl" (16 May 2019, 53078 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:

As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

```    1 # This file is a part of Julia. License is MIT: https://julialang.org/license
2
3 using Dates, Random
4 isdefined(Main, :PhysQuantities) || @eval Main include("testhelpers/PhysQuantities.jl")
5 using .Main.PhysQuantities
6
7 # Compare precision in a manner sensitive to subnormals, which lose
8 # precision compared to widening.
9 function cmp_sn(w, hi, lo, slopbits=0)
10     if !isfinite(hi)
11         if abs(w) > floatmax(typeof(hi))
12             return isinf(hi) && sign(w) == sign(hi)
13         end
14         if isnan(w) && isnan(hi)
15             return true
16         end
17         return w == hi
18     end
19     if abs(w) < subnormalmin(typeof(hi))
20         return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
21     end
22     # Compare w == hi + lo unless `lo` issubnormal
23     z = widen(hi) + widen(lo)
24     if !issubnormal(lo) && lo != 0
25         if slopbits == 0
26             return z == w
27         end
28         wr, zr = roundshift(w, slopbits), roundshift(z, slopbits)
29         return max(wr-1, zero(wr)) <= zr <= wr+1
30     end
31     # round w to the same number of bits as z
32     zu = asbits(z)
33     wu = asbits(w)
34     lastbit = false
35     while zu > 0 && !isodd(zu)
36         lastbit = isodd(wu)
37         zu = zu >> 1
38         wu = wu >> 1
39     end
40     return wu <= zu <= wu + lastbit
41 end
42
43 asbits(x) = reinterpret(Base.uinttype(typeof(x)), x)
44
45 function roundshift(x, n)
46     xu = asbits(x)
47     lastbit = false
48     for i = 1:n
49         lastbit = isodd(xu)
50         xu = xu >> 1
51     end
52     xu + lastbit
53 end
54
55 subnormalmin(::Type{T}) where T = reinterpret(T, Base.uinttype(T)(1))
56
57 function highprec_pair(x, y)
58     slopbits = (Base.Math.significand_bits(typeof(widen(x))) + 1) -
59         2*(Base.Math.significand_bits(typeof(x)) + 1)
60     hi, lo = Base.add12(x, y)
61     @test cmp_sn(widen(x) + widen(y), hi, lo)
62     hi, lo = Base.mul12(x, y)
63     @test cmp_sn(widen(x) * widen(y), hi, lo)
64     y == 0 && return nothing
65     hi, lo = Base.div12(x, y)
66     @test cmp_sn(widen(x) / widen(y), hi, lo, slopbits)
67     nothing
68 end
69 @testset "high precision" begin
70     # Because ranges rely on high precision arithmetic, test those utilities first
71     for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64)), i = 1:10^3
72         i = rand(I) >> 1  # test large values below
73         hi, lo = Base.splitprec(T, i)
74         @test widen(hi) + widen(lo) == i
75         @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
76     end
77     for (I, T) in ((Int16, Float16), (Int32, Float32), (Int64, Float64))
78         x = T(typemax(I))
79         Δi = ceil(I, eps(x))
80         for i = typemax(I)-2Δi:typemax(I)-Δi
81             hi, lo = Base.splitprec(T, i)
82             @test widen(hi) + widen(lo) == i
83             @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
84         end
85         for i = typemin(I):typemin(I)+Δi
86             hi, lo = Base.splitprec(T, i)
87             @test widen(hi) + widen(lo) == i
88             @test endswith(bitstring(hi), repeat('0', Base.Math.significand_bits(T) ÷ 2))
89         end
90     end
91
92     # # This tests every possible pair of Float16s. It takes too long for
93     # # ordinary use, which is why it's commented out.
94     # function pair16()
95     #     for yu in 0x0000:0xffff
96     #         for xu in 0x0000:0xffff
97     #             x, y = reinterpret(Float16, xu), reinterpret(Float16, yu)
98     #             highprec_pair(x, y)
99     #         end
100     #     end
101     # end
102
103     for T in (Float16, Float32) # skip Float64 (bit representation of BigFloat is not available)
104         for i = 1:10^5
105             x, y = rand(T), rand(T)
106             highprec_pair(x, y)
107             highprec_pair(-x, y)
108             highprec_pair(x, -y)
109             highprec_pair(-x, -y)
110         end
111         # Make sure we test dynamic range too
112         for i = 1:10^5
113             x, y = rand(T), rand(T)
114             x == 0 || y == 0 && continue
115             x, y = log(x), log(y)
116             highprec_pair(x, y)
117         end
118     end
119 end
120 asww(x) = widen(widen(x.hi)) + widen(widen(x.lo))
121 astuple(x) = (x.hi, x.lo)
122
123 function cmp_sn2(w, hi, lo, slopbits=0)
124     if !isfinite(hi)
125         if abs(w) > floatmax(typeof(hi))
126             return isinf(hi) && sign(w) == sign(hi)
127         end
128         if isnan(w) && isnan(hi)
129             return true
130         end
131         return w == hi
132     end
133     if abs(w) < subnormalmin(typeof(hi))
134         return (hi == zero(hi) || abs(w - widen(hi)) < abs(w)) && lo == zero(hi)
135     end
136     z = widen(hi) + widen(lo)
137     w == z && return true
138     zu, wu = asbits(z), asbits(w)
139     while zu > 0 && !isodd(zu)
140         zu = zu >> 1
141         wu = wu >> 1
142     end
143     zu = zu >> slopbits
144     wu = wu >> slopbits
145     return wu - 1 <= zu <= wu + 1
146 end
147 @testset "TwicePrecision" begin
148     # TwicePrecision test. These routines lose accuracy if you form
149     # intermediate subnormals; with Float16, this happens so frequently,
150     # let's only test Float32.
151     let T = Float32
152         Tw = widen(T)
153         slopbits = (Base.Math.significand_bits(Tw) + 1) -
154             2*(Base.Math.significand_bits(T) + 1)
155         for i = 1:10^5
156             x = Base.TwicePrecision{T}(rand())
157             y = Base.TwicePrecision{T}(rand())
158             xw, yw = asww(x), asww(y)
159             @test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
160             @test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
161             @test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
162             @test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
163             y = rand(T)
164             yw = widen(widen(y))
165             @test cmp_sn2(Tw(xw+yw), astuple(x+y)..., slopbits)
166             @test cmp_sn2(Tw(xw-yw), astuple(x-y)..., slopbits)
167             @test cmp_sn2(Tw(xw*yw), astuple(x*y)..., slopbits)
168             @test cmp_sn2(Tw(xw/yw), astuple(x/y)..., slopbits)
169         end
170     end
171
172     x1 = Base.TwicePrecision{Float64}(1)
173     x0 = Base.TwicePrecision{Float64}(0)
174     xinf = Base.TwicePrecision{Float64}(Inf)
175     @test Float64(x1+x0)  == 1
176     @test Float64(x1+0)   == 1
177     @test Float64(x1+0.0) == 1
178     @test Float64(x1*x0)  == 0
179     @test Float64(x1*0)   == 0
180     @test Float64(x1*0.0) == 0
181     @test Float64(x1/x0)  == Inf
182     @test Float64(x1/0)   == Inf
183     @test Float64(xinf*x1) == Inf
184     @test isnan(Float64(xinf*x0))
185     @test isnan(Float64(xinf*0))
186     @test isnan(Float64(xinf*0.0))
187     @test isnan(Float64(x0/x0))
188     @test isnan(Float64(x0/0))
189     @test isnan(Float64(x0/0.0))
190
191     x = Base.TwicePrecision(PhysQuantity{1}(4.0))
192     @test x.hi*2 === PhysQuantity{1}(8.0)
193     @test_throws ErrorException("Int is incommensurate with PhysQuantity") x*2   # not a MethodError for convert
194     @test x.hi/2 === PhysQuantity{1}(2.0)
195     @test_throws ErrorException("Int is incommensurate with PhysQuantity") x/2
196 end
197 @testset "ranges" begin
198     @test size(10:1:0) == (0,)
199     @testset "colon" begin
200         @inferred((:)(10, 1, 0))
201         @inferred((:)(1, .2, 2))
202         @inferred((:)(1., .2, 2.))
203         @inferred((:)(2, -.2, 1))
204         @inferred((:)(1, 0))
205         @inferred((:)(0.0, -0.5))
206     end
207
208     @testset "indexing" begin
209         L32 = @inferred(range(Int32(1), stop=Int32(4), length=4))
210         L64 = @inferred(range(Int64(1), stop=Int64(4), length=4))
211         @test @inferred(L32[1]) === 1.0 && @inferred(L64[1]) === 1.0
212         @test L32[2] == 2 && L64[2] == 2
213         @test L32[3] == 3 && L64[3] == 3
214         @test L32[4] == 4 && L64[4] == 4
215         @test @inferred(range(1.0, stop=2.0, length=2))[1] === 1.0
216         @test @inferred(range(1.0f0, stop=2.0f0, length=2))[1] === 1.0f0
217         @test @inferred(range(Float16(1.0), stop=Float16(2.0), length=2))[1] === Float16(1.0)
218
219         let r = 5:-1:1
220             @test r[1]==5
221             @test r[2]==4
222             @test r[3]==3
223             @test r[4]==2
224             @test r[5]==1
225         end
226         @test @inferred((0.1:0.1:0.3)[2]) === 0.2
227         @test @inferred((0.1f0:0.1f0:0.3f0)[2]) === 0.2f0
228
229         @test @inferred((1:5)[1:4]) === 1:4
230         @test @inferred((1.0:5)[1:4]) === 1.0:4
231         @test (2:6)[1:4] == 2:5
232         @test (1:6)[2:5] === 2:5
233         @test (1:6)[2:2:5] === 2:2:4
234         @test (1:2:13)[2:6] === 3:2:11
235         @test (1:2:13)[2:3:7] === 3:6:13
236
237         @test isempty((1:4)[5:4])
238         @test_throws BoundsError (1:10)[8:-1:-2]
239
240         let r = typemax(Int)-5:typemax(Int)-1
241             @test_throws BoundsError r[7]
242         end
243     end
244     @testset "length" begin
245         @test length(.1:.1:.3) == 3
246         @test length(1.1:1.1:3.3) == 3
247         @test length(1.1:1.3:3) == 2
248         @test length(1:1:1.8) == 1
249         @test length(1:.2:2) == 6
250         @test length(1.:.2:2.) == 6
251         @test length(2:-.2:1) == 6
252         @test length(2.:-.2:1.) == 6
253         @test length(2:.2:1) == 0
254         @test length(2.:.2:1.) == 0
255
256         @test length(1:0) == 0
257         @test length(0.0:-0.5) == 0
258         @test length(1:2:0) == 0
259         @test length(Char(0):Char(0x001fffff)) == 2097152
260         @test length(typemax(UInt64)//one(UInt64):1:typemax(UInt64)//one(UInt64)) == 1
261     end
262     @testset "findall(::Base.Fix2{typeof(in)}, ::Array)" begin
263         @test findall(in(3:20), [5.2, 3.3]) == findall(in(Vector(3:20)), [5.2, 3.3])
264
265         let span = 5:20,
266             r = -7:3:42
267             @test findall(in(span), r) == 5:10
268             r = 15:-2:-38
269             @test findall(in(span), r) == 1:6
270         end
271     end
272     @testset "reverse" begin
273         @test reverse(reverse(1:10)) == 1:10
274         @test reverse(reverse(typemin(Int):typemax(Int))) == typemin(Int):typemax(Int)
275         @test reverse(reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)
276     end
277     @testset "intersect" begin
278         @test intersect(1:5, 2:3) == 2:3
279         @test intersect(-3:5, 2:8) == 2:5
280         @test intersect(-8:-3, -8:-3) == -8:-3
281         @test intersect(1:5, 5:13) == 5:5
282         @test isempty(intersect(-8:-3, -2:2))
283         @test isempty(intersect(-3:7, 2:1))
284         @test intersect(1:11, -2:3:15) == 1:3:10
285         @test intersect(1:11, -2:2:15) == 2:2:10
286         @test intersect(1:11, -2:1:15) == 1:11
287         @test intersect(1:11, 15:-1:-2) == 1:11
288         @test intersect(1:11, 15:-4:-2) == 3:4:11
289         @test intersect(-20:-5, -10:3:-2) == -10:3:-7
290         @test isempty(intersect(-5:5, -6:13:20))
291         @test isempty(intersect(1:11, 15:4:-2))
292         @test isempty(intersect(11:1, 15:-4:-2))
293         #@test intersect(-5:5, 1+0*(1:3)) == 1:1
294         #@test isempty(intersect(-5:5, 6+0*(1:3)))
295         @test intersect(-15:4:7, -10:-2) == -7:4:-3
296         @test intersect(13:-2:1, -2:8) == 7:-2:1
297         @test isempty(intersect(13:2:1, -2:8))
298         @test isempty(intersect(13:-2:1, 8:-2))
299         #@test intersect(5+0*(1:4), 2:8) == 5+0*(1:4)
300         #@test isempty(intersect(5+0*(1:4), -7:3))
301         @test intersect(0:3:24, 0:4:24) == 0:12:24
302         @test intersect(0:4:24, 0:3:24) == 0:12:24
303         @test intersect(0:3:24, 24:-4:0) == 0:12:24
304         @test intersect(24:-3:0, 0:4:24) == 24:-12:0
305         @test intersect(24:-3:0, 24:-4:0) == 24:-12:0
306         @test intersect(1:3:24, 0:4:24) == 4:12:16
307         @test intersect(0:6:24, 0:4:24) == 0:12:24
308         @test isempty(intersect(1:6:2400, 0:4:2400))
309         @test intersect(-51:5:100, -33:7:125) == -26:35:79
310         @test intersect(-51:5:100, -32:7:125) == -11:35:94
311         #@test intersect(0:6:24, 6+0*(0:4:24)) == 6:6:6
312         #@test intersect(12+0*(0:6:24), 0:4:24) == AbstractRange(12, 0, 5)
313         #@test isempty(intersect(6+0*(0:6:24), 0:4:24))
314         @test intersect(-10:3:24, -10:3:24) == -10:3:23
315         @test isempty(intersect(-11:3:24, -10:3:24))
316         @test intersect(typemin(Int):2:typemax(Int),1:10) == 2:2:10
317         @test intersect(1:10,typemin(Int):2:typemax(Int)) == 2:2:10
318
319         @test intersect(reverse(typemin(Int):2:typemax(Int)),typemin(Int):2:typemax(Int)) == reverse(typemin(Int):2:typemax(Int))
320         @test intersect(typemin(Int):2:typemax(Int),reverse(typemin(Int):2:typemax(Int))) == typemin(Int):2:typemax(Int)
321
322         @test intersect(UnitRange(1,2),3) == UnitRange(3,2)
323         @test intersect(UnitRange(1,2), UnitRange(1,5), UnitRange(3,7), UnitRange(4,6)) == UnitRange(4,3)
324
325         @test intersect(1:3, 2) === intersect(2, 1:3) === 2:2
326         @test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0]
327     end
328     @testset "sort/sort!/partialsort" begin
329         @test sort(UnitRange(1,2)) == UnitRange(1,2)
330         @test sort!(UnitRange(1,2)) == UnitRange(1,2)
331         @test sort(1:10, rev=true) == 10:-1:1
332         @test sort(-3:3, by=abs) == [0,-1,1,-2,2,-3,3]
333         @test partialsort(1:10, 4) == 4
334     end
335     @testset "in" begin
336         @test 0 in UInt(0):100:typemax(UInt)
337         @test last(UInt(0):100:typemax(UInt)) in UInt(0):100:typemax(UInt)
338         @test -9223372036854775790 in -9223372036854775790:100:9223372036854775710
339         @test -9223372036854775690 in -9223372036854775790:100:9223372036854775710
340         @test -90 in -9223372036854775790:100:9223372036854775710
341         @test 10 in -9223372036854775790:100:9223372036854775710
342         @test 110 in -9223372036854775790:100:9223372036854775710
343         @test 9223372036854775610 in -9223372036854775790:100:9223372036854775710
344         @test 9223372036854775710 in -9223372036854775790:100:9223372036854775710
345
346
347         @test !(3.5 in 1:5)
348         @test (3 in 1:5)
349         @test (3 in 5:-1:1)
350         #@test (3 in 3+0*(1:5))
351         #@test !(4 in 3+0*(1:5))
352
353         let r = 0.0:0.01:1.0
354             @test (r[30] in r)
355         end
356         let r = (-4*Int64(maxintfloat(Int === Int32 ? Float32 : Float64))):5
357             @test (3 in r)
358             @test (3.0 in r)
359         end
360
361         @test !(1 in 1:0)
362         @test !(1.0 in 1.0:0.0)
363     end
364     @testset "in() works across types, including non-numeric types (#21728)" begin
365         @test 1//1 in 1:3
366         @test 1//1 in 1.0:3.0
367         @test !(5//1 in 1:3)
368         @test !(5//1 in 1.0:3.0)
369         @test Complex(1, 0) in 1:3
370         @test Complex(1, 0) in 1.0:3.0
371         @test Complex(1.0, 0.0) in 1:3
372         @test Complex(1.0, 0.0) in 1.0:3.0
373         @test !(Complex(1, 1) in 1:3)
374         @test !(Complex(1, 1) in 1.0:3.0)
375         @test !(Complex(1.0, 1.0) in 1:3)
376         @test !(Complex(1.0, 1.0) in 1.0:3.0)
377         @test !(π in 1:3)
378         @test !(π in 1.0:3.0)
379         @test !("a" in 1:3)
380         @test !("a" in 1.0:3.0)
381         @test !(1 in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
382         @test !(Complex(1, 0) in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
383         @test !(π in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
384         @test !("a" in Date(2017, 01, 01):Dates.Day(1):Date(2017, 01, 05))
385     end
386 end
387 @testset "indexing range with empty range (#4309)" begin
388     @test (3:6)[5:4] == 7:6
389     @test_throws BoundsError (3:6)[5:5]
390     @test_throws BoundsError (3:6)[5]
391     @test (0:2:10)[7:6] == 12:2:10
392     @test_throws BoundsError (0:2:10)[7:7]
393 end
394 # indexing with negative ranges (#8351)
395 for a=AbstractRange[3:6, 0:2:10], b=AbstractRange[0:1, 2:-1:0]
396     @test_throws BoundsError a[b]
397 end
398
399 # avoiding intermediate overflow (#5065)
400 @test length(1:4:typemax(Int)) == div(typemax(Int),4) + 1
401
402 @testset "overflow in length" begin
403     @test_throws OverflowError length(0:typemax(Int))
404     @test_throws OverflowError length(typemin(Int):typemax(Int))
405     @test_throws OverflowError length(-1:typemax(Int)-1)
406 end
407 @testset "loops involving typemin/typemax" begin
408     n = 0
409     s = 0
410     # loops ending at typemax(Int)
411     for i = (typemax(Int)-1):typemax(Int)
412         s += 1
413         @test s <= 2
414     end
415     @test s == 2
416
417     s = 0
418     for i = (typemax(Int)-2):(typemax(Int)-1)
419         s += 1
420         @test s <= 2
421     end
422     @test s == 2
423
424     s = 0
425     for i = typemin(Int):(typemin(Int)+1)
426         s += 1
427         @test s <= 2
428     end
429     @test s == 2
430
431     # loops covering the full range of integers
432     s = 0
433     for i = typemin(UInt8):typemax(UInt8)
434         s += 1
435     end
436     @test s == 256
437
438     s = 0
439     for i = typemin(UInt):typemax(UInt)
440         i == 10 && break
441         s += 1
442     end
443     @test s == 10
444
445     s = 0
446     for i = typemin(UInt8):one(UInt8):typemax(UInt8)
447         s += 1
448     end
449     @test s == 256
450
451     s = 0
452     for i = typemin(UInt):1:typemax(UInt)
453         i == 10 && break
454         s += 1
455     end
456     @test s == 10
457
458     # loops past typemax(Int)
459     n = 0
460     s = Int128(0)
461     for i = typemax(UInt64)-2:typemax(UInt64)
462         n += 1
463         s += i
464     end
465     @test n == 3
466     @test s == 3*Int128(typemax(UInt64)) - 3
467
468     # loops over empty ranges
469     s = 0
470     for i = 0xff:0x00
471         s += 1
472     end
473     @test s == 0
474
475     s = 0
476     for i = Int128(typemax(Int128)):Int128(typemin(Int128))
477         s += 1
478     end
479     @test s == 0
480 end
481
482 @testset "sums of ranges" begin
483     @test sum(1:100) == 5050
484     @test sum(0:100) == 5050
485     @test sum(-100:100) == 0
486     @test sum(0:2:100) == 2550
487 end
488 @testset "overflowing sums (see #5798)" begin
489     if Sys.WORD_SIZE == 64
490         @test sum(Int128(1):10^18) == div(10^18 * (Int128(10^18)+1), 2)
491         @test sum(Int128(1):10^18-1) == div(10^18 * (Int128(10^18)-1), 2)
492     else
493         @test sum(Int64(1):10^9) == div(10^9 * (Int64(10^9)+1), 2)
494         @test sum(Int64(1):10^9-1) == div(10^9 * (Int64(10^9)-1), 2)
495     end
496 end
497 @testset "Tricky sums of StepRangeLen #8272" begin
498     @test sum(10000.:-0.0001:0) == 5.00000005e11
499     @test sum(0:0.001:1) == 500.5
500     @test sum(0:0.000001:1) == 500000.5
501     @test sum(0:0.1:10) == 505.
502 end
503 @testset "broadcasted operations with scalars" begin
504     @test broadcast(-, 1:3) === -1:-1:-3
505     @test broadcast(-, 1:3, 2) === -1:1
506     @test broadcast(-, 1:3, 0.25) === 1-0.25:3-0.25
507     @test broadcast(+, 1:3) === 1:3
508     @test broadcast(+, 1:3, 2) === 3:5
509     @test broadcast(+, 1:3, 0.25) === 1+0.25:3+0.25
510     @test broadcast(+, 1:2:6, 1) === 2:2:6
511     @test broadcast(+, 1:2:6, 0.3) === 1+0.3:2:5+0.3
512     @test broadcast(-, 1:2:6, 1) === 0:2:4
513     @test broadcast(-, 1:2:6, 0.3) === 1-0.3:2:5-0.3
514     @test broadcast(-, 2, 1:3) === 1:-1:-1
515 end
516 @testset "operations between ranges and arrays" begin
517     @test all(([1:5;] + (5:-1:1)) .== 6)
518     @test all(((5:-1:1) + [1:5;]) .== 6)
519     @test all(([1:5;] - (1:5)) .== 0)
520     @test all(((1:5) - [1:5;]) .== 0)
521 end
522 @testset "tricky floating-point ranges" begin
523     for (start, step, stop, len) in ((1, 1, 3, 3), (0, 1, 3, 4),
524                                     (3, -1, -1, 5), (1, -1, -3, 5),
525                                     (0, 1, 10, 11), (0, 7, 21, 4),
526                                     (0, 11, 33, 4), (1, 11, 34, 4),
527                                     (0, 13, 39, 4), (1, 13, 40, 4),
528                                     (11, 11, 33, 3), (3, 1, 11, 9),
529                                     (0, 10, 55, 0), (0, -1, 5, 0), (0, 10, 5, 0),
530                                     (0, 1, 5, 0), (0, -10, 5, 0), (0, -10, 0, 1),
531                                     (0, -1, 1, 0), (0, 1, -1, 0), (0, -1, -10, 11))
532         r = start/10:step/10:stop/10
533         a = Vector(start:step:stop)./10
534         ra = Vector(r)
535
536         @test r == a
537         @test isequal(r, a)
538
539         @test r == ra
540         @test isequal(r, ra)
541
542         @test hash(r) == hash(a)
543         @test hash(r) == hash(ra)
544
545         if len > 0
546             l = range(start/10, stop=stop/10, length=len)
547             la = Vector(l)
548
549             @test a == l
550             @test r == l
551             @test isequal(a, l)
552             @test isequal(r, l)
553
554             @test l == la
555             @test isequal(l, la)
556
557             @test hash(l) == hash(a)
558             @test hash(l) == hash(la)
559         end
560     end
561
562     @test 1.0:1/49:27.0 == range(1.0, stop=27.0, length=1275) == [49:1323;]./49
563     @test isequal(1.0:1/49:27.0, range(1.0, stop=27.0, length=1275))
564     @test isequal(1.0:1/49:27.0, Vector(49:1323)./49)
565     @test hash(1.0:1/49:27.0) == hash(range(1.0, stop=27.0, length=1275)) == hash(Vector(49:1323)./49)
566
567     @test [prevfloat(0.1):0.1:0.3;] == [prevfloat(0.1), 0.2, 0.3]
568     @test [nextfloat(0.1):0.1:0.3;] == [nextfloat(0.1), 0.2]
569     @test [prevfloat(0.0):0.1:0.3;] == [prevfloat(0.0), 0.1, 0.2]
570     @test [nextfloat(0.0):0.1:0.3;] == [nextfloat(0.0), 0.1, 0.2]
571     @test [0.1:0.1:prevfloat(0.3);] == [0.1, 0.2]
572     @test [0.1:0.1:nextfloat(0.3);] == [0.1, 0.2, nextfloat(0.3)]
573     @test [0.0:0.1:prevfloat(0.3);] == [0.0, 0.1, 0.2]
574     @test [0.0:0.1:nextfloat(0.3);] == [0.0, 0.1, 0.2, nextfloat(0.3)]
575     @test [0.1:prevfloat(0.1):0.3;] == [0.1, 0.2, 0.3]
576     @test [0.1:nextfloat(0.1):0.3;] == [0.1, 0.2]
577     @test [0.0:prevfloat(0.1):0.3;] == [0.0, prevfloat(0.1), prevfloat(0.2), 0.3]
578     @test [0.0:nextfloat(0.1):0.3;] == [0.0, nextfloat(0.1), nextfloat(0.2)]
579 end
580
581 function loop_range_values(::Type{T}) where T
582     for a = -5:25,
583         s = [-5:-1; 1:25; ],
584         d = 1:25,
585         n = -1:15
586
587         denom = convert(T, d)
588         strt = convert(T, a)/denom
589         Δ     = convert(T, s)/denom
590         stop  = convert(T, (a + (n - 1) * s)) / denom
591         vals  = T[a:s:(a + (n - 1) * s); ] ./ denom
592         r = strt:Δ:stop
593         @test [r;] == vals
594         @test [range(strt, stop=stop, length=length(r));] == vals
595         n = length(r)
596         @test [r[1:n];] == [r;]
597         @test [r[2:n];] == [r;][2:end]
598         @test [r[1:3:n];] == [r;][1:3:n]
599         @test [r[2:2:n];] == [r;][2:2:n]
600         @test [r[n:-1:2];] == [r;][n:-1:2]
601         @test [r[n:-2:1];] == [r;][n:-2:1]
602     end
603 end
604
605 @testset "issue #7420 for type \$T" for T = (Float32, Float64,) # BigFloat),
606     loop_range_values(T)
607 end
608
609 @testset "issue #20373 (unliftable ranges with exact end points)" begin
610     @test [3*0.05:0.05:0.2;]    == [range(3*0.05, stop=0.2, length=2);]   == [3*0.05,0.2]
611     @test [0.2:-0.05:3*0.05;]   == [range(0.2, stop=3*0.05, length=2);]   == [0.2,3*0.05]
612     @test [-3*0.05:-0.05:-0.2;] == [range(-3*0.05, stop=-0.2, length=2);] == [-3*0.05,-0.2]
613     @test [-0.2:0.05:-3*0.05;]  == [range(-0.2, stop=-3*0.05, length=2);] == [-0.2,-3*0.05]
614 end
615
616 function range_fuzztests(::Type{T}, niter, nrange) where {T}
617     for i = 1:niter, n in nrange
618         strt, Δ = randn(T), randn(T)
619         Δ == 0 && continue
620         stop = strt + (n-1)*Δ
621         # `n` is not necessarily unique s.t. `strt + (n-1)*Δ == stop`
622         # so test that `length(strt:Δ:stop)` satisfies this identity
623         # and is the closest value to `(stop-strt)/Δ` to do so
624         lo = hi = n
625         while strt + (lo-1)*Δ == stop; lo -= 1; end
626         while strt + (hi-1)*Δ == stop; hi += 1; end
627         m = clamp(round(Int, (stop-strt)/Δ) + 1, lo+1, hi-1)
628         r = strt:Δ:stop
629         @test m == length(r)
630         @test strt == first(r)
631         @test Δ == step(r)
632         @test_skip stop == last(r)
633         l = range(strt, stop=stop, length=n)
634         @test n == length(l)
635         @test strt == first(l)
636         @test stop  == last(l)
637     end
638 end
639 @testset "range fuzztests for \$T" for T = (Float32, Float64,)
640     range_fuzztests(T, 2^15, 1:5)
641 end
642
643 @testset "Inexact errors on 32 bit architectures. #22613" begin
644     @test first(range(log(0.2), stop=log(10.0), length=10)) == log(0.2)
645     @test last(range(log(0.2), stop=log(10.0), length=10)) == log(10.0)
646     @test length(Base.floatrange(-3e9, 1.0, 1, 1.0)) == 1
647 end
648
649 @testset "ranges with very small endpoints for type \$T" for T = (Float32, Float64)
650     z = zero(T)
651     u = eps(z)
652     @test first(range(u, stop=u, length=0)) == u
653     @test last(range(u, stop=u, length=0)) == u
654     @test first(range(-u, stop=u, length=0)) == -u
655     @test last(range(-u, stop=u, length=0)) == u
656     @test [range(-u, stop=u, length=0);] == []
657     @test [range(-u, stop=-u, length=1);] == [-u]
658     @test [range(-u, stop=u, length=2);] == [-u,u]
659     @test [range(-u, stop=u, length=3);] == [-u,0,u]
660     @test first(range(-u, stop=-u, length=0)) == -u
661     @test last(range(-u, stop=-u, length=0)) == -u
662     @test first(range(u, stop=-u, length=0)) == u
663     @test last(range(u, stop=-u, length=0)) == -u
664     @test [range(u, stop=-u, length=0);] == []
665     @test [range(u, stop=u, length=1);] == [u]
666     @test [range(u, stop=-u, length=2);] == [u,-u]
667     @test [range(u, stop=-u, length=3);] == [u,0,-u]
668     v = range(-u, stop=u, length=12)
669     @test length(v) == 12
670     @test [-3u:u:3u;] == [range(-3u, stop=3u, length=7);] == [-3:3;].*u
671     @test [3u:-u:-3u;] == [range(3u, stop=-3u, length=7);] == [3:-1:-3;].*u
672 end
673
674 @testset "range with very large endpoints for type \$T" for T = (Float32, Float64)
675     largeint = Int(min(maxintfloat(T), typemax(Int)))
676     a = floatmax()
677     for i = 1:5
678         @test [range(a, stop=a, length=1);] == [a]
679         @test [range(-a, stop=-a, length=1);] == [-a]
680         b = floatmax()
681         for j = 1:5
682             @test [range(-a, stop=b, length=0);] == []
683             @test [range(-a, stop=b, length=2);] == [-a,b]
684             @test [range(-a, stop=b, length=3);] == [-a,(b-a)/2,b]
685             @test [range(a, stop=-b, length=0);] == []
686             @test [range(a, stop=-b, length=2);] == [a,-b]
687             @test [range(a, stop=-b, length=3);] == [a,(a-b)/2,-b]
688             for c = largeint-3:largeint
689                 s = range(-a, stop=b, length=c)
690                 @test first(s) == -a
691                 @test last(s) == b
692                 @test length(s) == c
693                 s = range(a, stop=-b, length=c)
694                 @test first(s) == a
695                 @test last(s) == -b
696                 @test length(s) == c
697             end
698             b = prevfloat(b)
699         end
700         a = prevfloat(a)
701     end
702 end
703
704 # issue #20380
705 let r = LinRange(1,4,4)
706     @test isa(r[1:4], LinRange)
707 end
708
709 @testset "range with 1 or 0 elements (whose step length is NaN)" begin
710     @test issorted(range(1, stop=1, length=0))
711     @test issorted(range(1, stop=1, length=1))
712 end
713 # near-equal ranges
714 @test 0.0:0.1:1.0 != 0.0f0:0.1f0:1.0f0
715
716 # comparing and hashing ranges
717 @testset "comparing and hashing ranges" begin
718     Rs = AbstractRange[1:1, 1:1:1, 1:2, 1:1:2,
719                        map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0,
720                        0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)),
721                        1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994,
722                        range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20))]
723     for r in Rs
724         local r
725         ar = Vector(r)
726         @test r == ar
727         @test isequal(r,ar)
728         @test hash(r) == hash(ar)
729         for s in Rs
730             as = Vector(s)
731             @test isequal(r,s) == (hash(r)==hash(s))
732             @test (r==s) == (ar==as)
733         end
734     end
735 end
736
737 @testset "comparing UnitRanges and OneTo" begin
738     @test 1:2:10 == 1:2:10 != 1:3:10 != 1:3:13 != 2:3:13 == 2:3:11 != 2:11
739     @test 1:1:10 == 1:10 == 1:10 == Base.OneTo(10) == Base.OneTo(10)
740     @test 1:10 != 2:10 != 2:11 != Base.OneTo(11)
741     @test Base.OneTo(10) != Base.OneTo(11) != 1:10
742 end
743 # issue #2959
744 @test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0
745 #@test 1.0:(.3-.1)/.1 == 1.0:2.0
746
747 @testset "length with typemin/typemax" begin
748     let r = typemin(Int64):2:typemax(Int64), s = typemax(Int64):-2:typemin(Int64)
749         @test first(r) == typemin(Int64)
750         @test last(r) == (typemax(Int64)-1)
751         @test_throws OverflowError length(r)
752
753         @test first(s) == typemax(Int64)
754         @test last(s) == (typemin(Int64)+1)
755         @test_throws OverflowError length(s)
756     end
757
758     @test length(typemin(Int64):3:typemax(Int64)) == 6148914691236517206
759     @test length(typemax(Int64):-3:typemin(Int64)) == 6148914691236517206
760
761     for s in 3:100
762         @test length(typemin(Int):s:typemax(Int)) == length(big(typemin(Int)):big(s):big(typemax(Int)))
763         @test length(typemax(Int):-s:typemin(Int)) == length(big(typemax(Int)):big(-s):big(typemin(Int)))
764     end
765
766     @test length(UInt(1):UInt(1):UInt(0)) == 0
767     @test length(typemax(UInt):UInt(1):(typemax(UInt)-1)) == 0
768     @test length(typemax(UInt):UInt(2):(typemax(UInt)-1)) == 0
769     @test length((typemin(Int)+3):5:(typemin(Int)+1)) == 0
770 end
771 # issue #6364
772 @test length((1:64)*(pi/5)) == 64
773
774 @testset "issue #6973" begin
775     r1 = 1.0:0.1:2.0
776     r2 = 1.0f0:0.2f0:3.0f0
777     r3 = 1:2:21
778     @test r1 + r1 == 2*r1
779     @test r1 + r2 == 2.0:0.3:5.0
780     @test (r1 + r2) - r2 == r1
781     @test r1 + r3 == convert(StepRangeLen{Float64}, r3) + r1
782     @test r3 + r3 == 2 * r3
783 end
784
785 @testset "issue #7114" begin
786     let r = -0.004532318104333742:1.2597349521122731e-5:0.008065031416788989
787         @test length(r[1:end-1]) == length(r) - 1
788         @test isa(r[1:2:end],AbstractRange) && length(r[1:2:end]) == div(length(r)+1, 2)
789         @test r[3:5][2] ≈ r[4]
790         @test r[5:-2:1][2] ≈ r[3]
791         @test_throws BoundsError r[0:10]
792         @test_throws BoundsError r[1:10000]
793     end
794
795     let r = range(1/3, stop=5/7, length=6)
796         @test length(r) == 6
797         @test r[1] == 1/3
798         @test abs(r[end] - 5/7) <= eps(5/7)
799     end
800
801     let r = range(0.25, stop=0.25, length=1)
802         @test length(r) == 1
803         @test_throws ArgumentError range(0.25, stop=0.5, length=1)
804     end
805 end
806
807 # issue #7426
808 @test [typemax(Int):1:typemax(Int);] == [typemax(Int)]
809
810 #issue #7484
811 let r7484 = 0.1:0.1:1
812     @test [reverse(r7484);] == reverse([r7484;])
813 end
814
815 @testset "issue #7387" begin
816     for r in (0:1, 0.0:1.0)
817         local r
818         @test [r .+ im;] == [r;] .+ im
819         @test [r .- im;] == [r;] .- im
820         @test [r * im;] == [r;] * im
821         @test [r / im;] == [r;] / im
822     end
823 end
824 # Preservation of high precision upon addition
825 let r = (-0.1:0.1:0.3) + broadcast(+, -0.3:0.1:0.1, 1e-12)
826     @test r[3] == 1e-12
827 end
828
829 @testset "issue #7709" begin
830     @test length(map(identity, 0x01:0x05)) == 5
831     @test length(map(identity, 0x0001:0x0005)) == 5
832     @test length(map(identity, UInt64(1):UInt64(5))) == 5
833     @test length(map(identity, UInt128(1):UInt128(5))) == 5
834 end
835 @testset "issue #8531" begin
836     smallint = (Int === Int64 ?
837                 (Int8,UInt8,Int16,UInt16,Int32,UInt32) :
838                 (Int8,UInt8,Int16,UInt16))
839     for T in smallint
840         @test length(typemin(T):typemax(T)) == 2^(8*sizeof(T))
841     end
842 end
843
844 # issue #8584
845 @test (0:1//2:2)[1:2:3] == 0:1//1:1
846
847 # issue #12278
848 @test length(1:UInt(0)) == 0
849
850 @testset "zip" begin
851     i = 0
852     x = 1:2:8
853     y = 2:2:8
854     xy = 1:8
855     for (thisx, thisy) in zip(x, y)
856         @test thisx == xy[i+=1]
857         @test thisy == xy[i+=1]
858     end
859 end
860
861 @testset "issue #9962" begin
862     @test eltype(0:1//3:10) <: Rational
863     @test (0:1//3:10)[1] == 0
864     @test (0:1//3:10)[2] == 1//3
865 end
866 @testset "converting ranges (issue #10965)" begin
867     @test promote(0:1, UInt8(2):UInt8(5)) === (0:1, 2:5)
868     @test convert(UnitRange{Int}, 0:5) === 0:5
869     @test convert(UnitRange{Int128}, 0:5) === Int128(0):Int128(5)
870
871     @test promote(0:1:1, UInt8(2):UInt8(1):UInt8(5)) === (0:1:1, 2:1:5)
872     @test convert(StepRange{Int,Int}, 0:1:1) === 0:1:1
873     @test convert(StepRange{Int128,Int128}, 0:1:1) === Int128(0):Int128(1):Int128(1)
874
875     @test promote(0:1:1, 2:5) === (0:1:1, 2:1:5)
876     @test convert(StepRange{Int128,Int128}, 0:5) === Int128(0):Int128(1):Int128(5)
877     @test convert(StepRange, 0:5) === 0:1:5
878     @test convert(StepRange{Int128,Int128}, 0.:5) === Int128(0):Int128(1):Int128(5)
879
880     @test_throws ArgumentError StepRange(1.1,1,5.1)
881
882     @test promote(0f0:inv(3f0):1f0, 0.:2.:5.) === (0:1/3:1, 0.:2.:5.)
883
884     @test convert(StepRangeLen{Float64}, 0:1/3:1) === 0:1/3:1
885     @test convert(StepRangeLen{Float64}, 0f0:inv(3f0):1f0) === 0:1/3:1
886
887     @test promote(0:1/3:1, 0:5) === (0:1/3:1, 0.:1.:5.)
888     @test convert(StepRangeLen{Float64}, 0:5) === 0.:1.:5.
889     @test convert(StepRangeLen{Float64}, 0:1:5) === 0.:1.:5.
890     @test convert(StepRangeLen, 0:5) == 0:5
891     @test convert(StepRangeLen, 0:1:5) == 0:1:5
892
893     @test convert(LinRange{Float64}, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4)
894     @test convert(LinRange, 0.0:0.1:0.3) === LinRange{Float64}(0.0, 0.3, 4)
895     @test convert(LinRange, 0:3) === LinRange{Int}(0, 3, 4)
896
897     @test promote('a':'z', 1:2) === ('a':'z', 1:1:2)
898     @test eltype(['a':'z', 1:2]) == (StepRange{T,Int} where T)
899 end
900
901 @testset "LinRange ops" begin
902     @test 2*LinRange(0,3,4) == LinRange(0,6,4)
903     @test LinRange(0,3,4)*2 == LinRange(0,6,4)
904     @test LinRange(0,3,4)/3 == LinRange(0,1,4)
905     @test broadcast(-, 2, LinRange(0,3,4)) == LinRange(2,-1,4)
906     @test broadcast(+, 2, LinRange(0,3,4)) == LinRange(2,5,4)
907     @test -LinRange(0,3,4) == LinRange(0,-3,4)
908     @test reverse(LinRange(0,3,4)) == LinRange(3,0,4)
909 end
910 @testset "Issue #11245" begin
911     io = IOBuffer()
912     show(io, range(1, stop=2, length=3))
913     str = String(take!(io))
914 #    @test str == "range(1.0, stop=2.0, length=3)"
915     @test str == "1.0:0.5:2.0"
916 end
917
918 @testset "issue 10950" begin
919     r = 1//2:3
920     @test length(r) == 3
921     i = 1
922     for x in r
923         @test x == i//2
924         i += 2
925     end
926     @test i == 7
927 end
928
929 @testset "repr" begin
930     # repr/show should display the range nicely
931     # to test print_range in range.jl
932     replrepr(x) = repr("text/plain", x; context=IOContext(stdout, :limit=>true, :displaysize=>(24, 80)))
933     @test replrepr(1:4) == "1:4"
934     @test repr("text/plain", 1:4) == "1:4"
935     @test repr("text/plain", range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0"
936     @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"
937     @test repr(range(1, stop=5, length=7)) == "1.0:0.6666666666666666:5.0"
938     @test repr(LinRange{Float64}(1,5,7)) == "range(1.0, stop=5.0, length=7)"
939     @test replrepr(0:100.) == "0.0:1.0:100.0"
940     # next is to test a very large range, which should be fast because print_range
941     # only examines spacing of the left and right edges of the range, sufficient
942     # to cover the designated screen size.
943     @test replrepr(range(0, stop=100, length=10000)) == "0.0:0.010001000100010001:100.0"
944     @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"
945
946     @test sprint(show, UnitRange(1, 2)) == "1:2"
947     @test sprint(show, StepRange(1, 2, 5)) == "1:2:5"
948 end
949
950 @testset "Issue 11049 and related" begin
951     @test promote(range(0f0, stop=1f0, length=3), range(0., stop=5., length=2)) ===
952         (range(0., stop=1., length=3), range(0., stop=5., length=2))
953     @test convert(LinRange{Float64}, range(0., stop=1., length=3)) === LinRange(0., 1., 3)
954     @test convert(LinRange{Float64}, range(0f0, stop=1f0, length=3)) === LinRange(0., 1., 3)
955
956     @test promote(range(0., stop=1., length=3), 0:5) === (range(0., stop=1., length=3),
957                                                  range(0., stop=5., length=6))
958     @test convert(LinRange{Float64}, 0:5) === LinRange(0., 5., 6)
959     @test convert(LinRange{Float64}, 0:1:5) === LinRange(0., 5., 6)
960     @test convert(LinRange, 0:5) === LinRange{Int}(0, 5, 6)
961     @test convert(LinRange, 0:1:5) === LinRange{Int}(0, 5, 6)
962
963     function test_range_index(r, s)
964         @test typeof(r[s]) == typeof(r)
965         @test [r;][s] == [r[s];]
966     end
967     test_range_index(range(0.1, stop=0.3, length=3), 1:2)
968     test_range_index(range(0.1, stop=0.3, length=3), 1:0)
969     test_range_index(range(1.0, stop=1.0, length=1), 1:1)
970     test_range_index(range(1.0, stop=1.0, length=1), 1:0)
971     test_range_index(range(1.0, stop=2.0, length=0), 1:0)
972
973     function test_range_identity(r::AbstractRange{T}, mr) where T
974         @test -r == mr
975         @test -Vector(r) == Vector(mr)
976         @test isa(-r, typeof(r))
977
979         @test 1 .+ Vector(r) == Vector(1 .+ r) == Vector(r .+ 1)
982         @test 1 .- Vector(r) == Vector(1 .- r) == Vector(1 .+ mr)
983         @test Vector(r) .- 1 == Vector(r .- 1) == -Vector(mr .+ 1)
985
986         @test 1 * r * 1 == r
987         @test 2 * r * T(0.5) == r
988         @test isa(1 * r * 1, typeof(r))
989         @test r / 1 == r
990         @test r / 2 * 2 == r
991         @test r / T(0.5) * T(0.5) == r
992         @test isa(r / 1, typeof(r))
993
994         @test (2 * Vector(r) == Vector(r * 2) == Vector(2 * r) ==
995                Vector(r * T(2.0)) == Vector(T(2.0) * r) ==
996                Vector(r / T(0.5)) == -Vector(mr * T(2.0)))
997     end
998
999     test_range_identity(range(1.0, stop=27.0, length=10), range(-1.0, stop=-27.0, length=10))
1000     test_range_identity(range(1f0, stop=27f0, length=10), range(-1f0, stop=-27f0, length=10))
1001
1002     test_range_identity(range(1.0, stop=27.0, length=0), range(-1.0, stop=-27.0, length=0))
1003     test_range_identity(range(1f0, stop=27f0, length=0), range(-1f0, stop=-27f0, length=0))
1004
1005     test_range_identity(range(1.0, stop=1.0, length=1), range(-1.0, stop=-1.0, length=1))
1006     test_range_identity(range(1f0, stop=1f0, length=1), range(-1f0, stop=-1f0, length=1))
1007
1008     @test reverse(range(1.0, stop=27.0, length=1275)) == range(27.0, stop=1.0, length=1275)
1009     @test [reverse(range(1.0, stop=27.0, length=1275));] ==
1010         reverse([range(1.0, stop=27.0, length=1275);])
1011 end
1012 @testset "PR 12200 and related" begin
1013     for _r in (1:2:100, 1:100, 1f0:2f0:100f0, 1.0:2.0:100.0,
1014                range(1, stop=100, length=10), range(1f0, stop=100f0, length=10))
1015         float_r = float(_r)
1017         big_rdot = big.(_r)
1018         @test big_rdot == big_r
1019         @test typeof(big_r) == typeof(big_rdot)
1020         @test typeof(big_r).name === typeof(_r).name
1021         if eltype(_r) <: AbstractFloat
1022             @test isa(float_r, typeof(_r))
1023             @test eltype(big_r) === BigFloat
1024         else
1025             @test isa(float_r, AbstractRange)
1026             @test eltype(float_r) <: AbstractFloat
1027             @test eltype(big_r) === BigInt
1028         end
1029     end
1030
1031     @test_throws DimensionMismatch range(1., stop=5., length=5) + range(1., stop=5., length=6)
1032     @test_throws DimensionMismatch range(1., stop=5., length=5) - range(1., stop=5., length=6)
1033     @test_throws DimensionMismatch range(1., stop=5., length=5) .* range(1., stop=5., length=6)
1034     @test_throws DimensionMismatch range(1., stop=5., length=5) ./ range(1., stop=5., length=6)
1035
1036     @test_throws DimensionMismatch (1:5) + (1:6)
1037     @test_throws DimensionMismatch (1:5) - (1:6)
1038     @test_throws DimensionMismatch (1:5) .* (1:6)
1039     @test_throws DimensionMismatch (1:5) ./ (1:6)
1040
1041     @test_throws DimensionMismatch (1.:5.) + (1.:6.)
1042     @test_throws DimensionMismatch (1.:5.) - (1.:6.)
1043     @test_throws DimensionMismatch (1.:5.) .* (1.:6.)
1044     @test_throws DimensionMismatch (1.:5.) ./ (1.:6.)
1045
1046     function test_range_sum_diff(r1, r2, r_sum, r_diff)
1047         @test r1 + r2 == r_sum
1048         @test r2 + r1 == r_sum
1049         @test r1 - r2 == r_diff
1050         @test r2 - r1 == -r_diff
1051
1052         @test Vector(r1) + Vector(r2) == Vector(r_sum)
1053         @test Vector(r2) + Vector(r1) == Vector(r_sum)
1054         @test Vector(r1) - Vector(r2) == Vector(r_diff)
1055         @test Vector(r2) - Vector(r1) == Vector(-r_diff)
1056     end
1057
1058     test_range_sum_diff(1:5, 0:2:8, 1:3:13, 1:-1:-3)
1059     test_range_sum_diff(1.:5., 0.:2.:8., 1.:3.:13., 1.:-1.:-3.)
1060     test_range_sum_diff(range(1., stop=5., length=5), range(0., stop=-4., length=5),
1061                         range(1., stop=1., length=5), range(1., stop=9., length=5))
1062
1063     test_range_sum_diff(1:5, 0.:2.:8., 1.:3.:13., 1.:-1.:-3.)
1064     test_range_sum_diff(1:5, range(0, stop=8, length=5),
1065                         range(1, stop=13, length=5), range(1, stop=-3, length=5))
1066     test_range_sum_diff(1.:5., range(0, stop=8, length=5),
1067                         range(1, stop=13, length=5), range(1, stop=-3, length=5))
1068 end
1069 # Issue #12388
1070 let r = 0x02:0x05
1071     @test r[2:3] == 0x03:0x04
1072 end
1073
1074 @testset "Issue #13738" begin
1075     for r in (big(1):big(2), UInt128(1):UInt128(2), 0x1:0x2)
1076         local r
1077         rr = r[r]
1078         @test typeof(rr) == typeof(r)
1079         @test r[r] == r
1080         # these calls to similar must not throw:
1081         @test size(similar(r, size(r))) == size(similar(r, length(r)))
1082     end
1083 end
1084 @testset "sign, conj, ~ (Issue #16067)" begin
1085     A = -1:1
1086     B = -1.0:1.0
1087     @test sign.(A) == [-1,0,1]
1088     @test sign.(B) == [-1,0,1]
1089     @test typeof(sign.(A)) === Vector{Int}
1090     @test typeof(sign.(B)) === Vector{Float64}
1091
1092     @test conj(A) === A
1093     @test conj(B) === B
1094
1095     @test .~A == [0,-1,-2]
1096     @test typeof(.~A) == Vector{Int}
1097 end
1098
1099 @testset "conversion to Array" begin
1100     r = 1:3
1101     a = [1,2,3]
1102     @test convert(Array, r) == a
1103     @test convert(Array{Int}, r) == a
1104     @test convert(Array{Float64}, r) == a
1105     @test convert(Array{Int,1}, r) == a
1106     @test convert(Array{Float64,1}, r) == a
1107 end
1108
1109 @testset "OneTo" begin
1110     let r = Base.OneTo(-5)
1111         @test isempty(r)
1112         @test length(r) == 0
1113         @test size(r) == (0,)
1114     end
1115     let r = Base.OneTo(3)
1116         @test !isempty(r)
1117         @test length(r) == 3
1118         @test size(r) == (3,)
1119         @test step(r) == 1
1120         @test first(r) == 1
1121         @test last(r) == 3
1122         @test minimum(r) == 1
1123         @test maximum(r) == 3
1124         @test r[2] == 2
1125         @test r[2:3] === 2:3
1126         @test_throws BoundsError r[4]
1127         @test_throws BoundsError r[0]
1128         @test broadcast(+, r, 1) === 2:4
1129         @test 2*r === 2:2:6
1130         @test r + r === 2:2:6
1131         k = 0
1132         for i in r
1133             @test i == (k += 1)
1134         end
1135         @test intersect(r, Base.OneTo(2)) == Base.OneTo(2)
1136         @test intersect(r, 0:5) == 1:3
1137         @test intersect(r, 2) === intersect(2, r) === 2:2
1138         @test findall(in(r), r) === findall(in(1:length(r)), r) ===
1139               findall(in(r), 1:length(r)) === 1:length(r)
1140         io = IOBuffer()
1141         show(io, r)
1142         str = String(take!(io))
1143         @test str == "Base.OneTo(3)"
1144     end
1145     let r = Base.OneTo(7)
1146         @test findall(in(2:(length(r) - 1)), r) === 2:(length(r) - 1)
1147         @test findall(in(r), 2:(length(r) - 1)) === 1:(length(r) - 2)
1148     end
1149     @test convert(Base.OneTo, 1:2) === Base.OneTo{Int}(2)
1150     @test_throws ArgumentError("first element must be 1, got 2") convert(Base.OneTo, 2:3)
1151     @test_throws ArgumentError("step must be 1, got 2") convert(Base.OneTo, 1:2:5)
1152     @test Base.OneTo(1:2) === Base.OneTo{Int}(2)
1153     @test Base.OneTo(1:1:2) === Base.OneTo{Int}(2)
1154     @test Base.OneTo{Int32}(1:2) === Base.OneTo{Int32}(2)
1155     @test Base.OneTo(Int32(1):Int32(2)) === Base.OneTo{Int32}(2)
1156     @test Base.OneTo{Int16}(3.0) === Base.OneTo{Int16}(3)
1157     @test_throws InexactError(:Int16, Int16, 3.2) Base.OneTo{Int16}(3.2)
1158 end
1159
1160 @testset "range of other types" begin
1161     let r = range(0, stop=3//10, length=4)
1162         @test eltype(r) == Rational{Int}
1163         @test r[2] === 1//10
1164     end
1165
1166     let a = 1.0,
1167         b = nextfloat(1.0),
1168         ba = BigFloat(a),
1169         bb = BigFloat(b),
1170         r = range(ba, stop=bb, length=3)
1171         @test eltype(r) == BigFloat
1172         @test r[1] == a && r[3] == b
1173         @test r[2] == (ba+bb)/2
1174     end
1175
1176     let (a, b) = (rand(10), rand(10)),
1177         r = range(a, stop=b, length=5)
1178         @test r[1] == a && r[5] == b
1179         for i = 2:4
1180             x = ((5 - i) // 4) * a + ((i - 1) // 4) * b
1181             @test r[i] == x
1182         end
1183     end
1184 end
1185 @testset "issue #23178" begin
1186     r = range(Float16(0.1094), stop=Float16(0.9697), length=300)
1187     @test r[1] == Float16(0.1094)
1188     @test r[end] == Float16(0.9697)
1189 end
1190
1191 # issue #20382
1192 let r = @inferred((:)(big(1.0),big(2.0),big(5.0)))
1193     @test eltype(r) == BigFloat
1194 end
1195
1196 @testset "issue #14420" begin
1197     for r in (range(0.10000000000000045, stop=1, length=50), 0.10000000000000045:(1-0.10000000000000045)/49:1)
1198         local r
1199         @test r[1] === 0.10000000000000045
1200         @test r[end] === 1.0
1201     end
1202 end
1203 @testset "issue #20381" begin
1204     r = range(-big(1.0), stop=big(1.0), length=4)
1205     @test isa(@inferred(r[2]), BigFloat)
1206     @test r[2] ≈ big(-1.0)/3
1207 end
1208
1209 @testset "issue #20520" begin
1210     r = range(1.3173739f0, stop=1.3173739f0, length=3)
1211     @test length(r) == 3
1212     @test first(r) === 1.3173739f0
1213     @test last(r)  === 1.3173739f0
1214     @test r[2]     === 1.3173739f0
1215     r = range(1.0, stop=3+im, length=4)
1216     @test r[1] === 1.0+0.0im
1217     @test r[2] ≈ (5/3)+(1/3)im
1218     @test r[3] ≈ (7/3)+(2/3)im
1219     @test r[4] === 3.0+im
1220 end
1221
1222 # ambiguity between (:) methods (#20988)
1223 struct NotReal; val; end
1224 Base.:+(x, y::NotReal) = x + y.val
1225 Base.zero(y::NotReal) = zero(y.val)
1226 Base.rem(x, y::NotReal) = rem(x, y.val)
1227 Base.isless(x, y::NotReal) = isless(x, y.val)
1228 @test (:)(1, NotReal(1), 5) isa StepRange{Int,NotReal}
1229
1230 isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
1231 using .Main.Furlongs
1232
1233 @testset "dimensional correctness" begin
1234     @test length(Vector(Furlong(2):Furlong(10))) == 9
1235     @test length(range(Furlong(2), length=9)) == 9
1236     @test Vector(Furlong(2):Furlong(1):Furlong(10)) == Vector(range(Furlong(2), step=Furlong(1), length=9)) == Furlong.(2:10)
1237     @test Vector(Furlong(1.0):Furlong(0.5):Furlong(10.0)) ==
1238           Vector(Furlong(1):Furlong(0.5):Furlong(10)) == Furlong.(1:0.5:10)
1239 end
1240
1241 @testset "issue #22270" begin
1242     linsp = range(1.0, stop=2.0, length=10)
1243     @test typeof(linsp.ref) == Base.TwicePrecision{Float64}
1244     @test Float32(linsp.ref) === convert(Float32, linsp.ref)
1245     @test Float32(linsp.ref) ≈ linsp.ref.hi + linsp.ref.lo
1246 end
1247
1248 @testset "issue #23300" begin
1249     x = -5:big(1.0):5
1250     @test map(Float64, x) === -5.0:1.0:5.0
1251     @test map(Float32, x) === -5.0f0:1.0f0:5.0f0
1252     @test map(Float16, x) === Float16(-5.0):Float16(1.0):Float16(5.0)
1253     @test map(BigFloat, x) === x
1254 end
1255
1256 @testset "broadcasting returns ranges" begin
1257     x, r = 2, 1:5
1258     @test @inferred(x .+ r) === 3:7
1259     @test @inferred(r .+ x) === 3:7
1260     @test @inferred(r .- x) === -1:3
1261     @test @inferred(x .- r) === 1:-1:-3
1262     @test @inferred(x .* r) === 2:2:10
1263     @test @inferred(r .* x) === 2:2:10
1264     @test @inferred(r ./ x) === 0.5:0.5:2.5
1265     @test @inferred(x ./ r) == 2 ./ [r;] && isa(x ./ r, Vector{Float64})
1266     @test @inferred(r .\ x) == 2 ./ [r;] && isa(x ./ r, Vector{Float64})
1267     @test @inferred(x .\ r) === 0.5:0.5:2.5
1268
1269     @test @inferred(2 .* (r .+ 1) .+ 2) === 6:2:14
1270 end
1271
1272 @testset "Bad range calls" begin
1273     @test_throws ArgumentError range(1)
1274     @test_throws ArgumentError range(nothing)
1275     @test_throws ArgumentError range(1, step=4)
1276     @test_throws ArgumentError range(nothing, length=2)
1277     @test_throws ArgumentError range(1.0, step=0.25, stop=2.0, length=5)
1278 end
1279
1280 @testset "issue #23300#issuecomment-371575548" begin
1281     for (start, stop) in ((-5, 5), (-5.0, 5), (-5, 5.0), (-5.0, 5.0))
1282         @test @inferred(range(big(start), stop=big(stop), length=11)) isa LinRange{BigFloat}
1283         @test Float64.(@inferred(range(big(start), stop=big(stop), length=11))) == range(start, stop=stop, length=11)
1284         @test Float64.(@inferred(map(exp, range(big(start), stop=big(stop), length=11)))) == map(exp, range(start, stop=stop, length=11))
1285     end
1286 end
1287
1288 @testset "Issue #26532" begin
1289     x = range(3, stop=3, length=5)
1290     @test step(x) == 0.0
1291     @test x isa StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
1292 end
1293
1294 @testset "Issue #26608" begin
1295     @test_throws BoundsError (Int8(-100):Int8(100))[400]
1296     @test_throws BoundsError (-100:100)[typemax(UInt)]
1297     @test_throws BoundsError (false:true)[3]
1298 end
1299
1300 module NonStandardIntegerRangeTest
1301
1302 using Test
1303
1304 struct Position <: Integer
1305     val::Int
1306 end
1307 Position(x::Position) = x # to resolve ambiguity with boot.jl:728
1308
1309 struct Displacement <: Integer
1310     val::Int
1311 end
1312 Displacement(x::Displacement) = x # to resolve ambiguity with boot.jl:728
1313
1314 Base.:-(x::Displacement) = Displacement(-x.val)
1315 Base.:-(x::Position, y::Position) = Displacement(x.val - y.val)
1316 Base.:-(x::Position, y::Displacement) = Position(x.val - y.val)
1317 Base.:-(x::Displacement, y::Displacement) = Displacement(x.val - y.val)
1318 Base.:+(x::Position, y::Displacement) = Position(x.val + y.val)
1319 Base.:+(x::Displacement, y::Displacement) = Displacement(x.val + y.val)
1320 Base.:(<=)(x::Position, y::Position) = x.val <= y.val
1321 Base.:(<)(x::Position, y::Position) = x.val < y.val
1322 Base.:(<)(x::Displacement, y::Displacement) = x.val < y.val
1323
1324 # for StepRange computation:
1325 Base.Unsigned(x::Displacement) = Unsigned(x.val)
1326 Base.rem(x::Displacement, y::Displacement) = Displacement(rem(x.val, y.val))
1327 Base.div(x::Displacement, y::Displacement) = Displacement(div(x.val, y.val))
1328
1329 # required for collect (summing lengths); alternatively, should unsafe_length return Int by default?
1330 Base.promote_rule(::Type{Displacement}, ::Type{Int}) = Int
1331 Base.convert(::Type{Int}, x::Displacement) = x.val
1332
1333 @testset "Ranges with nonstandard Integers" begin
1334     for (start, stop) in [(2, 4), (3, 3), (3, -2)]
1335         @test collect(Position(start) : Position(stop)) == Position.(start : stop)
1336     end
1337
1338     for start in [3, 0, -2]
1339         @test collect(Base.OneTo(Position(start))) == Position.(Base.OneTo(start))
1340     end
1341
1342     for step in [-3, -2, -1, 1, 2, 3]
1343         for start in [-1, 0, 2]
1344             for stop in [start, start - 1, start + 2 * step, start + 2 * step + 1]
1345                 r1 = StepRange(Position(start), Displacement(step), Position(stop))
1346                 @test collect(r1) == Position.(start : step : stop)
1347
1348                 r2 = Position(start) : Displacement(step) : Position(stop)
1349                 @test r1 === r2
1350             end
1351         end
1352     end
1353 end
1354
1355 end # module NonStandardIntegerRangeTest
1356
1357 @testset "Issue #26619" begin
1358     @test length(UInt(100) : -1 : 1) === UInt(100)
1359     @test collect(UInt(5) : -1 : 3) == [UInt(5), UInt(4), UInt(3)]
1360
1361     let r = UInt(5) : -2 : 2
1362         @test r.start === UInt(5)
1363         @test r.step === -2
1364         @test r.stop === UInt(3)
1365         @test collect(r) == [UInt(5), UInt(3)]
1366     end
1367
1368     for step in [-3, -2, -1, 1, 2, 3]
1369         for start in [0, 15]
1370             for stop in [0, 15]
1371                 @test collect(UInt(start) : step : UInt(stop)) == start : step : stop
1372             end
1373         end
1374     end
1375 end
1376
1377 @testset "constant-valued ranges (issues #10391 and #29052)" begin
1378     for r in ((1:4), (1:1:4), (1.0:4.0))
1379         if eltype(r) === Int
1380             @test_broken @inferred(0 * r) == [0.0, 0.0, 0.0, 0.0]
1381             @test_broken @inferred(0 .* r) == [0.0, 0.0, 0.0, 0.0]
1382             @test_broken @inferred(r + (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
1383             @test_broken @inferred(r .+ (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
1384         else
1385             @test @inferred(0 * r) == [0.0, 0.0, 0.0, 0.0]
1386             @test @inferred(0 .* r) == [0.0, 0.0, 0.0, 0.0]
1387             @test @inferred(r + (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
1388             @test @inferred(r .+ (4:-1:1)) == [5.0, 5.0, 5.0, 5.0]
1389         end
1390         @test @inferred(r .+ (4.0:-1:1)) == [5.0, 5.0, 5.0, 5.0]
1391         @test @inferred(0.0 * r) == [0.0, 0.0, 0.0, 0.0]
1392         @test @inferred(0.0 .* r) == [0.0, 0.0, 0.0, 0.0]
1393         @test @inferred(r / Inf) == [0.0, 0.0, 0.0, 0.0]
1394         @test @inferred(r ./ Inf) == [0.0, 0.0, 0.0, 0.0]
1395     end
1396
1397     @test_broken @inferred(range(0, step=0, length=4)) == [0, 0, 0, 0]
1398     @test @inferred(range(0, stop=0, length=4)) == [0, 0, 0, 0]
1399     @test @inferred(range(0.0, step=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1400     @test @inferred(range(0.0, stop=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1401     @test @inferred(range(0, step=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1402     @test @inferred(range(0.0, step=0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1403     @test @inferred(range(0, stop=0.0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1404     @test @inferred(range(0.0, stop=0, length=4)) == [0.0, 0.0, 0.0, 0.0]
1405
1406     z4 = 0.0 * (1:4)
1407     @test @inferred(z4 .+ (1:4)) === 1.0:1.0:4.0
1408     @test @inferred(z4 .+ z4) === z4
1409 end
1410
1411 @testset "getindex" begin
1412     @test getindex((typemax(UInt64)//one(UInt64):typemax(UInt64)//one(UInt64)), 1) == typemax(UInt64)//one(UInt64)
1413 end
1414
1415 @testset "Issue #30006" begin
1416     @test Base.Slice(Base.OneTo(5))[Int32(1)] == Int32(1)
1417     @test Base.Slice(Base.OneTo(3))[Int8(2)] == Int8(2)
1418     @test Base.Slice(1:10)[Int32(2)] == Int32(2)
1419     @test Base.Slice(1:10)[Int8(2)] == Int8(2)
1420 end
1421
1422 @testset "allocation of TwicePrecision call" begin
1423     0:286.493442:360
1424     0:286:360
1425     @test @allocated(0:286.493442:360) == 0
1426     @test @allocated(0:286:360) == 0
1427 end
1428
1429 @testset "range with start and stop" begin
1430     for starts in [-1, 0, 1, 10]
1431         for stops in [-2, 0, 2, 100]
1432             for lengths in [2, 10, 100]
1433                 if stops >= starts
1434                     @test range(starts, stops, length=lengths) == range(starts, stop=stops, length=lengths)
1435                 end
1436             end
1437             for steps in [0.01, 1, 2]
1438                 @test range(starts, stops, step=steps) == range(starts, stop=stops, step=steps)
1439             end
1440         end
1441     end
1442     # require a keyword arg
1443     @test_throws ArgumentError range(1, 100)
1444 end
1445
1446 @testset "Reverse empty ranges" begin
1447     @test reverse(1:0) === 0:-1:1
1448     @test reverse(Base.OneTo(0)) === 0:-1:1
1449     # Almost `1.0:-1.0:2.0`, only different is the step which is
1450     # `Base.TwicePrecision(-1.0, 0.0)`
1451     @test reverse(1.0:0.0) === StepRangeLen(Base.TwicePrecision(1.0, 0.0),
1452                                             Base.TwicePrecision(-1.0, -0.0), 0)
1453     @test reverse(reverse(1.0:0.0)) === 1.0:0.0
1454 end
```