Skip to content

Commit a65c831

Browse files
committed
Add unit test
1 parent 94e6224 commit a65c831

File tree

4 files changed

+132
-35
lines changed

4 files changed

+132
-35
lines changed

src/index.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,10 @@ import { IPriorityQueue } from "../typing/priority-queue";
33
export class PriorityQueue<T> implements IPriorityQueue<T> {
44
private queue: T[];
55
private comparator: (item1: T, item2: T) => boolean;
6-
private swapper: (index1: number, index2: number) => void;
7-
8-
constructor(
9-
queue: T[] = [],
10-
comparator?: (item1: T, item2: T) => boolean,
11-
swapper?: (index1: number, index2: number) => void
12-
) {
13-
this.queue = queue;
14-
this.comparator = comparator;
15-
this.swapper = swapper;
166

17-
if (this.queue.length > 0) {
18-
this._build();
19-
}
7+
constructor(comparator?: (item1: T, item2: T) => boolean) {
8+
this.queue = [];
9+
this.comparator = comparator;
2010
}
2111

2212
/**
@@ -80,16 +70,6 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
8070
return [...this.queue];
8171
}
8272

83-
/**
84-
* Build min or max heap
85-
*/
86-
private _build() {
87-
const mid = Math.floor(this.queue.length / 2);
88-
for (let i = mid - 1; i >= 0; i--) {
89-
this._heapify(i);
90-
}
91-
}
92-
9373
/**
9474
* Compare parent value and children value and swap them if conditions are satisfied
9575
* @param index
@@ -123,13 +103,9 @@ export class PriorityQueue<T> implements IPriorityQueue<T> {
123103
* @param index2
124104
*/
125105
private _swap(index1: number, index2: number) {
126-
if (this.swapper) {
127-
this.swapper(index1, index2);
128-
} else {
129-
const temp = this.queue[index1];
130-
this.queue[index1] = this.queue[index2];
131-
this.queue[index2] = temp;
132-
}
106+
const temp = this.queue[index1];
107+
this.queue[index1] = this.queue[index2];
108+
this.queue[index2] = temp;
133109
}
134110

135111
/**

test/basic.spec.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/fixtures/test-case.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const testCase1 = [2, 7, 4, 1, 8, 1];
2+
export const testCase2 = [4, 3, 4, 3, 2];

test/priority-queue.spec.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { PriorityQueue } from '../src';
2+
import { IPriorityQueue } from '../typing/priority-queue';
3+
import { testCase1, testCase2 } from './fixtures/test-case';
4+
5+
describe('testing priority queue', () => {
6+
let p: IPriorityQueue<number>;
7+
8+
describe('max priority queue', () => {
9+
beforeEach(() => {
10+
p = new PriorityQueue();
11+
});
12+
13+
afterEach(() => {
14+
p = null;
15+
});
16+
17+
it('should return max heap of test case 1', () => {
18+
for (let i = 0; i < testCase1.length; i++) {
19+
p.push(testCase1[i]);
20+
}
21+
const actual = p.toArray();
22+
const expected = [8, 7, 4, 1, 2, 1];
23+
expect(actual).toEqual(expected);
24+
});
25+
26+
it('should return max heap of test case 2', () => {
27+
for (let i = 0; i < testCase2.length; i++) {
28+
p.push(testCase2[i]);
29+
}
30+
const actual = p.toArray();
31+
const expected = [4, 3, 4, 3, 2];
32+
expect(actual).toEqual(expected);
33+
});
34+
35+
it('should get the max value without remove', () => {
36+
for (let i = 0; i < testCase2.length; i++) {
37+
p.push(testCase2[i]);
38+
}
39+
const max = p.top();
40+
expect(max).toEqual(4);
41+
42+
const actual = p.toArray();
43+
const expected = [4, 3, 4, 3, 2];
44+
expect(actual).toEqual(expected);
45+
});
46+
47+
it('should extract the max value & rebuild max heap of test case 2', () => {
48+
for (let i = 0; i < testCase2.length; i++) {
49+
p.push(testCase2[i]);
50+
}
51+
expect(p.size()).toEqual(testCase2.length);
52+
const max = p.pop();
53+
expect(max).toEqual(4);
54+
expect(p.size()).toEqual(testCase2.length - 1);
55+
56+
const actual = p.toArray();
57+
const expected = [4, 3, 2, 3];
58+
expect(actual).toEqual(expected);
59+
});
60+
61+
it('should call empty & call pop and top with empty queue', () => {
62+
for (let i = 0; i < testCase1.length; i++) {
63+
p.push(testCase1[i]);
64+
}
65+
66+
const expected = [8, 7, 4, 2, 1, 1];
67+
let index = 0;
68+
69+
while (!p.empty()) {
70+
const item = p.pop();
71+
expect(item).toEqual(expected[index]);
72+
index++;
73+
}
74+
75+
let item = p.top();
76+
expect(item).toEqual(null);
77+
item = p.pop();
78+
expect(item).toEqual(null);
79+
});
80+
81+
});
82+
83+
describe('min priority queue', () => {
84+
beforeEach(() => {
85+
p = new PriorityQueue(function (a, b) {
86+
return a > b;
87+
});
88+
});
89+
90+
afterEach(() => {
91+
p = null;
92+
});
93+
94+
it('should return min heap of test case 1', () => {
95+
for (let i = 0; i < testCase1.length; i++) {
96+
p.push(testCase1[i]);
97+
}
98+
const actual = p.toArray();
99+
const expected = [1, 2, 1, 7, 8, 4];
100+
expect(actual).toEqual(expected);
101+
});
102+
103+
it('should return min heap of test case 2', () => {
104+
for (let i = 0; i < testCase2.length; i++) {
105+
p.push(testCase2[i]);
106+
}
107+
const actual = p.toArray();
108+
const expected = [2, 3, 4, 4, 3];
109+
expect(actual).toEqual(expected);
110+
});
111+
112+
it('should extract the min value & rebuild min heap of test case 2', () => {
113+
for (let i = 0; i < testCase2.length; i++) {
114+
p.push(testCase2[i]);
115+
}
116+
const min = p.pop();
117+
expect(min).toEqual(2);
118+
119+
const actual = p.toArray();
120+
const expected = [3, 3, 4, 4];
121+
expect(actual).toEqual(expected);
122+
});
123+
});
124+
});

0 commit comments

Comments
 (0)