Transformer architecture implemented in TypeScript with WebGPU.
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.
 
 
 
 
nn/test/group-queue.spec.ts

57 lines
2.8 KiB

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<T>(list: List<QueueItem<T>>): 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<T>(queue: GroupQueue<T>, value: T): T[][] {
queue.insert(value);
return [...queue].map(json);
}
it('0 -> []', () => expect([...insert(new GroupQueue<number>([]), 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<T>(queue: GroupQueue<T>, value: T): T[][] {
const item = queue.itemOf(value);
if (item) {
queue.remove(item);
}
return [...queue].map(json);
}
it('[] - 0', () => expect([...remove(new GroupQueue<number>([]), 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<T>(queue: GroupQueue<T>): [T[], T[][]] {
return [[...queue.pop() ?? []].map(({ value }) => value), [...queue].map(json)];
}
it('[]', () => expect([...pop(new GroupQueue<number>([]))]).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']]]));
});
});