You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
3.9 KiB
178 lines
3.9 KiB
import { describe, expect, it } from '@jest/globals';
|
|
import { L, undef } from '../src/core';
|
|
import { toBoolean as $b, fromBoolean as _b } from '../src/bool';
|
|
import { toNumber as $, Num, Succ, Zero, fromNumber as _, eq, ifz, mul, pred, sub, succ, sum } from '../src/num';
|
|
|
|
describe('Num', () => {
|
|
const infinity: Num = new L(() => Succ._(infinity).value);
|
|
|
|
describe('toNumber', () => {
|
|
it('converts 0', () => {
|
|
expect($(Zero)).toBe(0);
|
|
});
|
|
|
|
it('converts 1', () => {
|
|
expect($(Succ._(Zero))).toBe(1);
|
|
});
|
|
|
|
it('converts 2', () => {
|
|
expect($(Succ._(Succ._(Zero)))).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('fromNumber', () => {
|
|
it('converts 0', () => {
|
|
expect($(_(0))).toBe(0);
|
|
});
|
|
|
|
it('converts 1', () => {
|
|
expect($(_(1))).toBe(1);
|
|
});
|
|
|
|
it('converts 2', () => {
|
|
expect($(_(2))).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('ifz', () => {
|
|
it('returns zero branch', () => {
|
|
expect($b(ifz._(_(0))._(_b(true))._(_b(false)))).toBe(true);
|
|
});
|
|
|
|
it('returns non-zero branch', () => {
|
|
expect($b(ifz._(_(1))._(_b(true))._(_b(false)))).toBe(false);
|
|
});
|
|
|
|
it('returns non-zero branch', () => {
|
|
expect($b(ifz._(_(10))._(_b(true))._(_b(false)))).toBe(false);
|
|
});
|
|
|
|
it('handles infinity', () => {
|
|
expect($b(ifz._(infinity)._(_b(true))._(_b(false)))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('succ', () => {
|
|
it('succ 0 is 1', () => {
|
|
expect($(succ._(_(0)))).toBe(1);
|
|
});
|
|
|
|
it('succ 1 is 2', () => {
|
|
expect($(succ._(_(1)))).toBe(2);
|
|
});
|
|
|
|
it('succ 10 is 11', () => {
|
|
expect($(succ._(_(10)))).toBe(11);
|
|
});
|
|
});
|
|
|
|
describe('pred', () => {
|
|
it('pred 0 is 0', () => {
|
|
expect($(pred._(_(0)))).toBe(0);
|
|
});
|
|
|
|
it('pred 1 is 0', () => {
|
|
expect($(pred._(_(1)))).toBe(0);
|
|
});
|
|
|
|
it('pred 10 is 9', () => {
|
|
expect($(pred._(_(10)))).toBe(9);
|
|
});
|
|
});
|
|
|
|
describe('eq', () => {
|
|
it('0 eq 0 is True', () => {
|
|
expect($b(eq._(_(0))._(_(0)))).toBe(true);
|
|
});
|
|
|
|
it('0 eq 1 is False', () => {
|
|
expect($b(eq._(_(0))._(_(1)))).toBe(false);
|
|
});
|
|
|
|
it('1 eq 0 is False', () => {
|
|
expect($b(eq._(_(1))._(_(0)))).toBe(false);
|
|
});
|
|
|
|
it('3 eq 3 is True', () => {
|
|
expect($b(eq._(_(3))._(_(3)))).toBe(true);
|
|
});
|
|
|
|
it('3 eq 7 is False', () => {
|
|
expect($b(eq._(_(3))._(_(7)))).toBe(false);
|
|
});
|
|
|
|
it('7 eq 3 is False', () => {
|
|
expect($b(eq._(_(7))._(_(3)))).toBe(false);
|
|
});
|
|
|
|
it('handles infinity', () => {
|
|
expect($b(eq._(_(7))._(infinity))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('sum', () => {
|
|
it('0 sum 0 is 0', () => {
|
|
expect($(sum._(_(0))._(_(0)))).toBe(0);
|
|
});
|
|
|
|
it('0 sum 1 is 1', () => {
|
|
expect($(sum._(_(0))._(_(1)))).toBe(1);
|
|
});
|
|
|
|
it('1 sum 0 is 1', () => {
|
|
expect($(sum._(_(1))._(_(0)))).toBe(1);
|
|
});
|
|
|
|
it('3 sum 4 is 7', () => {
|
|
expect($(sum._(_(3))._(_(4)))).toBe(7);
|
|
});
|
|
});
|
|
|
|
describe('sub', () => {
|
|
it('0 sub 0 is 0', () => {
|
|
expect($(sub._(_(0))._(_(0)))).toBe(0);
|
|
});
|
|
|
|
it('0 sub 1 is 0', () => {
|
|
expect($(sub._(_(0))._(_(1)))).toBe(0);
|
|
});
|
|
|
|
it('1 sub 0 is 1', () => {
|
|
expect($(sub._(_(1))._(_(0)))).toBe(1);
|
|
});
|
|
|
|
it('7 sub 4 is 3', () => {
|
|
expect($(sub._(_(7))._(_(4)))).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('mul', () => {
|
|
it('0 mul 0 is 0', () => {
|
|
expect($(mul._(_(0))._(_(0)))).toBe(0);
|
|
});
|
|
|
|
it('0 mul 10 is 0', () => {
|
|
expect($(mul._(_(0))._(_(10)))).toBe(0);
|
|
});
|
|
|
|
it('10 mul 0 is 0', () => {
|
|
expect($(mul._(_(1))._(_(0)))).toBe(0);
|
|
});
|
|
|
|
it('1 mul 10 is 10', () => {
|
|
expect($(mul._(_(1))._(_(10)))).toBe(10);
|
|
});
|
|
|
|
it('10 mul 1 is 10', () => {
|
|
expect($(mul._(_(10))._(_(1)))).toBe(10);
|
|
});
|
|
|
|
it('3 mul 4 is 12', () => {
|
|
expect($(mul._(_(3))._(_(4)))).toBe(12);
|
|
});
|
|
|
|
it('0 mul ignores second argument', () => {
|
|
expect($(mul._(_(0))._(undef))).toBe(0);
|
|
});
|
|
});
|
|
});
|
|
|