Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Jul 19, 2023
1 parent f2c9c20 commit 65a08b4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/machine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function throw_input_error(
string(index),
".\nLast ",
string(length(slice)),
" bytes were:\n\n"
" byte(s) were:\n\n"
)
write(buf, bytes, "\n\n")

Expand Down
77 changes: 77 additions & 0 deletions test/byteset.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module TestByteSet

using Automa: Automa, ByteSet
using Test


function test_membership(members)
bs = ByteSet(members)
refset = Set{UInt8}([UInt8(i) for i in members])
@test refset == Set{UInt8}(collect(bs))
@test all(i -> in(i, bs), refset)
end

function test_inversion(bs)
inv = ~bs
all = true
for i in 0x00:0xff
all &= (in(i, bs) in(i, inv))
end
@test all
end

@testset "Instantiation" begin
@test isempty(ByteSet())
@test iszero(length(ByteSet()))

for set in ["hello", "kdjy82zxxcbnpw", [0x00, 0x1a, 0xff, 0xf8, 0xd2]]
test_membership(set)
end
end

@testset "Min/max" begin
@test_throws ArgumentError maximum(ByteSet())
@test_throws ArgumentError minimum(ByteSet())
@test minimum(ByteSet("xylophone")) == UInt8('e')
@test maximum(ByteSet([0xa1, 0x0f, 0x4e, 0xf1, 0x40, 0x39])) == 0xf1
end

@testset "Contiguity" begin
@test Automa.is_contiguous(ByteSet(0x03:0x41))
@test Automa.is_contiguous(ByteSet())
@test Automa.is_contiguous(ByteSet(0x51))
@test Automa.is_contiguous(ByteSet(0xc1:0xd2))
@test Automa.is_contiguous(ByteSet(0x00:0xff))

@test !Automa.is_contiguous(ByteSet([0x12:0x3a; 0x3c:0x4a]))
@test !Automa.is_contiguous(ByteSet([0x01, 0x02, 0x04, 0x05]))
end

@testset "Inversion" begin
test_inversion(ByteSet())
test_inversion(ByteSet(0x00:0xff))
test_inversion(ByteSet([0x04, 0x06, 0x91, 0x92]))
test_inversion(ByteSet(0x54:0x71))
test_inversion(ByteSet(0x12:0x11))
test_inversion(ByteSet("abracadabra"))
end

@testset "Set operations" begin
sets = map(ByteSet, [
[],
[0x00:0xff;],
[0x00:0x02; 0x04; 0x19],
[0x01; 0x03; 0x09; 0xa1; 0xa1],
[0x41:0x8f; 0xd1:0xe1; 0xa0:0xf0],
[0x81:0x89; 0xd0:0xd0]
])
ssets = map(Set, sets)
for (s1, ss1) in zip(sets, ssets), (s2, ss2) in zip(sets, ssets)
for f in [union, intersect, symdiff, setdiff]
@test Set(f(s1, s2)) == f(ss1, ss2)
end
@test isdisjoint(s1, s2) == isdisjoint(ss1, ss2)
end
end

end # module
15 changes: 15 additions & 0 deletions test/input_error.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module TestInputError

using Automa
using Test

@testset "Input error" begin
machine = compile(re"xyz")
@eval function test_input_error(data)
$(generate_code(machine))
end

@test_throws Exception test_input_error("a")
end

end # module
6 changes: 2 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ using Test
end

@testset "ByteSet" begin
x = Automa.ByteSet()
@test isempty(x)
@test_throws ArgumentError minimum(x)
@test_throws ArgumentError maximum(x)
include("byteset.jl")
end

@testset "RegExp" begin
Expand Down Expand Up @@ -124,6 +121,7 @@ include("test16.jl")
include("test17.jl")
include("test18.jl")
include("test19.jl")
include("input_error.jl")
include("simd.jl")
include("unicode.jl")
include("validator.jl")
Expand Down
2 changes: 2 additions & 0 deletions test/test11.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using Automa
using Test

@testset "Test11" begin
@test_throws Exception precond!(re"A", :foo; when=:never)

a = re"[a-z]+"
precond!(a, :le)
a = rep1(a)
Expand Down

0 comments on commit 65a08b4

Please sign in to comment.