-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
85 lines (75 loc) · 2.89 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
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
test('Truth Combos', function() {
var symbols = {};
var symCount = 6;
for (var i = 0; i < symCount; i ++) {
symbols['sym' + i] = true;
}
var combos = truth.truthCombos(symbols);
ok(combos.length === Math.pow(2, symCount), 'Check number of truth combos.');
});
function testExpr(expr, ast) {
var result = truth.parse(truth.tokenize(expr));
var actualAst = result[0];
same(actualAst, ast, 'Verify expression ' + expr);
}
function assertSameAst(expr1, expr2) {
var ast1 = truth.parse(truth.tokenize(expr1))[0];
var ast2 = truth.parse(truth.tokenize(expr2))[0];
same(ast1, ast2, 'Expressions do not parse the same expr1: ' + expr1 + ' expr2: ' + expr2);
}
test('Parser', function() {
testExpr('singletoken', 'singletoken');
testExpr('(((singletoken)))', 'singletoken');
testExpr('a&b', ['&', 'a', 'b']);
testExpr('a & b', ['&', 'a', 'b']);
testExpr('a & (((((b)))))', ['&', 'a', 'b']);
testExpr('verylong101symbolname1 & ((b))', ['&', 'verylong101symbolname1', 'b']);
testExpr('b & (b & (b & (b & b)))', ['&', 'b',
['&', 'b',
['&', 'b',
['&', 'b', 'b']]]]);
testExpr('b & (b & ((!b) & (b & b)))', ['&', 'b',
['&', 'b',
['&', ['!', 'b'],
['&', 'b', 'b']]]]);
testExpr('!(a)', ['!', 'a']);
testExpr('!a', ['!', 'a']);
testExpr('(!a)', ['!', 'a']);
testExpr('!(a)', ['!', 'a']);
testExpr('~a', ['~', 'a']);
testExpr('(~a)', ['~', 'a']);
testExpr('(~((a)))', ['~', 'a']);
testExpr('a & b & c & d', ['&', 'a',
['&', 'b',
['&', 'c', 'd']]]);
});
test('Operator precedence', function() {
assertSameAst('a & b', 'a & b');
assertSameAst('a & b | c', '(a & b) | c');
assertSameAst('c | a & b', 'c | (a & b)');
});
test('evalExpr', function() {
var expr = ['&', 'a', 'b'];
var bindings = {'a' : true,
'b' : false};
equals(truth.evalExpr(expr, bindings), false);
bindings = {'a' : true,
'b' : true};
equals(truth.evalExpr(expr, bindings), true);
expr = ['^', 'a', 'b'];
bindings = {'a' : true,
'b' : false};
equals(truth.evalExpr(expr, bindings), true);
bindings = {'a' : true,
'b' : true};
equals(truth.evalExpr(expr, bindings), false);
expr = ['!', 'a'];
bindings = {'a' : true};
equals(truth.evalExpr(expr, bindings), false);
expr = ['~', 'a'];
equals(truth.evalExpr(expr, bindings), false);
});
test('tokenizer', function() {
same(truth.tokenize('longsymbol'), ['longsymbol']);
same(truth.tokenize('( longsymbol | ||'), ['(', 'longsymbol', '|', '|', '|']);
});