Unit test cases for standardized PriorityQueue and Heap.

This commit is contained in:
Revone 2023-08-19 22:25:06 +08:00
parent e8f06223dd
commit 97b0072ea1
12 changed files with 51 additions and 24 deletions

View file

@ -0,0 +1,5 @@
describe('AbstractGraph Operation Test', () => {
it('should xxx', function () {
expect(true).toBe(true);
});
});

View file

@ -1,6 +1,6 @@
import {DirectedEdge, DirectedGraph, DirectedVertex, VertexId} from '../../../../src';
describe('DirectedGraph Test1', () => {
describe('DirectedGraph Operation Test', () => {
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;
beforeEach(() => {

View file

@ -1,2 +1,2 @@
export * from './directed-graph.test';
export * from './undirected-graph';
export * from './undirected-graph.test';

View file

@ -0,0 +1,5 @@
describe('UndirectedGraph Operation Test', () => {
it('should xxx', function () {
});
});

View file

@ -1,3 +0,0 @@
describe('UndirectedGraph Test1', () => {
});

View file

@ -1,4 +1,4 @@
describe('Heap Test1', () => {
describe('Heap Operation Test', () => {
it('should xxx', function () {
expect(true).toBe(true);
});

View file

@ -1,6 +1,6 @@
import {HeapItem, MaxHeap} from '../../../../src';
describe('MaxHeap Test1', () => {
describe('MaxHeap Operation Test', () => {
it('should object Max Heap operations be proper', function () {
const maxHeap = new MaxHeap<{ keyA: string }>();

View file

@ -1,6 +1,7 @@
import {HeapItem, MinHeap} from '../../../../src';
describe('MinHeap Test1', () => {
describe('MinHeap Operation Test', () => {
it('should numeric Min Heap operations be proper', function () {
const minNumHeap = new MinHeap<number>();
expect(minNumHeap).toBeInstanceOf(MinHeap);

View file

@ -1,6 +1,6 @@
import {MaxPriorityQueue} from '../../../../src';
describe('MaxPriorityQueue Test1', () => {
describe('MaxPriorityQueue Operation Test', () => {
it('should add elements and maintain heap property', () => {
const priorityQueue = new MaxPriorityQueue<number>();
@ -73,4 +73,35 @@ describe('MaxPriorityQueue Test1', () => {
expect(maxPQ.poll()?.keyA).toBe(3);
expect(maxPQ.poll()?.keyA).toBe(1);
});
});
describe('MaxPriorityQueue Performance Test', () => {
it('should the poll method adheres to a time complexity of O(log n) and executed correctly under large scale distinct data', () => {
const magnitude = 10000;
const nodes = Array.from(new Set<number>(Array.from(new Array(magnitude), () => Math.floor(Math.random() * magnitude * 100))));
expect(nodes.length).toBeGreaterThan(magnitude / 2);
const maxPQ = new MaxPriorityQueue<number>({nodes});
let prev = Number.MAX_SAFE_INTEGER;
const startTime = performance.now();
while (maxPQ.size > 0) {
const polled = maxPQ.poll();
if (polled) {
expect(prev).toBeGreaterThan(polled);
prev = polled;
}
}
expect(performance.now() - startTime).toBeLessThan(Math.log2(magnitude) * 1000);
});
it('should sorted.length to be the same as original data', () => {
// const magnitude = 1000;
// const maxPriorityQueue = new MaxPriorityQueue<number>({nodes: Array.from(new Array<number>(magnitude), () => Math.floor(Math.random() * magnitude))});
// const nodeCount = maxPriorityQueue.getNodes().length;
// const sorted = maxPriorityQueue.sort();
//
// expect(sorted.length).toBe(nodeCount); // TODO Plan to support sorting of duplicate elements.
});
});

View file

@ -1,6 +1,6 @@
import {MinPriorityQueue} from '../../../../src';
describe('MinPriorityQueue Test1', () => {
describe('MinPriorityQueue Operation Test', () => {
it('should check if a node exists in the queue', () => {
const priorityQueue = new MinPriorityQueue<number>();
@ -64,17 +64,4 @@ describe('MinPriorityQueue Test1', () => {
expect(sortedArray).toEqual([1, 3, 5, 7]);
});
});
describe('MinPriorityQueue Test2', () => {
it('should sorted.length to be the same as original data', () => {
// const sortCase3: number[] = Array.from(new Array<number>(100), () => Math.floor(Math.random() * 2));
//
// const minPriorityQueue = new MinPriorityQueue<number>({nodes: sortCase3});
// const nodeCount = minPriorityQueue.getNodes().length;
// const sorted = minPriorityQueue.sort();
// expect(sorted.length).toBe(nodeCount); // TODO Plan to support sorting of duplicate elements.
});
});

View file

@ -1,6 +1,6 @@
import {PriorityQueue} from '../../../../src';
describe('PriorityQueue Test1', () => {
describe('PriorityQueue Operation Test', () => {
it('should validate a priority queue', () => {
const minPQ = new PriorityQueue<number>({nodes: [1, 5, 7, 9, 3, 6, 2], comparator: (a, b) => a - b});
@ -13,4 +13,5 @@ describe('PriorityQueue Test1', () => {
comparator: (a, b) => b - a
})).toBe(false);
});
});