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.
724 lines
21 KiB
724 lines
21 KiB
import { describe, expect, it } from '@jest/globals';
|
|
import { toNative as $$, _, fromNative as __, ___, undef } from '../src/core';
|
|
import { toNOrd as $o, NOrd } from '../src/ord';
|
|
import { toBoolean as $b, iif } from '../src/bool';
|
|
import { toNumber as $n, toNumber as $nn, Num, Succ, Zero, fromNumber as _n, fromNumber as _nn, cmp as cmpn, eq as eqn, even, gt as ngt, show as nshow, odd, succ, sum } from '../src/num';
|
|
import { toChar as $c } from '../src/char';
|
|
import { toArray as $a, Cons, List, Nil, fromArray as _a, all, any, append, cmp, concat, cons, eq, filter, foldl, foldlz, foldr, foldrz, ge, get, gt, head, intersperse, iterate, le, len, lt, map, nul, set, show, singleton, snoc, tail, update } from '../src/list';
|
|
import { toString as $s } from '../src/string';
|
|
|
|
describe('List', () => {
|
|
const infinite: List<Num> = iterate(Succ)(Zero);
|
|
|
|
const _na: (xs: number[]) => List<Num>
|
|
= xs => xs.length === 0 ? Nil : Cons(_nn(xs[0]))(___(() => _na(xs.slice(1)).run()));
|
|
|
|
const $na: (xs: List<Num>) => number[]
|
|
= xs => $$(xs(__([]))(_(x => _(xs => __([$nn(x), ...$na(xs)])))));
|
|
|
|
describe('toArray', () => {
|
|
it('converts []', () => {
|
|
expect($a(Nil)).toEqual([]);
|
|
});
|
|
|
|
it('converts [0]', () => {
|
|
expect($a(Cons(_n(0))(Nil)).map($n)).toEqual([0]);
|
|
});
|
|
|
|
it('converts [0, 1]', () => {
|
|
expect($a(Cons(_n(0))(Cons(_n(1))(Nil))).map($n)).toEqual([0, 1]);
|
|
});
|
|
});
|
|
|
|
describe('fromArray', () => {
|
|
it('converts []', () => {
|
|
expect($a(_a([]))).toEqual([]);
|
|
});
|
|
|
|
it('converts [0]', () => {
|
|
expect($a(_a([0].map(_n))).map($n)).toEqual([0]);
|
|
});
|
|
|
|
it('converts 2', () => {
|
|
expect($a(_a([0, 1].map(_n))).map($n)).toEqual([0, 1]);
|
|
});
|
|
});
|
|
|
|
describe('cons', () => {
|
|
it('0 cons Nil is [0]', () => {
|
|
expect($na(cons(_n(0))(Nil))).toEqual([0]);
|
|
});
|
|
|
|
it('0 cons [1] is [0, 1]', () => {
|
|
expect($na(cons(_n(0))(_na([1])))).toEqual([0, 1]);
|
|
});
|
|
|
|
it('0 cons [1, 2] is [0, 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('snoc', () => {
|
|
it('Nil snoc 0 is [0]', () => {
|
|
expect($na(snoc(Nil)(_n(0)))).toEqual([0]);
|
|
});
|
|
|
|
it('[1] snoc 0 is [1, 0]', () => {
|
|
expect($na(snoc(_na([1]))(_n(0)))).toEqual([1, 0]);
|
|
});
|
|
|
|
it('[1, 2] snoc 0 is [1, 2, 0]', () => {
|
|
expect($na(snoc(_na([1, 2]))(_n(0)))).toEqual([1, 2, 0]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(snoc(infinite)(_n(0))(undef)(_(x => _(_xs => x))))).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('nul', () => {
|
|
it('nul Nil is True', () => {
|
|
expect($b(nul(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('nul [1] is False', () => {
|
|
expect($b(nul(_na([1])))).toEqual(false);
|
|
});
|
|
|
|
it('nul [1, 2] is False', () => {
|
|
expect($b(nul(_na([1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(nul(infinite))).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe('len', () => {
|
|
it('len Nil is 0', () => {
|
|
expect($n(len(_na([])))).toEqual(0);
|
|
});
|
|
|
|
it('len [1] is 1', () => {
|
|
expect($n(len(_na([1])))).toEqual(1);
|
|
});
|
|
|
|
it('len [1, 2] is 2', () => {
|
|
expect($n(len(_na([1, 2])))).toEqual(2);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(ngt(len(infinite))(Zero))).toEqual(true);
|
|
});
|
|
|
|
it('ignores items', () => {
|
|
expect($n(len(cons(undef)(cons(undef)(Nil))))).toEqual(2);
|
|
});
|
|
});
|
|
|
|
describe('singleton', () => {
|
|
it('singleton 0 is [0]', () => {
|
|
expect($na(singleton(_n(0)))).toEqual([0]);
|
|
});
|
|
});
|
|
|
|
describe('append', () => {
|
|
it('Nil append Nil is Nil', () => {
|
|
expect($na(append(Nil)(Nil))).toEqual([]);
|
|
});
|
|
|
|
it('[0] append [1] is [0, 1]', () => {
|
|
expect($na(append(_na([0]))(_na([1])))).toEqual([0, 1]);
|
|
});
|
|
|
|
it('[0, 1] append [2, 3] is [0, 1, 2, 3]', () => {
|
|
expect($na(append(_na([0, 1]))(_na([2, 3])))).toEqual([0, 1, 2, 3]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(append(infinite)(infinite)(undef)(_(x => _(_xs => x))))).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('concat', () => {
|
|
it('concat [Nil, Nil, Nil] is Nil', () => {
|
|
expect($na(concat(cons(Nil)(cons(Nil)(cons(Nil)(Nil)))))).toEqual([]);
|
|
});
|
|
|
|
it('concat [[0], [1], [2]] is [0, 1, 2]', () => {
|
|
expect($na(concat(cons(_na([0]))(cons(_na([1]))(cons(_na([2]))(Nil)))))).toEqual([0, 1, 2]);
|
|
});
|
|
|
|
it('concat [[0, 1], [2, 3], [4, 5]] is [0, 1, 2, 3, 4, 5]', () => {
|
|
expect($na(concat(cons(_na([0, 1]))(cons(_na([2, 3]))(cons(_na([4, 5]))(Nil)))))).toEqual([0, 1, 2, 3, 4, 5]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
const dinfinite: List<Num> = cons(infinite)(___(() => dinfinite.run()));
|
|
expect($n(concat(dinfinite)(undef)(_(x => _(_xs => x))))).toBe(0);
|
|
});
|
|
});
|
|
|
|
// describe('repeat', () => {
|
|
// it('repeat 0 is [0, 0, 0, ...]', () => {
|
|
// expect($na(
|
|
// repeat(_n(0))<List<Num>>(_((x0: Num) => _((x1s: List<Num>) =>
|
|
// x1s<List<Num>>(_((x1: Num) => _((x2s: List<Num>) =>
|
|
// x2s<List<Num>>(_((x2: Num) => _((_xrs: List<Num>): List<Num> => cons(x0)(cons(x1)(cons(x2)(Nil)))))))))))))).toEqual([0, 0, 0]);
|
|
// });
|
|
// });
|
|
|
|
// describe('iterate', () => {
|
|
// it('iterate succ 0 is [0, 1, 2, ...]', () => {
|
|
// expect($na(
|
|
// iterate(_n(0))<List<Num>>(_((x0: Num) => _((x1s: List<Num>) =>
|
|
// x1s<List<Num>>(_((x1: Num) => _((x2s: List<Num>) =>
|
|
// x2s<List<Num>>(_((x2: Num) => _((_xrs: List<Num>): List<Num> => cons(x0)(cons(x1)(cons(x2)(Nil)))))))))))))).toEqual([0, 1, 2]);
|
|
// });
|
|
// });
|
|
|
|
describe('head', () => {
|
|
it('returns nothing from Nil', () => {
|
|
expect(() => $$(head(Nil))).toThrow();
|
|
});
|
|
|
|
it('returns only item', () => {
|
|
expect($n(head(_na([0])))).toBe(0);
|
|
});
|
|
|
|
it('returns first item', () => {
|
|
expect($n(head(_na([0, 1])))).toBe(0);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(head(infinite))).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('tail', () => {
|
|
it('drops nothing from Nil', () => {
|
|
expect(() => $na(tail(Nil))).toThrow();
|
|
});
|
|
|
|
it('drops only item', () => {
|
|
expect($na(tail(_na([0])))).toEqual([]);
|
|
});
|
|
|
|
it('drops first item', () => {
|
|
expect($na(tail(_na([0, 1])))).toEqual([1]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(head(tail(infinite)))).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('foldl', () => {
|
|
it('folds Nil', () => {
|
|
expect($n(foldl(sum)(Zero)(_na([])))).toBe(0);
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect($n(foldl(sum)(Zero)(_na([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect($n(foldl(sum)(Zero)(_na([1, 2])))).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('foldlz', () => {
|
|
it('does not fold Nil', () => {
|
|
expect(() => $n(foldlz(sum)(Nil))).toThrow();
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect($n(foldlz(sum)(_na([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect($n(foldlz(sum)(_na([1, 2])))).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('foldr', () => {
|
|
it('folds Nil', () => {
|
|
expect($n(foldr(sum)(Zero)(_na([])))).toBe(0);
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect($n(foldr(sum)(Zero)(_na([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect($n(foldr(sum)(Zero)(_na([1, 2])))).toBe(3);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(
|
|
foldr
|
|
(_(x => _(a => sum(x)(iif(eqn(_n(5))(x))(Zero)(a)))))
|
|
(Zero)
|
|
(infinite)))
|
|
.toBe(15);
|
|
});
|
|
});
|
|
|
|
describe('foldrz', () => {
|
|
it('does not fold Nil', () => {
|
|
expect(() => $n(foldrz(sum)(Nil))).toThrow();
|
|
});
|
|
|
|
it.failing('folds single item', () => {
|
|
expect($n(foldrz(sum)(_na([1])))).toBe(1);
|
|
});
|
|
|
|
it.failing('folds several items', () => {
|
|
expect($n(foldrz(sum)(_na([1, 2])))).toBe(3);
|
|
});
|
|
|
|
it.failing('handles infinite lists', () => {
|
|
expect($n(
|
|
foldrz
|
|
(_(x => _(a => sum(x)(iif(eqn(_n(5))(x))(Zero)(a)))))
|
|
(infinite)))
|
|
.toBe(15);
|
|
});
|
|
});
|
|
|
|
describe('cmp', () => {
|
|
it('Nil cmp Nil is EQ', () => {
|
|
expect($o(cmp(cmpn)(_na([]))(_na([])))).toEqual(NOrd.EQ);
|
|
});
|
|
|
|
it('Nil cmp [0, 1, 2] is LT', () => {
|
|
expect($o(cmp(cmpn)(_na([]))(_na([0, 1, 2])))).toEqual(NOrd.LT);
|
|
});
|
|
|
|
it('[0, 1, 2] cmp Nil is GT', () => {
|
|
expect($o(cmp(cmpn)(_na([0, 1, 2]))(_na([])))).toEqual(NOrd.GT);
|
|
});
|
|
|
|
it('[0] cmp [0, 1, 2] is LT', () => {
|
|
expect($o(cmp(cmpn)(_na([0]))(_na([0, 1, 2])))).toEqual(NOrd.LT);
|
|
});
|
|
|
|
it('[0, 1, 2] cmp [0] is GT', () => {
|
|
expect($o(cmp(cmpn)(_na([0, 1, 2]))(_na([0])))).toEqual(NOrd.GT);
|
|
});
|
|
|
|
it('[1, 2, 0] cmp [0, 1, 2] is GT', () => {
|
|
expect($o(cmp(cmpn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(NOrd.GT);
|
|
});
|
|
|
|
it('[0, 1, 2] cmp [1, 2, 0] is LT', () => {
|
|
expect($o(cmp(cmpn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(NOrd.LT);
|
|
});
|
|
|
|
it('[0, 1, 2] cmp [0, 1, 2] is EQ', () => {
|
|
expect($o(cmp(cmpn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(NOrd.EQ);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($o(cmp(cmpn)(_na([0, 1, 2]))(infinite))).toEqual(NOrd.LT);
|
|
expect($o(cmp(cmpn)(infinite)(_na([0, 1, 2])))).toEqual(NOrd.GT);
|
|
});
|
|
});
|
|
|
|
describe('lt', () => {
|
|
it('Nil lt Nil is False', () => {
|
|
expect($b(lt(cmpn)(_na([]))(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('Nil lt [0, 1, 2] is True', () => {
|
|
expect($b(lt(cmpn)(_na([]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] lt Nil is False', () => {
|
|
expect($b(lt(cmpn)(_na([0, 1, 2]))(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('[0] lt [0, 1, 2] is True', () => {
|
|
expect($b(lt(cmpn)(_na([0]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] lt [0] is False', () => {
|
|
expect($b(lt(cmpn)(_na([0, 1, 2]))(_na([0])))).toEqual(false);
|
|
});
|
|
|
|
it('[1, 2, 0] lt [0, 1, 2] is False', () => {
|
|
expect($b(lt(cmpn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] lt [1, 2, 0] is True', () => {
|
|
expect($b(lt(cmpn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] lt [0, 1, 2] is False', () => {
|
|
expect($b(lt(cmpn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(lt(cmpn)(_na([0, 1, 2]))(infinite))).toEqual(true);
|
|
expect($b(lt(cmpn)(infinite)(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe('le', () => {
|
|
it('Nil le Nil is True', () => {
|
|
expect($b(le(cmpn)(_na([]))(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('Nil le [0, 1, 2] is True', () => {
|
|
expect($b(le(cmpn)(_na([]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] le Nil is False', () => {
|
|
expect($b(le(cmpn)(_na([0, 1, 2]))(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('[0] le [0, 1, 2] is True', () => {
|
|
expect($b(le(cmpn)(_na([0]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] le [0] is False', () => {
|
|
expect($b(le(cmpn)(_na([0, 1, 2]))(_na([0])))).toEqual(false);
|
|
});
|
|
|
|
it('[1, 2, 0] le [0, 1, 2] is False', () => {
|
|
expect($b(le(cmpn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] le [1, 2, 0] is True', () => {
|
|
expect($b(le(cmpn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] le [0, 1, 2] is True', () => {
|
|
expect($b(le(cmpn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(le(cmpn)(_na([0, 1, 2]))(infinite))).toEqual(true);
|
|
expect($b(le(cmpn)(infinite)(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe('eq', () => {
|
|
it('Nil eq Nil is True', () => {
|
|
expect($b(eq(eqn)(_na([]))(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('Nil eq [0, 1, 2] is False', () => {
|
|
expect($b(eq(eqn)(_na([]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] eq Nil is False', () => {
|
|
expect($b(eq(eqn)(_na([0, 1, 2]))(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('[0] eq [0, 1, 2] is False', () => {
|
|
expect($b(eq(eqn)(_na([0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] eq [0] is False', () => {
|
|
expect($b(eq(eqn)(_na([0, 1, 2]))(_na([0])))).toEqual(false);
|
|
});
|
|
|
|
it('[1, 2, 0] eq [0, 1, 2] is False', () => {
|
|
expect($b(eq(eqn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] eq [1, 2, 0] is False', () => {
|
|
expect($b(eq(eqn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] eq [0, 1, 2] is True', () => {
|
|
expect($b(eq(eqn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(eq(eqn)(_na([0, 1, 2]))(infinite))).toEqual(false);
|
|
expect($b(eq(eqn)(infinite)(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe('ge', () => {
|
|
it('Nil ge Nil is True', () => {
|
|
expect($b(ge(cmpn)(_na([]))(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('Nil ge [0, 1, 2] is False', () => {
|
|
expect($b(ge(cmpn)(_na([]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] ge Nil is True', () => {
|
|
expect($b(ge(cmpn)(_na([0, 1, 2]))(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('[0] ge [0, 1, 2] is False', () => {
|
|
expect($b(ge(cmpn)(_na([0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] ge [0] is True', () => {
|
|
expect($b(ge(cmpn)(_na([0, 1, 2]))(_na([0])))).toEqual(true);
|
|
});
|
|
|
|
it('[1, 2, 0] ge [0, 1, 2] is True', () => {
|
|
expect($b(ge(cmpn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] ge [1, 2, 0] is False', () => {
|
|
expect($b(ge(cmpn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] ge [0, 1, 2] is True', () => {
|
|
expect($b(ge(cmpn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(ge(cmpn)(_na([0, 1, 2]))(infinite))).toEqual(false);
|
|
expect($b(ge(cmpn)(infinite)(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('gt', () => {
|
|
it('Nil gt Nil is False', () => {
|
|
expect($b(gt(cmpn)(_na([]))(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('Nil gt [0, 1, 2] is False', () => {
|
|
expect($b(gt(cmpn)(_na([]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] gt Nil is True', () => {
|
|
expect($b(gt(cmpn)(_na([0, 1, 2]))(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('[0] gt [0, 1, 2] is False', () => {
|
|
expect($b(gt(cmpn)(_na([0]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] gt [0] is True', () => {
|
|
expect($b(gt(cmpn)(_na([0, 1, 2]))(_na([0])))).toEqual(true);
|
|
});
|
|
|
|
it('[1, 2, 0] gt [0, 1, 2] is True', () => {
|
|
expect($b(gt(cmpn)(_na([1, 2, 0]))(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('[0, 1, 2] gt [1, 2, 0] is False', () => {
|
|
expect($b(gt(cmpn)(_na([0, 1, 2]))(_na([1, 2, 0])))).toEqual(false);
|
|
});
|
|
|
|
it('[0, 1, 2] gt [0, 1, 2] is False', () => {
|
|
expect($b(gt(cmpn)(_na([0, 1, 2]))(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(gt(cmpn)(_na([0, 1, 2]))(infinite))).toEqual(false);
|
|
expect($b(gt(cmpn)(infinite)(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('map', () => {
|
|
it('maps Nil', () => {
|
|
expect($na(map(succ)(_na([])))).toEqual([]);
|
|
});
|
|
|
|
it('maps only item', () => {
|
|
expect($na(map(succ)(_na([0])))).toEqual([1]);
|
|
});
|
|
|
|
it('maps several items', () => {
|
|
expect($na(map(succ)(_na([0, 1])))).toEqual([1, 2]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(head(map(succ)(infinite)))).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('filter', () => {
|
|
it('filter Nil', () => {
|
|
expect($na(filter(odd)(_na([])))).toEqual([]);
|
|
});
|
|
|
|
it('filters only item', () => {
|
|
expect($na(filter(odd)(_na([])))).toEqual([]);
|
|
});
|
|
|
|
it('filters several items', () => {
|
|
expect($na(filter(odd)(_na([0, 1])))).toEqual([1]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(head(filter(odd)(infinite)))).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('any', () => {
|
|
it('any Nil is False', () => {
|
|
expect($b(any(odd)(_na([])))).toEqual(false);
|
|
});
|
|
|
|
it('any for no matching item is False', () => {
|
|
expect($b(any(odd)(_na([0, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('any for matching item is True', () => {
|
|
expect($b(any(odd)(_na([0, 1, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(any(odd)(infinite))).toEqual(true);
|
|
});
|
|
});
|
|
|
|
describe('all', () => {
|
|
it('all Nil is True', () => {
|
|
expect($b(all(even)(_na([])))).toEqual(true);
|
|
});
|
|
|
|
it('all for non-matching item is False', () => {
|
|
expect($b(all(even)(_na([0, 1, 2])))).toEqual(false);
|
|
});
|
|
|
|
it('all for no non-matching item is True', () => {
|
|
expect($b(all(even)(_na([0, 2])))).toEqual(true);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($b(all(even)(infinite))).toEqual(false);
|
|
});
|
|
});
|
|
|
|
describe('get', () => {
|
|
it('returns nothing from Nil', () => {
|
|
expect(() => $n(get(_n(0))(Nil))).toThrow();
|
|
});
|
|
|
|
it('returns nothing with out of bounds index', () => {
|
|
expect(() => $n(get(_n(100))(_na([0, 1, 2, 3])))).toThrow();
|
|
});
|
|
|
|
it('[0, 1, 2, 3][0] is 0', () => {
|
|
expect($n(get(_n(0))(_na([0, 1, 2, 3])))).toBe(0);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][1] is 1', () => {
|
|
expect($n(get(_n(1))(_na([0, 1, 2, 3])))).toBe(1);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][2] is 2', () => {
|
|
expect($n(get(_n(2))(_na([0, 1, 2, 3])))).toBe(2);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][3] get 3 is 3', () => {
|
|
expect($n(get(_n(3))(_na([0, 1, 2, 3])))).toBe(3);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(get(_n(4))(infinite))).toBe(4);
|
|
});
|
|
});
|
|
|
|
describe('set', () => {
|
|
it('can not update Nil', () => {
|
|
expect(() => $na(set(_n(10))(_n(0))(Nil))).toThrow();
|
|
});
|
|
|
|
it('can not update out of bounds index', () => {
|
|
expect(() => $na(set(_n(10))(_n(100))(_na([0, 1, 2, 3])))).toThrow();
|
|
});
|
|
|
|
it('[0, 1, 2, 3][0] = 10 is [10, 1, 2, 3]', () => {
|
|
expect($na(set(_n(10))(_n(0))(_na([0, 1, 2, 3])))).toEqual([10, 1, 2, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][1] = 10 is [0, 10, 2, 3]', () => {
|
|
expect($na(set(_n(10))(_n(1))(_na([0, 1, 2, 3])))).toEqual([0, 10, 2, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][2] = 10 is [0, 1, 10, 3]', () => {
|
|
expect($na(set(_n(10))(_n(2))(_na([0, 1, 2, 3])))).toEqual([0, 1, 10, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][3] = 10 is [0, 1, 2, 10]', () => {
|
|
expect($na(set(_n(10))(_n(3))(_na([0, 1, 2, 3])))).toEqual([0, 1, 2, 10]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(get(_n(4))(set(_n(10))(_n(4))(infinite)))).toBe(10);
|
|
});
|
|
});
|
|
|
|
describe('update', () => {
|
|
it('can not update Nil', () => {
|
|
expect(() => $na(update(succ)(_n(0))(Nil))).toThrow();
|
|
});
|
|
|
|
it('can not update out of bounds index', () => {
|
|
expect(() => $na(update(succ)(_n(100))(_na([0, 1, 2, 3])))).toThrow();
|
|
});
|
|
|
|
it('[0, 1, 2, 3][0]++ is [10, 1, 2, 3]', () => {
|
|
expect($na(update(succ)(_n(0))(_na([0, 1, 2, 3])))).toEqual([1, 1, 2, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][1]++ is [0, 10, 2, 3]', () => {
|
|
expect($na(update(succ)(_n(1))(_na([0, 1, 2, 3])))).toEqual([0, 2, 2, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][2]++ is [0, 1, 10, 3]', () => {
|
|
expect($na(update(succ)(_n(2))(_na([0, 1, 2, 3])))).toEqual([0, 1, 3, 3]);
|
|
});
|
|
|
|
it('[0, 1, 2, 3][3]++ is [0, 1, 2, 10]', () => {
|
|
expect($na(update(succ)(_n(3))(_na([0, 1, 2, 3])))).toEqual([0, 1, 2, 4]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(get(_n(4))(update(succ)(_n(4))(infinite)))).toBe(5);
|
|
});
|
|
});
|
|
|
|
describe('intersperse', () => {
|
|
it('0 intersperse Nil is Nil', () => {
|
|
expect($na(intersperse(_n(0))(Nil))).toEqual([]);
|
|
});
|
|
|
|
it('0 intersperse [1] is [1]', () => {
|
|
expect($na(intersperse(_n(0))(_na([1])))).toEqual([1]);
|
|
});
|
|
|
|
it('0 intersperse [1, 2, 3] is [1, 0, 2, 0, 3]', () => {
|
|
expect($na(intersperse(_n(0))(_na([1, 2, 3])))).toEqual([1, 0, 2, 0, 3]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($n(head(intersperse(_n(0))(infinite)))).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe('show', () => {
|
|
it('show Nil is "[]"', () => {
|
|
expect($s(show(nshow)(_na([])))).toBe('[]');
|
|
});
|
|
|
|
it('show [0] is "[0]"', () => {
|
|
expect($s(show(nshow)(_na([0])))).toBe('[0]');
|
|
});
|
|
|
|
it('show [0, 1, 2] is "[0, 1, 2]"', () => {
|
|
expect($s(show(nshow)(_na([0, 1, 2])))).toBe('[0, 1, 2]');
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect($c(head(show(nshow)(infinite)))).toBe('[');
|
|
});
|
|
});
|
|
});
|
|
|