|
1 | 1 | import unittest |
2 | 2 | import MLC.Log.log as lg |
| 3 | +import sys |
3 | 4 | from MLC.Log.log import set_logger |
4 | | -from MLC.matlab_engine import MatlabEngine |
5 | 5 | from MLC.mlc_parameters.mlc_parameters import Config |
6 | 6 | from MLC.Common.LispTreeExpr.LispTreeExpr import LispTreeExpr |
7 | 7 | from MLC import config as config_path |
8 | 8 |
|
9 | 9 | import os |
10 | 10 |
|
| 11 | + |
11 | 12 | class SimplificationTest(unittest.TestCase): |
| 13 | + # Instead of calculate the Lisp Expression using MATLAB, consume them |
| 14 | + # from this dictionary. Take into consideration that the dictionary |
| 15 | + # has to be regenerated if some test is changed |
| 16 | + simplified_expressions = {"test_simplify_cos_node_number": "(root -0.1411)", |
| 17 | + "test_simplify_cos_node_sensor": "(root (cos S0))", |
| 18 | + "test_simplify_div_node_both_numbers": "(root 0.8093)", |
| 19 | + "test_simplify_div_node_number_and_sensor": "(root (/ 3.4092 S0))", |
| 20 | + "test_simplify_div_node_number_and_zero": "(root 0.0000)", |
| 21 | + "test_simplify_div_node_sensor_and_number": "(root (/ S0 4.2123))", |
| 22 | + "test_simplify_div_node_sensor_and_right_identity": "(root S0)", |
| 23 | + "test_simplify_div_node_sensor_left_identity": "(root S0)", |
| 24 | + "test_simplify_div_node_zero_and_sensor": "(root 0.0000)", |
| 25 | + "test_simplify_exp_node_number": "(root 5.5422)", |
| 26 | + "test_simplify_exp_node_sensor": "(root (exp S0))", |
| 27 | + "test_simplify_log_node_number": "(root 0.5379)", |
| 28 | + "test_simplify_log_node_sensor": "(root (log S0))", |
| 29 | + "test_simplify_log_node_zero": "(root -4.6052)", |
| 30 | + "test_simplify_minus_node_both_numbers": "(root -0.8031)", |
| 31 | + "test_simplify_minus_node_number_and_sensor": "(root (- 3.4092 S0))", |
| 32 | + "test_simplify_minus_node_sensor_and_left_identity": "(root 0.0000)", |
| 33 | + "test_simplify_minus_node_sensor_and_number": "(root (- S0 4.2123))", |
| 34 | + "test_simplify_minus_node_sensor_and_right_identity": "(root 0.0000)", |
| 35 | + "test_simplify_mult_node_both_numbers": "(root 14.3606)", |
| 36 | + "test_simplify_mult_node_left_zero": "(root 0.0000)", |
| 37 | + "test_simplify_mult_node_number_and_sensor": "(root (* 3.4092 S0))", |
| 38 | + "test_simplify_mult_node_sensor_and_number": "(root (* S0 4.2123))", |
| 39 | + "test_simplify_mult_node_sensor_right_zero": "(root 0.0000)", |
| 40 | + "test_simplify_plus_node_both_numbers": "(root 7.6215)", |
| 41 | + "test_simplify_plus_node_number_and_sensor": "(root (+ 3.4092 S0))", |
| 42 | + "test_simplify_plus_node_sensor_and_left_identity": "(root S0)", |
| 43 | + "test_simplify_plus_node_sensor_and_number": "(root (+ S0 4.2123))", |
| 44 | + "test_simplify_plus_node_sensor_and_right_identity": "(root S0)", |
| 45 | + "test_simplify_sin_node_number": "(root 0.9900)", |
| 46 | + "test_simplify_sin_node_sensor": "(root (sin S0))", |
| 47 | + "test_simplify_tanh_node_number": "(root 0.9369)", |
| 48 | + "test_simplify_tanh_node_sensor": "(root (tanh S0))"} |
12 | 49 |
|
13 | 50 | @classmethod |
14 | 51 | def setUpClass(cls): |
15 | | - set_logger("testing") |
| 52 | + set_logger("file") |
16 | 53 | config = Config.get_instance() |
17 | 54 | config.read(os.path.join(config_path.get_test_path(), 'mlc/individual/configuration.ini')) |
18 | 55 |
|
19 | | - def setUp(self): |
20 | | - self._eng = MatlabEngine.engine() |
21 | | - |
22 | | - def _assert_expressions(self, expression): |
23 | | - expected = self._eng.simplify_my_LISP(expression) |
| 56 | + def _assert_expressions(self, expression, function_name): |
| 57 | + # expected = self._eng.simplify_my_LISP(expression) |
| 58 | + expected = SimplificationTest.simplified_expressions[function_name] |
24 | 59 | tree = LispTreeExpr(expression) |
25 | 60 | tree.simplify_tree() |
26 | 61 | obtained = tree.get_simplified_tree_as_string() |
27 | | - |
28 | 62 | self.assertEquals(obtained, expected) |
29 | 63 |
|
30 | 64 | ########################### PLUS NODE ##################################### |
31 | 65 | def test_simplify_plus_node_number_and_sensor(self): |
32 | 66 | expression = '(root (+ 3.4092 S0))' |
33 | | - self._assert_expressions(expression) |
| 67 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
34 | 68 |
|
35 | 69 | def test_simplify_plus_node_sensor_and_number(self): |
36 | 70 | expression = '(root (+ S0 4.2123))' |
37 | | - self._assert_expressions(expression) |
| 71 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
38 | 72 |
|
39 | 73 | def test_simplify_plus_node_sensor_and_right_identity(self): |
40 | 74 | expression = '(root (+ S0 0))' |
41 | | - self._assert_expressions(expression) |
| 75 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
42 | 76 |
|
43 | 77 | def test_simplify_plus_node_sensor_and_left_identity(self): |
44 | 78 | expression = '(root (+ 0 S0))' |
45 | | - self._assert_expressions(expression) |
| 79 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
46 | 80 |
|
47 | 81 | def test_simplify_plus_node_both_numbers(self): |
48 | 82 | expression = '(root (+ 3.4092 4.2123))' |
49 | | - self._assert_expressions(expression) |
| 83 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
50 | 84 |
|
51 | 85 | ########################### MINUS NODE #################################### |
52 | 86 | def test_simplify_minus_node_number_and_sensor(self): |
53 | 87 | expression = '(root (- 3.4092 S0))' |
54 | | - self._assert_expressions(expression) |
| 88 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
55 | 89 |
|
56 | 90 | def test_simplify_minus_node_sensor_and_number(self): |
57 | 91 | expression = '(root (- S0 4.2123))' |
58 | | - self._assert_expressions(expression) |
| 92 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
59 | 93 |
|
60 | 94 | def test_simplify_minus_node_sensor_and_right_identity(self): |
61 | 95 | expression = '(root (- S0 0))' |
62 | | - self._assert_expressions(expression) |
| 96 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
63 | 97 |
|
64 | 98 | def test_simplify_minus_node_sensor_and_left_identity(self): |
65 | 99 | expression = '(root (- 0 S0))' |
66 | | - self._assert_expressions(expression) |
| 100 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
67 | 101 |
|
68 | 102 | def test_simplify_minus_node_both_numbers(self): |
69 | 103 | expression = '(root (- 3.4092 4.2123))' |
70 | | - self._assert_expressions(expression) |
| 104 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
71 | 105 |
|
72 | 106 | ########################### MULT NODE ##################################### |
73 | 107 | def test_simplify_mult_node_number_and_sensor(self): |
74 | 108 | expression = '(root (* 3.4092 S0))' |
75 | | - self._assert_expressions(expression) |
| 109 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
76 | 110 |
|
77 | 111 | def test_simplify_mult_node_sensor_and_number(self): |
78 | 112 | expression = '(root (* S0 4.2123))' |
79 | | - self._assert_expressions(expression) |
| 113 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
80 | 114 |
|
81 | 115 | def test_simplify_mult_node_left_zero(self): |
82 | 116 | expression = '(root (* S0 0))' |
83 | | - self._assert_expressions(expression) |
| 117 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
84 | 118 |
|
85 | 119 | def test_simplify_mult_node_sensor_right_zero(self): |
86 | 120 | expression = '(root (* 0 S0))' |
87 | | - self._assert_expressions(expression) |
| 121 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
88 | 122 |
|
89 | 123 | def test_simplify_minus_node_sensor_and_right_identity(self): |
90 | 124 | expression = '(root (* S0 0))' |
91 | | - self._assert_expressions(expression) |
| 125 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
92 | 126 |
|
93 | 127 | def test_simplify_minus_node_sensor_and_left_identity(self): |
94 | 128 | expression = '(root (* 0 S0))' |
95 | | - self._assert_expressions(expression) |
| 129 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
96 | 130 |
|
97 | 131 | def test_simplify_mult_node_both_numbers(self): |
98 | 132 | expression = '(root (* 3.4092 4.2123))' |
99 | | - self._assert_expressions(expression) |
| 133 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
100 | 134 |
|
101 | 135 | ########################### DIV NODE ###################################### |
102 | 136 | def test_simplify_div_node_number_and_sensor(self): |
103 | 137 | expression = '(root (/ 3.4092 S0))' |
104 | | - self._assert_expressions(expression) |
| 138 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
105 | 139 |
|
106 | 140 | def test_simplify_div_node_sensor_and_number(self): |
107 | 141 | expression = '(root (/ S0 4.2123))' |
108 | | - self._assert_expressions(expression) |
| 142 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
109 | 143 |
|
110 | 144 | def test_simplify_div_node_zero_and_sensor(self): |
111 | 145 | expression = '(root (/ 0 S0))' |
112 | | - self._assert_expressions(expression) |
| 146 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
113 | 147 |
|
114 | 148 | def test_simplify_div_node_sensor_and_right_identity(self): |
115 | 149 | expression = '(root (/ S0 1))' |
116 | | - self._assert_expressions(expression) |
| 150 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
117 | 151 |
|
118 | 152 | def test_simplify_div_node_sensor_left_identity(self): |
119 | 153 | expression = '(root (* 1 S0))' |
120 | | - self._assert_expressions(expression) |
| 154 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
121 | 155 |
|
122 | 156 | def test_simplify_div_node_both_numbers(self): |
123 | 157 | expression = '(root (/ 3.4092 4.2123))' |
124 | | - self._assert_expressions(expression) |
| 158 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
125 | 159 |
|
126 | 160 | def test_simplify_div_node_number_and_zero(self): |
127 | 161 | expression = '(root (/ 3.4092 0))' |
128 | | - self._assert_expressions(expression) |
| 162 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
129 | 163 |
|
130 | 164 | ########################### SIN NODE ###################################### |
131 | 165 | def test_simplify_sin_node_number(self): |
132 | 166 | expression = '(root (sin 1.7124))' |
133 | | - self._assert_expressions(expression) |
| 167 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
134 | 168 |
|
135 | 169 | def test_simplify_sin_node_sensor(self): |
136 | 170 | expression = '(root (sin S0))' |
137 | | - self._assert_expressions(expression) |
| 171 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
138 | 172 |
|
139 | 173 | ########################### COS NODE ###################################### |
140 | 174 | def test_simplify_cos_node_number(self): |
141 | 175 | expression = '(root (cos 1.7124))' |
142 | | - self._assert_expressions(expression) |
| 176 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
143 | 177 |
|
144 | 178 | def test_simplify_cos_node_sensor(self): |
145 | 179 | expression = '(root (cos S0))' |
146 | | - self._assert_expressions(expression) |
| 180 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
147 | 181 |
|
148 | 182 | ########################### LOG NODE ###################################### |
149 | 183 | def test_simplify_log_node_number(self): |
150 | 184 | expression = '(root (log 1.7124))' |
151 | | - self._assert_expressions(expression) |
| 185 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
152 | 186 |
|
153 | 187 | def test_simplify_log_node_sensor(self): |
154 | 188 | expression = '(root (log S0))' |
155 | | - self._assert_expressions(expression) |
| 189 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
156 | 190 |
|
157 | 191 | def test_simplify_log_node_zero(self): |
158 | 192 | expression = '(root (log 0))' |
159 | | - self._assert_expressions(expression) |
| 193 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
160 | 194 |
|
161 | 195 | ########################### EXP NODE ###################################### |
162 | 196 | def test_simplify_exp_node_number(self): |
163 | 197 | expression = '(root (exp 1.7124))' |
164 | | - self._assert_expressions(expression) |
| 198 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
165 | 199 |
|
166 | 200 | def test_simplify_exp_node_sensor(self): |
167 | 201 | expression = '(root (exp S0))' |
168 | | - self._assert_expressions(expression) |
| 202 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
169 | 203 |
|
170 | 204 | ########################### TANH NODE ###################################### |
171 | 205 | def test_simplify_tanh_node_number(self): |
172 | 206 | expression = '(root (tanh 1.7124))' |
173 | | - self._assert_expressions(expression) |
| 207 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
174 | 208 |
|
175 | 209 | def test_simplify_tanh_node_sensor(self): |
176 | 210 | expression = '(root (tanh S0))' |
177 | | - self._assert_expressions(expression) |
178 | | - |
| 211 | + self._assert_expressions(expression, sys._getframe().f_code.co_name) |
0 commit comments