Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Created June 15, 2014 18:00
Show Gist options
  • Save joshuakfarrar/5ca19303cd684ae8c7a8 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/5ca19303cd684ae8c7a8 to your computer and use it in GitHub Desktop.
queue written using TDD
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;
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