|
| 1 | +<html> |
| 2 | +<head> |
| 3 | +<title>Binary Search Tree Tests</title> |
| 4 | +<!-- Combo-handled YUI CSS files: --> |
| 5 | +<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"> |
| 6 | +<!-- Combo-handled YUI JS files: --> |
| 7 | +<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> |
| 8 | +<script type="text/javascript" src="binary-search-tree.js"></script> |
| 9 | + |
| 10 | + |
| 11 | +</head> |
| 12 | +<body> |
| 13 | +<h1>Binary Search Tree Tests</h1> |
| 14 | +<script type="text/javascript"> |
| 15 | + |
| 16 | +YAHOO.namespace("test"); |
| 17 | + |
| 18 | +YAHOO.test.BinarySearchTree = (function(){ |
| 19 | + |
| 20 | + var assert = YAHOO.util.Assert; |
| 21 | + |
| 22 | + //------------------------------------------------------------------------- |
| 23 | + // Base Test Suite |
| 24 | + //------------------------------------------------------------------------- |
| 25 | + |
| 26 | + var suite = new YAHOO.tool.TestSuite("Doubly Linked List Tests"); |
| 27 | + |
| 28 | + //------------------------------------------------------------------------- |
| 29 | + // Test Case for adding |
| 30 | + //------------------------------------------------------------------------- |
| 31 | + |
| 32 | + suite.add(new YAHOO.tool.TestCase({ |
| 33 | + |
| 34 | + name : "add() Tests", |
| 35 | + |
| 36 | + setUp: function(){ |
| 37 | + this.tree = new BinarySearchTree(); |
| 38 | + }, |
| 39 | + |
| 40 | + tearDown: function(){ |
| 41 | + delete this.tree; |
| 42 | + }, |
| 43 | + |
| 44 | + //--------------------------------------------------------------------- |
| 45 | + // Tests |
| 46 | + //--------------------------------------------------------------------- |
| 47 | + |
| 48 | + testAddSingle: function(){ |
| 49 | + this.tree.add(5); |
| 50 | + |
| 51 | + assert.areEqual(1, this.tree.size(), "Tree should have one item."); |
| 52 | + assert.areEqual(5, this.tree._root.value, "First item should have value of 5."); |
| 53 | + }, |
| 54 | + |
| 55 | + testAddMultiple: function(){ |
| 56 | + this.tree.add(5); |
| 57 | + this.tree.add(10); |
| 58 | + |
| 59 | + assert.areEqual(2, this.tree.size(), "Tree should have two items."); |
| 60 | + assert.areEqual(5, this.tree._root.value, "First item should have value of 5."); |
| 61 | + }, |
| 62 | + |
| 63 | + testAddDuplicates: function(){ |
| 64 | + this.tree.add(5); |
| 65 | + this.tree.add(5); |
| 66 | + |
| 67 | + assert.areEqual(1, this.tree.size(), "Tree should have one item."); |
| 68 | + } |
| 69 | + })); |
| 70 | + |
| 71 | + //------------------------------------------------------------------------- |
| 72 | + // Test Case for contains() |
| 73 | + //------------------------------------------------------------------------- |
| 74 | + |
| 75 | + suite.add(new YAHOO.tool.TestCase({ |
| 76 | + |
| 77 | + name : "contains() Tests", |
| 78 | + |
| 79 | + setUp: function(){ |
| 80 | + this.tree = new BinarySearchTree(); |
| 81 | + }, |
| 82 | + |
| 83 | + tearDown: function(){ |
| 84 | + delete this.tree; |
| 85 | + }, |
| 86 | + |
| 87 | + //--------------------------------------------------------------------- |
| 88 | + // Tests |
| 89 | + //--------------------------------------------------------------------- |
| 90 | + |
| 91 | + testContains: function(){ |
| 92 | + this.tree.add(5); |
| 93 | + |
| 94 | + assert.isTrue(this.tree.contains(5)); |
| 95 | + assert.isFalse(this.tree.contains(10)); |
| 96 | + |
| 97 | + } |
| 98 | + })); |
| 99 | + |
| 100 | + //------------------------------------------------------------------------- |
| 101 | + // Test Case for removing values |
| 102 | + //------------------------------------------------------------------------- |
| 103 | + |
| 104 | + suite.add(new YAHOO.tool.TestCase({ |
| 105 | + |
| 106 | + name : "remove() Tests", |
| 107 | + |
| 108 | + setUp: function(){ |
| 109 | + this.tree = new BinarySearchTree(); |
| 110 | + this.tree.add(5); |
| 111 | + this.tree.add(10); |
| 112 | + this.tree.add(6); |
| 113 | + }, |
| 114 | + |
| 115 | + tearDown: function(){ |
| 116 | + delete this.tree; |
| 117 | + }, |
| 118 | + |
| 119 | + //--------------------------------------------------------------------- |
| 120 | + // Tests |
| 121 | + //--------------------------------------------------------------------- |
| 122 | + |
| 123 | + testRemoveFirstItem: function(){ |
| 124 | + this.tree.remove(5); |
| 125 | + assert.areEqual(2, this.tree.size(), "There should only be two items left."); |
| 126 | + assert.areEqual(10, this.tree._root.value, "Root value should now be 10."); |
| 127 | + assert.isFalse(this.tree.contains(5)); |
| 128 | + }, |
| 129 | + |
| 130 | + testRemoveFirstItemToo: function(){ |
| 131 | + this.tree.remove(10); |
| 132 | + this.tree.remove(5); |
| 133 | + assert.areEqual(1, this.tree.size(), "There should only be one item left."); |
| 134 | + assert.areEqual(6, this.tree._root.value, "Root value should now be 6."); |
| 135 | + assert.isFalse(this.tree.contains(5)); |
| 136 | + assert.isFalse(this.tree.contains(10)); |
| 137 | + }, |
| 138 | + |
| 139 | + testRemoveMiddleItem: function(){ |
| 140 | + this.tree.remove(10); |
| 141 | + assert.areEqual(2, this.tree.size(), "There should only be two items left."); |
| 142 | + assert.isFalse(this.tree.contains(10)); |
| 143 | + }, |
| 144 | + |
| 145 | + testRemoveLastItem: function(){ |
| 146 | + this.tree.remove(6); |
| 147 | + assert.areEqual(2, this.tree.size(), "There should only be two items left."); |
| 148 | + assert.isFalse(this.tree.contains(6)); |
| 149 | + }, |
| 150 | + |
| 151 | + testRemoveLastAll: function(){ |
| 152 | + this.tree.remove(6); |
| 153 | + this.tree.remove(5); |
| 154 | + this.tree.remove(10); |
| 155 | + assert.areEqual(0, this.tree.size(), "There should only be two items left."); |
| 156 | + assert.isFalse(this.tree.contains(6)); |
| 157 | + assert.isFalse(this.tree.contains(5)); |
| 158 | + assert.isFalse(this.tree.contains(10)); |
| 159 | + } |
| 160 | + })); |
| 161 | + |
| 162 | + //------------------------------------------------------------------------- |
| 163 | + // Test Case for converting to an array |
| 164 | + //------------------------------------------------------------------------- |
| 165 | + |
| 166 | + suite.add(new YAHOO.tool.TestCase({ |
| 167 | + |
| 168 | + name : "toArray() Tests", |
| 169 | + |
| 170 | + setUp: function(){ |
| 171 | + this.tree = new BinarySearchTree(); |
| 172 | + }, |
| 173 | + |
| 174 | + tearDown: function(){ |
| 175 | + delete this.tree; |
| 176 | + }, |
| 177 | + |
| 178 | + //--------------------------------------------------------------------- |
| 179 | + // Tests |
| 180 | + //--------------------------------------------------------------------- |
| 181 | + |
| 182 | + testToArrayForEmptyList: function(){ |
| 183 | + var value = this.tree.toArray(); |
| 184 | + assert.isInstanceOf(Array, value, "Value should be an array."); |
| 185 | + assert.areEqual(0, value.length, "Array should be empty."); |
| 186 | + }, |
| 187 | + |
| 188 | + testToArrayForOneItemList: function(){ |
| 189 | + this.tree.add(5); |
| 190 | + var value = this.tree.toArray(); |
| 191 | + assert.isInstanceOf(Array, value, "Value should be an array."); |
| 192 | + assert.areEqual(1, value.length, "Array should have 1 item."); |
| 193 | + assert.areEqual(5, value[0], "The only item should be 5."); |
| 194 | + }, |
| 195 | + |
| 196 | + testToArrayForTwoItemList: function(){ |
| 197 | + this.tree.add(5); |
| 198 | + this.tree.add(10); |
| 199 | + var value = this.tree.toArray(); |
| 200 | + assert.isInstanceOf(Array, value, "Value should be an array."); |
| 201 | + assert.areEqual(2, value.length, "Array should have 2 items."); |
| 202 | + assert.areEqual(5, value[0], "The first item should be 5."); |
| 203 | + assert.areEqual(10, value[1], "The second item should be 10."); |
| 204 | + }, |
| 205 | + |
| 206 | + testToArrayForMultipleItems: function(){ |
| 207 | + this.tree.add(55); |
| 208 | + this.tree.add(10); |
| 209 | + this.tree.add(29); |
| 210 | + this.tree.add(40); |
| 211 | + this.tree.add(10); |
| 212 | + this.tree.add(5); |
| 213 | + this.tree.add(16); |
| 214 | + this.tree.add(25); |
| 215 | + |
| 216 | + var value = this.tree.toArray(); |
| 217 | + assert.isInstanceOf(Array, value, "Value should be an array."); |
| 218 | + assert.areEqual(7, value.length, "Array should have 7 items."); |
| 219 | + YAHOO.util.ArrayAssert.itemsAreEqual([5, 10, 16, 25, 29, 40, 55], value); |
| 220 | + |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | + })); |
| 226 | + |
| 227 | + //return it |
| 228 | + return suite; |
| 229 | + |
| 230 | +})(); |
| 231 | + |
| 232 | +(function (){ |
| 233 | + //create the logger |
| 234 | + var logger = new YAHOO.tool.TestLogger(); |
| 235 | + |
| 236 | + //add the tests |
| 237 | + YAHOO.tool.TestRunner.add(YAHOO.test.BinarySearchTree); |
| 238 | + YAHOO.tool.TestRunner.run(); |
| 239 | + |
| 240 | +})(); |
| 241 | + |
| 242 | + |
| 243 | +</script> |
| 244 | +</body> |
| 245 | +</html> |
0 commit comments