test_reg.py 1.04 KB
Newer Older
1
from lib.parsing import Parser
2
from lib.dfa import DFA
3
4
5

def is_can(dfa: str) -> bool:
    parser = Parser()
6
    return parse(dfa).is_canonical()
7
    
8
9
10
11
12
13
14
def parse(dfa: str) -> DFA:
    parser = Parser()
    return parser.str_to_dfa(dfa)

def to_str(dfa: DFA) -> str:
    parser = Parser()
    return parser.dfa_to_str(dfa)
15
16

def test_can() -> None:
17
18
    ex1 = "init=1 (1,a)=1 (1,b)=2 (2,b)=1 (2,a)=1 final={2}"
    assert is_can(ex1)
19
20
21
    assert is_can("init=1 (1,b)=3 (1,a)=2 (2,b)=1 (2,a)=1 (3,a)=2 (3,b)=2 final={2}")
    assert not is_can("init=1 (1,b)=3 (1,c)=2 (2,b)=1 (2,a)=1 (3,a)=2 (3,b)=2 final={2}")
    assert not is_can("init=1 (1,a)=1 (1,b)=2 (2,b)=1 (2,b)=1 (unreach,a)=1 (unreach,b)=unreach final={2}")
22
23
24
25
26

    assert DFA.is_identical(parse(ex1).canonize(), parse(ex1))
    auto_can = parse("init=x (x,a)=z (x,b)=y (y,a)=z (y,b)=z (z,a)=z (z,b)=z final={z}").canonize()
    expect_can = parse("init=1 (1,a)=2 (1,b)=3 (2,a)=2 (2,b)=2 (3,a)=2 (3,b)=2 final={2}")
    assert DFA.is_identical(auto_can, expect_can), f"{to_str(auto_can)}{to_str(expect_can)}"