More library functions
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { toBoolean as $b, fromBoolean as _b } from '../src/bool';
|
||||
import { fromNumber as _n } from '../src/num';
|
||||
import { toChar as $, fromChar as _, eq } from '../src/char';
|
||||
|
||||
describe('Char', () => {
|
||||
describe('toChar', () => {
|
||||
it('converts "0"', () => {
|
||||
expect($(_n(48))).toBe('0');
|
||||
});
|
||||
|
||||
it('converts "a"', () => {
|
||||
expect($(_n(97))).toBe('a');
|
||||
});
|
||||
|
||||
it('converts "Z"', () => {
|
||||
expect($(_n(90))).toBe('Z');
|
||||
});
|
||||
|
||||
it('converts "-"', () => {
|
||||
expect($(_n(45))).toBe('-');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromChar', () => {
|
||||
it('converts "0"', () => {
|
||||
expect($(_('0'))).toBe('0');
|
||||
});
|
||||
|
||||
it('converts "a"', () => {
|
||||
expect($(_('a'))).toBe('a');
|
||||
});
|
||||
|
||||
it('converts "Z"', () => {
|
||||
expect($(_('Z'))).toBe('Z');
|
||||
});
|
||||
|
||||
it('converts "-"', () => {
|
||||
expect($(_('-'))).toBe('-');
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq', () => {
|
||||
it('"0" eq "0" is True', () => {
|
||||
expect(eq).toDeferEvaluationOf(_('0'), _('0'));
|
||||
expect($b(eq._(_('0'))._(_('0')))).toBe(true);
|
||||
});
|
||||
|
||||
it('"0" eq "a" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(_('0'), _('a'));
|
||||
expect($b(eq._(_('0'))._(_('a')))).toBe(false);
|
||||
});
|
||||
|
||||
it('"a" eq "a" is True', () => {
|
||||
expect(eq).toDeferEvaluationOf(_('a'), _('a'));
|
||||
expect($b(eq._(_('a'))._(_('a')))).toBe(true);
|
||||
});
|
||||
|
||||
it('"a" eq "z" is True', () => {
|
||||
expect(eq).toDeferEvaluationOf(_('a'), _('z'));
|
||||
expect($b(eq._(_('a'))._(_('z')))).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
+44
-3
@@ -1,9 +1,8 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { L, _, toNative } from '../src/core';
|
||||
import { L, _, toNative, undef } from '../src/core';
|
||||
import { toBoolean as $b, iif } from '../src/bool';
|
||||
import { toNumber as $n, Num, Succ, Zero, fromNumber as _n, eq as eqn, even, odd, succ, sum } from '../src/num';
|
||||
import { toArray as $a, toNumberArray as $na, Cons, List, Nil, fromArray as _a, fromNumberArray as _na, all, any, eq, filter, foldl, foldlz, foldr, foldrz, head, map, tail } from '../src/list';
|
||||
|
||||
import { toArray as $a, toNumberArray as $na, Cons, List, Nil, fromArray as _a, fromNumberArray as _na, all, any, concat, cons, eq, filter, foldl, foldlz, foldr, foldrz, head, map, tail } from '../src/list';
|
||||
|
||||
describe('List', () => {
|
||||
const iterate: (n: Num) => List<Num> = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value);
|
||||
@@ -37,6 +36,48 @@ describe('List', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('cons', () => {
|
||||
it('0 cons Nil is [0]', () => {
|
||||
expect(cons).toDeferEvaluationOf(_n(0), Nil);
|
||||
expect($na(cons._(_n(0))._(Nil))).toEqual([0]);
|
||||
});
|
||||
|
||||
it('0 cons [1] is [0, 1]', () => {
|
||||
expect(cons).toDeferEvaluationOf(_n(0), _na([1]));
|
||||
expect($na(cons._(_n(0))._(_na([1])))).toEqual([0, 1]);
|
||||
});
|
||||
|
||||
it('0 cons [1, 2] is [0, 1, 2]', () => {
|
||||
expect(cons).toDeferEvaluationOf(_n(0), _na([1, 2]));
|
||||
expect($na(cons._(_n(0))._(_na([1, 2])))).toEqual([0, 1, 2]);
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect($n(cons._(_n(0))._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('concat', () => {
|
||||
it('Nil concat Nil is Nil', () => {
|
||||
expect(concat).toDeferEvaluationOf(Nil, Nil);
|
||||
expect($na(concat._(Nil)._(Nil))).toEqual([]);
|
||||
});
|
||||
|
||||
it('[0] concat [1] is [0, 1]', () => {
|
||||
expect(concat).toDeferEvaluationOf(_na([0]), _na([1]));
|
||||
expect($na(concat._(_na([0]))._(_na([1])))).toEqual([0, 1]);
|
||||
});
|
||||
|
||||
it('[0, 1] concat [2, 3] is [0, 1, 2, 3]', () => {
|
||||
expect(concat).toDeferEvaluationOf(_na([0, 1]), _na([2, 3]));
|
||||
expect($na(concat._(_na([0, 1]))._(_na([2, 3])))).toEqual([0, 1, 2, 3]);
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect($n(concat._(infinite)._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('head', () => {
|
||||
it('returns nothing from Nil', () => {
|
||||
expect(() => head._(Nil)).not.toThrow();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { undef } from '../src/core';
|
||||
import { toBoolean as $b, fromBoolean as _b, not } from '../src/bool';
|
||||
import { toNumber as $n, fromNumber as _n, succ } from '../src/num';
|
||||
import { Pair, both, first, fst, second, snd } from '../src/pair';
|
||||
|
||||
describe('Pair', () => {
|
||||
describe('fst', () => {
|
||||
it('returns first', () => {
|
||||
expect($b(fst._(Pair._(_b(false))._(_n(0))))).toBe(false);
|
||||
});
|
||||
|
||||
it('ignores second', () => {
|
||||
expect($b(fst._(Pair._(_b(false))._(undef)))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('snd', () => {
|
||||
it('returns second', () => {
|
||||
expect($n(snd._(Pair._(_b(false))._(_n(0))))).toBe(0);
|
||||
});
|
||||
|
||||
it('ignores first', () => {
|
||||
expect($n(snd._(Pair._(undef)._(_n(0))))).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('first', () => {
|
||||
it('updates first', () => {
|
||||
expect($b(fst._(first._(not)._(Pair._(_b(false))._(_n(0)))))).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores second', () => {
|
||||
expect($b(fst._(first._(not)._(Pair._(_b(false))._(undef))))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('second', () => {
|
||||
it('updates second', () => {
|
||||
expect($n(snd._(second._(succ)._(Pair._(_b(false))._(_n(0)))))).toBe(1);
|
||||
});
|
||||
|
||||
it('ignores first', () => {
|
||||
expect($n(snd._(second._(succ)._(Pair._(undef)._(_n(0)))))).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('both', () => {
|
||||
it('updates both', () => {
|
||||
expect($b(fst._(both._(not)._(succ)._(Pair._(_b(false))._(_n(0)))))).toBe(true);
|
||||
expect($n(snd._(both._(not)._(succ)._(Pair._(_b(false))._(_n(0)))))).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,106 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { L, _, undef } from '../src/core';
|
||||
import { toBoolean as $b } from '../src/bool';
|
||||
import { toChar as $c, fromChar as _c, eq as eqc } from '../src/char';
|
||||
import { toString as $s, Cons, Empty, String, fromString as _s, concat, eq } from '../src/string';
|
||||
|
||||
describe('String', () => {
|
||||
const infinite: String = new L(() => Cons._(_c('a'))._(infinite).value);;
|
||||
|
||||
describe('toString', () => {
|
||||
it('converts ""', () => {
|
||||
expect($s(Empty)).toBe('');
|
||||
});
|
||||
|
||||
it('converts "a"', () => {
|
||||
expect($s(Cons._(_c('a'))._(Empty))).toBe('a');
|
||||
});
|
||||
|
||||
it('converts "abc"', () => {
|
||||
expect($s(Cons._(_c('a'))._(Cons._(_c('b'))._(Cons._(_c('c'))._(Empty))))).toBe('abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromString', () => {
|
||||
it('converts ""', () => {
|
||||
expect($s(_s(''))).toBe('');
|
||||
});
|
||||
|
||||
it('converts "a"', () => {
|
||||
expect($s(_s('a'))).toBe('a');
|
||||
});
|
||||
|
||||
it('converts "2"', () => {
|
||||
expect($s(_s('abc'))).toBe('abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('concat', () => {
|
||||
it('"" concat "" is ""', () => {
|
||||
expect(concat).toDeferEvaluationOf(Empty, Empty);
|
||||
expect($s(concat._(Empty)._(Empty))).toBe('');
|
||||
});
|
||||
|
||||
it('"a" concat "b" is "ab"', () => {
|
||||
expect(concat).toDeferEvaluationOf(_s('a'), _s('b'));
|
||||
expect($s(concat._(_s('a'))._(_s('b')))).toBe('ab');
|
||||
});
|
||||
|
||||
it('"abc" concat "def" is "abcdef"', () => {
|
||||
expect(concat).toDeferEvaluationOf(_s('abc'), _s('def'));
|
||||
expect($s(concat._(_s('abc'))._(_s('def')))).toBe('abcdef');
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect($c(concat._(infinite)._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe('a');
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq', () => {
|
||||
it('"" eq "" is True', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s(''), _s(''));
|
||||
expect($b(eq._(_s(''))._(_s('')))).toBe(true);
|
||||
});
|
||||
|
||||
it('"" eq "abc" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s(''), _s("abc"));
|
||||
expect($b(eq._(_s(''))._(_s("abc")))).toBe(false);
|
||||
});
|
||||
|
||||
it('"abc" eq "" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s(''));
|
||||
expect($b(eq._(_s("abc"))._(_s('')))).toBe(false);
|
||||
});
|
||||
|
||||
it('"a" eq "abc" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s('a'), _s("abc"));
|
||||
expect($b(eq._(_s('a'))._(_s("abc")))).toBe(false);
|
||||
});
|
||||
|
||||
it('"abc" eq "a" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s('a'));
|
||||
expect($b(eq._(_s("abc"))._(_s('a')))).toBe(false);
|
||||
});
|
||||
|
||||
|
||||
it('"bca" eq "abc" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s("bca"), _s("abc"));
|
||||
expect($b(eq._(_s("bca"))._(_s("abc")))).toBe(false);
|
||||
});
|
||||
|
||||
it('"abc" eq "bca" is False', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s("bca"));
|
||||
expect($b(eq._(_s("abc"))._(_s("bca")))).toBe(false);
|
||||
});
|
||||
|
||||
it('"abc" eq "abc" is True', () => {
|
||||
expect(eq).toDeferEvaluationOf(eqc, _s("abc"), _s("abc"));
|
||||
expect($b(eq._(_s("abc"))._(_s("abc")))).toBe(true);
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect($b(eq._(infinite)._(_s("abc")))).toBe(false);
|
||||
expect($b(eq._(_s("abc"))._(infinite))).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user