Skip to content

Instantly share code, notes, and snippets.

@vindard
Created April 22, 2022 15:54
Show Gist options
  • Save vindard/fad02e2b26edcba56c7c32b917de46cd to your computer and use it in GitHub Desktop.
Save vindard/fad02e2b26edcba56c7c32b917de46cd to your computer and use it in GitHub Desktop.
Test the conditional equivalence of 2 conditional statements
// Live run here: https://replit.com/@vindard/Conditional-Equivalence-TS
type fn = (a: boolean, b: boolean) => boolean
const test = (fn1: fn, fn2: fn) => {
const check = (a: boolean, b: boolean) => fn1(a, b) === fn2(a, b)
const cases: {[key: string]: [boolean, boolean]} = {
ft: [false, true],
ff: [false, false],
tt: [true, true],
tf: [true, false],
}
const checkRanArr: boolean[] = []
const checkRanObj: {[key: string]: boolean} = {}
for (const label in cases) {
const checkRan = check(...cases[label])
checkRanArr.push(checkRan)
checkRanObj[label] = checkRan
}
return checkRanArr.every(el => !!el) ? true : checkRanObj
}
// <-- ADD FUNCTIONS TO TEST HERE
const test1 = (a, b) => a === b
const test2 = (a, b) => !a !== b
const xnor1 = (a, b) => !(!a != !b)
const xnor2 = (a, b) => !a === !b
// -->
// <-- SET TEST PAIR HERE
const testPair = [xnor1, xnor2]
// -->
console.log(...testPair)
console.log(test(...testPair))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment