Created
June 15, 2014 18:00
-
-
Save joshuakfarrar/5ca19303cd684ae8c7a8 to your computer and use it in GitHub Desktop.
queue written using TDD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Queue() { | |
this.elements = []; | |
} | |
Queue.prototype.getSize = function() { | |
return this.elements.length; | |
} | |
Queue.prototype.push = function(element) { | |
this.elements.push(element); | |
} | |
Queue.prototype.pop = function() { | |
return this.elements.shift(); | |
} | |
module.exports = Queue; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
, Queue = require('./queue'); | |
describe('Queue', function() { | |
beforeEach(function(done) { | |
queue = new Queue(); | |
done(); | |
}); | |
it('should initialize empty', function() { | |
assert.equal(0, queue.getSize()); | |
}); | |
it('should return null if attempting to shift while empty', function() { | |
assert.equal(null, queue.pop()); | |
}); | |
it('should have size of 1 when X is pushed onto it', function() { | |
queue.push(0); | |
assert.equal(1, queue.getSize()); | |
}); | |
it('should have size of 2 when X and Y are pushed onto it', function() { | |
queue.push(0); | |
queue.push(1); | |
assert.equal(2, queue.getSize()); | |
}); | |
it('should pop X when X is pushed', function() { | |
queue.push(0); | |
assert.equal(0, queue.pop()); | |
}); | |
it('should pop Y, then X when X and Y are pushed', function() { | |
queue.push(0); | |
queue.push(1); | |
assert.equal(0, queue.pop()); | |
assert.equal(1, queue.pop()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment