-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bb.py
94 lines (79 loc) · 2.78 KB
/
test_bb.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
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
import busybeaver as bb
import pickle
import unittest
class Tape(unittest.TestCase):
def test_initial_conditions(self):
t = bb.Tape()
self.assertEqual(t.position, 0)
self.assertEqual(t.leftmost, 0)
self.assertEqual(t.rightmost, 0)
self.assertEqual(t.shifts, 0)
self.assertEqual(t.read(), 0)
t = bb.Tape(position=10, default_factory=lambda: 3)
self.assertEqual(t.position, 10)
self.assertEqual(t.leftmost, 0)
self.assertEqual(t.rightmost, 10)
self.assertEqual(t.shifts, 0)
self.assertEqual(t.read(), 3)
t = bb.Tape(position=-10, default_factory=lambda: -4)
self.assertEqual(t.position, -10)
self.assertEqual(t.leftmost, -10)
self.assertEqual(t.rightmost, 0)
self.assertEqual(t.shifts, 0)
self.assertEqual(t.read(), -4)
def test_read_write(self):
t = bb.Tape(default_factory=lambda: "default")
t.write("one")
self.assertEqual(t.read(), "one")
t.left()
self.assertEqual(t.position, -1)
self.assertEqual(t.shifts, 1)
self.assertEqual(t.leftmost, -1)
self.assertEqual(t.rightmost, 0)
self.assertEqual(t.read(), "default")
t.write("minus1")
self.assertEqual(t.read(), "minus1")
t.left()
t.write("minus2")
self.assertEqual(t.read(), "minus2")
t.right()
self.assertEqual(t.read(), "minus1")
t.right()
self.assertEqual(t.read(), "one")
t.right()
self.assertEqual(t.read(), "default")
t.position -= 3
self.assertEqual(t.position, -2)
self.assertEqual(t.read(), "minus2")
self.assertEqual(t.leftmost, -2)
self.assertEqual(t.rightmost, 1)
self.assertEqual(t.shifts, 8)
self.assertEqual(list(t.values()),
["minus2", "minus1", "one", "default"])
self.assertEqual(str(t), "minus2 minus1 one default")
def test_shifts(self):
t = bb.Tape(position=-2)
self.assertEqual(t.shifts, 0)
t.position -= 3
self.assertEqual(t.shifts, 3)
t = bb.Tape(position=1)
self.assertEqual(t.shifts, 0)
t.position = 10
self.assertEqual(t.shifts, 9)
self.assertEqual(t.leftmost, 0)
self.assertEqual(t.rightmost, 10)
t = bb.Tape(position=-3)
self.assertEqual(t.shifts, 0)
t.position = 4
self.assertEqual(t.shifts, 7)
self.assertEqual(t.leftmost, -3)
self.assertEqual(t.rightmost, 4)
class TuringMachine(unittest.TestCase):
pass
class BusyBeaver(unittest.TestCase):
def test_pickle(self):
b = bb.BusyBeaver({})
p = pickle.dumps(b)
self.assertEqual(b, pickle.loads(p))
if __name__ == "__main__":
unittest.main()