forked from ludralph/singly-linked-list-exercise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlyLinkedList.spec.js
166 lines (154 loc) · 6.6 KB
/
singlyLinkedList.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
var expect = chai.expect;
var singlyLinkedList;
var node;
beforeEach(function () {
singlyLinkedList = new SinglyLinkedList();
node = new Node(15);
});
describe('#singlyLinkedList', function () {
it('contains a head that is null', function () {
expect(singlyLinkedList.head).to.equal(null);
expect(singlyLinkedList.tail).to.equal(null);
expect(singlyLinkedList.hasOwnProperty('head')).to.equal(true);
expect(singlyLinkedList.hasOwnProperty('tail')).to.equal(true);
});
it('contains a length property that begins at 0', function () {
expect(singlyLinkedList.length).to.equal(0);
expect(singlyLinkedList.hasOwnProperty('length')).to.equal(true);
});
});
describe('#Node', function () {
it('contains a value', function () {
expect(node.value).to.equal(15);
expect(node.hasOwnProperty('value')).to.equal(true);
expect(node.hasOwnProperty('next')).to.equal(true);
});
});
describe('#push', function () {
it('inserts a node at the end of the list and increments the length of the list', function () {
singlyLinkedList.push(5);
expect(singlyLinkedList.length).to.equal(1);
expect(singlyLinkedList.head.value).to.equal(5);
singlyLinkedList.push(10);
expect(singlyLinkedList.length).to.equal(2);
expect(singlyLinkedList.head.next.value).to.equal(10);
singlyLinkedList.push(15);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.next.next.value).to.equal(15);
});
it('returns the singlyLinkedList so that the method can be chained', function () {
singlyLinkedList.push(5).push(10).push(15);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.next.next.value).to.equal(15);
});
});
describe('#pop', function () {
it('removes a node at the end of the list and decrements the length of the list', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.pop()).to.equal(20);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.next.next.value).to.equal(15);
});
it('returns undefined if there are no nodes to remove', function () {
expect(singlyLinkedList.pop()).to.equal(undefined);
expect(singlyLinkedList.length).to.equal(0);
});
});
describe('#unshift', function () {
it('inserts a node at the beginning of the list and increments the length of the list', function () {
singlyLinkedList.unshift(5);
expect(singlyLinkedList.length).to.equal(1);
expect(singlyLinkedList.head.value).to.equal(5);
singlyLinkedList.unshift(10);
expect(singlyLinkedList.length).to.equal(2);
expect(singlyLinkedList.head.value).to.equal(10);
expect(singlyLinkedList.head.next.value).to.equal(5);
singlyLinkedList.unshift(15);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.value).to.equal(15);
expect(singlyLinkedList.head.next.value).to.equal(10);
expect(singlyLinkedList.head.next.next.value).to.equal(5);
});
it('returns the singlyLinkedList so that the method can be chained', function () {
singlyLinkedList.unshift(5).unshift(10).unshift(15);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.next.next.value).to.equal(5);
});
});
describe('#shift', function () {
it('removes a node at the beginning of the list and decrements the length of the list', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.shift()).to.equal(5);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.shift()).to.equal(10);
expect(singlyLinkedList.length).to.equal(2);
expect(singlyLinkedList.shift()).to.equal(15);
expect(singlyLinkedList.length).to.equal(1);
expect(singlyLinkedList.shift()).to.equal(20);
expect(singlyLinkedList.length).to.equal(0);
});
it('returns undefined if there are no nodes to remove', function () {
expect(singlyLinkedList.shift()).to.equal(undefined);
expect(singlyLinkedList.length).to.equal(0);
});
});
describe('#set', function () {
it('finds a node and replaces its value or returns undefined if the node is not found', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
expect(singlyLinkedList.length).to.equal(4);
singlyLinkedList.set(0, 10);
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.head.value).to.equal(10);
singlyLinkedList.set(10, 10);
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.head.value).to.equal(10);
singlyLinkedList.set(2, 100);
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.head.next.next.value).to.equal(100);
});
});
describe('#_get', function () {
it('finds a node and returns its value ', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
expect(singlyLinkedList._get(0).value).to.equal(5);
expect(singlyLinkedList._get(1).value).to.equal(10);
expect(singlyLinkedList._get(2).value).to.equal(15);
expect(singlyLinkedList._get(3).value).to.equal(20);
expect(singlyLinkedList._get(4)).to.equal(null);
});
});
describe('#_insert', function () {
it('inserts a node and correct adjusts the next properties of other nodes', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
singlyLinkedList._insert(2, 12);
expect(singlyLinkedList.length).to.equal(5);
expect(singlyLinkedList.head.value).to.equal(5);
expect(singlyLinkedList.head.next.value).to.equal(10);
expect(singlyLinkedList.head.next.next.value).to.equal(12);
expect(singlyLinkedList.head.next.next.next.value).to.equal(15);
expect(singlyLinkedList.head.next.next.next.next.value).to.equal(20);
});
});
describe('#remove', function () {
it('contains a root that is null', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
singlyLinkedList.remove(2);
expect(singlyLinkedList.length).to.equal(3);
expect(singlyLinkedList.head.value).to.equal(5);
expect(singlyLinkedList.head.next.value).to.equal(10);
expect(singlyLinkedList.head.next.next.value).to.equal(20);
});
});
describe('#reverse', function () {
it('reverses all of the nodes', function () {
singlyLinkedList.push(5).push(10).push(15).push(20);
singlyLinkedList.reverse();
expect(singlyLinkedList.length).to.equal(4);
expect(singlyLinkedList.head.value).to.equal(20);
expect(singlyLinkedList.head.next.value).to.equal(15);
expect(singlyLinkedList.head.next.next.value).to.equal(10);
expect(singlyLinkedList.head.next.next.next.value).to.equal(5);
});
});