|
@@ -34,144 +34,6 @@ except (ImportError, NameError) as e:
|
34
|
34
|
file=sys.stderr)
|
35
|
35
|
raise e
|
36
|
36
|
|
37
|
|
-class Test0RpnModule(unittest.TestCase):
|
38
|
|
-
|
39
|
|
- def test_init(self):
|
40
|
|
- """ RPNExpr instanciation """
|
41
|
|
- expr = pyrpn.RPNExpr("42", 2)
|
42
|
|
-
|
43
|
|
- def test_init_badargs(self):
|
44
|
|
- """ RPNExpr instanciation with bad arguments """
|
45
|
|
- badargs = [('', 2), (), ('ab+',), ('ab+',300), (42, 42), ('ab', '42')]
|
46
|
|
- for badarg in badargs:
|
47
|
|
- with self.assertRaises((ValueError, TypeError)):
|
48
|
|
- expr = pyrpn.RPNExpr(*badarg)
|
49
|
|
-
|
50
|
|
- def test_init_loop(self):
|
51
|
|
- """ Testing pyrpn.RPNExpr multiple instanciation """
|
52
|
|
- exprs = []
|
53
|
|
- for i in range(256):
|
54
|
|
- expr = pyrpn.RPNExpr("42", 2)
|
55
|
|
-
|
56
|
|
- def test_init_args(self):
|
57
|
|
- """ Testing pyrpn.RPNExpr instanciation with arguments """
|
58
|
|
- for argc in range(0, 256):
|
59
|
|
- with self.subTest("RPNExpr('42', %d)" % argc):
|
60
|
|
- expr = pyrpn.RPNExpr("42", argc)
|
61
|
|
-
|
62
|
|
- def test_init_stack_sz(self):
|
63
|
|
- """ Instanciate RPNExpr specifiing a stack_size """
|
64
|
|
- for argc in range(0,256, 8):
|
65
|
|
- for sz in range(4, 256, 2):
|
66
|
|
- with self.subTest("RPNExpr('42', %d,%d)" % (argc, sz)):
|
67
|
|
- expr = pyrpn.RPNExpr("42", argc, sz)
|
68
|
|
-
|
69
|
|
- def test_op_dict(self):
|
70
|
|
- """ Testing RPNExpr.get_ops() method """
|
71
|
|
- known_ops = dict([
|
72
|
|
- ('add', '+'),
|
73
|
|
- ('sub', '-'),
|
74
|
|
- ('mul', '*'),
|
75
|
|
- ('div', '/'),
|
76
|
|
- ('mod', '%'),
|
77
|
|
- ('neg', '!'),
|
78
|
|
- ('not', '~'),
|
79
|
|
- ('and', '&'),
|
80
|
|
- ('or', '|'),
|
81
|
|
- ('xor', '^'),
|
82
|
|
- ('>>', 'r'),
|
83
|
|
- ('<<', 'l'),
|
84
|
|
- ('xchg', 'x'),
|
85
|
|
- ('dup', 'd'),
|
86
|
|
- ('pop', 'p'),
|
87
|
|
- ])
|
88
|
|
- for long, short in pyrpn.get_ops().items():
|
89
|
|
- self.assertIn(long, known_ops)
|
90
|
|
- self.assertEqual(short, known_ops[long])
|
91
|
|
-
|
92
|
|
- def test_rand_expr(self):
|
93
|
|
- """ Testing RPNExpr.random_expr() """
|
94
|
|
- result = {}
|
95
|
|
- counters = {'vals': 0, 'args': 0}
|
96
|
|
- all_count = 0
|
97
|
|
- for sz in range(1,64,3):
|
98
|
|
- for argc in range(1,128):
|
99
|
|
- rnd_expr = pyrpn.random_expr(argc, sz)
|
100
|
|
- #print(repr(rnd_expr))
|
101
|
|
- for tok in rnd_expr.split(' '):
|
102
|
|
- all_count += 1
|
103
|
|
- if len(tok) == 0:
|
104
|
|
- continue
|
105
|
|
- try:
|
106
|
|
- itok = int(tok, 0)
|
107
|
|
- counters['vals'] += 1
|
108
|
|
- continue
|
109
|
|
- except Exception:
|
110
|
|
- pass
|
111
|
|
- if tok[0] == 'A':
|
112
|
|
- counters['args'] += 1
|
113
|
|
- else:
|
114
|
|
- if tok not in counters:
|
115
|
|
- counters[tok] = 0
|
116
|
|
- counters[tok] += 1
|
117
|
|
- all_ops = len(pyrpn.get_ops()) + 2
|
118
|
|
- entropy = 1-sum([(n/all_count)**2
|
119
|
|
- for _, n in counters.items()])
|
120
|
|
- self.assertGreater(entropy, 1-(1/all_ops), "Low entropy !")
|
121
|
|
-
|
122
|
|
-
|
123
|
|
-class TestRpnCompile(unittest.TestCase):
|
124
|
|
-
|
125
|
|
- def test_basic(self):
|
126
|
|
- """ Compile 6 7 * """
|
127
|
|
- expr = pyrpn.RPNExpr("6 7 *", 2)
|
128
|
|
-
|
129
|
|
- def test_base_error(self):
|
130
|
|
- """ Compile error a b + """
|
131
|
|
- with self.assertRaises(ValueError):
|
132
|
|
- expr = pyrpn.RPNExpr("a b +", 3)
|
133
|
|
-
|
134
|
|
- def test_various_compile(self):
|
135
|
|
- """ Compile various expressions """
|
136
|
|
- exprs = (("42 2 + * /", 0),
|
137
|
|
- ("A1 A2 A0 * + /", 3),
|
138
|
|
- )
|
139
|
|
- for expr, argc in exprs:
|
140
|
|
- res = pyrpn.RPNExpr(expr, argc)
|
141
|
|
-
|
142
|
|
- def test_long_code(self):
|
143
|
|
- """ Compile long expressions """
|
144
|
|
- for i in range(0x100,0x10000,0x500):
|
145
|
|
- with self.subTest('Testing expression with %X ops' % i):
|
146
|
|
- for argc in range(1,32, 8):
|
147
|
|
- args = [random.randint(0,IMAX) for _ in range(argc)]
|
148
|
|
- expr = pyrpn.RPNExpr(pyrpn.random_expr(argc, i), argc)
|
149
|
|
- del(expr)
|
150
|
|
-
|
151
|
|
- def test_very_long(self):
|
152
|
|
- """ Compile a very long expression """
|
153
|
|
- argc = 4
|
154
|
|
- codelen = 0x500000
|
155
|
|
- import time
|
156
|
|
- for i in range(3):
|
157
|
|
- args = [random.randint(0,IMAX) for _ in range(argc)]
|
158
|
|
- expr_str = pyrpn.random_expr(argc, codelen)
|
159
|
|
- expr = pyrpn.RPNExpr(expr_str, argc)
|
160
|
|
- del(expr)
|
161
|
|
-
|
162
|
|
- def test_pickling(self):
|
163
|
|
- """ Pickle & unpickle tests """
|
164
|
|
- argc = 4
|
165
|
|
- for _ in range(512):
|
166
|
|
- expr = pyrpn.RPNExpr(pyrpn.random_expr(2,10), argc)
|
167
|
|
- pik = pickle.dumps(expr)
|
168
|
|
- new_expr = pickle.loads(pik)
|
169
|
|
- self.assertEqual(str(expr), str(new_expr))
|
170
|
|
- args = [random.randint(0,IMAX) for _ in range(argc)]
|
171
|
|
- self.assertEqual(expr.eval(*args), new_expr.eval(*args))
|
172
|
|
-
|
173
|
|
-
|
174
|
|
-
|
175
|
37
|
class TestRpnEval(unittest.TestCase):
|
176
|
38
|
|
177
|
39
|
def test_arithm(self):
|
|
@@ -289,13 +151,5 @@ class TestRpnEval(unittest.TestCase):
|
289
|
151
|
'%s%r != %s' % (rpn, args, pyexpr))
|
290
|
152
|
|
291
|
153
|
|
292
|
|
-class SequentialTestLoader(unittest.TestLoader):
|
293
|
|
- def getTestCaseNames(self, testCaseClass):
|
294
|
|
- test_names = super().getTestCaseNames(testCaseClass)
|
295
|
|
- testcase_methods = list(testCaseClass.__dict__.keys())
|
296
|
|
- test_names.sort(key=testcase_methods.index)
|
297
|
|
- return test_names
|
298
|
|
-
|
299
|
|
-
|
300
|
154
|
if __name__ == '__main__':
|
301
|
|
- unittest.main(testLoader=SequentialTestLoader())
|
|
155
|
+ unittest.main()
|