Browse Source

Splitting TestCase in different files

Yann Weber 4 years ago
parent
commit
b4dc6e5f18
3 changed files with 214 additions and 147 deletions
  1. 124
    0
      tests/tests_pyrpn.py
  2. 89
    0
      tests/tests_rpn_compile.py
  3. 1
    147
      tests/tests_rpn_eval.py

+ 124
- 0
tests/tests_pyrpn.py View File

@@ -0,0 +1,124 @@
1
+#!/usr/bin/python3
2
+# Copyright 2020 Weber Yann
3
+#
4
+# This file is part of geneifs.
5
+#
6
+#        geneifs is free software: you can redistribute it and/or modify
7
+#        it under the terms of the GNU General Public License as published by
8
+#        the Free Software Foundation, either version 3 of the License, or
9
+#        (at your option) any later version.
10
+#
11
+#        geneifs is distributed in the hope that it will be useful,
12
+#        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+#        GNU General Public License for more details.
15
+#
16
+#        You should have received a copy of the GNU General Public License
17
+#        along with geneifs.  If not, see <http://www.gnu.org/licenses/>.
18
+#
19
+
20
+import sys
21
+import random
22
+import math
23
+import pickle
24
+
25
+import unittest
26
+from unittest import skip
27
+
28
+IMAX = (1<<64)-1
29
+
30
+try:
31
+    import pyrpn
32
+except (ImportError, NameError) as e:
33
+    print("Error importing pyrpn. Try to run make.",
34
+          file=sys.stderr)
35
+    raise e
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
+if __name__ == '__main__':
124
+    unittest.main()

+ 89
- 0
tests/tests_rpn_compile.py View File

@@ -0,0 +1,89 @@
1
+#!/usr/bin/python3
2
+# Copyright 2020 Weber Yann
3
+#
4
+# This file is part of geneifs.
5
+#
6
+#        geneifs is free software: you can redistribute it and/or modify
7
+#        it under the terms of the GNU General Public License as published by
8
+#        the Free Software Foundation, either version 3 of the License, or
9
+#        (at your option) any later version.
10
+#
11
+#        geneifs is distributed in the hope that it will be useful,
12
+#        but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+#        GNU General Public License for more details.
15
+#
16
+#        You should have received a copy of the GNU General Public License
17
+#        along with geneifs.  If not, see <http://www.gnu.org/licenses/>.
18
+#
19
+
20
+import sys
21
+import random
22
+import math
23
+import pickle
24
+
25
+import unittest
26
+from unittest import skip
27
+
28
+IMAX = (1<<64)-1
29
+
30
+try:
31
+    import pyrpn
32
+except (ImportError, NameError) as e:
33
+    print("Error importing pyrpn. Try to run make.",
34
+          file=sys.stderr)
35
+    raise e
36
+
37
+class TestRpnCompile(unittest.TestCase):
38
+
39
+    def test_basic(self):
40
+        """ Compile 6 7 * """
41
+        expr = pyrpn.RPNExpr("6 7 *", 2)
42
+
43
+    def test_base_error(self):
44
+        """ Compile error a b + """
45
+        with self.assertRaises(ValueError):
46
+            expr = pyrpn.RPNExpr("a b +", 3)
47
+
48
+    def test_various_compile(self):
49
+        """ Compile various expressions """
50
+        exprs = (("42 2 + * /", 0),
51
+            ("A1 A2 A0 * + /", 3),
52
+        )
53
+        for expr, argc in exprs:
54
+            res = pyrpn.RPNExpr(expr, argc)
55
+
56
+    def test_long_code(self):
57
+        """ Compile long expressions """
58
+        for i in range(0x100,0x10000,0x500):
59
+            with self.subTest('Testing expression with %X ops' % i):
60
+                for argc in range(1,32, 8):
61
+                    args = [random.randint(0,IMAX) for _ in range(argc)]
62
+                    expr = pyrpn.RPNExpr(pyrpn.random_expr(argc, i), argc)
63
+                    del(expr)
64
+
65
+    def test_very_long(self):
66
+        """ Compile a very long expression """
67
+        argc = 4
68
+        codelen = 0x500000
69
+        import time
70
+        for i in range(3):
71
+            args = [random.randint(0,IMAX) for _ in range(argc)]
72
+            expr_str = pyrpn.random_expr(argc, codelen)
73
+            expr = pyrpn.RPNExpr(expr_str, argc)
74
+            del(expr)
75
+
76
+    def test_pickling(self):  
77
+        """ Pickle & unpickle tests """
78
+        argc = 4
79
+        for _ in range(512):
80
+            expr = pyrpn.RPNExpr(pyrpn.random_expr(2,10), argc)
81
+            pik = pickle.dumps(expr)
82
+            new_expr = pickle.loads(pik)
83
+            self.assertEqual(str(expr), str(new_expr))
84
+            args = [random.randint(0,IMAX) for _ in range(argc)]
85
+            self.assertEqual(expr.eval(*args), new_expr.eval(*args))
86
+
87
+
88
+if __name__ == '__main__':
89
+    unittest.main()

tests/tests_rpn.py → tests/tests_rpn_eval.py View File

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

Loading…
Cancel
Save