Skip to content

Commit

Permalink
linspace with 1 or 0 elements is sorted (#15245)
Browse files Browse the repository at this point in the history
* ranges with 1 or 0 elements are sorted regardless of step sign

* dates ranges of length <= 1 are sorted
  • Loading branch information
gustafsson authored and tkelman committed Oct 3, 2016
1 parent fe85d33 commit 5a2553f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ reverse(r::LinSpace) = LinSpace(r.stop, r.start, r.len, r.divisor)
## sorting ##

issorted(r::AbstractUnitRange) = true
issorted(r::Range) = step(r) >= zero(step(r))
issorted(r::Range) = length(r) <= 1 || step(r) >= zero(step(r))

sort(r::AbstractUnitRange) = r
sort!(r::AbstractUnitRange) = r
Expand Down
12 changes: 6 additions & 6 deletions test/dates/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ function test_all_combos()
@test length(reverse(dr)) == 0
@test first(reverse(dr)) > l1
@test last(reverse(dr)) <= l1
@test !issorted(dr)
@test sortperm(dr) == 0:-1:1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test !(l1 in dr)
@test !(l1 in dr)
@test !(l1-neg_step in dr)
Expand Down Expand Up @@ -106,7 +106,7 @@ function test_all_combos()
end
@test !isempty(reverse(dr))
@test length(reverse(dr)) == len
@test !issorted(dr)
@test issorted(dr) == (len <= 1)
@test l in dr
end
end
Expand Down Expand Up @@ -182,8 +182,8 @@ function test_all_combos()
@test length(reverse(dr)) == 0
@test first(reverse(dr)) > l1
@test last(reverse(dr)) <= l1
@test !issorted(dr)
@test sortperm(dr) == 0:-1:1
@test issorted(dr)
@test sortperm(dr) == 1:1:0
@test !(l1 in dr)
@test !(l1 in dr)
@test !(l1-neg_step in dr)
Expand Down Expand Up @@ -212,7 +212,7 @@ function test_all_combos()
@test !isempty(reverse(dr))
@test length(reverse(dr)) == len
@test last(reverse(dr)) >= l
@test !issorted(dr)
@test issorted(dr) == (len <= 1)
@test l in dr

end
Expand Down
4 changes: 4 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ for T = (Float32, Float64)
end
end

# linspace with 1 or 0 elements (whose step length is NaN)
@test issorted(linspace(1,1,0))
@test issorted(linspace(1,1,1))

# near-equal ranges
@test 0.0:0.1:1.0 != 0.0f0:0.1f0:1.0f0

Expand Down

0 comments on commit 5a2553f

Please sign in to comment.