From 6ee12e0b49b19aa636df3389d9dd062e200f7e1f Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Mon, 17 Jun 2019 19:00:07 +0200 Subject: [PATCH 1/7] Make empty ranges equal --- base/range.jl | 10 ++++++---- base/strings/util.jl | 8 ++++---- test/ranges.jl | 4 +++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/base/range.jl b/base/range.jl index 9fbc198d1c372..3c73ca1bbe07f 100644 --- a/base/range.jl +++ b/base/range.jl @@ -714,18 +714,20 @@ show(io::IO, r::UnitRange) = print(io, repr(first(r)), ':', repr(last(r))) show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")") ==(r::T, s::T) where {T<:AbstractRange} = - (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) + (isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))) ==(r::OrdinalRange, s::OrdinalRange) = - (first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s)) + (isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))) ==(r::T, s::T) where {T<:Union{StepRangeLen,LinRange}} = - (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) + (isempty(r) & isempty(s)) | ((first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s))) ==(r::Union{StepRange{T},StepRangeLen{T,T}}, s::Union{StepRange{T},StepRangeLen{T,T}}) where {T} = - (first(r) == first(s)) & (last(r) == last(s)) & (step(r) == step(s)) + (isempty(r) & isempty(s)) | ((first(r) == first(s)) & (step(r) == step(s)) & (last(r) == last(s))) function ==(r::AbstractRange, s::AbstractRange) lr = length(r) if lr != length(s) return false + elseif iszero(lr) + return true end yr, ys = iterate(r), iterate(s) while yr !== nothing diff --git a/base/strings/util.jl b/base/strings/util.jl index b97a76a24b3e8..ad2e8b54a72e6 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -324,8 +324,8 @@ end function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Array) i = 1 # firstindex(str) n = lastindex(str) - r = something(findfirst(splitter,str), 0) - if r != 0:-1 + r = findfirst(splitter,str) + if !isnothing(r) j, k = first(r), nextind(str,last(r)) while 0 < j <= n && length(strs) != limit-1 if i < k @@ -335,8 +335,8 @@ function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, i = k end (k <= j) && (k = nextind(str,j)) - r = something(findnext(splitter,str,k), 0) - r == 0:-1 && break + r = findnext(splitter,str,k) + isnothing(r) && break j, k = first(r), nextind(str,last(r)) end end diff --git a/test/ranges.jl b/test/ranges.jl index 38c73b1bef792..5ab735ffc782b 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -765,7 +765,8 @@ end map(Int32,1:3:17), map(Int64,1:3:17), 1:0, 1:-1:0, 17:-3:0, 0.0:0.1:1.0, map(Float32,0.0:0.1:1.0),map(Float32,LinRange(0.0, 1.0, 11)), 1.0:eps():1.0 .+ 10eps(), 9007199254740990.:1.0:9007199254740994, - range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20))] + range(0, stop=1, length=20), map(Float32, range(0, stop=1, length=20)), + 3:2, 5:-2:7, range(0.0, step=2.0, length=0), 3//2:3//2:0//1, LinRange(2,3,0)] for r in Rs local r ar = Vector(r) @@ -785,6 +786,7 @@ end @test 1:1:10 == 1:10 == 1:10 == Base.OneTo(10) == Base.OneTo(10) @test 1:10 != 2:10 != 2:11 != Base.OneTo(11) @test Base.OneTo(10) != Base.OneTo(11) != 1:10 + @test Base.OneTo(0) == 5:4 end # issue #2959 @test 1.0:1.5 == 1.0:1.0:1.5 == 1.0:1.0 From aa604605f5f0d9c496209190ea600c859933ac13 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Thu, 20 Jun 2019 16:57:36 +0200 Subject: [PATCH 2/7] Adapt tests to new range comparison behavior --- stdlib/Dates/test/ranges.jl | 8 +++---- stdlib/REPL/test/replcompletions.jl | 15 ++++++++----- test/core.jl | 2 +- test/ranges.jl | 8 +++---- test/regex.jl | 4 ++-- test/sorting.jl | 34 +++++++++++++++++------------ test/strings/search.jl | 6 ++--- 7 files changed, 44 insertions(+), 33 deletions(-) diff --git a/stdlib/Dates/test/ranges.jl b/stdlib/Dates/test/ranges.jl index 473940d1ce98b..b21ae01195386 100644 --- a/stdlib/Dates/test/ranges.jl +++ b/stdlib/Dates/test/ranges.jl @@ -35,7 +35,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) == 1:1:0 + @test sortperm(dr) === 1:1:0 @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -93,7 +93,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) == 1:1:0 + @test sortperm(dr) === 1:1:0 @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) @@ -153,7 +153,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) == 1:1:0 + @test sortperm(dr) === 1:1:0 @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -211,7 +211,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) == 1:1:0 + @test sortperm(dr) === 1:1:0 @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) diff --git a/stdlib/REPL/test/replcompletions.jl b/stdlib/REPL/test/replcompletions.jl index 5f3d9e9e57c19..f2628ee3cf4a9 100644 --- a/stdlib/REPL/test/replcompletions.jl +++ b/stdlib/REPL/test/replcompletions.jl @@ -126,7 +126,7 @@ end let s = "Main.CompletionFoo." c, r = test_complete(s) @test "bar" in c - @test r == 20:19 + @test r === 20:19 @test s[r] == "" end @@ -550,7 +550,12 @@ end # The return type is of importance, before #8995 it would return nothing # which would raise an error in the repl code. -@test (String[], 0:-1, false) == test_scomplete("\$a") +let c, r, res + c, r, res = test_scomplete("\$a") + @test c == String[] + @test r === 0:-1 + @test res === false +end if Sys.isunix() let s, c, r @@ -598,7 +603,7 @@ let s, c, r s = "/tmp/" c,r = test_scomplete(s) @test !("tmp/" in c) - @test r == 6:5 + @test r === 6:5 @test s[r] == "" end @@ -615,7 +620,7 @@ let s, c, r file = joinpath(path, "repl completions") s = "/tmp " c,r = test_scomplete(s) - @test r == 6:5 + @test r === 6:5 end # Test completing paths with an escaped trailing space @@ -929,7 +934,7 @@ end let s = "" c, r = test_complete_context(s) @test "bar" in c - @test r == 1:0 + @test r === 1:0 @test s[r] == "" end diff --git a/test/core.jl b/test/core.jl index 14375439ebfee..b883636b380de 100644 --- a/test/core.jl +++ b/test/core.jl @@ -6701,7 +6701,7 @@ function f27597(y) return y end @test f27597([1]) == [1] -@test f27597([]) == 1:0 +@test f27597([]) === 1:0 # issue #22291 wrap22291(ind) = (ind...,) diff --git a/test/ranges.jl b/test/ranges.jl index 5ab735ffc782b..052603b55893e 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -422,10 +422,10 @@ end end end @testset "indexing range with empty range (#4309)" begin - @test (3:6)[5:4] == 7:6 + @test (3:6)[5:4] === 7:6 @test_throws BoundsError (3:6)[5:5] @test_throws BoundsError (3:6)[5] - @test (0:2:10)[7:6] == 12:2:10 + @test (0:2:10)[7:6] === 12:2:10 @test_throws BoundsError (0:2:10)[7:7] end # indexing with negative ranges (#8351) @@ -1479,11 +1479,11 @@ end for stops in [-2, 0, 2, 100] for lengths in [2, 10, 100] if stops >= starts - @test range(starts, stops, length=lengths) == range(starts, stop=stops, length=lengths) + @test range(starts, stops, length=lengths) === range(starts, stop=stops, length=lengths) end end for steps in [0.01, 1, 2] - @test range(starts, stops, step=steps) == range(starts, stop=stops, step=steps) + @test range(starts, stops, step=steps) === range(starts, stop=stops, step=steps) end end end diff --git a/test/regex.jl b/test/regex.jl index aad5413fd250b..7732b4a9ddafa 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -49,8 +49,8 @@ # findall: @test findall(r"\w+", "foo bar") == [1:3, 5:7] @test findall(r"\w+", "foo bar", overlap=true) == [1:3, 2:3, 3:3, 5:7, 6:7, 7:7] - @test findall(r"\w*", "foo bar") == [1:3, 4:3, 5:7, 8:7] - @test findall(r"\b", "foo bar") == [1:0, 4:3, 5:4, 8:7] + @test all(findall(r"\w*", "foo bar") .=== [1:3, 4:3, 5:7, 8:7]) # use === to compare empty ranges + @test all(findall(r"\b", "foo bar") .=== [1:0, 4:3, 5:4, 8:7]) # use === to compare empty ranges # Named subpatterns let m = match(r"(?.)(.)(?.)", "xyz") diff --git a/test/sorting.jl b/test/sorting.jl index 8b58a2d2620ef..ce8c4e868b7cf 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -35,6 +35,12 @@ let a=[1:10;] end @test sum(randperm(6)) == 21 +# Compare ranges by comparing their `first` and `last` elements and their `length`. This +# returns `false` if empty ranges have different startpoints, which is relevant for +# `searchsorted` +==ᵣ(r::AbstractRange, s::AbstractRange) = + (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) + @testset "searchsorted" begin numTypes = [ Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128, @@ -46,16 +52,16 @@ end @test searchsorted(fill(1, 15), 1, 6, 10, Forward) == 6:10 for R in numTypes, T in numTypes - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) == 1:0 + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(0)) === 1:0 @test searchsorted(R[1, 1, 2, 2, 3, 3], T(1)) == 1:2 @test searchsorted(R[1, 1, 2, 2, 3, 3], T(2)) == 3:4 - @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) == 7:6 - @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) == 5:4 + @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) === 7:6 + @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) === 5:4 - @test searchsorted(1:3, T(0)) == 1:0 + @test searchsorted(1:3, T(0)) ==ᵣ 1:0 @test searchsorted(1:3, T(1)) == 1:1 @test searchsorted(1:3, T(2)) == 2:2 - @test searchsorted(1:3, T(4)) == 4:3 + @test searchsorted(1:3, T(4)) ==ᵣ 4:3 @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10 @@ -67,31 +73,31 @@ end rg_r = reverse(rg) rgv, rgv_r = [rg;], [rg_r;] for i = I - @test searchsorted(rg,i) == searchsorted(rgv,i) - @test searchsorted(rg_r,i,rev=true) == searchsorted(rgv_r,i,rev=true) + @test searchsorted(rg,i) === searchsorted(rgv,i) + @test searchsorted(rg_r,i,rev=true) === searchsorted(rgv_r,i,rev=true) end end rg = 0.0:0.01:1.0 for i = 2:101 @test searchsorted(rg, rg[i]) == i:i - @test searchsorted(rg, prevfloat(rg[i])) == i:i-1 - @test searchsorted(rg, nextfloat(rg[i])) == i+1:i + @test searchsorted(rg, prevfloat(rg[i])) === i:i-1 + @test searchsorted(rg, nextfloat(rg[i])) === i+1:i end rg_r = reverse(rg) for i = 1:100 @test searchsorted(rg_r, rg_r[i], rev=true) == i:i - @test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) == i+1:i - @test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) == i:i-1 + @test searchsorted(rg_r, prevfloat(rg_r[i]), rev=true) === i+1:i + @test searchsorted(rg_r, nextfloat(rg_r[i]), rev=true) === i:i-1 end @test searchsorted(1:10, 1, by=(x -> x >= 5)) == searchsorted([1:10;], 1, by=(x -> x >= 5)) @test searchsorted(1:10, 10, by=(x -> x >= 5)) == searchsorted([1:10;], 10, by=(x -> x >= 5)) - @test searchsorted([], 0) == 1:0 - @test searchsorted([1,2,3], 0) == 1:0 - @test searchsorted([1,2,3], 4) == 4:3 + @test searchsorted([], 0) === 1:0 + @test searchsorted([1,2,3], 0) === 1:0 + @test searchsorted([1,2,3], 4) === 4:3 @testset "issue 8866" begin @test searchsortedfirst(500:1.0:600, -1.0e20) == 1 diff --git a/test/strings/search.jl b/test/strings/search.jl index 8de73e5c652e5..ff0fbf05d770b 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -247,7 +247,7 @@ end for i = 1:lastindex(u8str) @test findnext("", u8str, i) == i:i-1 end -@test findfirst("", "") == 1:0 +@test findfirst("", "") === 1:0 # string backward search with a zero-char string for i = 1:lastindex(astr) @@ -256,7 +256,7 @@ end for i = 1:lastindex(u8str) @test findprev("", u8str, i) == i:i-1 end -@test findlast("", "") == 1:0 +@test findlast("", "") === 1:0 # string forward search with a zero-char regex for i = 1:lastindex(astr) @@ -377,7 +377,7 @@ s_18109 = "fooα🐨βcd3" @testset "findall" begin @test findall("fooo", "foo") == UnitRange{Int}[] @test findall("ing", "Spinning laughing dancing") == [6:8, 15:17, 23:25] - @test findall("", "foo") == [1:0, 2:1, 3:2, 4:3] + @test all(findall("", "foo") .=== [1:0, 2:1, 3:2, 4:3]) # use === to compare empty ranges @test findall("αβ", "blαh blαβ blαββy") == findall("αβ", "blαh blαβ blαββy", overlap=true) == [9:11, 16:18] @test findall("aa", "aaaaaa") == [1:2, 3:4, 5:6] @test findall("aa", "aaaaaa", overlap=true) == [1:2, 2:3, 3:4, 4:5, 5:6] From f3775deb15dd0fc0281b4edd63a13bc9b8f77954 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Thu, 20 Jun 2019 20:09:21 +0200 Subject: [PATCH 3/7] Fix tests on 32-bit systems --- stdlib/Dates/test/ranges.jl | 8 ++++---- stdlib/REPL/test/replcompletions.jl | 15 ++++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/stdlib/Dates/test/ranges.jl b/stdlib/Dates/test/ranges.jl index b21ae01195386..0535c350ecb32 100644 --- a/stdlib/Dates/test/ranges.jl +++ b/stdlib/Dates/test/ranges.jl @@ -35,7 +35,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) === 1:1:0 + @test sortperm(dr) === Int64(1):Int64(1):Int64(0) @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -93,7 +93,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) === 1:1:0 + @test sortperm(dr) === Int64(1):Int64(1):Int64(0) @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) @@ -153,7 +153,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) === 1:1:0 + @test sortperm(dr) === Int64(1):Int64(1):Int64(0) @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -211,7 +211,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) === 1:1:0 + @test sortperm(dr) === Int64(1):Int64(1):Int64(0) @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) diff --git a/stdlib/REPL/test/replcompletions.jl b/stdlib/REPL/test/replcompletions.jl index f2628ee3cf4a9..542cf0da2640c 100644 --- a/stdlib/REPL/test/replcompletions.jl +++ b/stdlib/REPL/test/replcompletions.jl @@ -95,6 +95,11 @@ test_scomplete(s) = map_completion_text(shell_completions(s,lastindex(s))) test_bslashcomplete(s) = map_completion_text(bslash_completions(s,lastindex(s))[2]) test_complete_context(s) = map_completion_text(completions(s,lastindex(s),Main.CompletionFoo)) +# Compare ranges by comparing their `first` and `last` elements and their `length`. This +# returns `false` if empty ranges have different startpoints. +==ᵣ(r::AbstractRange, s::AbstractRange) = + (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) + let s = "" c, r = test_complete(s) @test "CompletionFoo" in c @@ -126,7 +131,7 @@ end let s = "Main.CompletionFoo." c, r = test_complete(s) @test "bar" in c - @test r === 20:19 + @test r ==ᵣ 20:19 @test s[r] == "" end @@ -553,7 +558,7 @@ end let c, r, res c, r, res = test_scomplete("\$a") @test c == String[] - @test r === 0:-1 + @test r ==ᵣ 0:-1 @test res === false end @@ -603,7 +608,7 @@ let s, c, r s = "/tmp/" c,r = test_scomplete(s) @test !("tmp/" in c) - @test r === 6:5 + @test r ==ᵣ 6:5 @test s[r] == "" end @@ -620,7 +625,7 @@ let s, c, r file = joinpath(path, "repl completions") s = "/tmp " c,r = test_scomplete(s) - @test r === 6:5 + @test r ==ᵣ 6:5 end # Test completing paths with an escaped trailing space @@ -934,7 +939,7 @@ end let s = "" c, r = test_complete_context(s) @test "bar" in c - @test r === 1:0 + @test r ==ᵣ 1:0 @test s[r] == "" end From a0821f45ed614e1d34a703ca7bb537a4728ca9a0 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Thu, 20 Jun 2019 21:04:52 +0200 Subject: [PATCH 4/7] Fix tests for Dates --- stdlib/Dates/test/ranges.jl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/stdlib/Dates/test/ranges.jl b/stdlib/Dates/test/ranges.jl index 0535c350ecb32..4a35c33923a2d 100644 --- a/stdlib/Dates/test/ranges.jl +++ b/stdlib/Dates/test/ranges.jl @@ -7,6 +7,11 @@ using Dates using InteractiveUtils: subtypes +# Compare ranges by comparing their `first` and `last` elements and their `length`. This +# returns `false` if empty ranges have different startpoints. +==ᵣ(r::AbstractRange, s::AbstractRange) = + (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) + let for T in (Dates.Date, Dates.DateTime) f1 = T(2014); l1 = T(2013, 12, 31) @@ -35,7 +40,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) === Int64(1):Int64(1):Int64(0) + @test sortperm(dr) ==ᵣ 1:1:0 @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -93,7 +98,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) === Int64(1):Int64(1):Int64(0) + @test sortperm(dr) ==ᵣ 1:1:0 @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) @@ -153,7 +158,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) === Int64(1):Int64(1):Int64(0) + @test sortperm(dr) ==ᵣ 1:1:0 @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -211,7 +216,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) === Int64(1):Int64(1):Int64(0) + @test sortperm(dr) ==ᵣ 1:1:0 @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) From 548b667d40b261d9d38543a5784a2edbfbb72d40 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Mon, 1 Jul 2019 19:25:34 +0200 Subject: [PATCH 5/7] Add NEWS.md item for #32348 --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 643eb29aa1526..583c339b6ca8d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,6 +9,7 @@ New language features Language changes ---------------- +* Empty ranges now compare equal, regardless of their startpoint and step ([#32348]). Multi-threading changes ----------------------- From 648c78e474197b784c23c41c0735564e7dca32df Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Tue, 21 Apr 2020 09:07:29 +0200 Subject: [PATCH 6/7] Update tests --- test/offsetarray.jl | 2 +- test/sorting.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 58a83cc2c9311..7479821be8268 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -502,7 +502,7 @@ v = OffsetArray(rand(8), (-2,)) @test sort(A, dims=2) == OffsetArray(sort(parent(A), dims=2), A.offsets) # Issue #33977 soa = OffsetArray([2,2,3], -2) -@test searchsorted(soa, 1) == -1:-2 +@test searchsorted(soa, 1) === -1:-2 @test searchsortedfirst(soa, 1) == -1 @test searchsortedlast(soa, 1) == -2 @test first(sort!(soa; alg=QuickSort)) == 2 diff --git a/test/sorting.jl b/test/sorting.jl index 99d31962b1c01..f19af948db597 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -187,11 +187,11 @@ end for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0)) @test searchsorted(v, 3, rev=true) == 1:1 @test searchsorted(v, 3.0, rev=true) == 1:1 - @test searchsorted(v, 2.5, rev=true) == 2:1 + @test searchsorted(v, 2.5, rev=true) === 2:1 @test searchsorted(v, 2, rev=true) == 2:2 - @test searchsorted(v, 1.2, rev=true) == 3:2 + @test searchsorted(v, 1.2, rev=true) === 3:2 @test searchsorted(v, 1, rev=true) == 3:3 - @test searchsorted(v, 0.1, rev=true) == 4:3 + @test searchsorted(v, 0.1, rev=true) === 4:3 end end end From afcaa9c7a6cd47cd7cf0b391b10b67895a287b14 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Tue, 28 Apr 2020 13:52:21 +0200 Subject: [PATCH 7/7] Simplify tests --- stdlib/Dates/test/ranges.jl | 13 ++++--------- stdlib/REPL/test/replcompletions.jl | 15 +++++---------- test/sorting.jl | 10 ++-------- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/stdlib/Dates/test/ranges.jl b/stdlib/Dates/test/ranges.jl index 4a35c33923a2d..f10350c3cb6f5 100644 --- a/stdlib/Dates/test/ranges.jl +++ b/stdlib/Dates/test/ranges.jl @@ -7,11 +7,6 @@ using Dates using InteractiveUtils: subtypes -# Compare ranges by comparing their `first` and `last` elements and their `length`. This -# returns `false` if empty ranges have different startpoints. -==ᵣ(r::AbstractRange, s::AbstractRange) = - (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) - let for T in (Dates.Date, Dates.DateTime) f1 = T(2014); l1 = T(2013, 12, 31) @@ -40,7 +35,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) ==ᵣ 1:1:0 + @test sortperm(dr) === StepRange{Int64,Int}(1:1:0) @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -98,7 +93,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) ==ᵣ 1:1:0 + @test sortperm(dr) === StepRange{Int64,Int}(1:1:0) @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) @@ -158,7 +153,7 @@ let @test first(reverse(dr)) < f1 @test last(reverse(dr)) >= f1 @test issorted(dr) - @test sortperm(dr) ==ᵣ 1:1:0 + @test sortperm(dr) === StepRange{Int64,Int}(1:1:0) @test !(f1 in dr) @test !(l1 in dr) @test !(f1 - pos_step in dr) @@ -216,7 +211,7 @@ let @test first(reverse(dr)) > l1 @test last(reverse(dr)) <= l1 @test issorted(dr) - @test sortperm(dr) ==ᵣ 1:1:0 + @test sortperm(dr) === StepRange{Int64,Int}(1:1:0) @test !(l1 in dr) @test !(l1 in dr) @test !(l1 - neg_step in dr) diff --git a/stdlib/REPL/test/replcompletions.jl b/stdlib/REPL/test/replcompletions.jl index 1996c87cf7dcf..d37bfd7f3ad7e 100644 --- a/stdlib/REPL/test/replcompletions.jl +++ b/stdlib/REPL/test/replcompletions.jl @@ -101,11 +101,6 @@ test_scomplete(s) = map_completion_text(shell_completions(s,lastindex(s))) test_bslashcomplete(s) = map_completion_text(bslash_completions(s,lastindex(s))[2]) test_complete_context(s) = map_completion_text(completions(s,lastindex(s),Main.CompletionFoo)) -# Compare ranges by comparing their `first` and `last` elements and their `length`. This -# returns `false` if empty ranges have different startpoints. -==ᵣ(r::AbstractRange, s::AbstractRange) = - (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) - let s = "" c, r = test_complete(s) @test "CompletionFoo" in c @@ -137,7 +132,7 @@ end let s = "Main.CompletionFoo." c, r = test_complete(s) @test "bar" in c - @test r ==ᵣ 20:19 + @test r === UnitRange{Int64}(20:19) @test s[r] == "" end @@ -600,7 +595,7 @@ end let c, r, res c, r, res = test_scomplete("\$a") @test c == String[] - @test r ==ᵣ 0:-1 + @test r === UnitRange{Int64}(0:-1) @test res === false end @@ -650,7 +645,7 @@ let s, c, r s = "/tmp/" c,r = test_scomplete(s) @test !("tmp/" in c) - @test r ==ᵣ 6:5 + @test r === UnitRange{Int64}(6:5) @test s[r] == "" end @@ -667,7 +662,7 @@ let s, c, r file = joinpath(path, "repl completions") s = "/tmp " c,r = test_scomplete(s) - @test r ==ᵣ 6:5 + @test r === UnitRange{Int64}(6:5) end # Test completing paths with an escaped trailing space @@ -981,7 +976,7 @@ end let s = "" c, r = test_complete_context(s) @test "bar" in c - @test r ==ᵣ 1:0 + @test r === UnitRange{Int64}(1:0) @test s[r] == "" end diff --git a/test/sorting.jl b/test/sorting.jl index 44b12894635cd..ad5438514261e 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -77,12 +77,6 @@ end @test_throws ArgumentError partialsortperm!([1,2], [2,3,1], 1:2) end -# Compare ranges by comparing their `first` and `last` elements and their `length`. This -# returns `false` if empty ranges have different startpoints, which is relevant for -# `searchsorted` -==ᵣ(r::AbstractRange, s::AbstractRange) = - (first(r) == first(s)) & (length(r) == length(s)) & (last(r) == last(s)) - @testset "searchsorted" begin numTypes = [ Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128, @@ -100,10 +94,10 @@ end @test searchsorted(R[1, 1, 2, 2, 3, 3], T(4)) === 7:6 @test searchsorted(R[1, 1, 2, 2, 3, 3], 2.5) === 5:4 - @test searchsorted(1:3, T(0)) ==ᵣ 1:0 + @test searchsorted(1:3, T(0)) === 1:0 @test searchsorted(1:3, T(1)) == 1:1 @test searchsorted(1:3, T(2)) == 2:2 - @test searchsorted(1:3, T(4)) ==ᵣ 4:3 + @test searchsorted(1:3, T(4)) === 4:3 @test searchsorted(R[1:10;], T(1), by=(x -> x >= 5)) == 1:4 @test searchsorted(R[1:10;], T(10), by=(x -> x >= 5)) == 5:10