Ver código fonte

Add tests for turmit methods + add new RpnSymbol + some tests

Yann Weber 6 anos atrás
pai
commit
54f31ee4a6
3 arquivos alterados com 168 adições e 9 exclusões
  1. 32
    0
      gte/turmit.py
  2. 15
    0
      tests/test_rpnsymbol.py
  3. 121
    9
      tests/test_turmit.py

+ 32
- 0
gte/turmit.py Ver arquivo

@@ -2,6 +2,7 @@ import collections
2 2
 import inspect
3 3
 
4 4
 _op_list = collections.OrderedDict()
5
+_var_list = collections.OrderedDict()
5 6
 
6 7
 def RpnOp(method):
7 8
     ''' @brief Decorator for RPN operation that autodetect argument count
@@ -20,13 +21,42 @@ def RpnOp(method):
20 21
             return None
21 22
         try:
22 23
             for topush in res:
24
+                if not isinstance(topush, int):
25
+                    raise ValueError('Turmit.%s() returned a list containing a\
26
+ %s : %s' % (method.__name__, type(topush), topush))
23 27
                 self._push(topush)
24 28
         except TypeError:
29
+            if not isinstance(res, int):
30
+                raise ValueError('Turmit.%s() returned a list containing a\
31
+%s : %s' % (method.__name__, type(res), res))
25 32
             self._push(res)
26 33
         return res
27 34
     _op_list[method.__name__] = (method, wrapped)
28 35
     return wrapped
29 36
 
37
+class RpnSymbol(object):
38
+    ''' @brief Designed to handle operation and operand for Turmit expr '''
39
+
40
+    OPERATION = 0x0
41
+    VALUE = 0x1
42
+    VARIABLE = 0x3
43
+
44
+
45
+    def __init__(self, value, optype = VALUE):
46
+        self.optype = optype
47
+        self.value = value
48
+        if optype == self.OPERATION:
49
+            self.value = list(_op_list.keys())[value % len(_op_list)]
50
+        elif optype == self.VARIABLE:
51
+            self.value %= len(_var_list)
52
+
53
+    def __str__(self):
54
+        if self.optype == self.OPERATION:
55
+            return _op_list[self.value][0].__name__.upper()
56
+        elif self.optype == self.VALUE:
57
+            return '0x%04X' % self.value
58
+        else:
59
+            return _var_list[self.value]
30 60
 
31 61
 class Turmit(object):
32 62
     ''' @brief Represent a turmit that act given an RPN expression with an
@@ -38,6 +68,8 @@ class Turmit(object):
38 68
         self._stack = [ 0 for _ in range(stack_size)]
39 69
         ## @brief Stack head index
40 70
         self._cur = stack_size - 1
71
+        ## @brief Stores turmit direction
72
+        self._dir = 0
41 73
 
42 74
     @property
43 75
     def shead(self):

+ 15
- 0
tests/test_rpnsymbol.py Ver arquivo

@@ -0,0 +1,15 @@
1
+import unittest
2
+
3
+from gte.turmit import RpnSymbol, _op_list
4
+
5
+class RpnSymbolTestCase(unittest.TestCase):
6
+    
7
+    def test_operations(self):
8
+        add_idx = list(_op_list.keys()).index('add')
9
+
10
+        sym = RpnSymbol(add_idx, RpnSymbol.OPERATION)
11
+        self.assertEqual(str(sym), 'ADD')
12
+
13
+    def test_value(self):
14
+        sym = RpnSymbol(42, RpnSymbol.VALUE)
15
+        self.assertEqual(str(sym), '0x002A')

+ 121
- 9
tests/test_turmit.py Ver arquivo

@@ -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)

Carregando…
Cancelar
Salvar