-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.spec.ts
185 lines (147 loc) · 5.77 KB
/
DoublyLinkedList.spec.ts
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
import { IList } from "../IList";
import { DoublyLinkedList } from "./DoublyLinkedList";
describe('Doubly Linked List: ', () => {
describe('Fails when trying to: ', () => {
var collection = new DoublyLinkedList<number>();
it('Iterate an empty collection', () => {
expect(() => collection.get(1)).toThrow(Error);
});
it('Remove last position item of an empty collection', () => {
expect(() => collection.pop()).toThrow(Error);
});
it('Remove first position item of an empty collection', () => {
expect(() => collection.shift()).toThrow(Error);
});
it('Insert item out of the items range', () => {
expect(() => collection.insert(-1, 10)).toThrow(Error);
expect(() => collection.insert(1, 10)).toThrow(Error);
})
})
describe('Is capable of replacing items: ', () => {
var collection: IList<number> = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.push(20);
it("In the first position", () => {
collection.set(0, 1);
expect(collection.get(0)).toBe(1);
});
it("In an intermediary position", () => {
collection.set(2, 12);
expect(collection.get(2)).toBe(12);
});
it("In the last position", () => {
collection.set(3, 50);
expect(collection.get(3)).toBe(50);
expect(collection.lenght).toBe(4);
});
})
describe('Is capable of adding items: ', () => {
it("By pushing items to collection's last position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
expect(collection.get(1)).toBe(10);
})
it("By unshifting items to collection's first position", () => {
var collection = new DoublyLinkedList<number>();
collection.unshift(5);
collection.unshift(10);
expect(collection.get(1)).toBe(5);
})
});
describe('Is capable of inserting items: ', () => {
it("In the first position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.insert(0, 1);
expect(collection.get(0)).toBe(1);
expect(collection.get(1)).toBe(5);
expect(collection.lenght).toBe(2);
})
it("In a fist half intermediary position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.insert(1, 99);
expect(collection.get(1)).toBe(99);
expect(collection.get(2)).toBe(10);
expect(collection.lenght).toBe(4);
})
it("In a last half intermediary position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.push(20);
collection.insert(3, 99);
expect(collection.get(3)).toBe(99);
expect(collection.get(4)).toBe(20);
expect(collection.lenght).toBe(5);
})
it("In the last position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.insert(3, 99);
expect(collection.get(3)).toBe(99);
expect(collection.lenght).toBe(4);
})
})
describe('Is capable of removing items: ', () => {
it("By 'poping' items of collection`s last position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.push(20);
collection.pop();
expect(collection.lenght).toBe(3);
});
it("By 'shifting' items on collection`s first position", () => {
var collection = new DoublyLinkedList<number>();
collection.unshift(5);
collection.unshift(10);
collection.unshift(15);
collection.unshift(20);
collection.shift();
expect(collection.get(0)).toBe(15);
expect(collection.lenght).toBe(3);
});
it("By 'removing' items on collection`s first position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.remove(0);
expect(collection.get(0)).toBe(10);
expect(collection.lenght).toBe(2);
});
it("By 'removing' items on collection`s last position", () => {
var collection = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.remove(2);
expect(collection.get(1)).toBe(10);
expect(collection.lenght).toBe(2);
});
});
describe('Is capable of Inverting the collection: ', () => {
it("By 'reversing' items", () => {
var collection: IList<number> = new DoublyLinkedList<number>();
collection.push(5);
collection.push(10);
collection.push(15);
collection.push(20);
collection.reverse();
expect(collection.get(0)).toBe(20);
expect(collection.get(1)).toBe(15);
expect(collection.get(2)).toBe(10);
expect(collection.get(3)).toBe(5);
})
})
});