-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoolio.test.js
128 lines (113 loc) · 3.05 KB
/
Boolio.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
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
function api() {
const boolio = new Boolio("foo && bar");
boolio.atoms(); // ['foo', 'bar']
boolio.evaluate({ foo: true, bar: true }); // true
boolio.truthTable(); /*
{
atoms: ['foo', 'bar'],
rows: [
[true, true, true],
[true, false, true],
[false, true, true],
[false, false, false]
]
}
*/
}
const Boolio = require("./Boolio");
function evaluate(expression, values) {
return new Boolio(expression).evaluate(values);
}
function evaluateNode(ast, values) {
return new Boolio().evaluateNode(ast, values);
}
const T = true;
const F = false;
it.each([[T, T, T], [T, F, F], [F, T, F], [F, F, F]])(
"given a = %s, b = %s: a and b === %s",
(a, b, result) => {
const ast = {
type: "and",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
};
expect(evaluateNode(ast, { a, b })).toEqual(result);
}
);
it.each([[T, T, T], [T, F, T], [F, T, T], [F, F, F]])(
"given a = %s, b = %s: a or b === %s",
(a, b, result) => {
const ast = {
type: "or",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
};
expect(evaluateNode(ast, { a, b })).toEqual(result);
}
);
it.each([[T, F], [F, T]])("given a = %s, not a === %s", (a, result) => {
const ast = {
type: "not",
argument: { type: "atom", name: "a" }
};
expect(evaluateNode(ast, { a })).toEqual(result);
});
it.each([
[T, T, T, T],
[T, T, F, T],
[T, F, F, F],
[T, F, T, T],
[F, F, T, T],
[F, T, T, T],
[F, T, F, F],
[F, F, F, F]
])("given a = %s, b = %s, c = %s: a and b or c === %s", (a, b, c, result) => {
const ast = {
type: "or",
left: {
type: "and",
left: { type: "atom", name: "a" },
right: { type: "atom", name: "b" }
},
right: { type: "atom", name: "c" }
};
expect(evaluateNode(ast, { a, b, c })).toEqual(result);
});
it.each([[T, T, T], [T, F, F], [F, T, F], [F, F, F]])(
"given a = %s, b = %s: a && b === %s",
(a, b, result) => {
expect(evaluate("a && b", { a, b })).toEqual(result);
}
);
it("finds the unique atoms in an expression", () => {
const boolio = new Boolio("foo && bar(1) || (bar(2) && !foo)");
expect(boolio.atoms()).toEqual(new Set(["foo", "bar(1)", "bar(2)"]));
});
it("generates a truth table for an expression", () => {
const boolio = new Boolio("foo && bar || foo && baz");
expect(boolio.truthTable()).toEqual({
atoms: ["foo", "bar", "baz"],
rows: [
[T, T, T, T],
[T, T, F, T],
[T, F, T, T],
[T, F, F, F],
[F, T, T, F],
[F, T, F, F],
[F, F, T, F],
[F, F, F, F]
]
});
});
function essentialPrimeImplicants() {
return [];
}
// it("reduces prime implicants to essential prime implicants", () => {
// expect(
// essentialPrimeImplicants([
// { term: [null, F, F, null], minTerms: [0, 1, 8, 9] },
// { term: [null, F, null, T], minTerms: [0, 1, 3, 11] },
// { term: [null, null, T, T], minTerms: [3, 7, 11, 15] }
// ], [0, 1, 3, 7, 8, 9, 11, 15])
// ).toEqual([[null, F, F, null], [null, null, T, T]]);
// });