def test_even_length_dfa(self): even_length_dfa = Dfa(2, get_alphabet(), 0, 0) even_length_dfa.add_transition(0, 1, '0') even_length_dfa.add_transition(0, 1, '1') even_length_dfa.add_transition(1, 0, '0') even_length_dfa.add_transition(1, 0, '1') for i in range(1000): s: str = get_random_string() self.assertEqual(not bool(len(s) % 2), even_length_dfa.accepts(s)) self.assertEqual(True, even_length_dfa.accepts(''))
def test_odd_length_dfa(self): odd_length_dfa = Dfa(2, get_alphabet(), 0, 1) odd_length_dfa.add_transition(0, 1, '0') odd_length_dfa.add_transition(0, 1, '1') odd_length_dfa.add_transition(1, 0, '0') odd_length_dfa.add_transition(1, 0, '1') for i in range(1000): s: str = get_random_string() self.assertEqual(bool(len(s) % 2), odd_length_dfa.accepts(s)) self.assertEqual(False, odd_length_dfa.accepts(''))
def test_empty_dfa(self): d = Dfa(1, get_alphabet(), 0) d.add_transition(0, 0, '0') d.add_transition(0, 0, '1') for i in range(1000): self.assertEqual(False, d.accepts(get_random_string())) self.assertEqual(False, d.accepts(''))
def test_odd_0s(self): d = Dfa(2, get_alphabet(), 0, 1) d.add_transition(0, 0, '1') d.add_transition(0, 1, '0') d.add_transition(1, 0, '0') d.add_transition(1, 1, '1') for i in range(1000): s: str = get_random_string() j: int = 0 for symbol in s: if symbol == '0': j += 1 self.assertEqual(bool(j % 2), d.accepts(s))
def test_three0_in_row(self): d = Dfa(4, get_alphabet(), 0, 3) d.add_transition(0, 0, '1') d.add_transition(0, 1, '0') d.add_transition(1, 0, '1') d.add_transition(1, 2, '0') d.add_transition(2, 0, '1') d.add_transition(2, 3, '0') d.add_transition(3, 3, '0') d.add_transition(3, 3, '1') for i in range(1000): s: str = get_random_string() self.assertEqual('000' in s, d.accepts(s))
def test_even_0_or_len(self): d = Dfa(4, get_alphabet(), 0, 0, 1, 2) d.add_transition(0, 3, '0') d.add_transition(0, 1, '1') d.add_transition(1, 2, '0') d.add_transition(1, 0, '1') d.add_transition(2, 1, '0') d.add_transition(2, 3, '1') d.add_transition(3, 0, '0') d.add_transition(3, 2, '1') for i in range(1000): s: str = get_random_string() ctr: int = s.count('0') expected: bool = bool(not (ctr % 2)) or bool(not (len(s) % 2)) self.assertEqual(expected, d.accepts(s))