Skip to content

Commit d679da5

Browse files
committed
Basic heap specs. typo in heap.
1 parent 6613e2d commit d679da5

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/data-structures/heap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
*
4848
* @public
4949
* @constructor
50-
* @param {Function} cmp Function used for comparition between the elements.
50+
* @param {Function} cmp Function used for comparison between the elements.
5151
*/
5252
exports.Heap = function (cmp) {
5353
this._heap = [];

test/data-structures/heap.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
var mod = require('../../src/data-structures/heap.js');
4+
var Heap = mod.Heap;
5+
6+
describe('Heap', function () {
7+
it('should be a constructor function', function () {
8+
expect(typeof Heap).toBe('function');
9+
});
10+
it('should have default comparison function', function () {
11+
var heap = new Heap();
12+
expect(typeof heap._cmp).toBe('function');
13+
});
14+
it('should add an object properly', function () {
15+
var heap = new Heap();
16+
heap.add(1);
17+
expect(heap._heap[0]).toBe(1);
18+
});
19+
it('should remove an object properly', function () {
20+
var heap = new Heap();
21+
heap.add(1);
22+
var res = heap.extract();
23+
expect(res).toBe(1);
24+
expect(heap._heap.length).toBe(0);
25+
});
26+
it('should add multiple nodes properly', function () {
27+
var heap = new Heap();
28+
heap.add(55);
29+
heap.add(11);
30+
heap.add(66);
31+
expect(heap._heap.indexOf(55)).toBeGreaterThan(-1);
32+
expect(heap._heap.indexOf(11)).toBeGreaterThan(-1);
33+
expect(heap._heap.indexOf(66)).toBeGreaterThan(-1);
34+
});
35+
it('should remove multiple nodes properly (max heap)', function () {
36+
var heap = new Heap();
37+
heap.add(55);
38+
heap.add(11);
39+
heap.add(66);
40+
var res = heap.extract();
41+
expect(res).toBe(66);
42+
res = heap.extract();
43+
expect(res).toBe(55);
44+
res = heap.extract();
45+
expect(res).toBe(11);
46+
});
47+
it('should remove multiple nodes properly (min heap)', function () {
48+
var heap = new Heap(function (a, b) {
49+
return b - a;
50+
});
51+
heap.add(55);
52+
heap.add(11);
53+
heap.add(66);
54+
var res = heap.extract();
55+
expect(res).toBe(11);
56+
res = heap.extract();
57+
expect(res).toBe(55);
58+
res = heap.extract();
59+
expect(res).toBe(66);
60+
});
61+
});

0 commit comments

Comments
 (0)