UNPKG

computer-science-in-javascript

Version:

Collection of classic computer science paradigms, algorithms, and approaches written in JavaScript.

229 lines (172 loc) 8.4 kB
<html> <head> <title>Doubly 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>Doubly 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("Doubly 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>