From 07f9aa342e006b104cfd739a8e86cee31203b80b Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Tue, 30 Sep 2014 16:19:21 +0530 Subject: [PATCH] fix #8531 (invalid range length) --- base/range.jl | 4 ++-- test/ranges.jl | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index 6979e3575ffa2..caac244b703b2 100644 --- a/base/range.jl +++ b/base/range.jl @@ -207,10 +207,10 @@ let smallint = (Int === Int64 ? function length{T <: smallint}(r::StepRange{T}) isempty(r) && return int(0) - div(int(r.stop+r.step - r.start), int(r.step)) + div(int(r.stop)+int(r.step) - int(r.start), int(r.step)) end - length{T <: smallint}(r::UnitRange{T}) = int(r.stop - r.start + 1) + length{T <: smallint}(r::UnitRange{T}) = int(r.stop) - int(r.start) + 1 end first{T}(r::OrdinalRange{T}) = oftype(T, r.start) diff --git a/test/ranges.jl b/test/ranges.jl index f0d5bc281b23d..b72f30c458497 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -363,3 +363,12 @@ for f in (mean, median) @test_approx_eq f(2:0.1:n) f([2:0.1:n]) end end + +# issue #8531 +let 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