forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.test.js
232 lines (192 loc) · 6.85 KB
/
LinkedList.test.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const { Node, LinkedList } = require('.');
describe('Data Structures: Linked Lists', () => {
describe('Node of a List', () => {
it('Should be a class', () => {
expect(typeof Node.prototype.constructor).toEqual('function');
});
it('Should set the data and next field of a node', () => {
const node = new Node('Hello', null);
expect(node.data).toEqual('Hello');
expect(node.next).toEqual(null);
});
});
describe('LinkedList Instance', () => {
it('Should be a class', () => {
expect(typeof LinkedList.prototype.constructor).toEqual('function');
});
it('Should set the data and next field of a node', () => {
const list = new LinkedList();
expect(list.head).not.toEqual(undefined);
expect(list.head).toEqual(null);
});
});
describe('LinkedList API', () => {
let list = new LinkedList();
beforeEach(() => {
list = new LinkedList();
});
describe('addAtBeginning(value)', () => {
it('Should add element at beginning', () => {
list.addAtBeginning(12);
expect(list.head.data).toEqual(12);
list.addAtBeginning(15);
expect(list.head.data).toEqual(15);
});
it('Should return the 10 as the first element in the list', () => {
list.addAtBeginning(10);
expect(list.getFirst().data).toEqual(10);
});
});
describe('addAtEnd(value)', () => {
it('Should add element at end', () => {
list.addAtBeginning(10);
list.addAtEnd(12);
expect(list.getLast().data).toEqual(12);
});
it('Should add at the beginning if the list is empty', () => {
list.addAtEnd(15);
expect(list.head.data).toEqual(15);
expect(list.head.next).toEqual(null);
});
it('Should return 4 as length of the list after adding at the end', () => {
list.addAtEnd(15);
list.addAtEnd(23);
list.addAtEnd(33);
list.addAtEnd(10);
expect(list.length()).toEqual(4);
});
});
describe('length() of the list', () => {
it('Should return 0 if the list is empty', () => {
expect(list.length()).toEqual(0);
});
it('Should return the present size of the list after adding elements', () => {
list.addAtBeginning(1);
list.addAtBeginning(2);
list.addAtBeginning(3);
expect(list.length()).toEqual(3);
});
it('Should return the present size of the list after removing elements', () => {
list.addAtBeginning(1);
list.addAtBeginning(2);
list.addAtBeginning(3);
expect(list.length()).toEqual(3);
list.removeFromEnd();
list.removeFromBeginning();
expect(list.length()).toEqual(1);
});
});
describe('removeFromBeginning()', () => {
it('Should remove element at front', () => {
list.addAtBeginning(12);
expect(list.removeFromBeginning().data).toEqual(12);
expect(list.length()).toEqual(0);
});
it('Should return the element after removing it', () => {
list.addAtBeginning(15);
list.addAtBeginning(16);
expect(list.removeFromBeginning().data).toEqual(16);
});
it('Should not throw error if the list is empty', () => {
expect(() => list.removeFromBeginning()).not.toThrow();
});
});
describe('removeFromEnd()', () => {
it('Should return `null` for empty list', () => {
expect(list.removeFromEnd()).toEqual(null);
});
it('Should remove element at last', () => {
list.addAtBeginning('Hello');
list.addAtBeginning(14);
list.addAtEnd(15);
expect(list.removeFromEnd().data).toEqual(15);
});
it('Should reduce the lengh of the list', () => {
list.addAtBeginning(14);
list.addAtEnd(15);
expect(list.length()).toEqual(2);
list.removeFromEnd();
expect(list.length()).toEqual(1);
});
it('Should return the last element after removing it', () => {
list.addAtBeginning(14);
list.addAtEnd(15);
expect(list.removeFromEnd().data).toEqual(15);
});
});
describe('getLast()', () => {
it('Should return `null` if the list is empty', () => {
expect(list.getLast()).toEqual(null);
});
it('Should return 10 as the last item in the list', () => {
list.addAtEnd(15);
list.addAtEnd(23);
list.addAtEnd(33);
list.addAtEnd(10);
expect(list.getLast().data).toEqual(10);
});
});
describe('getFirst()', () => {
it('Should return `null` if the list is empty', () => {
expect(list.getFirst()).toEqual(null);
});
it('Should return 15 as the last item in the list', () => {
list.addAtBeginning(15);
list.addAtEnd(23);
list.addAtEnd(33);
list.addAtEnd(10);
expect(list.getFirst().data).toEqual(15);
});
});
describe('Get/Add/Remove at specific positions', () => {
beforeEach(() => {
list.addAtBeginning('Hello');
list.addAtEnd('There!');
list.addAtEnd('Welcome');
});
describe('getAt(index)', () => {
it('Should return `null` for empty list regardless of index value', () => {
list.delete();
expect(list.getAt(10)).toEqual(null);
});
it('Should return the node for given index', () => {
expect(list.getAt(1).data).toEqual('There!');
});
it('Should return the last element for large index', () => {
expect(list.getAt(10).data).toEqual('Welcome');
});
});
describe('addAt(index, value)', () => {
it('Should add at the beginning of empty list', () => {
list.delete();
list.addAt(10, 'Boom');
expect(list.getFirst().data).toEqual('Boom');
});
it('Should add at the end of the list if the index is out of bound', () => {
list.addAtEnd(1010);
list.addAt(10, 'Boom');
expect(list.getLast().data).toEqual('Boom');
});
it('Should add new element at the given position', () => {
list.addAt(2, 'Stranger');
expect(list.getAt(2).data).toEqual('Stranger');
expect(list.getAt(1).data).toEqual('There!');
expect(list.getAt(0).data).toEqual('Hello');
});
});
describe('removeAt(index)', () => {
it('Should return null for empty list', () => {
list.delete();
expect(list.removeAt(10)).toEqual(null);
});
it('Should remove and return last element for large index value', () => {
expect(list.removeAt(10).data).toEqual('Welcome');
});
it('Should remove and return the element at given index value', () => {
expect(list.removeAt(3).data).toEqual('Welcome');
expect(list.removeAt(2).data).toEqual('There!');
});
});
});
});
});