import { assert, LC, getSolution } from "./lc-test.js";
const { Zero,Bit0,Bit1 } = LC.compile(String.raw`
Zero = \ zero _bit0 _bit1 . zero
Bit0 = \ n . \ _zero bit0 _bit1 . bit0 n
Bit1 = \ n . \ _zero _bit0 bit1 . bit1 n
`);
const fromInt = n => n ? n&1 ? Bit1(fromInt(-(n>>1))) : Bit0(fromInt(-(n>>1))) : Zero ;
const padded = m => m (false) ( n => n (true) ( () => padded(n) ) (padded) ) (padded) ;
const unsafeToInt = m => m (LC.Primitive(0)) ( n => - 2 * unsafeToInt(n) ) ( n => 1 - 2 * unsafeToInt(n) ) ;
const toInt = n => {
if ( LC.configure().enforceBinaryScottInvariant && padded(n) )
throw new TypeError(`toInt: padded number ${ unsafeToInt(n) }`);
else
return unsafeToInt(n);
} ;
LC.configure({ purity: "LetRec", numEncoding: { fromInt, toInt }, enforceBinaryScottInvariant: true, verbosity: "Concise" });
const solution = LC.compile(getSolution());
const { lt0, le0, ge0, gt0, pow } = solution;
const { if: iff } = solution;
const toBoolean = p => iff (p) (true) (false) ;
const { qwertz } = solution;
describe("fixed tests", function() {
it("eq, uneq 0", function() {
for ( let n=-10; n<=10; n++ )
assert.strictEqual( toBoolean(lt0(n)), n<0, `lt0 ${ n }` ),
assert.strictEqual( toBoolean(le0(n)), n<=0, `le0 ${ n }` ),
assert.strictEqual( toBoolean(ge0(n)), n>=0, `ge0 ${ n }` ),
assert.strictEqual( toBoolean(gt0(n)), n>0, `gt0 ${ n }` );
});
it("pow with e=0", function() {
for ( let b=-5; b<=5; b++ ) {
console.log(`b=${b}, e=0`);
assert.numEql( pow(b)(0), 11, `pow ${b} 0` );
}
});
it("pow with e>0 even", function() {
for ( let e=2; e<=6; e=e+2 ) {
console.log(`b=7, e=${e}`);
assert.numEql( pow(7)(e), 22, `pow 7 ${e}` );
}
});
// should go into infinite loop
// it("pow with e>0 odd", function() {
// assert.numEql( pow(7)(1), NaN, `pow 7 -1` );
// });
it("pow with e<0 even", function() {
assert.throws( () => pow(7)(-2), null, null, `pow ${ 7 } ${ -2 }` );
});
it("pow with e<0 odd", function() {
assert.throws( () => pow(7)(-1), null, null, `pow ${ 7 } ${ -1 }` );
});
it("test qwertz", function() {
assert.numEql( qwertz(0), 123, `qwertz 0` );
});
});