Esempio n. 1
0
def test_insert():
    nfa1 = fsa.NFA(1)
    nfa1.add_transition(1, "a", 2)
    nfa1.add_transition(2, "b", 3)
    nfa1.add_final_state(3)

    nfa2 = fsa.NFA(4)
    nfa2.add_transition(4, "x", 5)
    nfa2.add_transition(4, "y", 5)
    nfa2.insert(4, nfa1, 5)
    nfa2.add_final_state(5)

    assert nfa2.accept("x")
    assert nfa2.accept("y")
    assert nfa2.accept("ab")
    assert not nfa2.accept("a")
Esempio n. 2
0
def test_empty_string():
    nfa = fsa.NFA(1)
    nfa.add_final_state(1)

    assert nfa.accept("")
    assert not nfa.accept("a")

    dfa = nfa.to_dfa()
    assert dfa.accept("")
    assert not dfa.accept("a")
Esempio n. 3
0
def test_nfa():
    nfa = fsa.NFA(0)
    nfa.add_transition(0, "a", 1)
    nfa.add_transition(0, fsa.EPSILON, 4)
    nfa.add_transition(0, "b", 1)
    nfa.add_transition(1, "c", 4)
    nfa.add_final_state(4)

    assert nfa.accept("")
    assert nfa.accept("ac")
    assert nfa.accept("bc")
    assert not nfa.accept("c")
Esempio n. 4
0
def test_nfa2():
    nfa = fsa.NFA(1)
    nfa.add_transition(1, "a", 2)
    nfa.add_transition(1, "c", 4)
    nfa.add_transition(2, "b", 3)
    nfa.add_transition(2, fsa.EPSILON, 1)
    nfa.add_transition(3, "a", 2)
    nfa.add_transition(4, "c", 3)
    nfa.add_transition(4, fsa.EPSILON, 3)
    nfa.add_final_state(3)

    assert nfa.accept("ab")
    assert nfa.accept("abab")
    assert nfa.accept("cc")
    assert nfa.accept("c")
    assert nfa.accept("ccab")
    assert nfa.accept("ccacc")
    assert nfa.accept("ccac")
    assert nfa.accept("abacab")

    assert not nfa.accept("b")
    assert not nfa.accept("a")
    assert not nfa.accept("cb")
    assert not nfa.accept("caa")

    dfa = nfa.to_dfa()
    assert dfa.accept("ab")
    assert dfa.accept("abab")
    assert dfa.accept("cc")
    assert dfa.accept("c")
    assert dfa.accept("ccab")
    assert dfa.accept("ccacc")
    assert dfa.accept("ccac")
    assert dfa.accept("abacab")

    assert not dfa.accept("b")
    assert not dfa.accept("a")
    assert not dfa.accept("cb")
    assert not dfa.accept("caa")