def test_set_new_transtable(): alphabet = ["a", "b"] states = ["1", "2"] table = TransitionTable(alphabet, states) table.add_transition("a", "2", "2") table.add_transition("b", "2", "2") table.add_transition("a", "1", "2") table.add_transition("b", "1", "1") dfa = DFA(table) # invalid table try: dfa.trans_table = {} except ValueError: pass # new table new_table = TransitionTable(alphabet, states) new_table.add_transition("b", "2", "2") new_table.add_transition("a", "1", "2") new_table.add_transition("b", "1", "1") dfa.trans_table = new_table
def test_consume(): alphabet = ["a", "b"] states = ["1", "2"] table = TransitionTable(alphabet, states) table.add_transition("b", "2", "2") table.add_transition("a", "1", "2") table.add_transition("b", "1", "1") dfa = DFA(table) # accepted string accepted = dfa.consume("ba") assert accepted, "'ba' is expected to be accepted." accepted = dfa.consume("babbb") assert accepted, "'babbb' is expected to be accepted." # not acceptable string accepted = dfa.consume("b") assert not accepted, "'b' is expected not to be accepted." accepted = dfa.consume("bbb") assert not accepted, "'bbb' is expected not to be accepted." # dead state accepted = dfa.consume("bbaba") assert not accepted, "'bbaba' is expected not to be accepted." # invalid transition accepted = dfa.consume("babbbc") assert not accepted, "'bac' is expected not to be accepted."
def test_print(): alphabet = ["a", "b", "c", "d"] states = ["1", "2", "3"] table = TransitionTable(alphabet, states) table.add_transition("a", "1", "2") table.add_transition("b", "1", "3") table.add_transition("c", "1", "1") table.add_transition("d", "1", "1") print >> sys.stderr, "\n" print >> sys.stderr, table
def test_consume(): alphabet = ["a", "b", "c", "d"] states = ["1", "2", "3"] table = TransitionTable(alphabet, states) table.add_transition("a", "1", "2") table.add_transition("b", "1", "3") table.add_transition("c", "1", "1") next_state = table.consume("a", "1") assert next_state == "2", "expect '2', get {}".format(next_state) next_state = table.consume("b", "1") assert next_state == "3", "expect '3', get {}".format(next_state) # test InvalidTransition try: # invalid symbol table.consume("q", "1") except InvalidTransition: pass try: # invalid state table.consume("a", "4") except InvalidTransition: pass # testing DeadSate try: table.consume("d", "1") except DeadState: pass
def get_table(alphabet, states): return TransitionTable(alphabet, states)