forked from choskim/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 8
/
singly_list_spec.js
200 lines (159 loc) · 5.03 KB
/
singly_list_spec.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
describe("SinglyList", function() {
var singly;
beforeEach(function() {
singly = new SinglyList();
});
it("should have an initial length of 0", function() {
expect(singly._length).toEqual(0);
});
it("should have a head that is null", function() {
expect(singly.head).toEqual(null);
});
describe("#add(data)", function() {
describe("first invocation with 'first'", function() {
var head = null;
beforeEach(function() {
head = singly.add("first");
});
it("should have a length of 1", function() {
expect(singly._length).toEqual(1);
});
it("should return head", function() {
expect(head).toEqual(singly.head);
});
it("should have 'first' as head's data", function() {
expect(head.data).toEqual('first');
});
it("should have null as head's next", function() {
expect(head.next).toEqual(null);
});
});
describe("second invocation with 'second'", function() {
var head = null,
node = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
});
it("should have a length of 2", function() {
expect(singly._length).toEqual(2);
});
it("should have 'second' as node's data", function() {
expect(node.data).toEqual('second');
});
it("should have node as head's next", function() {
expect(head.next).toEqual(node);
});
it("should have null as node's next", function() {
expect(node.next).toEqual(null);
});
});
});
describe("#searchNodeAt(position)", function() {
describe("with an invalid position", function() {
var head = null,
node = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
});
it("should throw an error with a position of -1", function() {
expect(function() {
singly.searchNodeAt(-1);
}).toThrowError("Failure: non-existent node in this list.");
});
it("should throw an error with a position of 20", function() {
expect(function() {
singly.searchNodeAt(20);
}).toThrowError("Failure: non-existent node in this list.");
});
});
describe("with a position of 1", function() {
var head = null,
node = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
});
it("should return head", function() {
expect(singly.searchNodeAt(1)).toEqual(head);
});
});
describe("with a position of 2", function() {
var head = null,
node = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
});
it("should return node", function() {
expect(singly.searchNodeAt(2)).toEqual(node);
});
});
});
describe("#remove(position)", function() {
describe("at an invalid position", function() {
var head = null,
node = null,
deletedNode = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
});
it("should throw an error with a position of -1", function() {
expect(function() {
singly.remove(-1);
}).toThrowError("Failure: non-existent node in this list.");
});
it("should throw an error with a position of 20", function() {
expect(function() {
singly.remove(20);
}).toThrowError("Failure: non-existent node in this list.");
});
});
describe("at 1", function() {
var head = null,
node = null,
deletedNode = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
deletedNode = singly.remove(1);
});
it("should have a length of 1", function() {
expect(singly._length).toEqual(1);
});
it("should return head", function() {
expect(deletedNode).toEqual(head);
});
it("should have node as head", function() {
expect(singly.head).toEqual(node);
});
it("should have null as head's next", function() {
expect(singly.head.next).toEqual(null);
});
});
describe("at 2", function() {
var head = null,
node = null,
deletedNode = null;
beforeEach(function() {
head = singly.add("first");
node = singly.add("second");
deletedNode = singly.remove(2);
});
it("should have a length of 1", function() {
expect(singly._length).toEqual(1);
});
it("should return node", function() {
expect(deletedNode).toEqual(node);
});
it("should have head as head", function() {
expect(singly.head).toEqual(head);
});
it("should have node as head's next", function() {
expect(singly.head.next).toEqual(null);
});
});
});
});