Skip to content

Commit

Permalink
Updated doubly linked list documentation and added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed May 1, 2009
1 parent 614312f commit 023aaed
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 68 deletions.
228 changes: 228 additions & 0 deletions data-structures/doubly-linked-list/doubly-linked-list-tests.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<html>
<head>
<title>Linked List Tests</title>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/logger/assets/logger.css&2.7.0/build/yuitest/assets/testlogger.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/logger/logger-min.js&2.7.0/build/yuitest/yuitest-min.js"></script>
<script type="text/javascript" src="doubly-linked-list.js"></script>


</head>
<body>
<h1>Linked List Tests</h1>
<script type="text/javascript">

YAHOO.namespace("test");

YAHOO.test.DoublyLinkedList = (function(){

var assert = YAHOO.util.Assert;

//-------------------------------------------------------------------------
// Base Test Suite
//-------------------------------------------------------------------------

var suite = new YAHOO.tool.TestSuite("Linked List Tests");

//-------------------------------------------------------------------------
// Test Case for adding
//-------------------------------------------------------------------------

suite.add(new YAHOO.tool.TestCase({

name : "add() Tests",

setUp: function(){
this.list = new DoublyLinkedList();
},

tearDown: function(){
delete this.list;
},

//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------

testAddSingle: function(){
this.list.add("Hi");

assert.areEqual(1, this.list.size(), "List should have one item.");
assert.areEqual("Hi", this.list._head.data, "First item should have data of 'Hi'.");
assert.isNull(this.list._head.next, "The next pointer of the first item should be null.");
},

testAddMultiple: function(){
this.list.add("Hi");
this.list.add("Yo");

assert.areEqual(2, this.list.size(), "List should have one item.");
assert.areEqual("Hi", this.list._head.data, "First item should have data of 'Hi'.");
assert.isNull(this.list._head.next.next, "The next pointer of the second item should be null.");
}
}));

//-------------------------------------------------------------------------
// Test Case for retrieving values
//-------------------------------------------------------------------------

suite.add(new YAHOO.tool.TestCase({

name : "item() Tests",

setUp: function(){
this.list = new DoublyLinkedList();
this.list.add("Hi");
this.list.add("Yo");
this.list.add("Bye");
},

tearDown: function(){
delete this.list;
},

//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------

testGetFirstItem: function(){
var value = this.list.item(0);
assert.areEqual("Hi", value, "First item should be 'Hi'.");
},

testGetMiddleItem: function(){
var value = this.list.item(1);
assert.areEqual("Yo", value, "Second item should be 'Yo'.");
},

testGetLastItem: function(){
var value = this.list.item(2);
assert.areEqual("Bye", value, "Second item should be 'Bye'.");
},

testGetInvalidItem: function(){
var value = this.list.item(5);
assert.isNull(value, "Invalid items should return null.");
},


}));

//-------------------------------------------------------------------------
// Test Case for removing values
//-------------------------------------------------------------------------

suite.add(new YAHOO.tool.TestCase({

name : "remove() Tests",

setUp: function(){
this.list = new DoublyLinkedList();
this.list.add("Hi");
this.list.add("Yo");
this.list.add("Bye");
},

tearDown: function(){
delete this.list;
},

//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------

testRemoveFirstItem: function(){
var value = this.list.remove(0);
assert.areEqual("Hi", value, "Removed item should be 'Hi'.");
assert.areEqual(2, this.list.size(), "There should only be two items left.");
assert.areEqual("Yo", this.list.item(0), "First item should now be 'Yo'.");
assert.areEqual("Bye", this.list.item(1), "Last item should now be 'Bye'.");
assert.isNull(this.list.item(2), "Item in position 2 should be null.");
},

testRemoveMiddleItem: function(){
var value = this.list.remove(1);
assert.areEqual("Yo", value, "Removed item should be 'Yo'.");
assert.areEqual(2, this.list.size(), "There should only be two items left.");
assert.areEqual("Hi", this.list.item(0), "First item should now be 'Yo'.");
assert.areEqual("Bye", this.list.item(1), "Last item should now be 'Bye'.");
assert.isNull(this.list.item(2), "Item in position 2 should be null.");
},

testRemoveLastItem: function(){
var value = this.list.remove(2);
assert.areEqual("Bye", value, "Removed item should be 'Bye'.");
assert.areEqual(2, this.list.size(), "There should only be two items left.");
assert.areEqual("Hi", this.list.item(0), "First item should now be 'Hi'.");
assert.areEqual("Yo", this.list.item(1), "Last item should now be 'Yo'.");
assert.isNull(this.list.item(2), "Item in position 2 should be null.");
}
}));

//-------------------------------------------------------------------------
// Test Case for converting to an array
//-------------------------------------------------------------------------

suite.add(new YAHOO.tool.TestCase({

name : "toArray() Tests",

setUp: function(){
this.list = new DoublyLinkedList();
},

tearDown: function(){
delete this.list;
},

//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------

testToArrayForEmptyList: function(){
var value = this.list.toArray();
assert.isInstanceOf(Array, value, "Value should be an array.");
assert.areEqual(0, value.length, "Array should be empty.");
},

testToArrayForOneItemList: function(){
this.list.add("Hi");
var value = this.list.toArray();
assert.isInstanceOf(Array, value, "Value should be an array.");
assert.areEqual(1, value.length, "Array should have 1 item.");
assert.areEqual("Hi", value[0], "The only item should be 'Hi'.");
},

testToArrayForTwoItemList: function(){
this.list.add("Hi");
this.list.add("Yo");
var value = this.list.toArray();
assert.isInstanceOf(Array, value, "Value should be an array.");
assert.areEqual(2, value.length, "Array should have 2 items.");
assert.areEqual("Hi", value[0], "The first item should be 'Hi'.");
assert.areEqual("Yo", value[1], "The second item should be 'Yo'.");
}


}));

//return it
return suite;

})();

(function (){
//create the logger
var logger = new YAHOO.tool.TestLogger();

//add the tests
YAHOO.tool.TestRunner.add(YAHOO.test.DoublyLinkedList);
YAHOO.tool.TestRunner.run();

})();


</script>
</body>
</html>
66 changes: 0 additions & 66 deletions data-structures/doubly-linked-list/doubly-linked-list.htm

This file was deleted.

4 changes: 2 additions & 2 deletions data-structures/doubly-linked-list/doubly-linked-list.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Doubly Linked List implementation in JavaScript
* Copyright (c) 2009 Nicholas C. Zakas
* See LICENSE for details on license.
* MIT Licensed - see LICENSE for details on license.
*/

/**
* A linked list implementation in JavaScript.
* @class LinkedList
* @class DoublyLinkedList
* @constructor
*/
function DoublyLinkedList() {
Expand Down

0 comments on commit 023aaed

Please sign in to comment.