|
@@ -1,7 +1,7 @@
|
1
|
1
|
import unittest
|
2
|
|
-from unittest import skip
|
|
2
|
+import inspect
|
3
|
3
|
|
4
|
|
-from gte.turmit import Turmit
|
|
4
|
+from gte.turmit import Turmit, _op_list
|
5
|
5
|
|
6
|
6
|
class TurmitTestCase(unittest.TestCase):
|
7
|
7
|
|
|
@@ -20,10 +20,19 @@ class TurmitTestCase(unittest.TestCase):
|
20
|
20
|
t._push(2)
|
21
|
21
|
self.assertEqual(t.shead, 2)
|
22
|
22
|
|
|
23
|
+ def _rpn(self, rpn, narg = 2):
|
|
24
|
+ if rpn not in _op_list:
|
|
25
|
+ raise unittest.SkipTest('"%s" not implemented' % rpn)
|
|
26
|
+ method, wrapped = _op_list[rpn]
|
|
27
|
+ if narg != len(inspect.signature(method).parameters) - 1:
|
|
28
|
+ self.fail('Expected %s to take %d arguments, but Turmit.%s() takes \
|
|
29
|
+%d arguments' % (rpn, narg, rpn, len(inspect.signature(method).parameters) - 1))
|
|
30
|
+
|
23
|
31
|
def test_add(self):
|
24
|
32
|
''' Test turmit add method '''
|
25
|
|
- t = Turmit()
|
|
33
|
+ self._rpn('add')
|
26
|
34
|
|
|
35
|
+ t = Turmit()
|
27
|
36
|
t._push(42)
|
28
|
37
|
t._push(8)
|
29
|
38
|
t.add()
|
|
@@ -43,6 +52,8 @@ class TurmitTestCase(unittest.TestCase):
|
43
|
52
|
|
44
|
53
|
def test_mem_sz(self):
|
45
|
54
|
''' Test turmit mem_sz() operation '''
|
|
55
|
+ self._rpn('mem_sz', 1)
|
|
56
|
+
|
46
|
57
|
t = Turmit(8)
|
47
|
58
|
self.assertEqual(len(t._stack), 8)
|
48
|
59
|
|
|
@@ -57,10 +68,10 @@ class TurmitTestCase(unittest.TestCase):
|
57
|
68
|
self.assertEqual(t._cur, 0)
|
58
|
69
|
self.assertEqual(t.shead, 42)
|
59
|
70
|
|
60
|
|
-
|
61
|
|
- @skip('Need implementation')
|
62
|
71
|
def test_sub(self):
|
63
|
72
|
''' Test turmit sub() method '''
|
|
73
|
+ self._rpn('sub')
|
|
74
|
+
|
64
|
75
|
t = Turmit()
|
65
|
76
|
|
66
|
77
|
t._push(50)
|
|
@@ -80,9 +91,10 @@ class TurmitTestCase(unittest.TestCase):
|
80
|
91
|
self.assertEqual(t._cur, 0)
|
81
|
92
|
self.assertEqual(t.shead, 0)
|
82
|
93
|
|
83
|
|
- @skip('Need implementation')
|
84
|
94
|
def test_mul(self):
|
85
|
95
|
''' Test turmit mul() method '''
|
|
96
|
+ self._rpn('mul')
|
|
97
|
+
|
86
|
98
|
t = Turmit()
|
87
|
99
|
|
88
|
100
|
t._push(7)
|
|
@@ -102,9 +114,10 @@ class TurmitTestCase(unittest.TestCase):
|
102
|
114
|
self.assertEqual(t._cur, 0)
|
103
|
115
|
self.assertEqual(t.shead, 42 *42)
|
104
|
116
|
|
105
|
|
- @skip('Need implementation')
|
106
|
117
|
def test_mod(self):
|
107
|
118
|
''' Test turmit mod() method '''
|
|
119
|
+ self._rpn('mod')
|
|
120
|
+
|
108
|
121
|
t = Turmit()
|
109
|
122
|
t._push(42)
|
110
|
123
|
t._push(4)
|
|
@@ -113,11 +126,110 @@ class TurmitTestCase(unittest.TestCase):
|
113
|
126
|
self.assertEqual(t._cur, 0)
|
114
|
127
|
self.assertEqual(t.shead, 2)
|
115
|
128
|
|
116
|
|
- @skip('Need implementation')
|
|
129
|
+ def test_pow(self):
|
|
130
|
+ ''' Test turmit pow() method '''
|
|
131
|
+ self._rpn('pow')
|
|
132
|
+
|
|
133
|
+ t = Turmit()
|
|
134
|
+ t._push(2)
|
|
135
|
+ t._push(4)
|
|
136
|
+
|
|
137
|
+ t.pow()
|
|
138
|
+ self.assertEqual(t._cur, 0)
|
|
139
|
+ self.assertEqual(t.shead, 16)
|
|
140
|
+
|
|
141
|
+ def test_sqrt(self):
|
|
142
|
+ ''' Test turmit sqrt() method '''
|
|
143
|
+ self._rpn('sqrt', 1)
|
|
144
|
+
|
|
145
|
+ t = Turmit()
|
|
146
|
+ t._push(42)
|
|
147
|
+
|
|
148
|
+ t.sqrt()
|
|
149
|
+
|
|
150
|
+ self.assertEqual(t._cur, 0)
|
|
151
|
+ self.assertEqual(t.shead, 6)
|
|
152
|
+
|
|
153
|
+ def test_and(self):
|
|
154
|
+ ''' Test turmit binary and() method '''
|
|
155
|
+ self._rpn('and')
|
|
156
|
+
|
|
157
|
+ t = Turmit()
|
|
158
|
+ t._push(42)
|
|
159
|
+ t._push(10)
|
|
160
|
+
|
|
161
|
+ t.bin_and()
|
|
162
|
+
|
|
163
|
+ self.assertEqual(t._cur, 0)
|
|
164
|
+ self.assertEqual(t.shead, 10)
|
|
165
|
+
|
|
166
|
+ def test_or(self):
|
|
167
|
+ ''' Test turmit binary or() method '''
|
|
168
|
+ self._rpn('or')
|
|
169
|
+
|
|
170
|
+ t = Turmit()
|
|
171
|
+ t._push(42)
|
|
172
|
+ t._push(7)
|
|
173
|
+
|
|
174
|
+ t.bin_or()
|
|
175
|
+
|
|
176
|
+ self.assertEqual(t._cur, 0)
|
|
177
|
+ self.assertEqual(t.shead, 47)
|
|
178
|
+
|
|
179
|
+ def test_xor(self):
|
|
180
|
+ ''' Test turmit binary xor() method '''
|
|
181
|
+ self._rpn('xor')
|
|
182
|
+
|
|
183
|
+ t = Turmit()
|
|
184
|
+ t._push(42)
|
|
185
|
+ t._push(11)
|
|
186
|
+
|
|
187
|
+ t.bin_xor()
|
|
188
|
+ self.assertEqual(t._cur, 0)
|
|
189
|
+ self.assertEqual(t.shead, 33)
|
|
190
|
+
|
|
191
|
+ def test_rshift(self):
|
|
192
|
+ ''' Test turmit rshift method '''
|
|
193
|
+ self._rpn('rshift')
|
|
194
|
+
|
|
195
|
+ t = Turmit()
|
|
196
|
+ t._push(42)
|
|
197
|
+ t._push(2)
|
|
198
|
+
|
|
199
|
+ t.rshift()
|
|
200
|
+ self.assertEqual(t._cur, 0)
|
|
201
|
+ self.assertEqual(t.shead, 10)
|
|
202
|
+
|
|
203
|
+ def test_lshift(self):
|
|
204
|
+ ''' Test turmit lshift method '''
|
|
205
|
+ self._rpn('lshift')
|
|
206
|
+
|
|
207
|
+ t = Turmit()
|
|
208
|
+ t._push(10)
|
|
209
|
+ t._push(2)
|
|
210
|
+ t.lshift()
|
|
211
|
+ self.assertEqual(t._cur, 0)
|
|
212
|
+ self.assertEqual(t.shead, 40)
|
|
213
|
+
|
117
|
214
|
def test_dup(self):
|
118
|
215
|
''' Test turmit dup() method '''
|
|
216
|
+ self._rpn('dup')
|
|
217
|
+
|
119
|
218
|
t = Turmit()
|
120
|
219
|
t._push(42)
|
121
|
220
|
t.dup()
|
122
|
221
|
self.assertEqual(t._cur, 1)
|
123
|
|
- sefl.assertEqual(t.shead, 42)
|
|
222
|
+ self.assertEqual(t.shead, 42)
|
|
223
|
+
|
|
224
|
+ def test_pop(self):
|
|
225
|
+ ''' Test turmit pop() method '''
|
|
226
|
+ self._rpn('pop', 0)
|
|
227
|
+
|
|
228
|
+ t = Turmit()
|
|
229
|
+ t._push(10)
|
|
230
|
+ t._push(2)
|
|
231
|
+ t.pop()
|
|
232
|
+ self.assertEqual(t._cur, 0)
|
|
233
|
+ self.asertEqual(t.shead, 10)
|
|
234
|
+ t.pop()
|
|
235
|
+ self.assertEqual(t._cur, len(t._stack) - 1)
|