diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/tests/test_automata.py b/tests/test_automata.py index dbd766a..dff027b 100644 --- a/tests/test_automata.py +++ b/tests/test_automata.py @@ -1,10 +1,22 @@ from algorithms.automata import DFA +import unittest import unittest class TestDFA(unittest.TestCase): + def setUp(self): + # DFA transitions table example + self.transitions = { + 'q0': {'a': 'q1', 'b': None}, + 'q1': {'a': 'q1', 'b': 'q2'}, + 'q2': {'a': 'q1', 'b': 'q3'}, + 'q3': {'a': None, 'b': None} + } + self.start = 'q0' + self.final = ['q3'] + def test_DFA(self): transitions = { 'a': {'1': 'a', '0': 'b'}, @@ -43,6 +55,31 @@ def test_DFA(self): self.assertEqual(False, DFA(transitions2, start2, final2, "aaabbb")) self.assertEqual(True, DFA(transitions2, start2, final2, "baabba")) + def test_reject_string(self): + # Test case where DFA should reject the string + string = 'aaa' + result = DFA(self.transitions, self.start, self.final, string) + self.assertFalse(result) + + def test_invalid_transition(self): + # Test case where DFA encounters an invalid transition + string = 'b' + result = DFA(self.transitions, self.start, self.final, string) + self.assertFalse(result) + + def test_empty_string(self): + # Test case where the string is empty, start state is not a final state + string = '' + result = DFA(self.transitions, self.start, self.final, string) + self.assertFalse(result) + + def test_empty_string_with_final_start(self): + # Test case where the string is empty and start state is a final state + str = 'aab' + start_final = ['q0'] + result = DFA(self.transitions, self.start, start_final, '') + self.assertTrue(result) + if __name__ == '__main__': unittest.main()