-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfa_test.py
57 lines (46 loc) · 1.74 KB
/
dfa_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
from dfa import Dfa
class DfaTest(unittest.TestCase):
def test_dfa_1(self):
transition_function = {
("A", "0"): "A",
("A", "1"): "B",
("B", "0"): "C",
("B", "1"): "D",
("C", "1"): "E",
("D", "0"): "E",
("E", "0"): "F",
("F", "1"): "G",
}
starting_state = "A"
final_states = {"G"}
dfa = Dfa(transition_function, starting_state, final_states)
self.assertTrue(dfa.is_string_accepted("0000011001"))
self.assertTrue(dfa.is_string_accepted("011001"))
self.assertFalse(dfa.is_string_accepted(""))
self.assertFalse(dfa.is_string_accepted("1"))
self.assertFalse(dfa.is_string_accepted("1011001"))
def test_dfa_2(self):
transition_function = {
("A", "0"): "B",
}
starting_state = "A"
final_states = {"B"}
dfa = Dfa(transition_function, starting_state, final_states)
self.assertTrue(dfa.is_string_accepted("0"))
self.assertFalse(dfa.is_string_accepted(""))
self.assertFalse(dfa.is_string_accepted("1"))
self.assertFalse(dfa.is_string_accepted("01"))
def test_dfa_3(self):
transition_function = {
("A", "0"): "A",
}
starting_state = "A"
final_states = {"A"}
dfa = Dfa(transition_function, starting_state, final_states)
self.assertTrue(dfa.is_string_accepted(""))
self.assertTrue(dfa.is_string_accepted("0"))
self.assertTrue(dfa.is_string_accepted("00"))
self.assertFalse(dfa.is_string_accepted("1"))
self.assertFalse(dfa.is_string_accepted("01"))
self.assertFalse(dfa.is_string_accepted("101"))