From 71ac150b866691fdeb65c830e9a1fae7f30026ea Mon Sep 17 00:00:00 2001 From: Jakob Nybo Nissen Date: Wed, 11 Dec 2024 11:54:47 +0100 Subject: [PATCH] Add tests --- Project.toml | 6 +-- src/basic.jl | 3 +- test/runtests.jl | 104 ++++++++++++++++++++++++++++++----------------- 3 files changed, 70 insertions(+), 43 deletions(-) diff --git a/Project.toml b/Project.toml index 2aa6245..575679e 100644 --- a/Project.toml +++ b/Project.toml @@ -6,15 +6,15 @@ authors = ["Jakob Nybo Nissen "] [weakdeps] StringViews = "354b36f9-a18e-4713-926e-db85100087ba" +[extensions] +StringViewsExt = "StringViews" + [compat] Aqua = "0.8.7" StringViews = "1" Test = "1.11" julia = "1.11" -[extensions] -StringViewsExt = "StringViews" - [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" StringViews = "354b36f9-a18e-4713-926e-db85100087ba" diff --git a/src/basic.jl b/src/basic.jl index 0ec45b2..6a04b81 100644 --- a/src/basic.jl +++ b/src/basic.jl @@ -227,8 +227,7 @@ Base.@propagate_inbounds function _findprev( @boundscheck (start > length(mem) && throw(BoundsError(mem, start))) start < 1 && return nothing im = @inbounds truncate(ImmutableMemoryView(mem), start) - v_ind = @something memrchr(im, byte) return nothing - v_ind + start - 1 + memrchr(im, byte) end function memrchr(mem::ImmutableMemoryView{T}, byte::T) where {T <: Union{Int8, UInt8}} diff --git a/test/runtests.jl b/test/runtests.jl index d48df91..84476a7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -372,19 +372,6 @@ end @test v2 == v1 end - @testset "Find" begin - mem = MemoryView([4, 3, 2]) - @test findfirst(==(2), mem) == 3 - - mem = MemoryView(Int8[6, 2, 7, 0, 2]) - @test findfirst(iszero, mem) == 4 - @test findfirst(==(Int8(0)), mem) == 4 - - mem = MemoryView(UInt8[1, 4, 2, 5, 6]) - @test findnext(==(0x04), mem, 1) == 2 - @test findnext(==(0x04), mem, 3) === nothing - end - @testset "Reverse and reverse!" begin for v in [ ["a", "abc", "a", "c", "kij"], @@ -465,31 +452,72 @@ end end @testset "Find" begin - mem = ImmutableMemoryView([1, 2, 3, 4]) - @test findfirst(isodd, mem) == 1 - @test findfirst(isodd, mem[2:end]) == 2 - @test findfirst(mem[1:0]) === nothing - - @test findlast(isodd, mem) == 3 - @test findlast(isodd, mem[1:2]) == 1 - @test findlast(isodd, mem[1:0]) === nothing - - @test findnext(isodd, mem, 0x02) == 3 - @test findnext(isodd, mem, 3) == 3 - @test findnext(isodd, mem, 0x04) === nothing - @test findnext(isodd, mem, 10) === nothing - - @test_throws BoundsError findnext(isodd, mem, 0) - @test_throws BoundsError findnext(isodd, mem, -1) - - @test findprev(isodd, mem, 4) == 3 - @test findprev(isodd, mem, 0x03) == 3 - @test findprev(isodd, mem, 2) == 1 - @test findprev(isodd, mem, 0x00) === nothing - @test findprev(isodd, mem, -10) === nothing - - @test_throws BoundsError findprev(isodd, mem, 5) - @test_throws BoundsError findprev(isodd, mem, 7) + @testset "Generic find" begin + mem = ImmutableMemoryView([1, 2, 3, 4]) + @test findfirst(isodd, mem) == 1 + @test findfirst(isodd, mem[2:end]) == 2 + @test findfirst(mem[1:0]) === nothing + + @test findlast(isodd, mem) == 3 + @test findlast(isodd, mem[1:2]) == 1 + @test findlast(isodd, mem[1:0]) === nothing + + @test findnext(isodd, mem, 0x02) == 3 + @test findnext(isodd, mem, 3) == 3 + @test findnext(isodd, mem, 0x04) === nothing + @test findnext(isodd, mem, 10) === nothing + + @test_throws BoundsError findnext(isodd, mem, 0) + @test_throws BoundsError findnext(isodd, mem, -1) + + @test findprev(isodd, mem, 4) == 3 + @test findprev(isodd, mem, 0x03) == 3 + @test findprev(isodd, mem, 2) == 1 + @test findprev(isodd, mem, 0x00) === nothing + @test findprev(isodd, mem, -10) === nothing + + @test_throws BoundsError findprev(isodd, mem, 5) + @test_throws BoundsError findprev(isodd, mem, 7) + end + + @testset "Memchr routines" begin + for T in Any[Int8, UInt8] + mem = MemoryView(T[6, 2, 7, 0, 2, 1]) + @test findfirst(iszero, mem) == 4 + @test findfirst(==(2), mem) == 2 + @test findnext(==(2), mem, 3) == 5 + @test findnext(==(7), mem, 4) === nothing + @test findnext(==(2), mem, 7) === nothing + @test_throws BoundsError findnext(iszero, mem, 0) + @test_throws BoundsError findnext(iszero, mem, -3) + + @test findlast(iszero, mem) == 4 + @test findprev(iszero, mem, 3) === nothing + @test findprev(iszero, mem, 4) == 4 + @test findprev(==(2), mem, 5) == 5 + @test findprev(==(2), mem, 4) == 2 + @test findprev(==(9), mem, 3) === nothing + @test findprev(==(2), mem, -2) === nothing + @test findprev(iszero, mem, 0) === nothing + @test_throws BoundsError findprev(iszero, mem, 7) + end + mem = MemoryView(Int8[2, 3, -1]) + @test findfirst(==(0xff), mem) === nothing + @test findprev(==(0xff), mem, 3) === nothing + end + + @testset "Find" begin + mem = MemoryView([4, 3, 2]) + @test findfirst(==(2), mem) == 3 + + mem = MemoryView(Int8[6, 2, 7, 0, 2]) + @test findfirst(iszero, mem) == 4 + @test findfirst(==(Int8(0)), mem) == 4 + + mem = MemoryView(UInt8[1, 4, 2, 5, 6]) + @test findnext(==(0x04), mem, 1) == 2 + @test findnext(==(0x04), mem, 3) === nothing + end end end