-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_turing_machine.py
More file actions
162 lines (129 loc) · 4.62 KB
/
test_turing_machine.py
File metadata and controls
162 lines (129 loc) · 4.62 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
import sys
import pytest
from turing_machine import TuringMachine
@pytest.fixture
def transitions():
return {
('q0', '#'): ('End', '#', 'R'),
('End', ''): ('qa', '', 'R'),
('q0', '0'): ('FindDelimiter0', 'X', 'R'),
('FindDelimiter0', '#'): ('Check0', '#', 'R'),
('Check0', '0'): ('FindLeftmost', 'X', 'L'),
('q0', '1'): ('FindDelimiter1', 'X', 'R'),
('FindDelimiter1', '#'): ('Check1', '#', 'R'),
('Check1', '1'): ('FindLeftmost', 'X', 'L'),
('FindLeftmost', '0'): ('FindLeftmost', '0', 'L'),
('FindLeftmost', '1'): ('FindLeftmost', '1', 'L'),
('FindLeftmost', 'X'): ('FindLeftmost', 'X', 'L'),
('FindLeftmost', '#'): ('FindLeftmost', '#', 'L'),
('FindLeftmost', ''): ('FindNext', '', 'R'),
('FindNext', 'X'): ('FindNext', 'X', 'R'),
('FindNext', '0'): ('FindDelimiter0', 'X', 'R'),
('FindNext', '1'): ('FindDelimiter1', 'X', 'R'),
('FindNext', '#'): ('End', '#', 'R'),
('FindDelimiter0', '0'): ('FindDelimiter0', '0', 'R'),
('FindDelimiter0', '1'): ('FindDelimiter0', '1', 'R'),
('FindDelimiter1', '0'): ('FindDelimiter1', '0', 'R'),
('FindDelimiter1', '1'): ('FindDelimiter1', '1', 'R'),
('Check0', 'X'): ('Check0', 'X', 'R'),
('Check1', 'X'): ('Check1', 'X', 'R'),
('End', 'X'): ('End', 'X', 'R')
}
@pytest.fixture
def machine(transitions):
return TuringMachine(transitions)
def test_accepts(machine):
assert machine.accepts('11110011001010#11110011001010', step_limit=1000)
assert machine.accepts('11110011001010#11110011001010', step_limit=2) is None
def test_rejects(machine):
assert machine.rejects('1000#10001')
@pytest.mark.parametrize(
'input_',
(
'',
[],
[''],
)
)
def test_empy_input(machine, input_):
assert machine.rejects(input_)
@pytest.mark.parametrize(
'transitions',
(
{('q0', ''): ('q0', '', 'L')},
)
)
def test_go_left(machine):
execution = machine.run('')
assert next(execution) == (
None,
{
'left_hand_side': [''],
'right_hand_side': [],
'state': 'q0',
'symbol': '',
},
)
for _ in range(3):
assert next(execution) == (
None,
{
'left_hand_side': [],
'right_hand_side': [''],
'state': 'q0',
'symbol': '',
},
)
@pytest.mark.parametrize(
('colored', 'begin_marker', 'end_marker'),
(
(False, '[', ']'),
(True, '\x1b[47;1m', '\x1b[0m'),
),
)
def test_debug(machine, capsys, begin_marker, end_marker, colored):
machine.debug('101X101', colored=colored)
out, err = capsys.readouterr()
assert not err
assert out == (
'q0 {b}1{e}01X101\n'
'FindDelimiter1 X{b}0{e}1X101\n'
'FindDelimiter1 X0{b}1{e}X101\n'
'FindDelimiter1 X01{b}X{e}101\n'
'qr X01X{b}1{e}01\n'
''.format(b=begin_marker, e=end_marker)
)
def test_scrip_w_hash_w(capsys):
if 'w_hash_w' in sys.modules:
del sys.modules['w_hash_w']
import w_hash_w
out, err = capsys.readouterr()
assert not err
assert out == (
'q0 [1]0#10\n'
'FindDelimiter1 X[0]#10\n'
'FindDelimiter1 X0[#]10\n'
'Check1 X0#[1]0\n'
'FindLeftmost X0[#]X0\n'
'FindLeftmost X[0]#X0\n'
'FindLeftmost [X]0#X0\n'
'FindLeftmost []X0#X0\n'
'FindNext [X]0#X0\n'
'FindNext X[0]#X0\n'
'FindDelimiter0 XX[#]X0\n'
'Check0 XX#[X]0\n'
'Check0 XX#X[0]\n'
'FindLeftmost XX#[X]X\n'
'FindLeftmost XX[#]XX\n'
'FindLeftmost X[X]#XX\n'
'FindLeftmost [X]X#XX\n'
'FindLeftmost []XX#XX\n'
'FindNext [X]X#XX\n'
'FindNext X[X]#XX\n'
'FindNext XX[#]XX\n'
'End XX#[X]X\n'
'End XX#X[X]\n'
'End XX#XX[]\n'
'qa XX#XX[]\n'
)