-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathheap.test.js
77 lines (66 loc) · 1.36 KB
/
heap.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Heap from './heap';
describe('Heap', () => {
let heap;
beforeEach(() => {
heap = new Heap();
});
it('exposes the public API', () => {
expect(heap).toHaveProperty('add');
expect(heap).toHaveProperty('extract');
expect(heap).toHaveProperty('toString');
});
});
describe('Max Heap', () => {
let heap;
beforeEach(() => {
heap = new Heap();
});
it('maxHeap.add', () => {
// 乱序加入
heap.add(3);
heap.add(1);
heap.add(5);
heap.add(2);
heap.add(4);
heap.add(6);
expect(heap.toString()).toBe('6,4,5,1,2,3');
});
it('maxHeap.extract', () => {
// 乱序加入
heap.add(1);
heap.add(4);
heap.add(2);
heap.add(5);
heap.add(3);
heap.add(6);
expect(heap.extract()).toBe(6);
expect(heap.toString()).toBe('5,4,2,1,3');
});
});
describe('Min Heap', () => {
let heap;
beforeEach(() => {
heap = new Heap('min');
});
it('minHeap.add', () => {
// 乱序加入
heap.add(3);
heap.add(1);
heap.add(5);
heap.add(2);
heap.add(4);
heap.add(6);
expect(heap.toString()).toBe('1,2,5,3,4,6');
});
it('minHeap.extract', () => {
// 乱序加入
heap.add(1);
heap.add(4);
heap.add(2);
heap.add(5);
heap.add(3);
heap.add(6);
expect(heap.extract()).toBe(1);
expect(heap.toString()).toBe('2,3,6,5,4');
});
});