-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
48 lines (45 loc) · 1.11 KB
/
test.js
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
import { buildExpression, calculateAst, drawAst} from "./calc.js";
const expressions = [
["1 + 1", 2],
["1 - 1", 0],
["1 * 0", 0],
["2 / 2", 1],
["(1)", 1],
["-1 + 1", 0],
["1.1 - 1.1", 0],
["1 / 2 + 1 / 2", 1],
["2 * 2 / 2 + 2", 4],
["(2 + 1) * 2 - 6", 0],
["2 * (2 - 1) * 2 - 4", 0],
["2 * ((2 - 1) + 2)", 6],
["1 + 2 * 3 * ((4 - 5) + 6) * 7 * 8 + 9", 1690],
["((2 + 1)) + 1", 4],
["++0", 1],
["--0", -1],
["--0 + 1", 0],
["6 - -6", 12],
["1 - --0", 2],
["1 - ++0", 0],
["--(1)", 0],
["--(1)", 0],
["---1", -2],
["---1 - 1", -3],
["---1 - --1", -2],
["---1 - ---1", 0],
["++(- - -6- - - -6)", 1],
]
function main(expressions) {
for (const [expression, expectedResult] of expressions) {
const ast = buildExpression(expression);
const result = calculateAst(ast);
if (result == expectedResult) {
console.log("✅", expression, "=", result);
} else {
console.log("❌", "Failed to calculate:", expression);
console.log("\t", "Expected:", expectedResult);
console.log("\t", "Actual:", result);
drawAst(ast);
}
}
}
main(expressions);