Skip to content

Commit

Permalink
add tests for PackedVector and rewriting with automata
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Jul 30, 2024
1 parent 2819bbd commit 76eb55c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
11 changes: 9 additions & 2 deletions test/automata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ end
rs =
KB.RewritingSystem([(A, ε), (B, ε)], lenlexord, reduced = true)
ia = KB.IndexAutomaton(rs)
pa = KB.PrefixAutomaton(rs)
@test KB.rewrite(testword, rs) ==
KB.rewrite(testword, ia) ==
KB.rewrite(testword, pa) ==
Word([1])

rs = KB.RewritingSystem(
Expand All @@ -88,13 +90,18 @@ end
reduced = true,
)
ia = KB.IndexAutomaton(rs)
pa = KB.PrefixAutomaton(rs)

@test !isempty(ia)
@test !isempty(pa)

@test KB.rewrite(testword, rs) == KB.rewrite(testword, ia)
@test KB.rewrite(testword, rs) ==
KB.rewrite(testword, ia) ==
KB.rewrite(testword, pa)

w = Word([1, 3, 4, 1, 4, 4, 1, 1, 4, 2, 3, 2, 4, 2, 2, 3, 1, 2, 1])
@test KB.rewrite(w, rs) == KB.rewrite(w, ia)
@test KB.rewrite(w, rs) == KB.rewrite(w, ia) == KB.rewrite(w, pa)

@test sprint(show, ia) isa String
@test sprint(show, pa) isa String
end
53 changes: 51 additions & 2 deletions test/kbs.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "KBS" begin
@testset "KBStack" begin
lenlex = let A = Alphabet([:a, :b, :A, :B])
KB.setinverse!(A, :a, :A)
KB.setinverse!(A, :b, :B)
Expand All @@ -19,7 +19,6 @@
@test Set(KB.rules(knuthbendix(KB.Settings(KB.KBPlain()), R))) == crs
@test Set(KB.rules(knuthbendix(KB.Settings(KB.KBStack()), R))) == crs
@test Set(KB.rules(knuthbendix(KB.Settings(KB.KBS2AlgRuleDel()), R))) == crs
@test Set(KB.rules(knuthbendix(KB.Settings(KB.KBIndex()), R))) == crs

@testset "io for RewritingSystem" begin
@test sprint(show, MIME"text/plain"(), RC) isa String
Expand All @@ -37,3 +36,53 @@
@test occursin("• verbosity : 2", res)
end
end

@testset "Automaton rewriting" begin
rws = KB.ExampleRWS.Hurwitz4()
RC = knuthbendix(KB.Settings(KB.KBStack()), rws)

idxA = KB.IndexAutomaton(RC)
pfxA = KB.PrefixAutomaton(RC)

w = Word([1, 1, 1, 2, 2, 2, 1, 1, 3, 2, 3, 3, 1, 1, 1, 3, 3, 1, 2, 2, 3, 2])
v = Word([1, 2, 1, 2, 1, 3])
@test v == KB.rewrite(w, RC)
@test v == KB.rewrite(w, idxA)
@test v == KB.rewrite(w, pfxA)

let at = idxA
rwb = KB.RewritingBuffer{UInt16}(at)
k = @allocated begin
KB.Words.store!(rwb, w)
KB.rewrite!(rwb, at)
end
@test length(w) == 22
@test rwb.output == v

@test 0 == @allocated begin
KB.Words.store!(rwb, w)
KB.rewrite!(rwb, at)
end
end

let at = pfxA
rwb = KB.RewritingBuffer{UInt16}(at)
k = @allocated begin
KB.Words.store!(rwb, w)
KB.rewrite!(rwb, at)
end
@test length(w) == 22
@test rwb.output == v

@test 0 == @allocated begin
KB.Words.store!(rwb, w)
KB.rewrite!(rwb, at)
end
end

@test all(1:10) do _
w = Word(rand(1:length(alphabet(rws)), 50))
v = KB.rewrite(w, RC)
return v == KB.rewrite(w, idxA) == KB.rewrite(w, pfxA)
end
end
2 changes: 1 addition & 1 deletion test/kbs1.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testset "KBS1" begin
@testset "KBPlain" begin
Al = Alphabet([:a, :A, :b, :B])
KB.setinverse!(Al, :a, :A)
KB.setinverse!(Al, :b, :B)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KnuthBendix.Automata
include("abstract_words.jl")

@testset "KnuthBendix.jl" begin
include("packed_vector.jl")
include("words.jl")
include("bufferwords.jl")
include("alphabets.jl")
Expand Down

0 comments on commit 76eb55c

Please sign in to comment.