Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Dec 11, 2024
1 parent 00a49a8 commit 71ac150
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 43 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]
[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"
Expand Down
3 changes: 1 addition & 2 deletions src/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
104 changes: 66 additions & 38 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 71ac150

Please sign in to comment.