boolean-expressions
Version:
Boolean expression parser and evaluator. Makes use of the Ohm grammar package with grammar rules found [here](https://github.com/avadavat/boolean-expressions/blob/master/src/grammar/grammarRules.ts).
428 lines (423 loc) • 10.4 kB
text/typescript
import {
andOperators,
orOperators,
xorOperators,
notOperators,
ifOperators,
biconditionalOperators,
converseOperators,
nonimplicationOperators,
converseNonimplicationOperators,
nandOperators,
norOperators,
xnorOperators
} from "./operators";
export interface TestCase {
expression: string;
variableNames: string[];
truthTable: boolean[][];
}
function generateTestCases(): TestCase[] {
const testCases: TestCase[] = [
{
expression: "p and notq",
variableNames: ["p", "notq"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "not p and q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "p and not q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "p or not q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "not p or q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "not p xor q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "not p and not q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "p and q and s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p and q and not s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p and not q and s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p and not (q and s)",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "not p and q and s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p",
variableNames: ["p"],
truthTable: [
[],
[]
]
},
{
expression: "p and q or s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "(p and q) or s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "if p then q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "p and q -> s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p -> q and s",
variableNames: ["p", "q", "s"],
truthTable: [
[],
[],
[],
[],
[],
[],
[],
[]
]
},
{
expression: "p and q and true",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "p and false and q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
},
{
expression: "true or p and q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
}
];
// AND operators:
andOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// OR operators:
orOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// XOR operators:
xorOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// NOT operators:
notOperators.forEach(op => {
testCases.push({
expression: op + " p",
variableNames: ["p"],
truthTable: [
[],
[]
]
});
});
// IF operators:
ifOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// BICONDITIONAL operators:
biconditionalOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// CONVERSE operators:
converseOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// NONIMPLICATION operators:
nonimplicationOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// CONVERSE NONIMPLICATION operators:
converseNonimplicationOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// NAND operators:
nandOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// NOR operators:
norOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
// XNOR operators:
xnorOperators.forEach(op => {
testCases.push({
expression: "p " + op + " q",
variableNames: ["p", "q"],
truthTable: [
[],
[],
[],
[]
]
});
});
return testCases;
}
export default generateTestCases;