Skip to content

Instantly share code, notes, and snippets.

@onteria
Created May 27, 2011 19:50
Show Gist options
  • Save onteria/996019 to your computer and use it in GitHub Desktop.
Save onteria/996019 to your computer and use it in GitHub Desktop.
Unit Test for linked lists module
/**
* @author onteria
* @description
* @name
*/
var ll = require('./LinkedList');
var assert = require('assert');
exports.testEmptyHead = function() {
var list = new ll.LinkedList([]);
assert.equal(list.head, undefined);
}
exports.testSingleItemPrepend = function() {
var test_object = {value: 'test'};
var list = new ll.LinkedList([test_object]);
assert.deepEqual(test_object, list.head.value);
assert.equal(list.length, 1);
assert.equal(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
}
exports.testSingleItemAppend = function() {
var test_object = {value: 'test'};
var list = new ll.LinkedList([test_object], false);
assert.deepEqual(test_object, list.head.value);
assert.equal(list.length, 1);
assert.equal(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
}
exports.testDoubleItemPrepend = function() {
var test_object = {value: 'test2'};
var head_object = {value: 'test'};
var list = new ll.LinkedList([test_object,head_object]);
assert.deepEqual(head_object, list.head.value);
assert.deepEqual(test_object, list.head.nextNode.value);
assert.equal(list.length, 2);
assert.notEqual(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
assert.deepEqual(head_object, list.head.nextNode.prevNode.value);
}
exports.testDoubleItemAppend = function() {
var test_object = {value: 'test2'};
var head_object = {value: 'test'};
var list = new ll.LinkedList([head_object,test_object], false);
assert.deepEqual(head_object, list.head.value);
assert.deepEqual(test_object, list.head.nextNode.value);
assert.equal(list.length, 2);
assert.notEqual(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
assert.deepEqual(head_object, list.head.nextNode.prevNode.value);
}
exports.testTrippleItemPrepend = function() {
var test_object = {value: 'test2'};
var test2_object = {value: 'test2'};
var head_object = {value: 'test'};
var list = new ll.LinkedList([test2_object,test_object,head_object]);
assert.deepEqual(head_object, list.head.value);
assert.deepEqual(test_object, list.head.nextNode.value);
assert.deepEqual(test2_object, list.head.nextNode.nextNode.value);
assert.equal(list.length, 3);
assert.notEqual(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
assert.notEqual(list.head.nextNode.prevNode, undefined);
assert.notEqual(list.head.nextNode.nextNode, undefined);
assert.notEqual(list.head.nextNode.nextNode.prevNode, undefined);
assert.equal(list.head.nextNode.nextNode.nextNode, undefined);
assert.deepEqual(head_object, list.head.nextNode.prevNode.value);
}
exports.testTrippleItemAppend = function() {
var test_object = {value: 'test2'};
var test2_object = {value: 'test2'};
var head_object = {value: 'test'};
var list = new ll.LinkedList([head_object, test_object, test2_object], false);
assert.deepEqual(head_object, list.head.value);
assert.deepEqual(test_object, list.head.nextNode.value);
assert.deepEqual(test2_object, list.head.nextNode.nextNode.value);
assert.equal(list.length, 3);
assert.notEqual(list.head.nextNode, undefined);
assert.equal(list.head.prevNode, undefined);
assert.notEqual(list.head.nextNode.prevNode, undefined);
assert.notEqual(list.head.nextNode.nextNode, undefined);
assert.notEqual(list.head.nextNode.nextNode.prevNode, undefined);
assert.equal(list.head.nextNode.nextNode.nextNode, undefined);
assert.deepEqual(head_object, list.head.nextNode.prevNode.value);
}
if (module === require.main)
require("test").run(exports);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment