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.
208 lines
6.7 KiB
208 lines
6.7 KiB
import { describe, expect, it } from '@jest/globals';
|
|
import { $, _, Lazy } from '../src/core';
|
|
import { fromBoolean, iif } from '../src/bool';
|
|
import { toNumber, Zero, sum, succ, Num, Succ, odd } from '../src/num';
|
|
import { Cons, filter, foldl, foldlz, foldr, foldrz, fromArray, fromNumberArray, head, List, map, Nil, tail, toArray, toNumberArray } from '../src/list';
|
|
|
|
describe('List', () => {
|
|
const iterate: (n: number) => List<Lazy<number>> = n => new Lazy(() => Cons(_(n))(iterate(n + 1)).value);
|
|
const niterate: (n: Num) => List<Num> = n => new Lazy(() => Cons(n)(niterate(Succ(n))).value);
|
|
|
|
describe('toArray', () => {
|
|
it('converts []', () => {
|
|
expect(toArray(Nil)).toEqual([]);
|
|
});
|
|
|
|
it('converts [0]', () => {
|
|
expect(toArray(Cons(_(0))(Nil))).toEqual([0]);
|
|
});
|
|
|
|
it('converts [0, 1]', () => {
|
|
expect(toArray(Cons(_(0))(Cons(_(1))(Nil)))).toEqual([0, 1]);
|
|
});
|
|
});
|
|
|
|
describe('fromArray', () => {
|
|
it('converts []', () => {
|
|
expect(toArray(fromArray([]))).toEqual([]);
|
|
});
|
|
|
|
it('converts [0]', () => {
|
|
expect(toArray(fromArray([0].map(_)))).toEqual([0]);
|
|
});
|
|
|
|
it('converts 2', () => {
|
|
expect(toArray(fromArray([0, 1].map(_)))).toEqual([0, 1]);
|
|
});
|
|
});
|
|
|
|
describe('head', () => {
|
|
it('returns nothing from empty list', () => {
|
|
expect(() => head(Nil)).not.toThrow();
|
|
expect(() => $(head(Nil))).toThrow();
|
|
});
|
|
|
|
it('returns only item', () => {
|
|
expect(head).toDeferEvaluationOf(Cons(_(0))(Nil));
|
|
expect(head(Cons(_(0))(Nil))).toEvaluateTo(0);
|
|
});
|
|
|
|
it('returns first item', () => {
|
|
expect(head).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil)));
|
|
expect(head(Cons(_(0))(Cons(_(1))(Nil)))).toEvaluateTo(0);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect(head(iterate(0))).toEvaluateTo(0);
|
|
});
|
|
});
|
|
|
|
describe('tail', () => {
|
|
it('drops nothing from empty list', () => {
|
|
expect(() => tail(Nil)).not.toThrow();
|
|
expect(() => toArray(tail(Nil))).toThrow();
|
|
});
|
|
|
|
it('drops only item', () => {
|
|
expect(tail).toDeferEvaluationOf(Cons(_(0))(Nil));
|
|
expect(toArray(tail(Cons(_(0))(Nil)))).toEqual([]);
|
|
});
|
|
|
|
it('drops first item', () => {
|
|
expect(tail).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil)));
|
|
expect(toArray(tail(Cons(_(0))(Cons(_(1))(Nil))))).toEqual([1]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect(head(tail(iterate(0)))).toEvaluateTo(1);
|
|
});
|
|
});
|
|
|
|
describe('foldl', () => {
|
|
it('folds empty list', () => {
|
|
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([]));
|
|
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([])))).toBe(0);
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
|
|
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
|
|
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1, 2])))).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('foldlz', () => {
|
|
it('does not fold empty list', () => {
|
|
expect(() => foldlz(sum)(Nil)).not.toThrow();
|
|
expect(() => toNumber(foldlz(sum)(Nil))).toThrow();
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
|
|
expect(toNumber(foldlz(sum)(fromNumberArray([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
|
|
expect(toNumber(foldlz(sum)(fromNumberArray([1, 2])))).toBe(3);
|
|
});
|
|
});
|
|
|
|
describe('foldr', () => {
|
|
it('folds empty list', () => {
|
|
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([]));
|
|
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([])))).toBe(0);
|
|
});
|
|
|
|
it('folds single item', () => {
|
|
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
|
|
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1])))).toBe(1);
|
|
});
|
|
|
|
it('folds several items', () => {
|
|
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
|
|
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1, 2])))).toBe(3);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect(foldr<Lazy<number>, Lazy<number>>(x => a =>
|
|
iif<Lazy<number>>
|
|
(x.then(x => fromBoolean(x === 0)))
|
|
(x)
|
|
(x.then(x => a.then(a => _(x + a))))
|
|
)(_(0))(iterate(-5))).toEvaluateTo(-15);
|
|
});
|
|
});
|
|
|
|
describe('foldrz', () => {
|
|
it('does not fold empty list', () => {
|
|
expect(() => foldrz(sum)(Nil)).not.toThrow();
|
|
expect(() => toNumber(foldrz(sum)(Nil))).toThrow();
|
|
});
|
|
|
|
it.failing('folds single item', () => {
|
|
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
|
|
expect(toNumber(foldrz(sum)(fromNumberArray([1])))).toBe(1);
|
|
});
|
|
|
|
it.failing('folds several items', () => {
|
|
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
|
|
expect(toNumber(foldrz(sum)(fromNumberArray([1, 2])))).toBe(3);
|
|
});
|
|
|
|
it.failing('handles infinite lists', () => {
|
|
expect(foldrz<Lazy<number>>(x => a =>
|
|
iif<Lazy<number>>
|
|
(x.then(x => fromBoolean(x === 0)))
|
|
(x)
|
|
(x.then(x => a.then(a => _(x + a))))
|
|
)(iterate(-5))).toEvaluateTo(-10);
|
|
});
|
|
});
|
|
|
|
describe('map', () => {
|
|
it('maps empty list', () => {
|
|
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([]));
|
|
expect(toNumberArray(map(succ)(fromNumberArray([])))).toEqual([]);
|
|
});
|
|
|
|
it('maps only item', () => {
|
|
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0]));
|
|
expect(toNumberArray(map(succ)(fromNumberArray([0])))).toEqual([1]);
|
|
});
|
|
|
|
it('maps several items', () => {
|
|
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1]));
|
|
expect(toNumberArray(map(succ)(fromNumberArray([0, 1])))).toEqual([1, 2]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect(toNumber(head(map(succ)(niterate(Zero))))).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('filter', () => {
|
|
it('filter empty list', () => {
|
|
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([]));
|
|
expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEqual([]);
|
|
});
|
|
|
|
it('filters only item', () => {
|
|
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0]));
|
|
expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEqual([]);
|
|
});
|
|
|
|
it('filters several items', () => {
|
|
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1]));
|
|
expect(toNumberArray(filter(odd)(fromNumberArray([0, 1])))).toEqual([1]);
|
|
});
|
|
|
|
it('handles infinite lists', () => {
|
|
expect(toNumber(head(filter(odd)(niterate(Zero))))).toBe(1);
|
|
});
|
|
});
|
|
});
|
|
|