|
@@ -1,8 +1,294 @@
|
1
|
1
|
import unittest
|
|
2
|
+import inspect
|
2
|
3
|
|
3
|
|
-import cturmit
|
|
4
|
+from cturmit import CTurmit as Turmit
|
|
5
|
+from gte.rpnlib import _op_list
|
4
|
6
|
|
5
|
|
-class CTurmitTestCase(unittest.TestCase):
|
|
7
|
+class TurmitTestCase(unittest.TestCase):
|
6
|
8
|
|
7
|
9
|
def test_init(self):
|
8
|
|
- ct = cturmit.CTurmit()
|
|
10
|
+ ''' Test Turmit class __init__ '''
|
|
11
|
+ t = Turmit(42)
|
|
12
|
+ self.assertEqual(len(t._stack), 42)
|
|
13
|
+ self.assertEqual(t.shead, 0)
|
|
14
|
+ self.assertEqual(len(t._stack) - 1 , t._cur)
|
|
15
|
+
|
|
16
|
+ def test_init_args(self):
|
|
17
|
+ ''' Test Turmit __init__ arguments '''
|
|
18
|
+ t = Turmit()
|
|
19
|
+ t = Turmit(prog_size = 42)
|
|
20
|
+ t = Turmit(prog = 'G R B 44 MOD ADD 43 MOD ADD 42 MOD')
|
|
21
|
+ t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
|
|
22
|
+ signed_int = False, stack_size=42)
|
|
23
|
+
|
|
24
|
+ def test_call(self):
|
|
25
|
+ ''' Test Turmit.__call__ '''
|
|
26
|
+ t = Turmit(prog = 'G R B ADD ADD 42 MOD', max_int = 0xFFFF,
|
|
27
|
+ signed_int = False, stack_size=42)
|
|
28
|
+ r = t(x=1, y=2, r=255, g=255, b=255)
|
|
29
|
+ self.assertEqual(r, 9)
|
|
30
|
+ r = t(x=2, y=2, r=255, g=0, b=0)
|
|
31
|
+ self.assertEqual(r, 3)
|
|
32
|
+
|
|
33
|
+ def test_init_badargs(self):
|
|
34
|
+ ''' Test Turmit __init__ bad arguments '''
|
|
35
|
+ with self.assertRaises(RuntimeError):
|
|
36
|
+ t = Turmit(foobar=42)
|
|
37
|
+ with self.assertRaises(ValueError):
|
|
38
|
+ t = Turmit(-1)
|
|
39
|
+ with self.assertRaises(ValueError):
|
|
40
|
+ t = Turmit(max_int = -1)
|
|
41
|
+ with self.assertRaises(ValueError):
|
|
42
|
+ t = Turmit(prog='foobar')
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+class TurmitOperationTestCase(unittest.TestCase):
|
|
47
|
+
|
|
48
|
+ def test_push(self):
|
|
49
|
+ ''' Test Turmit _push() method '''
|
|
50
|
+ t = Turmit()
|
|
51
|
+ t._push(1)
|
|
52
|
+ self.assertEqual(t.shead, 1)
|
|
53
|
+ t._push(2)
|
|
54
|
+ self.assertEqual(t.shead, 2)
|
|
55
|
+
|
|
56
|
+ def _rpn(self, rpn, narg = 2):
|
|
57
|
+ if rpn not in _op_list:
|
|
58
|
+ raise unittest.SkipTest('"%s" not implemented' % rpn)
|
|
59
|
+ method, wrapped = _op_list[rpn]
|
|
60
|
+ if narg != len(inspect.signature(method).parameters) - 1:
|
|
61
|
+ self.fail('Expected %s to take %d arguments, but Turmit.%s() takes \
|
|
62
|
+%d arguments' % (rpn, narg, rpn, len(inspect.signature(method).parameters) - 1))
|
|
63
|
+
|
|
64
|
+ def test_add(self):
|
|
65
|
+ ''' Test turmit add method '''
|
|
66
|
+ self._rpn('add')
|
|
67
|
+
|
|
68
|
+ t = Turmit()
|
|
69
|
+ t._push(42)
|
|
70
|
+ t._push(8)
|
|
71
|
+ t.add()
|
|
72
|
+ self.assertEqual(t._cur, 0)
|
|
73
|
+ self.assertEqual(t.shead, 50)
|
|
74
|
+
|
|
75
|
+ t._push(42)
|
|
76
|
+ t._push(8)
|
|
77
|
+ t.add()
|
|
78
|
+ self.assertEqual(t._cur, 1)
|
|
79
|
+ self.assertEqual(t.shead, 50)
|
|
80
|
+
|
|
81
|
+ t.add()
|
|
82
|
+
|
|
83
|
+ self.assertEqual(t._cur, 0)
|
|
84
|
+ self.assertEqual(t.shead, 100)
|
|
85
|
+
|
|
86
|
+ def test_mem_sz(self):
|
|
87
|
+ ''' Test turmit mem_sz() operation '''
|
|
88
|
+ self._rpn('mem_sz', 1)
|
|
89
|
+
|
|
90
|
+ t = Turmit(8)
|
|
91
|
+ self.assertEqual(len(t._stack), 8)
|
|
92
|
+
|
|
93
|
+ t._push(42)
|
|
94
|
+ t.mem_sz()
|
|
95
|
+ self.assertEqual(len(t._stack), 42)
|
|
96
|
+ self.assertEqual(t._cur, 7)
|
|
97
|
+
|
|
98
|
+ t._push(42)
|
|
99
|
+ t._push(8)
|
|
100
|
+ t.mem_sz()
|
|
101
|
+ self.assertEqual(t._cur, 0)
|
|
102
|
+ self.assertEqual(t.shead, 42)
|
|
103
|
+
|
|
104
|
+ def test_sub(self):
|
|
105
|
+ ''' Test turmit sub() method '''
|
|
106
|
+ self._rpn('sub')
|
|
107
|
+
|
|
108
|
+ t = Turmit()
|
|
109
|
+
|
|
110
|
+ t._push(50)
|
|
111
|
+ t._push(8)
|
|
112
|
+
|
|
113
|
+ t.sub()
|
|
114
|
+ self.assertEqual(t._cur, 0)
|
|
115
|
+ self.assertEqual(t.shead, 42)
|
|
116
|
+
|
|
117
|
+ t._push(50)
|
|
118
|
+ t._push(8)
|
|
119
|
+ t.sub()
|
|
120
|
+ self.assertEqual(t._cur, 1)
|
|
121
|
+ self.assertEqual(t.shead, 42)
|
|
122
|
+
|
|
123
|
+ t.sub()
|
|
124
|
+ self.assertEqual(t._cur, 0)
|
|
125
|
+ self.assertEqual(t.shead, 0)
|
|
126
|
+
|
|
127
|
+ def test_mul(self):
|
|
128
|
+ ''' Test turmit mul() method '''
|
|
129
|
+ self._rpn('mul')
|
|
130
|
+
|
|
131
|
+ t = Turmit()
|
|
132
|
+
|
|
133
|
+ t._push(7)
|
|
134
|
+ t._push(6)
|
|
135
|
+
|
|
136
|
+ t.mul()
|
|
137
|
+ self.assertEqual(t._cur, 0)
|
|
138
|
+ self.assertEqual(t.shead, 42)
|
|
139
|
+
|
|
140
|
+ t._push(7)
|
|
141
|
+ t._push(6)
|
|
142
|
+ t.mul()
|
|
143
|
+ self.assertEqual(t._cur, 1)
|
|
144
|
+ self.assertEqual(t.shead, 42)
|
|
145
|
+
|
|
146
|
+ t.mul()
|
|
147
|
+ self.assertEqual(t._cur, 0)
|
|
148
|
+ self.assertEqual(t.shead, 42 *42)
|
|
149
|
+
|
|
150
|
+ def test_div(self):
|
|
151
|
+ ''' Test turmit division capabilities '''
|
|
152
|
+ self._rpn('div')
|
|
153
|
+
|
|
154
|
+ t = Turmit()
|
|
155
|
+
|
|
156
|
+ t._push(43)
|
|
157
|
+ t._push(7)
|
|
158
|
+
|
|
159
|
+ t.div()
|
|
160
|
+ self.assertEqual(t._cur, 0)
|
|
161
|
+ self.assertEqual(t.shead, 6)
|
|
162
|
+
|
|
163
|
+ def test_mod(self):
|
|
164
|
+ ''' Test turmit mod() method '''
|
|
165
|
+ self._rpn('mod')
|
|
166
|
+
|
|
167
|
+ t = Turmit()
|
|
168
|
+ t._push(42)
|
|
169
|
+ t._push(4)
|
|
170
|
+
|
|
171
|
+ t.mod()
|
|
172
|
+ self.assertEqual(t._cur, 0)
|
|
173
|
+ self.assertEqual(t.shead, 2)
|
|
174
|
+
|
|
175
|
+ def test_pow(self):
|
|
176
|
+ ''' Test turmit pow() method '''
|
|
177
|
+ self._rpn('pow')
|
|
178
|
+
|
|
179
|
+ t = Turmit()
|
|
180
|
+ t._push(2)
|
|
181
|
+ t._push(4)
|
|
182
|
+
|
|
183
|
+ t.pow()
|
|
184
|
+ self.assertEqual(t._cur, 0)
|
|
185
|
+ self.assertEqual(t.shead, 16)
|
|
186
|
+
|
|
187
|
+ def test_sqrt(self):
|
|
188
|
+ ''' Test turmit sqrt() method '''
|
|
189
|
+ self._rpn('sqrt', 1)
|
|
190
|
+
|
|
191
|
+ t = Turmit()
|
|
192
|
+ t._push(42)
|
|
193
|
+
|
|
194
|
+ t.sqrt()
|
|
195
|
+
|
|
196
|
+ self.assertEqual(t._cur, 0)
|
|
197
|
+ self.assertEqual(t.shead, 6)
|
|
198
|
+
|
|
199
|
+ def test_and(self):
|
|
200
|
+ ''' Test turmit binary and() method '''
|
|
201
|
+ self._rpn('bin_and')
|
|
202
|
+
|
|
203
|
+ t = Turmit()
|
|
204
|
+ t._push(10)
|
|
205
|
+ t._push(42)
|
|
206
|
+
|
|
207
|
+ t.bin_and()
|
|
208
|
+
|
|
209
|
+ self.assertEqual(t._cur, 0)
|
|
210
|
+ self.assertEqual(t.shead, 10)
|
|
211
|
+
|
|
212
|
+ def test_or(self):
|
|
213
|
+ ''' Test turmit binary or() method '''
|
|
214
|
+ self._rpn('bin_or')
|
|
215
|
+
|
|
216
|
+ t = Turmit()
|
|
217
|
+ t._push(42)
|
|
218
|
+ t._push(7)
|
|
219
|
+
|
|
220
|
+ t.bin_or()
|
|
221
|
+
|
|
222
|
+ self.assertEqual(t._cur, 0)
|
|
223
|
+ self.assertEqual(t.shead, 47)
|
|
224
|
+
|
|
225
|
+ def test_xor(self):
|
|
226
|
+ ''' Test turmit binary xor() method '''
|
|
227
|
+ self._rpn('bin_xor')
|
|
228
|
+
|
|
229
|
+ t = Turmit()
|
|
230
|
+ t._push(42)
|
|
231
|
+ t._push(11)
|
|
232
|
+
|
|
233
|
+ t.bin_xor()
|
|
234
|
+ self.assertEqual(t._cur, 0)
|
|
235
|
+ self.assertEqual(t.shead, 33)
|
|
236
|
+
|
|
237
|
+ def test_rshift(self):
|
|
238
|
+ ''' Test turmit rshift method '''
|
|
239
|
+ self._rpn('rshift')
|
|
240
|
+
|
|
241
|
+ t = Turmit()
|
|
242
|
+ t._push(42)
|
|
243
|
+ t._push(2)
|
|
244
|
+
|
|
245
|
+ t.rshift()
|
|
246
|
+ self.assertEqual(t._cur, 0)
|
|
247
|
+ self.assertEqual(t.shead, 10)
|
|
248
|
+
|
|
249
|
+ def test_lshift(self):
|
|
250
|
+ ''' Test turmit lshift method '''
|
|
251
|
+ self._rpn('lshift')
|
|
252
|
+
|
|
253
|
+ t = Turmit()
|
|
254
|
+ t._push(10)
|
|
255
|
+ t._push(2)
|
|
256
|
+ t.lshift()
|
|
257
|
+ self.assertEqual(t._cur, 0)
|
|
258
|
+ self.assertEqual(t.shead, 40)
|
|
259
|
+
|
|
260
|
+ def test_dup(self):
|
|
261
|
+ ''' Test turmit dup() method '''
|
|
262
|
+ self._rpn('dup', 1)
|
|
263
|
+
|
|
264
|
+ t = Turmit()
|
|
265
|
+ t._push(42)
|
|
266
|
+ t.dup()
|
|
267
|
+ self.assertEqual(t._cur, 1)
|
|
268
|
+ self.assertEqual(t.shead, 42)
|
|
269
|
+
|
|
270
|
+ def test_pop(self):
|
|
271
|
+ ''' Test turmit pop() method '''
|
|
272
|
+ self._rpn('pop', 1)
|
|
273
|
+
|
|
274
|
+ t = Turmit()
|
|
275
|
+ t._push(10)
|
|
276
|
+ t._push(2)
|
|
277
|
+ t.pop()
|
|
278
|
+ self.assertEqual(t._cur, 0)
|
|
279
|
+ self.assertEqual(t.shead, 10)
|
|
280
|
+ t.pop()
|
|
281
|
+ self.assertEqual(t._cur, len(t._stack) - 1)
|
|
282
|
+
|
|
283
|
+ def test_swp(self):
|
|
284
|
+ ''' Test turmit swp() method '''
|
|
285
|
+ self._rpn('swp', 2)
|
|
286
|
+
|
|
287
|
+ t = Turmit()
|
|
288
|
+ t._push(42)
|
|
289
|
+ t._push(1337)
|
|
290
|
+ t.swp()
|
|
291
|
+ self.assertEqual(t._cur, 1)
|
|
292
|
+ self.assertEqual(t.shead, 42)
|
|
293
|
+ t._pop()
|
|
294
|
+ self.assertEqual(t.shead, 1337)
|