-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cross_language.py
More file actions
233 lines (204 loc) · 6.57 KB
/
test_cross_language.py
File metadata and controls
233 lines (204 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python3
"""
Cross-language tests: runs the same expression sequences in Python and JavaScript
and verifies they produce the same scalar results.
The Python code is the reference. JavaScript must match.
"""
import unittest
import subprocess
import json
import numpy as np
from expression import Expression, TokenType
def evaluate_in_js(sequence):
"""Run a sequence of expressions in JS and return scalar results."""
js_code = """
const { Expression, TokenType } = require('./expression.js');
Expression.resetGlobalVariables();
const sequence = %s;
const results = [];
for (const expr of sequence) {
try {
const m = new Expression(expr);
// Skip evaluation for function definitions (no arguments to evaluate with)
const lv = m.lvalue;
if (lv !== null && lv.type === TokenType.LFUNCTION) {
results.push({ expr: expr, value: null, error: null, defined: true });
} else {
const scalar = m.evaluateScalar();
results.push({ expr: expr, value: scalar, error: null, defined: false });
}
} catch(e) {
results.push({ expr: expr, value: null, error: e.message, defined: false });
}
}
process.stdout.write(JSON.stringify(results));
""" % json.dumps(sequence)
result = subprocess.run(
['node', '-e', js_code],
capture_output=True, text=True, cwd='.',
timeout=30
)
if result.returncode != 0:
raise RuntimeError(f"JS error: {result.stderr}")
return json.loads(result.stdout)
def evaluate_in_python(sequence):
"""Run a sequence of expressions in Python and return scalar results."""
Expression.reset_global_variables()
results = []
for expr in sequence:
try:
m = Expression(expr)
# Skip evaluation for function definitions (no arguments to evaluate with)
lv = m.lvalue
if lv is not None and lv.type == TokenType.LFUNCTION:
results.append({"expr": expr, "value": None, "error": None, "defined": True})
else:
scalar = m.evaluate_scalar()
results.append({"expr": expr, "value": float(scalar), "error": None, "defined": False})
except Exception as e:
results.append({"expr": expr, "value": None, "error": str(e), "defined": False})
return results
class CrossLanguageTests(unittest.TestCase):
def assertResultsMatch(self, sequence, tol=1e-10):
py_results = evaluate_in_python(sequence)
js_results = evaluate_in_js(sequence)
self.assertEqual(len(py_results), len(js_results),
"Different number of results")
for py, js in zip(py_results, js_results):
expr = py["expr"]
# Function definitions: just check both recognized it
if py.get("defined"):
self.assertTrue(js.get("defined"),
f"Python saw '{expr}' as function def but JS did not")
continue
if py["error"] is not None:
self.assertIsNotNone(js["error"],
f"Python raised error for '{expr}' but JS did not. "
f"Py error: {py['error']}, JS value: {js['value']}")
else:
self.assertIsNone(js["error"],
f"JS raised error for '{expr}' but Python did not. "
f"JS error: {js['error']}, Py value: {py['value']}")
self.assertAlmostEqual(
py["value"], js["value"], delta=tol,
msg=f"Mismatch for '{expr}': Python={py['value']}, JS={js['value']}"
)
def test_basic_arithmetic(self):
self.assertResultsMatch([
"1+2",
"10-3",
"4*5",
"10/3",
"1+2*3",
"(1+2)*3",
"2*3+4/2",
])
def test_nested_parentheses(self):
self.assertResultsMatch([
"((1+2)*(3+4))/5",
"2*((3+4)/2)",
"(10-(4/2))*3",
])
def test_constants(self):
self.assertResultsMatch([
"pi",
"π",
"e",
"pi*2",
"e+1",
])
def test_functions(self):
self.assertResultsMatch([
"sin(0)",
"sin(1)",
"cos(0)",
"cos(1)",
"tan(1)",
"sqrt(2)",
"sqrt(4)",
"sin(pi/2)",
"cos(pi)",
])
def test_variable_assignment(self):
self.assertResultsMatch([
"a=5",
"b=10",
"a+b",
"a*b",
"a+b*2",
"c=a+b",
"c",
])
def test_variable_reassignment(self):
self.assertResultsMatch([
"x=1",
"x+1",
"x=10",
"x+1",
])
def test_function_definition(self):
self.assertResultsMatch([
"f(x):=x*2",
"f(3)",
"f(10)",
"f(1+2)",
])
def test_function_definition_square(self):
self.assertResultsMatch([
"g(x):=x*x",
"g(3)",
"g(5)",
"g(1+1)",
])
def test_uncertainty_scalar_mean(self):
"""Scalar mean of uncertainty expressions must be exact."""
self.assertResultsMatch([
"10±2",
"10±2+5",
"(10±2)*2",
"1±1%",
])
def test_define_scalar_mean(self):
self.assertResultsMatch([
"a:=10±2",
"a+a",
"a*2",
])
def test_complex_sequence(self):
self.assertResultsMatch([
"x=3",
"y=4",
"hyp=sqrt(x*x+y*y)",
"hyp",
"angle=sin(x/hyp)",
"angle",
])
def test_priority_comprehensive(self):
self.assertResultsMatch([
"3+4*2",
"(3+4)*2",
"6/3-2",
"6/(3-2)",
"2+3*4/2",
"2*(3+4)/2",
"8/2*(2+2)",
"100/(5*2)+6",
])
def test_errors_match(self):
"""Both languages should error on the same invalid inputs."""
self.assertResultsMatch([
"undefined_var",
])
def test_comma(self):
self.assertResultsMatch([
"1,1",
])
def test_function_with_expression(self):
self.assertResultsMatch([
"f(x):=x+1",
"f(f(1))",
"a=10",
"f(a)",
])
if __name__ == "__main__":
unittest.main()