mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-18 06:57:59 +01:00
71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
|
import { UniqueHeap } from '../unique-heap';
|
||
|
|
||
|
describe('Unique Heap', () => {
|
||
|
interface Result {
|
||
|
name: string;
|
||
|
}
|
||
|
|
||
|
function compare(a: Result, b: Result): boolean {
|
||
|
return a.name < b.name;
|
||
|
}
|
||
|
|
||
|
test('it should return no results when empty', () => {
|
||
|
const heap = new UniqueHeap<Result>(compare, 'name');
|
||
|
expect(heap.topK(5)).toEqual([]);
|
||
|
});
|
||
|
|
||
|
test("doesn't insert duplicate results", () => {
|
||
|
const heap = new UniqueHeap<Result>(compare, 'name');
|
||
|
|
||
|
heap.append({ name: 'name' });
|
||
|
heap.append({ name: 'name' });
|
||
|
|
||
|
expect(heap.topK(2)).toEqual([expect.objectContaining({ name: 'name' })]);
|
||
|
});
|
||
|
|
||
|
test('it should return results in reverse sorted order', () => {
|
||
|
const heap = new UniqueHeap<Result>(compare, 'name');
|
||
|
|
||
|
const names = [
|
||
|
'alpha',
|
||
|
'beta',
|
||
|
'gamma',
|
||
|
'delta',
|
||
|
'epsilon',
|
||
|
'zeta',
|
||
|
'eta',
|
||
|
'theta',
|
||
|
'iota',
|
||
|
'kappa',
|
||
|
'lambda',
|
||
|
'mu',
|
||
|
'nu',
|
||
|
'xi',
|
||
|
'omicron',
|
||
|
'pi',
|
||
|
'rho',
|
||
|
'sigma',
|
||
|
'tau',
|
||
|
'upsilon',
|
||
|
'phi',
|
||
|
'chi',
|
||
|
'psi',
|
||
|
'omega',
|
||
|
];
|
||
|
|
||
|
for (const name of names) {
|
||
|
heap.append({ name });
|
||
|
}
|
||
|
|
||
|
const results = heap.topK(5);
|
||
|
|
||
|
expect(results).toEqual([
|
||
|
expect.objectContaining({ name: 'zeta' }),
|
||
|
expect.objectContaining({ name: 'xi' }),
|
||
|
expect.objectContaining({ name: 'upsilon' }),
|
||
|
expect.objectContaining({ name: 'theta' }),
|
||
|
expect.objectContaining({ name: 'tau' }),
|
||
|
]);
|
||
|
});
|
||
|
});
|