Debian Codegraph @

packages/collections/tests/src/linkedlist.spec.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2017, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { expect } from 'chai';
11
12 import { find, map } from '@lumino/algorithm';
13
14 import { LinkedList } from '@lumino/collections';
15
16 describe('@lumino/collections', () => {
17 describe('LinkedList', () => {
18 describe('#constructor()', () => {
19 let list = new LinkedList<number>();
20 expect(list).to.be.an.instanceof(LinkedList);
21 });
22
23 describe('#isEmpty', () => {
24 it('should be `true` for an empty list', () => {
25 let list = new LinkedList<number>();
26 expect(list.isEmpty).to.equal(true);
27 });
28
29 it('should be `false` for a non-empty list', () => {
30 let data = [0, 1, 2, 3, 4, 5];
31 let list = LinkedList.from(data);
32 expect(list.isEmpty).to.equal(false);
33 });
34 });
35
36 describe('#length', () => {
37 it('should be `0` for an empty list', () => {
38 let list = new LinkedList<number>();
39 expect(list.length).to.equal(0);
40 });
41
42 it('should equal the number of items in a list', () => {
43 let data = [0, 1, 2, 3, 4, 5];
44 let list = LinkedList.from(data);
45 expect(list.length).to.equal(data.length);
46 });
47 });
48
49 describe('#first', () => {
50 it('should be the first value in the list', () => {
51 let data = [0, 1, 2, 3, 4, 5];
52 let list = LinkedList.from(data);
53 expect(list.first).to.equal(data[0]);
54 });
55
56 it('should be `undefined` if the list is empty', () => {
57 let list = new LinkedList<number>();
58 expect(list.first).to.equal(undefined);
59 });
60 });
61
62 describe('#last', () => {
63 it('should be the last value in the list', () => {
64 let data = [0, 1, 2, 3, 4, 5];
65 let list = LinkedList.from(data);
66 expect(list.last).to.equal(data[data.length - 1]);
67 });
68
69 it('should be `undefined` if the list is empty', () => {
70 let list = new LinkedList<number>();
71 expect(list.last).to.equal(undefined);
72 });
73 });
74
75 describe('#firstNode', () => {
76 it('should be the first node in the list', () => {
77 let data = [0, 1, 2, 3, 4, 5];
78 let list = LinkedList.from(data);
79 expect(list.firstNode!.value).to.equal(data[0]);
80 });
81
82 it('should be `null` if the list is empty', () => {
83 let list = new LinkedList<number>();
84 expect(list.firstNode).to.equal(null);
85 });
86 });
87
88 describe('#lastNode', () => {
89 it('should be the last node in the list', () => {
90 let data = [0, 1, 2, 3, 4, 5];
91 let list = LinkedList.from(data);
92 expect(list.lastNode!.value).to.equal(data[data.length - 1]);
93 });
94
95 it('should be `null` if the list is empty', () => {
96 let list = new LinkedList<number>();
97 expect(list.lastNode).to.equal(null);
98 });
99 });
100
101 describe('[Symbol.iterator]()', () => {
102 it('should return an iterator over the list values', () => {
103 let data = [0, 1, 2, 3, 4, 5];
104 let list = LinkedList.from(data);
105 let it1 = list[Symbol.iterator]();
106 let it2 = list[Symbol.iterator]();
107 expect(it1[Symbol.iterator]()).to.equal(it1);
108 expect(it2[Symbol.iterator]()).to.equal(it2);
109 expect(it1).to.not.equal(it2);
110 expect(Array.from(it1)).to.deep.equal(data);
111 expect(Array.from(it2)).to.deep.equal(data);
112 });
113 });
114
115 describe('#retro()', () => {
116 it('should return a reverse iterator over the list values', () => {
117 let data = [0, 1, 2, 3, 4, 5];
118 let reversed = data.slice().reverse();
119 let list = LinkedList.from(data);
120 let it1 = list.retro();
121 let it2 = list.retro();
122 expect(it1[Symbol.iterator]()).to.equal(it1);
123 expect(it2[Symbol.iterator]()).to.equal(it2);
124 expect(it1).to.not.equal(it2);
125 expect(Array.from(it1)).to.deep.equal(reversed);
126 expect(Array.from(it2)).to.deep.equal(reversed);
127 });
128 });
129
130 describe('#nodes()', () => {
131 it('should return an iterator over the list nodes', () => {
132 let data = [0, 1, 2, 3, 4, 5];
133 let list = LinkedList.from(data);
134 let it1 = list.nodes();
135 let it2 = list.nodes();
136 let v1 = map(it1, n => n.value);
137 let v2 = map(it2, n => n.value);
138 expect(it1[Symbol.iterator]()).to.equal(it1);
139 expect(it2[Symbol.iterator]()).to.equal(it2);
140 expect(it1).to.not.equal(it2);
141 expect(Array.from(v1)).to.deep.equal(data);
142 expect(Array.from(v2)).to.deep.equal(data);
143 });
144 });
145
146 describe('#retroNodes()', () => {
147 it('should return a reverse iterator over the list nodes', () => {
148 let data = [0, 1, 2, 3, 4, 5];
149 let reversed = data.slice().reverse();
150 let list = LinkedList.from(data);
151 let it1 = list.retroNodes();
152 let it2 = list.retroNodes();
153 let v1 = map(it1, n => n.value);
154 let v2 = map(it2, n => n.value);
155 expect(it1[Symbol.iterator]()).to.equal(it1);
156 expect(it2[Symbol.iterator]()).to.equal(it2);
157 expect(it1).to.not.equal(it2);
158 expect(Array.from(v1)).to.deep.equal(reversed);
159 expect(Array.from(v2)).to.deep.equal(reversed);
160 });
161 });
162
163 describe('#addFirst()', () => {
164 it('should add a value to the beginning of the list', () => {
165 let list = new LinkedList<number>();
166
167 expect(list.isEmpty).to.equal(true);
168 expect(list.length).to.equal(0);
169 expect(list.first).to.equal(undefined);
170 expect(list.last).to.equal(undefined);
171
172 let n1 = list.addFirst(99);
173
174 expect(list.isEmpty).to.equal(false);
175 expect(list.length).to.equal(1);
176 expect(list.first).to.equal(99);
177 expect(list.last).to.equal(99);
178
179 let n2 = list.addFirst(42);
180
181 expect(list.isEmpty).to.equal(false);
182 expect(list.length).to.equal(2);
183 expect(list.first).to.equal(42);
184 expect(list.last).to.equal(99);
185
186 let n3 = list.addFirst(7);
187
188 expect(list.isEmpty).to.equal(false);
189 expect(list.length).to.equal(3);
190 expect(list.first).to.equal(7);
191 expect(list.last).to.equal(99);
192
193 expect(Array.from(list)).to.deep.equal([7, 42, 99]);
194
195 expect(n1.list).to.equal(list);
196 expect(n1.next).to.equal(null);
197 expect(n1.prev).to.equal(n2);
198 expect(n1.value).to.equal(99);
199
200 expect(n2.list).to.equal(list);
201 expect(n2.next).to.equal(n1);
202 expect(n2.prev).to.equal(n3);
203 expect(n2.value).to.equal(42);
204
205 expect(n3.list).to.equal(list);
206 expect(n3.next).to.equal(n2);
207 expect(n3.prev).to.equal(null);
208 expect(n3.value).to.equal(7);
209 });
210 });
211
212 describe('#addLast()', () => {
213 it('should add a value to the end of the list', () => {
214 let list = new LinkedList<number>();
215
216 expect(list.isEmpty).to.equal(true);
217 expect(list.length).to.equal(0);
218 expect(list.first).to.equal(undefined);
219 expect(list.last).to.equal(undefined);
220
221 let n1 = list.addLast(99);
222
223 expect(list.isEmpty).to.equal(false);
224 expect(list.length).to.equal(1);
225 expect(list.first).to.equal(99);
226 expect(list.last).to.equal(99);
227
228 let n2 = list.addLast(42);
229
230 expect(list.isEmpty).to.equal(false);
231 expect(list.length).to.equal(2);
232 expect(list.first).to.equal(99);
233 expect(list.last).to.equal(42);
234
235 let n3 = list.addLast(7);
236
237 expect(list.isEmpty).to.equal(false);
238 expect(list.length).to.equal(3);
239 expect(list.first).to.equal(99);
240 expect(list.last).to.equal(7);
241
242 expect(Array.from(list)).to.deep.equal([99, 42, 7]);
243
244 expect(n1.list).to.equal(list);
245 expect(n1.next).to.equal(n2);
246 expect(n1.prev).to.equal(null);
247 expect(n1.value).to.equal(99);
248
249 expect(n2.list).to.equal(list);
250 expect(n2.next).to.equal(n3);
251 expect(n2.prev).to.equal(n1);
252 expect(n2.value).to.equal(42);
253
254 expect(n3.list).to.equal(list);
255 expect(n3.next).to.equal(null);
256 expect(n3.prev).to.equal(n2);
257 expect(n3.value).to.equal(7);
258 });
259 });
260
261 describe('#insertBefore()', () => {
262 it('should insert a value before the given reference node', () => {
263 let list = LinkedList.from([0, 1, 2, 3]);
264 let n1 = find(list.nodes(), n => n.value === 2)!;
265
266 let n2 = list.insertBefore(7, n1);
267 let n3 = list.insertBefore(8, n2);
268 let n4 = list.insertBefore(9, null);
269
270 let n5 = find(list.nodes(), n => n.value === 1);
271 let n6 = find(list.nodes(), n => n.value === 0);
272
273 expect(list.isEmpty).to.equal(false);
274 expect(list.length).to.equal(7);
275 expect(list.first).to.equal(9);
276 expect(list.last).to.equal(3);
277 expect(Array.from(list)).to.deep.equal([9, 0, 1, 8, 7, 2, 3]);
278
279 expect(n1.list).to.equal(list);
280 expect(n1.next).to.equal(list.lastNode);
281 expect(n1.prev).to.equal(n2);
282 expect(n1.value).to.equal(2);
283
284 expect(n2.list).to.equal(list);
285 expect(n2.next).to.equal(n1);
286 expect(n2.prev).to.equal(n3);
287 expect(n2.value).to.equal(7);
288
289 expect(n3.list).to.equal(list);
290 expect(n3.next).to.equal(n2);
291 expect(n3.prev).to.equal(n5);
292 expect(n3.value).to.equal(8);
293
294 expect(n4.list).to.equal(list);
295 expect(n4.next).to.equal(n6);
296 expect(n4.prev).to.equal(null);
297 expect(n4.value).to.equal(9);
298 });
299
300 it('should throw an error if the reference node is invalid', () => {
301 let list1 = LinkedList.from([0, 1, 2, 3]);
302 let list2 = LinkedList.from([0, 1, 2, 3]);
303 let insert = () => {
304 list2.insertBefore(4, list1.firstNode);
305 };
306 expect(insert).to.throw(Error);
307 });
308 });
309
310 describe('#insertAfter()', () => {
311 it('should insert a value after the given reference node', () => {
312 let list = LinkedList.from([0, 1, 2, 3]);
313 let n1 = find(list.nodes(), n => n.value === 2)!;
314
315 let n2 = list.insertAfter(7, n1);
316 let n3 = list.insertAfter(8, n2);
317 let n4 = list.insertAfter(9, null);
318
319 let n5 = find(list.nodes(), n => n.value === 1);
320 let n6 = find(list.nodes(), n => n.value === 3);
321
322 expect(list.isEmpty).to.equal(false);
323 expect(list.length).to.equal(7);
324 expect(list.first).to.equal(0);
325 expect(list.last).to.equal(9);
326 expect(Array.from(list)).to.deep.equal([0, 1, 2, 7, 8, 3, 9]);
327
328 expect(n1.list).to.equal(list);
329 expect(n1.next).to.equal(n2);
330 expect(n1.prev).to.equal(n5);
331 expect(n1.value).to.equal(2);
332
333 expect(n2.list).to.equal(list);
334 expect(n2.next).to.equal(n3);
335 expect(n2.prev).to.equal(n1);
336 expect(n2.value).to.equal(7);
337
338 expect(n3.list).to.equal(list);
339 expect(n3.next).to.equal(n6);
340 expect(n3.prev).to.equal(n2);
341 expect(n3.value).to.equal(8);
342
343 expect(n4.list).to.equal(list);
344 expect(n4.next).to.equal(null);
345 expect(n4.prev).to.equal(n6);
346 expect(n4.value).to.equal(9);
347 });
348
349 it('should throw an error if the reference node is invalid', () => {
350 let list1 = LinkedList.from([0, 1, 2, 3]);
351 let list2 = LinkedList.from([0, 1, 2, 3]);
352 let insert = () => {
353 list2.insertAfter(4, list1.firstNode);
354 };
355 expect(insert).to.throw(Error);
356 });
357 });
358
359 describe('#removeFirst()', () => {
360 it('should remove the first value from the list', () => {
361 let list = LinkedList.from([0, 1, 2, 3]);
362
363 expect(list.isEmpty).to.equal(false);
364 expect(list.length).to.equal(4);
365 expect(list.first).to.equal(0);
366 expect(list.last).to.equal(3);
367 expect(Array.from(list)).to.deep.equal([0, 1, 2, 3]);
368
369 let v1 = list.removeFirst();
370
371 expect(list.isEmpty).to.equal(false);
372 expect(list.length).to.equal(3);
373 expect(list.first).to.equal(1);
374 expect(list.last).to.equal(3);
375 expect(Array.from(list)).to.deep.equal([1, 2, 3]);
376
377 let v2 = list.removeFirst();
378
379 expect(list.isEmpty).to.equal(false);
380 expect(list.length).to.equal(2);
381 expect(list.first).to.equal(2);
382 expect(list.last).to.equal(3);
383 expect(Array.from(list)).to.deep.equal([2, 3]);
384
385 let v3 = list.removeFirst();
386
387 expect(list.isEmpty).to.equal(false);
388 expect(list.length).to.equal(1);
389 expect(list.first).to.equal(3);
390 expect(list.last).to.equal(3);
391 expect(Array.from(list)).to.deep.equal([3]);
392
393 let v4 = list.removeFirst();
394
395 expect(list.isEmpty).to.equal(true);
396 expect(list.length).to.equal(0);
397 expect(list.first).to.equal(undefined);
398 expect(list.last).to.equal(undefined);
399 expect(Array.from(list)).to.deep.equal([]);
400
401 let v5 = list.removeFirst();
402
403 expect(list.isEmpty).to.equal(true);
404 expect(list.length).to.equal(0);
405 expect(list.first).to.equal(undefined);
406 expect(list.last).to.equal(undefined);
407 expect(Array.from(list)).to.deep.equal([]);
408
409 expect(v1).to.equal(0);
410 expect(v2).to.equal(1);
411 expect(v3).to.equal(2);
412 expect(v4).to.equal(3);
413 expect(v5).to.equal(undefined);
414 });
415 });
416
417 describe('#removeLast()', () => {
418 it('should remove the last value from the list', () => {
419 let list = LinkedList.from([0, 1, 2, 3]);
420
421 expect(list.isEmpty).to.equal(false);
422 expect(list.length).to.equal(4);
423 expect(list.first).to.equal(0);
424 expect(list.last).to.equal(3);
425 expect(Array.from(list)).to.deep.equal([0, 1, 2, 3]);
426
427 let v1 = list.removeLast();
428
429 expect(list.isEmpty).to.equal(false);
430 expect(list.length).to.equal(3);
431 expect(list.first).to.equal(0);
432 expect(list.last).to.equal(2);
433 expect(Array.from(list)).to.deep.equal([0, 1, 2]);
434
435 let v2 = list.removeLast();
436
437 expect(list.isEmpty).to.equal(false);
438 expect(list.length).to.equal(2);
439 expect(list.first).to.equal(0);
440 expect(list.last).to.equal(1);
441 expect(Array.from(list)).to.deep.equal([0, 1]);
442
443 let v3 = list.removeLast();
444
445 expect(list.isEmpty).to.equal(false);
446 expect(list.length).to.equal(1);
447 expect(list.first).to.equal(0);
448 expect(list.last).to.equal(0);
449 expect(Array.from(list)).to.deep.equal([0]);
450
451 let v4 = list.removeLast();
452
453 expect(list.isEmpty).to.equal(true);
454 expect(list.length).to.equal(0);
455 expect(list.first).to.equal(undefined);
456 expect(list.last).to.equal(undefined);
457 expect(Array.from(list)).to.deep.equal([]);
458
459 let v5 = list.removeLast();
460
461 expect(list.isEmpty).to.equal(true);
462 expect(list.length).to.equal(0);
463 expect(list.first).to.equal(undefined);
464 expect(list.last).to.equal(undefined);
465 expect(Array.from(list)).to.deep.equal([]);
466
467 expect(v1).to.equal(3);
468 expect(v2).to.equal(2);
469 expect(v3).to.equal(1);
470 expect(v4).to.equal(0);
471 expect(v5).to.equal(undefined);
472 });
473 });
474
475 describe('#removeNode()', () => {
476 it('should remove the specified node from the list', () => {
477 let list = LinkedList.from([0, 1, 2, 3]);
478
479 expect(list.isEmpty).to.equal(false);
480 expect(list.length).to.equal(4);
481 expect(list.first).to.equal(0);
482 expect(list.last).to.equal(3);
483 expect(Array.from(list)).to.deep.equal([0, 1, 2, 3]);
484
485 let n1 = find(list.nodes(), n => n.value === 2)!;
486 list.removeNode(n1);
487 expect(list.isEmpty).to.equal(false);
488 expect(list.length).to.equal(3);
489 expect(list.first).to.equal(0);
490 expect(list.last).to.equal(3);
491 expect(Array.from(list)).to.deep.equal([0, 1, 3]);
492 expect(n1.list).to.equal(null);
493 expect(n1.next).to.equal(null);
494 expect(n1.prev).to.equal(null);
495 expect(n1.value).to.equal(2);
496
497 let n2 = find(list.nodes(), n => n.value === 3)!;
498 list.removeNode(n2);
499 expect(list.isEmpty).to.equal(false);
500 expect(list.length).to.equal(2);
501 expect(list.first).to.equal(0);
502 expect(list.last).to.equal(1);
503 expect(Array.from(list)).to.deep.equal([0, 1]);
504 expect(n2.list).to.equal(null);
505 expect(n2.next).to.equal(null);
506 expect(n2.prev).to.equal(null);
507 expect(n2.value).to.equal(3);
508
509 let n3 = find(list.nodes(), n => n.value === 0)!;
510 list.removeNode(n3);
511 expect(list.isEmpty).to.equal(false);
512 expect(list.length).to.equal(1);
513 expect(list.first).to.equal(1);
514 expect(list.last).to.equal(1);
515 expect(Array.from(list)).to.deep.equal([1]);
516 expect(n3.list).to.equal(null);
517 expect(n3.next).to.equal(null);
518 expect(n3.prev).to.equal(null);
519 expect(n3.value).to.equal(0);
520
521 let n4 = find(list.nodes(), n => n.value === 1)!;
522 list.removeNode(n4);
523 expect(list.isEmpty).to.equal(true);
524 expect(list.length).to.equal(0);
525 expect(list.first).to.equal(undefined);
526 expect(list.last).to.equal(undefined);
527 expect(Array.from(list)).to.deep.equal([]);
528 expect(n4.list).to.equal(null);
529 expect(n4.next).to.equal(null);
530 expect(n4.prev).to.equal(null);
531 expect(n4.value).to.equal(1);
532 });
533 });
534
535 describe('#clear()', () => {
536 it('should remove all values from the list', () => {
537 let list = LinkedList.from([0, 1, 2, 3]);
538
539 expect(list.isEmpty).to.equal(false);
540 expect(list.length).to.equal(4);
541 expect(list.first).to.equal(0);
542 expect(list.last).to.equal(3);
543 expect(Array.from(list)).to.deep.equal([0, 1, 2, 3]);
544
545 list.clear();
546
547 expect(list.isEmpty).to.equal(true);
548 expect(list.length).to.equal(0);
549 expect(list.first).to.equal(undefined);
550 expect(list.last).to.equal(undefined);
551 expect(Array.from(list)).to.deep.equal([]);
552 });
553 });
554
555 describe('.from()', () => {
556 it('should initialize a list from an iterable', () => {
557 let list1 = LinkedList.from([0, 1, 2, 3]);
558 let list2 = LinkedList.from(list1);
559 expect(list2.isEmpty).to.equal(false);
560 expect(list2.length).to.equal(4);
561 expect(list2.first).to.equal(0);
562 expect(list2.last).to.equal(3);
563 expect(Array.from(list2)).to.deep.equal([0, 1, 2, 3]);
564 });
565 });
566 });
567 });