Skip to content

Commit

Permalink
Adding working unit tests, fixed bug in peek
Browse files Browse the repository at this point in the history
  • Loading branch information
timgilbert committed Nov 11, 2012
1 parent d68290b commit 021a790
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
33 changes: 19 additions & 14 deletions js-weighted-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var WeightedList = (function() {
this.data = {};
this.length = 0;
this.hasData = false;
console.debug(initial);

initial = typeof initial !== 'undefined' ? initial : [];

Expand Down Expand Up @@ -81,18 +80,22 @@ var WeightedList = (function() {
_push_values: function(key, weight, data) {
//console.debug('k:', key, 'w:', weight, 'd:', data);

// uh, wait, what?
// TODO check for extant key instead of just nuking it
this.weights[key] = weight;

if (data !== null) {
if (typeof data !== 'undefined') {
this.hasData = true;
this.data[key] = data;
}
this.length++;
},

/**
* Add the given weight to the list item with the given key
* Add the given weight to the list item with the given key. Note that if
* the key does not already exist, this operation will silently create it.
*
* @todo might be nice to have a version of this that would throw an error
* on an unknown key.
*/
addWeight: function(key, weight) {
this.weights[key] += weight;
Expand All @@ -104,11 +107,17 @@ var WeightedList = (function() {
* from the list. (This is what the pop() method does.)
*/
peek: function(n, andRemove) {
if (n === null) {
if (typeof n === 'undefined') {
n = 1;
}
andRemove = !!andRemove;


if (this.length - n < 0) {
throw 'Stack underflow! Tried to retrieve ' + n +
' element' + (n === 1 ? '' : 's') +
' from a list of ' + this.length;
}

heap = this._buildWeightedHeap();
//console.debug('heap:', heap);
result = [];
Expand Down Expand Up @@ -141,20 +150,16 @@ var WeightedList = (function() {
*
*/
pop: function(n) {
return peek(n, true);
return this.peek(n, true);
},

/**
* Build a WeightedHeap instance based on the data we've got
*/
_buildWeightedHeap: function() {
var items = [];
for (var key in this.weights) {
// skip over Object.prototype monkey-patching per
// http://bonsaiden.github.com/JavaScript-Garden/#object.forinloop
if (this.weights.hasOwnProperty(key)) {
items.push([key, this.weights[key]]);
}
for (var key in this.weights) if (this.weights.hasOwnProperty(key)) {
items.push([key, this.weights[key]]);
}
//console.log('items',items);
return new _WeightedHeap(items);
Expand Down Expand Up @@ -184,7 +189,7 @@ var WeightedList = (function() {
var value = items[i][0];
this.heap.push(new _HeapNode(weight, value, weight));
}
// Now go through the heap and each node's weight to its parent
// Now go through the heap and add each node's weight to its parent
for (i = this.heap.length - 1; i > 1; i--) {
this.heap[i>>1].total += this.heap[i].total;
}
Expand Down
27 changes: 24 additions & 3 deletions tests/js-weighted-list.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,30 @@ var planets = [
}
});

test("WeightedList constructors", function() {
var wl = new WeightedList();
ok( {}, 'Object');
test('Basic operations', function() {
equal( wl.length, 0, 'Zero length');
deepEqual(wl.shuffle(), [], 'Empty list');
});

test('Error conditions', function() {
raises(function() {
wl.pop();
}, 'Stack underflow');
raises(function() {
wl.peek(2);
}, 'Peeking too much');
});

test('Adding elements by array', function() {
wl.push(['k', 3]);
deepEqual(wl.length, 1);
deepEqual(wl.shuffle(), ['k'], 'Shuffle');
});

test('Adding elements by object', function() {
wl.push({'key': 'x', 'weight': 2});
deepEqual(wl.length, 1);
deepEqual(wl.shuffle(), ['x'], 'Shuffle');
});

})();
Expand Down

0 comments on commit 021a790

Please sign in to comment.