From 3cd000d9b30e3bb6507bf7f7e2b4e9b58d487d93 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 27 Oct 2024 00:01:23 +0100 Subject: [PATCH] test: more automata tests --- lib_test/expect/test_automata.ml | 94 ++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/lib_test/expect/test_automata.ml b/lib_test/expect/test_automata.ml index 44948c9..18cd6aa 100644 --- a/lib_test/expect/test_automata.ml +++ b/lib_test/expect/test_automata.ml @@ -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) @@ -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" = @@ -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 @@ -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 @@ -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))))) + |}] +;;