-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.test.js
105 lines (59 loc) · 2.64 KB
/
index.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
const DoublyLinkedList = require('../index');
describe('DoublyLinkedList', () => {
it('add, remove, travers, insertAfter', () => {
const doublyLinkedList = new DoublyLinkedList();
expect(doublyLinkedList.print()).toEqual('');
doublyLinkedList.add(1);
doublyLinkedList.add(2);
doublyLinkedList.add(3);
doublyLinkedList.add(4);
expect(doublyLinkedList.print()).toEqual('1 2 3 4');
expect(doublyLinkedList.length()).toEqual(4);
doublyLinkedList.remove(3); // remove value
expect(doublyLinkedList.print()).toEqual('1 2 4');
doublyLinkedList.remove(9); // remove non existing value
expect(doublyLinkedList.print()).toEqual('1 2 4');
doublyLinkedList.remove(1); // remove head
expect(doublyLinkedList.print()).toEqual('2 4');
doublyLinkedList.remove(4); // remove tail
expect(doublyLinkedList.print()).toEqual('2');
expect(doublyLinkedList.length()).toEqual(1);
doublyLinkedList.remove(2); // remove tail, the list should be empty
expect(doublyLinkedList.print()).toEqual('');
expect(doublyLinkedList.length()).toEqual(0);
doublyLinkedList.add(2);
doublyLinkedList.add(6);
expect(doublyLinkedList.print()).toEqual('2 6');
doublyLinkedList.insertAfter(3, 2);
expect(doublyLinkedList.print()).toEqual('2 3 6');
doublyLinkedList.traverseReverse(function (node) {
console.log(node.data);
});
doublyLinkedList.insertAfter(4, 3);
expect(doublyLinkedList.print()).toEqual('2 3 4 6');
doublyLinkedList.insertAfter(5, 9); // insertAfter a non existing node
expect(doublyLinkedList.print()).toEqual('2 3 4 6');
doublyLinkedList.insertAfter(5, 4);
doublyLinkedList.insertAfter(7, 6); // insertAfter the tail
expect(doublyLinkedList.print()).toEqual('2 3 4 5 6 7');
doublyLinkedList.add(8); // add node with normal method
expect(doublyLinkedList.print()).toEqual('2 3 4 5 6 7 8');
expect(doublyLinkedList.length()).toEqual(7);
doublyLinkedList.traverse(function (node) {
node.data = node.data + 10;
});
expect(doublyLinkedList.print()).toEqual('12 13 14 15 16 17 18');
const outputTraverse = [];
doublyLinkedList.traverse(function (node) {
outputTraverse.push(node.data);
});
expect(doublyLinkedList.length()).toEqual(7);
expect(outputTraverse.join(' ')).toEqual('12 13 14 15 16 17 18');
const outputTraverseReverse = [];
doublyLinkedList.traverseReverse(function (node) {
outputTraverseReverse.push(node.data);
});
expect(doublyLinkedList.length()).toEqual(7);
expect(outputTraverseReverse.join(' ')).toEqual('18 17 16 15 14 13 12');
});
});