From e928eda27030c152ffd218fcaec277816facfb18 Mon Sep 17 00:00:00 2001 From: Stefan Karpinski Date: Sat, 28 Jun 2014 14:00:39 -0500 Subject: [PATCH] =?UTF-8?q?Fix=20#7420=20=E2=80=93=C2=A0indexing=20a=20Flo?= =?UTF-8?q?atRange=20with=20an=20OrdinalRange.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/range.jl | 5 +++-- test/ranges.jl | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/base/range.jl b/base/range.jl index d3a3bdb7462d4..f2985f78bce35 100644 --- a/base/range.jl +++ b/base/range.jl @@ -271,8 +271,9 @@ function getindex(r::StepRange, s::Range{Int}) end function getindex(r::FloatRange, s::OrdinalRange) - 0 < last(s) <= length(r) || throw(BoundsError()) - FloatRange(r[first(s)],step(r)*step(s),length(s),r.divisor) + isempty(s) || 1 <= first(s) <= length(r) && + 1 <= last(s) <= length(r) || throw(BoundsError()) + FloatRange(r.start + (first(s)-1)*r.step, step(s)*r.step, length(s), r.divisor) end function show(io::IO, r::Range) diff --git a/test/ranges.jl b/test/ranges.jl index bcb21d1f98c11..2c008221a3d57 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -232,7 +232,16 @@ for T = (Float32, Float64,),# BigFloat), start = convert(T,a)/den step = convert(T,s)/den stop = convert(T,(a+(n-1)*s))/den - @test [start:step:stop] == T[a:s:a+(n-1)*s]./den + r = start:step:stop + @test [r] == T[a:s:a+(n-1)*s]./den + # issue #7420 + 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 # near-equal ranges