Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加单元测试 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
37 changes: 37 additions & 0 deletions tests/test_automata.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from algorithms.automata import DFA


import unittest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Duplicate import statement for 'unittest'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Duplicate import statement for 'unittest'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Duplicate import statement for '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']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The 'setUp' method initializes 'self.transitions', 'self.start', and 'self.final' for a specific DFA configuration, which might not be reused effectively across different test methods if they require different DFA configurations.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The 'setUp' method defines a DFA that does not match the tests. The tests use different transition tables.


def test_DFA(self):
transitions = {
'a': {'1': 'a', '0': 'b'},
Expand Down Expand Up @@ -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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Misleading variable name 'str' which shadows a built-in name.
  • Suggestions:
    test_str = 'aab'
    

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Misleading variable name 'str' which shadows a built-in name.
  • Suggestions:
    test_str = 'aab'
    

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The 'test_empty_string_with_final_start' method uses a variable 'str' which is a built-in name. Consider renaming it to avoid shadowing built-in functions.
  • Suggestions:
    test_str = 'aab'
    

start_final = ['q0']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The test 'test_empty_string_with_final_start' modifies 'self.start' to 'q0' which is already the initial state. This test might not be testing a different scenario as intended.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Incorrect setup for 'test_empty_string_with_final_start'. 'start_final' should be the final state, not the start state.
  • Suggestions:
    self.final = ['q0']
    

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The 'setUp' method initializes 'self.final' as ['q3'], but 'test_empty_string_with_final_start' incorrectly attempts to use 'start_final' as the final states list.

result = DFA(self.transitions, self.start, start_final, '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • Incorrect use of 'start_final' as both the start state and final states list in 'test_empty_string_with_final_start'.
  • Suggestions:
    result = DFA(self.transitions, 'q0', ['q0'], '')
    

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comments:
    • The 'test_empty_string_with_final_start' method assigns 'start_final = ['q0']' but uses 'self.start' in the DFA call. This seems to be a logical mistake.
  • Suggestions:
    result = DFA(self.transitions, start_final, self.final, '')
    

self.assertTrue(result)


if __name__ == '__main__':
unittest.main()