def test_add_edge(): d = dfa.DFA() s = state.State('s2', True, False) d.add_state(s) d.set_alphabet({'a'}) d.add_edge(d.initial_state, s, 'a') assert (len(d.initial_state.edges) == 1)
def three_state_loop_dfa(): """ alphabet: {p, b} s1 -> p -> s2 s2 -> b -> s3 s3 -> p -> s1 initial: s1 accepting: s1 """ d = dfa.DFA() d.states = set() d.initial_state = None s1 = state.State('s1', True, True) s2 = state.State('s2', False, False) s3 = state.State('s3', False, False) d.add_state(s1) d.add_state(s2) d.add_state(s3) d.initial_state = s1 d.alphabet = {'p', 'b'} d.add_edge(s1, s2, 'p') d.add_edge(s2, s3, 'b') d.add_edge(s3, s1, 'p') return d
def test_add_state_exc(): d = dfa.DFA() s = state.State('s1', True, False) s_ninit = state.State('si', True, True) with pytest.raises(error.StateError): d.add_state(s) with pytest.raises(error.StateError): d.add_state(s_ninit)
def test_add_state(): d = dfa.DFA() print([s.label for s in d.states]) assert (len(d.states) == 1) s = state.State('s2', True, False) d.add_state(s) assert (len(d.states) == 2) assert (s in d.states)
def test_add_edge_exc(): d = dfa.DFA() s = state.State('s2', True, False) d.set_alphabet({'a'}) with pytest.raises(error.StateError): d.add_edge(d.initial_state, s, 'a') d.add_state(s) with pytest.raises(error.AlphabetError): d.add_edge(d.initial_state, s, 'b')
def test_text_file_output(): d = dfa.DFA() d.set_alphabet({'a'}) d.add_edge(d.initial_state, d.initial_state, 'a') output.to_file(d) with open(expected_txt_path, 'r') as f: assert (f.readline() == expected_string) #cleanup if os.path.exists(expected_txt_path): os.remove(expected_txt_path)
def two_state_dfa(): """ alphabet: {a} s1 -> a -> s2 s2 -> a -> s2 initial: s1 accepting: s2 """ d = dfa.DFA() d.states = set() d.initial_state = None s1 = state.State("s1", False, True) s2 = state.State("s2", True, False) d.add_state(s1) d.add_state(s2) d.initial_state = s1 d.set_alphabet({'a'}) d.add_edge(s1, s2, 'a') d.add_edge(s2, s2, 'a') return d
def five_state_dfa(): """ alphabet: {a, b, c} s1 -> a -> s2 s2 -> b -> s2 s2 -> a -> s3 s3 -> c -> s5 s5 -> b -> s5 s5 -> a -> s4 s4 -> a -> s5 initial: s1 accepting: s2, s5 """ d = dfa.DFA() d.states = set() d.initial_state = None s1 = state.State("s1", False, True) s2 = state.State("s2", True, False) s3 = state.State("s3", False, False) s4 = state.State("s4", False, False) s5 = state.State("s5", True, False) d.add_state(s1) d.add_state(s2) d.add_state(s3) d.add_state(s4) d.add_state(s5) d.initial_state = s1 d.set_alphabet({'a', 'b', 'c'}) d.add_edge(s1, s2, 'a') d.add_edge(s2, s2, 'b') d.add_edge(s2, s3, 'a') d.add_edge(s3, s5, 'c') d.add_edge(s5, s5, 'b') d.add_edge(s5, s4, 'a') d.add_edge(s4, s5, 'a') return d
def test_set_alphabet(): d = dfa.DFA() alphabet = {'a', 'b'} d.set_alphabet(alphabet) assert (d.alphabet == {'a', 'b'})
def test_text_output(): d = dfa.DFA() d.set_alphabet({'a'}) d.add_edge(d.initial_state, d.initial_state, 'a') assert (output.to_string(d) == expected_string)