diff --git a/src/machine.jl b/src/machine.jl index 7784774c..4d8499f9 100644 --- a/src/machine.jl +++ b/src/machine.jl @@ -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") diff --git a/test/byteset.jl b/test/byteset.jl new file mode 100644 index 00000000..bdf265fc --- /dev/null +++ b/test/byteset.jl @@ -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 diff --git a/test/input_error.jl b/test/input_error.jl new file mode 100644 index 00000000..0cb1e2cd --- /dev/null +++ b/test/input_error.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 50ddacfd..56ccebef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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") diff --git a/test/test11.jl b/test/test11.jl index cef20e85..907b2949 100644 --- a/test/test11.jl +++ b/test/test11.jl @@ -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)