Skip to content

Commit

Permalink
Merge pull request #8533 from rfourquet/range-length
Browse files Browse the repository at this point in the history
fix #8531 (invalid range length)
  • Loading branch information
timholy committed Sep 30, 2014
2 parents 46c7bbf + 07f9aa3 commit 43fcd82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 43fcd82

Please sign in to comment.