import { describe, expect, it } from '@jest/globals'; import { GroupQueue, QueueItem } from '../src/data/group-queue'; import { List } from '../src/data/list'; describe('GroupQueue', () => { function json(list: List>): T[] { return [...list].map(({ value }) => value); } describe('from array', () => { it('[]', () => expect([...new GroupQueue([])].map(json)).toEqual([])); it('[1]', () => expect([...new GroupQueue([1])].map(json)).toEqual([[1]])); it('["b", "c", "b", "c"]', () => expect([...new GroupQueue(['b', 'c', 'b', 'c'])].map(json)).toEqual([['b', 'b'], ['c', 'c']])); }); describe('length', () => { it('[]', () => expect(new GroupQueue([]).length).toEqual(0)); it('[1]', () => expect(new GroupQueue([1]).length).toEqual(1)); it('["b", "c", "b", "c"]', () => expect(new GroupQueue(['b', 'c', 'b', 'c']).length).toEqual(2)); }); describe('insert', () => { function insert(queue: GroupQueue, value: T): T[][] { queue.insert(value); return [...queue].map(json); } it('0 -> []', () => expect([...insert(new GroupQueue([]), 0)]).toEqual([[0]])); it('0 -> [1]', () => expect([...insert(new GroupQueue([1]), 0)]).toEqual([[1], [0]])); it('1 -> [1]', () => expect([...insert(new GroupQueue([1]), 1)]).toEqual([[1, 1]])); it('"d" -> ["b", "c", "b", "c"]', () => expect([...insert(new GroupQueue(['b', 'c', 'b', 'c']), 'd')]).toEqual([['b', 'b'], ['c', 'c'], ['d']])); it('"b" -> ["b", "c", "b", "c"]', () => expect([...insert(new GroupQueue(['b', 'c', 'b', 'c']), 'b')]).toEqual([['b', 'b', 'b'], ['c', 'c']])); }); describe('itemOf / remove', () => { function remove(queue: GroupQueue, value: T): T[][] { const item = queue.itemOf(value); if (item) { queue.remove(item); } return [...queue].map(json); } it('[] - 0', () => expect([...remove(new GroupQueue([]), 0)]).toEqual([])); it('[1] - 0', () => expect([...remove(new GroupQueue([1]), 0)]).toEqual([[1]])); it('[1] - 1', () => expect([...remove(new GroupQueue([1]), 1)]).toEqual([])); it('["b", "c", "b", "c"] - "d"', () => expect([...remove(new GroupQueue(['b', 'c', 'b', 'c']), 'd')]).toEqual([['b', 'b'], ['c', 'c']])); it('["b", "c", "b", "c"] - "b"', () => expect([...remove(new GroupQueue(['b', 'c', 'b', 'c']), 'b')]).toEqual([['c', 'c'], ['b']])); }); describe('pop', () => { function pop(queue: GroupQueue): [T[], T[][]] { return [[...queue.pop() ?? []].map(({ value }) => value), [...queue].map(json)]; } it('[]', () => expect([...pop(new GroupQueue([]))]).toEqual([[], []])); it('[1]', () => expect([...pop(new GroupQueue([1]))]).toEqual([[1], []])); it('["b", "c", "b", "c"]', () => expect([...pop(new GroupQueue(['b', 'c', 'b', 'c']))]).toEqual([['b', 'b'], [['c', 'c']]])); }); });