Skip to content

Commit

Permalink
test: more automata tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrinberg committed Oct 27, 2024
1 parent 4d34b48 commit 3cd000d
Showing 1 changed file with 84 additions and 10 deletions.
94 changes: 84 additions & 10 deletions lib_test/expect/test_automata.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include struct
let eps = eps
let cst = cst
let seq = seq
let rep = rep
end

let pp_state state = print_dyn (State.to_dyn state)
Expand All @@ -30,14 +31,20 @@ let str ids sem str =
loop (String.to_seq str)
;;

let rec loop wa d c =
print_dyn (State.to_dyn d);
match State.status d with
| Failed -> Format.printf "> failed@."
| Match _ -> Format.printf "> matched@."
| Running ->
let d = Automata.delta wa cat (Cset.of_char c) d in
loop wa d c
let loop ?(max = 100) wa d c =
let cset = Cset.of_char c in
let rec loop d n =
if n > 0
then (
print_dyn (State.to_dyn d);
match State.status d with
| Failed -> Format.printf "> failed@."
| Match _ -> Format.printf "> matched@."
| Running ->
let d = Automata.delta wa cat cset d in
loop d (n - 1))
in
loop d max
;;

let%expect_test "string" =
Expand Down Expand Up @@ -71,8 +78,8 @@ let%expect_test "string" =
;;

let%expect_test "alternation" =
let ids = Ids.create () in
let re =
let ids = Ids.create () in
let n = 4 in
let s =
let c = 'a' in
Expand Down Expand Up @@ -112,8 +119,8 @@ let%expect_test "alternation" =

let%expect_test "alternation shared prefix" =
let n = 4 in
let ids = Ids.create () in
let re =
let ids = Ids.create () in
let prefix =
let s =
let c = 'a' in
Expand Down Expand Up @@ -142,3 +149,70 @@ let%expect_test "alternation shared prefix" =
> failed
|}]
;;

let%expect_test "kleene star" =
let re =
let ids = Ids.create () in
rep ids `Greedy `First (cst ids (Cset.csingle 'z'))
in
let wa = Working_area.create () in
loop ~max:4 wa (State.create cat re) 'z';
[%expect
{|
((TExp (first (Rep 122))))
((TExp (first (Rep 122))) (TMarks ()))
((TExp (first (Rep 122))) (TMarks ()))
((TExp (first (Rep 122))) (TMarks ()))
|}];
loop ~max:3 wa (State.create cat re) 'a';
[%expect {|
((TExp (first (Rep 122))))
((TMarks ()))
> matched
|}]
;;

let%expect_test "derivative recomputation" =
let sem = `Longest in
let re =
let ids = Ids.create () in
let lhs = rep ids `Non_greedy sem (cst ids Cset.cany) in
let rhs =
seq
ids
sem
(Automata.mark ids Automata.Mark.start)
(Automata.alt ids [ cst ids (Cset.csingle 'z'); cst ids (Cset.csingle 'b') ])
in
seq ids sem lhs rhs
in
let wa = Working_area.create () in
loop ~max:7 wa (State.create cat re) 'z';
[%expect
{|
((TExp (long (Seq (Rep ((0 255))) (Seq (Mark 0) (Alt 122 98))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 0)))) Eps))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 1)))) Eps) (TMarks ((marks ((0 0))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 0)))) Eps) (TMarks ((marks ((0 1))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 1)))) Eps) (TMarks ((marks ((0 0))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 0)))) Eps) (TMarks ((marks ((0 1))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98))))
(TExp ((marks ((0 1)))) Eps) (TMarks ((marks ((0 0))))))
|}];
loop ~max:7 wa (State.create cat re) 'a';
[%expect
{|
((TExp (long (Seq (Rep ((0 255))) (Seq (Mark 0) (Alt 122 98))))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
((long (TSeq ((TExp (Rep ((0 255))))) (Seq (Mark 0) (Alt 122 98)))))
|}]
;;

0 comments on commit 3cd000d

Please sign in to comment.