-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
205 lines (168 loc) · 7.81 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<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="linked-list.js"></script>
</head>
<body>
<h1>Linked List Tests</h1>
<script type="text/javascript">
YAHOO.namespace("test");
YAHOO.test.LinkedList = (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 LinkedList();
},
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 LinkedList();
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 LinkedList();
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 LinkedList();
},
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.LinkedList);
YAHOO.tool.TestRunner.run();
})();
</script>
</body>
</html>