From e66c07204323071c6ef28375bfb38d496d46bcf4 Mon Sep 17 00:00:00 2001 From: liuyong Date: Sun, 3 Nov 2024 17:43:52 +0800 Subject: [PATCH 1/2] fix --- .gitignore | 1 + tests/test_automata.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 .gitignore 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..0ffe536 100644 --- a/tests/test_automata.py +++ b/tests/test_automata.py @@ -5,6 +5,17 @@ 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 +54,29 @@ 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 + start_final = ['q0'] + result = DFA(self.transitions, self.start, start_final, '') + self.assertTrue(result) if __name__ == '__main__': unittest.main() From 356a77570ab6f7d33d9ea7811f34a8608f8c46be Mon Sep 17 00:00:00 2001 From: liuyong Date: Sun, 3 Nov 2024 17:48:05 +0800 Subject: [PATCH 2/2] fix --- tests/test_automata.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_automata.py b/tests/test_automata.py index 0ffe536..dff027b 100644 --- a/tests/test_automata.py +++ b/tests/test_automata.py @@ -1,6 +1,7 @@ from algorithms.automata import DFA +import unittest import unittest @@ -74,9 +75,11 @@ def test_empty_string(self): 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()