-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrease-key-tests.js
More file actions
53 lines (48 loc) · 1.48 KB
/
decrease-key-tests.js
File metadata and controls
53 lines (48 loc) · 1.48 KB
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
'use strict';
module.exports.run = function (test, Heap) {
test('should throw an exception given a non-existent node', function (t) {
var heap = new Heap();
t.throws(function () {
heap.decreaseKey(undefined, 2);
});
});
test('should throw an exception given a new key larger than the old key', function (t) {
var heap = new Heap();
t.throws(function () {
var node = heap.insert(1, null);
heap.decreaseKey(node, 2);
});
});
test('should decrease the minimum node', function (t) {
var heap = new Heap();
var node1 = heap.insert(1, null);
heap.insert(2, null);
heap.decreaseKey(node1, -3);
var key = heap.findMinimum().key;
t.deepEqual(key, node1.key);
t.is(key, -3);
});
test('should decrease and bubble up a non-minimum node', function (t) {
var heap = new Heap();
heap.insert(1, null);
var node2 = heap.insert(2, null);
heap.decreaseKey(node2, -3);
var key = heap.findMinimum().key;
t.deepEqual(key, node2.key);
t.is(key, -3);
});
test('should decrease and bubble up a non-minimum node in a large heap', function (t) {
var heap = new Heap();
heap.insert(13, null);
heap.insert(26, null);
heap.insert(3, null);
heap.insert(-6, null);
var node5 = heap.insert(27, null);
heap.insert(88, null);
heap.insert(59, null);
heap.insert(-10, null);
heap.insert(16, null);
heap.decreaseKey(node5, -11);
t.deepEqual(heap.findMinimum().key, node5.key);
});
};