-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
增加单元测试 #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__pycache__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
from algorithms.automata import DFA | ||
|
||
|
||
import unittest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
start_final = ['q0'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
result = DFA(self.transitions, self.start, start_final, '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.assertTrue(result) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.