-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_arithmetic.js
40 lines (37 loc) · 1.07 KB
/
test_arithmetic.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
'use strict';
let myna = require('../myna');
let grammar = require('../grammars/grammar_arithmetic')(myna);
let evaluator = require('../tools/myna_arithmetic_evaluator.js')(myna);
let arithmeticTestInputs = [
["(42)", 42],
["6 * 7", 42],
["42", 42],
["0", 0],
["-42", -42],
["- 5", -5],
["-\t- 5", 5],
["-(42)", -42],
["+42", +42],
["+(42)", +42],
["2 * 3 * 7", 42],
["5 * 8 + 2", 42],
["2 + 5 * 8", 42],
["(5 * 8) + 2", 42],
["2 + (5 * 8)", 42],
["((5 * 8) + 2)", 42],
["(2 + (5 * 8))", 42],
["6 * (9 - 2)", 42],
["(9 - 2) * 6", 42],
["5 * 9 - 3", 42],
["-5 * 9", -45],
["3 - -5", 8],
["-3 - -5 * 9", 42]];
for (let test of arithmeticTestInputs) {
let input = test[0];
let expected = test[1];
let result = evaluator(input);
if (result !== expected)
console.log("test failed: " + test + ", instead was: " + result);
else
console.log("test passed: " + test)
}