-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdoublyLinkedList.test.js
More file actions
86 lines (67 loc) · 2.6 KB
/
doublyLinkedList.test.js
File metadata and controls
86 lines (67 loc) · 2.6 KB
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
const DoublyLinkedList = require('../../src/dataStructures/doublyLinkedList');
describe('Doubly linked list data structure', () => {
it ('Should have empty head and tail when instantiated', () => {
const list = new DoublyLinkedList();
expect(list.head).toBe(null);
expect(list.tail).toBe(null);
});
it('Should add the first node as head and tail of the list', () => {
const list = new DoublyLinkedList();
list.append(1);
expect(list.head).toBe(list.tail);
});
it('Should check whether an element is in the list', () => {
const list = new DoublyLinkedList();
list.append(1).append(2).append(3);
expect(list.includes(2)).toBe(true);
expect(list.includes(4)).toBe(false);
});
it('Should be able to get an element from the list', () => {
const list = new DoublyLinkedList();
expect(list.get(1)).toBe(null);
list.append(1).append(2).append(3);
expect(list.get(2).value).toBe(2);
expect(list.get(4)).toBe(null);
});
it('Should be able to remove an item from the list', () => {
const list = new DoublyLinkedList();
expect(list.remove(1)).toBe(false);
list.append(1).append(2).append(3).append(4).append(5);
expect(list.remove(2)).toBe(true);
expect(list.remove(6)).toBe(false);
list.remove(1);
expect(list.head.value).toBe(3);
list.remove(5);
expect(list.tail.value).toBe(4);
});
it('Should update references of neighboring elements of removed element', () => {
const list = new DoublyLinkedList();
list.append(1).append(2).append(3).append(4);
expect(list.get(2).next.value).toBe(3);
list.remove(3);
expect(list.get(2).next.value).toBe(4);
});
it('Should be able to insert an element into the list', () => {
const list = new DoublyLinkedList();
list.append(1).append(2).append(3);
expect(list.get(2).next.value).toBe(3);
list.insert(2.5, 2, 'after');
expect(list.get(2).next.value).toBe(2.5);
list.insert(2.25, 2.5, 'before');
expect(list.get(2.5).prev.value).toBe(2.25);
list.insert(4, 3, 'position');
expect(list.includes(4)).toBe(false);
list.insert(4, 3, 'after');
expect(list.tail.value).toBe(4);
list.insert(0, 1, 'before');
expect(list.head.value).toBe(0);
});
it('Should print a correct representation of the list as string', () => {
const list = new DoublyLinkedList();
expect(list.toString()).toBe('');
list.append(1).append(2).append(3).append(4).append(5);
expect(list.toString()).toBe('1, 2, 3, 4, 5');
expect(list.toString('backward')).toBe('5, 4, 3, 2, 1');
expect(list.toString('direction')).toBe('');
});
});