data-structure-typed 1.41.6 → 1.41.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +14 -11
- package/benchmark/report.html +14 -11
- package/benchmark/report.json +153 -202
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +5 -2
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +15 -2
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/abstract-graph.js +5 -5
- package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +5 -2
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +15 -2
- package/dist/mjs/src/data-structures/graph/abstract-graph.js +5 -5
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/rb-tree.ts +18 -2
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +110 -0
- package/test/integration/bst.test.ts +385 -0
- package/test/integration/heap.test.js +16 -0
- package/test/integration/index.html +51 -0
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +36 -0
- package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +45 -0
- package/test/performance/data-structures/binary-tree/bst.test.ts +36 -0
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +32 -0
- package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
- package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +49 -0
- package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/overall.test.ts +0 -0
- package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +30 -0
- package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
- package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
- package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
- package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
- package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
- package/test/performance/data-structures/queue/deque.test.ts +21 -0
- package/test/performance/data-structures/queue/queue.test.ts +25 -0
- package/test/performance/data-structures/stack/stack.test.ts +0 -0
- package/test/performance/data-structures/tree/tree.test.ts +0 -0
- package/test/performance/data-structures/trie/trie.test.ts +22 -0
- package/test/performance/reportor.ts +185 -0
- package/test/performance/types/index.ts +1 -0
- package/test/performance/types/reportor.ts +3 -0
- package/test/types/index.ts +1 -0
- package/test/types/utils/big-o.ts +1 -0
- package/test/types/utils/index.ts +2 -0
- package/test/types/utils/json2html.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +269 -0
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +320 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +486 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +840 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +66 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +435 -0
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +542 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +100 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +564 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +126 -0
- package/test/unit/data-structures/graph/overall.test.ts +49 -0
- package/test/unit/data-structures/graph/salty-edges.json +1 -0
- package/test/unit/data-structures/graph/salty-vertexes.json +1 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +74 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +66 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +103 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +186 -0
- package/test/unit/data-structures/heap/heap.test.ts +254 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +52 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +52 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +400 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +8 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +474 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/linked-list/skip-list.test.ts +86 -0
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +345 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +244 -0
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +73 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +63 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +53 -0
- package/test/unit/data-structures/queue/deque.test.ts +410 -0
- package/test/unit/data-structures/queue/queue.test.ts +207 -0
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +825 -0
- package/test/utils/array.ts +5514 -0
- package/test/utils/big-o.ts +207 -0
- package/test/utils/console.ts +31 -0
- package/test/utils/index.ts +7 -0
- package/test/utils/is.ts +56 -0
- package/test/utils/json2html.ts +322 -0
- package/test/utils/number.ts +13 -0
- package/test/utils/string.ts +1 -0
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import {SinglyLinkedList, SinglyLinkedListNode} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('SinglyLinkedListNode', () => {
|
|
4
|
+
it('should SinglyLinkedList', () => {
|
|
5
|
+
const node1 = new SinglyLinkedListNode<number>(2);
|
|
6
|
+
expect(node1.value).toBe(2);
|
|
7
|
+
node1.value = 1;
|
|
8
|
+
expect(node1.value).toBe(1);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('SinglyLinkedList Operation Test', () => {
|
|
13
|
+
let list: SinglyLinkedList<number>;
|
|
14
|
+
let objectList: SinglyLinkedList<{keyA: number}>;
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
list = new SinglyLinkedList<number>();
|
|
17
|
+
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('push', () => {
|
|
21
|
+
it('should add elements to the end of the list', () => {
|
|
22
|
+
list.push(1);
|
|
23
|
+
list.push(2);
|
|
24
|
+
expect(list.toArray()).toEqual([1, 2]);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('pop', () => {
|
|
29
|
+
it('should delete and return the last element of the list', () => {
|
|
30
|
+
list.push(1);
|
|
31
|
+
list.push(2);
|
|
32
|
+
list.push(3);
|
|
33
|
+
const popped = list.pop();
|
|
34
|
+
expect(popped).toBe(3);
|
|
35
|
+
expect(list.popLast()).toBe(2);
|
|
36
|
+
expect(list.toArray()).toEqual([1]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should return undefined if the list is empty', () => {
|
|
40
|
+
const popped = list.pop();
|
|
41
|
+
expect(popped).toBeUndefined();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('shift', () => {
|
|
46
|
+
it('should delete and return the first element of the list', () => {
|
|
47
|
+
list.push(1);
|
|
48
|
+
list.push(2);
|
|
49
|
+
list.push(3);
|
|
50
|
+
const shifted = list.shift();
|
|
51
|
+
expect(shifted).toBe(1);
|
|
52
|
+
expect(list.popFirst()).toBe(2);
|
|
53
|
+
expect(list.toArray()).toEqual([3]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should return undefined if the list is empty', () => {
|
|
57
|
+
const shifted = list.shift();
|
|
58
|
+
expect(shifted).toBeUndefined();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('unshift', () => {
|
|
63
|
+
it('should add elements to the beginning of the list', () => {
|
|
64
|
+
list.unshift(1);
|
|
65
|
+
list.addFirst(2);
|
|
66
|
+
expect(list.toArray()).toEqual([2, 1]);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('get', () => {
|
|
71
|
+
it('should return the element at the specified index', () => {
|
|
72
|
+
list.push(1);
|
|
73
|
+
list.push(2);
|
|
74
|
+
list.push(3);
|
|
75
|
+
const element = list.getAt(1);
|
|
76
|
+
expect(element).toBe(2);
|
|
77
|
+
expect(list.getNodeAt(2)?.value).toBe(3);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should return undefined for an out-of-bounds index', () => {
|
|
81
|
+
list.push(1);
|
|
82
|
+
const element = list.getAt(1);
|
|
83
|
+
expect(element).toBeUndefined();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('insertAfter', () => {
|
|
88
|
+
it('should insert an element after an existing value', () => {
|
|
89
|
+
list.push(1);
|
|
90
|
+
list.push(2);
|
|
91
|
+
list.push(3);
|
|
92
|
+
list.insertAfter(2, 4);
|
|
93
|
+
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should return false if the existing value is not found', () => {
|
|
97
|
+
list.push(1);
|
|
98
|
+
list.push(2);
|
|
99
|
+
list.push(3);
|
|
100
|
+
const result = list.insertAfter(5, 4);
|
|
101
|
+
expect(result).toBe(false);
|
|
102
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('countOccurrences', () => {
|
|
107
|
+
it('should count occurrences of a value in the list', () => {
|
|
108
|
+
list.push(1);
|
|
109
|
+
list.push(2);
|
|
110
|
+
list.push(2);
|
|
111
|
+
list.push(3);
|
|
112
|
+
const count = list.countOccurrences(2);
|
|
113
|
+
expect(count).toBe(2);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should return 0 if the value is not found', () => {
|
|
117
|
+
list.push(1);
|
|
118
|
+
list.push(2);
|
|
119
|
+
const count = list.countOccurrences(3);
|
|
120
|
+
expect(count).toBe(0);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('removeValue', () => {
|
|
125
|
+
it('should delete the first occurrence of a value from the list', () => {
|
|
126
|
+
list.push(1);
|
|
127
|
+
list.push(2);
|
|
128
|
+
list.push(3);
|
|
129
|
+
list.push(4);
|
|
130
|
+
list.push(5);
|
|
131
|
+
expect(list.delete(2)).toBe(true);
|
|
132
|
+
expect(list.toArray()).toEqual([1, 3, 4, 5]);
|
|
133
|
+
expect(list.delete(1)).toBe(true);
|
|
134
|
+
expect(list.toArray()).toEqual([3, 4, 5]);
|
|
135
|
+
expect(list.delete(5)).toBe(true);
|
|
136
|
+
expect(list.toArray()).toEqual([3, 4]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should return false if the value is not found', () => {
|
|
140
|
+
list.push(1);
|
|
141
|
+
list.push(2);
|
|
142
|
+
list.push(3);
|
|
143
|
+
const removed = list.delete(4);
|
|
144
|
+
expect(removed).toBe(false);
|
|
145
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('isEmpty', () => {
|
|
150
|
+
it('should return true for an empty list', () => {
|
|
151
|
+
expect(list.isEmpty()).toBe(true);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should return false for a non-empty list', () => {
|
|
155
|
+
list.push(1);
|
|
156
|
+
expect(list.isEmpty()).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('clear', () => {
|
|
161
|
+
it('should clear all elements from the list', () => {
|
|
162
|
+
list.push(1);
|
|
163
|
+
list.push(2);
|
|
164
|
+
list.push(3);
|
|
165
|
+
list.clear();
|
|
166
|
+
expect(list.toArray()).toEqual([]);
|
|
167
|
+
expect(list.length).toBe(0);
|
|
168
|
+
expect(list.isEmpty()).toBe(true);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('reverse', () => {
|
|
173
|
+
it('should reverse the order of elements in the list', () => {
|
|
174
|
+
list.push(1);
|
|
175
|
+
list.push(2);
|
|
176
|
+
list.push(3);
|
|
177
|
+
list.reverse();
|
|
178
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should handle an empty list', () => {
|
|
182
|
+
list.reverse();
|
|
183
|
+
expect(list.toArray()).toEqual([]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('should handle a list with a single element', () => {
|
|
187
|
+
list.push(1);
|
|
188
|
+
list.reverse();
|
|
189
|
+
expect(list.toArray()).toEqual([1]);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('indexOf', () => {
|
|
194
|
+
it('should return the index of the first occurrence of a value', () => {
|
|
195
|
+
list.push(1);
|
|
196
|
+
list.push(2);
|
|
197
|
+
list.push(3);
|
|
198
|
+
const index = list.indexOf(2);
|
|
199
|
+
expect(index).toBe(1);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should return -1 if the value is not found', () => {
|
|
203
|
+
list.push(1);
|
|
204
|
+
list.push(2);
|
|
205
|
+
list.push(3);
|
|
206
|
+
const index = list.indexOf(4);
|
|
207
|
+
expect(index).toBe(-1);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe('toArray', () => {
|
|
212
|
+
it('should convert the list to an array', () => {
|
|
213
|
+
list.push(1);
|
|
214
|
+
list.push(2);
|
|
215
|
+
list.push(3);
|
|
216
|
+
const array = list.toArray();
|
|
217
|
+
expect(array).toEqual([1, 2, 3]);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('should return an empty array for an empty list', () => {
|
|
221
|
+
const array = list.toArray();
|
|
222
|
+
expect(array).toEqual([]);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('insertBefore', () => {
|
|
227
|
+
it('should insert an element before an existing value', () => {
|
|
228
|
+
list.push(1);
|
|
229
|
+
list.push(2);
|
|
230
|
+
list.push(3);
|
|
231
|
+
list.insertBefore(2, 4);
|
|
232
|
+
expect(list.toArray()).toEqual([1, 4, 2, 3]);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should insert an element at the beginning', () => {
|
|
236
|
+
list.push(1);
|
|
237
|
+
list.push(2);
|
|
238
|
+
list.insertBefore(1, 3);
|
|
239
|
+
expect(list.toArray()).toEqual([3, 1, 2]);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('should return false if the existing value is not found', () => {
|
|
243
|
+
list.push(1);
|
|
244
|
+
list.push(2);
|
|
245
|
+
list.push(3);
|
|
246
|
+
const result = list.insertBefore(5, 4);
|
|
247
|
+
expect(result).toBe(false);
|
|
248
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('getLength', () => {
|
|
253
|
+
it('should return the correct length of the list', () => {
|
|
254
|
+
expect(list.length).toBe(0);
|
|
255
|
+
list.push(1);
|
|
256
|
+
list.push(2);
|
|
257
|
+
expect(list.length).toBe(2);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe('delete', () => {
|
|
262
|
+
it('should delete and return the element at the specified index', () => {
|
|
263
|
+
list.push(1);
|
|
264
|
+
list.push(2);
|
|
265
|
+
list.push(3);
|
|
266
|
+
const removed = list.deleteAt(1);
|
|
267
|
+
expect(removed).toBe(2);
|
|
268
|
+
expect(list.toArray()).toEqual([1, 3]);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('should return undefined for an out-of-bounds index', () => {
|
|
272
|
+
list.push(1);
|
|
273
|
+
const removed = list.deleteAt(1);
|
|
274
|
+
expect(removed).toBeUndefined();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should delete and return the first element', () => {
|
|
278
|
+
list.push(1);
|
|
279
|
+
list.push(2);
|
|
280
|
+
const removed = list.deleteAt(0);
|
|
281
|
+
expect(removed).toBe(1);
|
|
282
|
+
expect(list.toArray()).toEqual([2]);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should delete and return the last element', () => {
|
|
286
|
+
list.push(1);
|
|
287
|
+
list.push(2);
|
|
288
|
+
const removed = list.deleteAt(1);
|
|
289
|
+
expect(removed).toBe(2);
|
|
290
|
+
expect(list.toArray()).toEqual([1]);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe('push and pop', () => {
|
|
295
|
+
it('should push and pop elements correctly', () => {
|
|
296
|
+
list.push(1);
|
|
297
|
+
list.push(2);
|
|
298
|
+
expect(list.pop()).toBe(2);
|
|
299
|
+
expect(list.pop()).toBe(1);
|
|
300
|
+
expect(list.pop()).toBeUndefined();
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe('shift and unshift', () => {
|
|
305
|
+
it('should shift and unshift elements correctly', () => {
|
|
306
|
+
list.unshift(1);
|
|
307
|
+
list.unshift(2);
|
|
308
|
+
expect(list.shift()).toBe(2);
|
|
309
|
+
expect(list.shift()).toBe(1);
|
|
310
|
+
expect(list.shift()).toBeUndefined();
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe('insert and toArray', () => {
|
|
315
|
+
it('should insert elements and return array correctly', () => {
|
|
316
|
+
list.insertAt(0, 1);
|
|
317
|
+
list.insertAt(1, 3);
|
|
318
|
+
list.insertAt(1, 2);
|
|
319
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe('find', () => {
|
|
324
|
+
it('should find elements using a callback function', () => {
|
|
325
|
+
list.push(1);
|
|
326
|
+
list.push(2);
|
|
327
|
+
list.push(3);
|
|
328
|
+
const result = list.find(data => data % 2 === 0);
|
|
329
|
+
expect(result).toBe(2);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('should return undefined if element is not found', () => {
|
|
333
|
+
list.push(1);
|
|
334
|
+
list.push(3);
|
|
335
|
+
const result = list.find(data => data % 2 === 0);
|
|
336
|
+
expect(result).toBeNull();
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe('reverse', () => {
|
|
341
|
+
it('should reverse the order of elements', () => {
|
|
342
|
+
list.push(1);
|
|
343
|
+
list.push(2);
|
|
344
|
+
list.push(3);
|
|
345
|
+
list.reverse();
|
|
346
|
+
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
describe('countOccurrences', () => {
|
|
351
|
+
it('should count occurrences of a value', () => {
|
|
352
|
+
list.push(1);
|
|
353
|
+
list.push(2);
|
|
354
|
+
list.push(2);
|
|
355
|
+
list.push(3);
|
|
356
|
+
const count = list.countOccurrences(2);
|
|
357
|
+
expect(count).toBe(2);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('should return 0 if value is not found', () => {
|
|
361
|
+
list.push(1);
|
|
362
|
+
list.push(2);
|
|
363
|
+
const count = list.countOccurrences(3);
|
|
364
|
+
expect(count).toBe(0);
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('should insert and manipulate objects with numeric properties', () => {
|
|
369
|
+
const obj1 = {keyA: 1};
|
|
370
|
+
const obj2 = {keyA: 2};
|
|
371
|
+
const obj3 = {keyA: 3};
|
|
372
|
+
|
|
373
|
+
objectList.push(obj1);
|
|
374
|
+
objectList.push(obj2);
|
|
375
|
+
objectList.push(obj3);
|
|
376
|
+
|
|
377
|
+
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
378
|
+
|
|
379
|
+
const newObj = {keyA: 2.5}; // Corrected newObj value
|
|
380
|
+
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
381
|
+
expect(insertSuccess).toBe(true);
|
|
382
|
+
|
|
383
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
384
|
+
expect(getNode?.value).toEqual(newObj);
|
|
385
|
+
|
|
386
|
+
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
387
|
+
expect(deleted).toBe(true);
|
|
388
|
+
|
|
389
|
+
const poppedObj = objectList.pop();
|
|
390
|
+
expect(poppedObj).toBe(obj3);
|
|
391
|
+
|
|
392
|
+
const shiftedObj = objectList.shift();
|
|
393
|
+
expect(shiftedObj).toBe(obj1);
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
describe('SinglyLinkedList', () => {
|
|
398
|
+
let list: SinglyLinkedList<number>;
|
|
399
|
+
|
|
400
|
+
beforeEach(() => {
|
|
401
|
+
list = new SinglyLinkedList<number>();
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('should initialize an empty list', () => {
|
|
405
|
+
expect(list.head).toBeNull();
|
|
406
|
+
expect(list.tail).toBeNull();
|
|
407
|
+
expect(list.length).toBe(0);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
it('should push elements to the end of the list', () => {
|
|
411
|
+
list.push(1);
|
|
412
|
+
list.push(2);
|
|
413
|
+
expect(list.head!.value).toBe(1);
|
|
414
|
+
expect(list.tail!.value).toBe(2);
|
|
415
|
+
expect(list.length).toBe(2);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('should pop elements from the end of the list', () => {
|
|
419
|
+
list.push(1);
|
|
420
|
+
list.push(2);
|
|
421
|
+
const popped = list.pop();
|
|
422
|
+
expect(popped).toBe(2);
|
|
423
|
+
expect(list.head!.value).toBe(1);
|
|
424
|
+
expect(list.tail!.value).toBe(1);
|
|
425
|
+
expect(list.length).toBe(1);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it('should reverse the list', () => {
|
|
429
|
+
list.push(1);
|
|
430
|
+
list.push(2);
|
|
431
|
+
list.push(3);
|
|
432
|
+
list.reverse();
|
|
433
|
+
expect(list.head!.value).toBe(3);
|
|
434
|
+
expect(list.tail!.value).toBe(1);
|
|
435
|
+
// Add more assertions for reversed order.
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it('should convert the list to an array', () => {
|
|
439
|
+
list.push(1);
|
|
440
|
+
list.push(2);
|
|
441
|
+
list.push(3);
|
|
442
|
+
const array = list.toArray();
|
|
443
|
+
expect(array).toEqual([1, 2, 3]);
|
|
444
|
+
// @ts-ignore
|
|
445
|
+
expect([...list]).toEqual([1, 2, 3]);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('should filter the list', () => {
|
|
449
|
+
list.push(1);
|
|
450
|
+
list.push(2);
|
|
451
|
+
list.push(3);
|
|
452
|
+
expect(list.filter(value => value !== 2).toArray()).toEqual([1, 3]);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it('should forEach the list', () => {
|
|
456
|
+
list.push(1);
|
|
457
|
+
list.push(2);
|
|
458
|
+
list.push(3);
|
|
459
|
+
list.forEach(value => value++);
|
|
460
|
+
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('should map the list', () => {
|
|
464
|
+
list.addLast(1);
|
|
465
|
+
list.push(2);
|
|
466
|
+
list.push(3);
|
|
467
|
+
expect(list.map(value => value * 2).toArray()).toEqual([2, 4, 6]);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('should reduce the list', () => {
|
|
471
|
+
const list1 = SinglyLinkedList.fromArray([1, 2, 3]);
|
|
472
|
+
expect(list1.reduce((acc, value) => acc + value, 0)).toEqual(6);
|
|
473
|
+
});
|
|
474
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// import {SkipLinkedList} from '../../../../src'
|
|
2
|
+
|
|
3
|
+
describe('SkipLinkedList Operation Test', () => {
|
|
4
|
+
it('should xxx', function () {
|
|
5
|
+
expect(true).toBeTruthy();
|
|
6
|
+
});
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
describe('SkipLinkedList Performance Test', () => {
|
|
10
|
+
it('should xxx', function () {
|
|
11
|
+
expect(true).toBeTruthy();
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {SkipList} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('SkipList', () => {
|
|
4
|
+
let skipList: SkipList<number, string>;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
skipList = new SkipList<number, string>();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should insert and retrieve elements correctly', () => {
|
|
11
|
+
skipList.add(1, 'One');
|
|
12
|
+
skipList.add(2, 'Two');
|
|
13
|
+
skipList.add(3, 'Three');
|
|
14
|
+
|
|
15
|
+
expect(skipList.get(1)).toBe('One');
|
|
16
|
+
expect(skipList.get(2)).toBe('Two');
|
|
17
|
+
expect(skipList.get(3)).toBe('Three');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should return undefined for non-existent keys', () => {
|
|
21
|
+
skipList.add(1, 'One');
|
|
22
|
+
skipList.add(2, 'Two');
|
|
23
|
+
|
|
24
|
+
expect(skipList.get(3)).toBeUndefined();
|
|
25
|
+
expect(skipList.get(0)).toBeUndefined();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should delete elements correctly', () => {
|
|
29
|
+
skipList.add(1, 'One');
|
|
30
|
+
skipList.add(2, 'Two');
|
|
31
|
+
skipList.add(3, 'Three');
|
|
32
|
+
|
|
33
|
+
skipList.delete(2);
|
|
34
|
+
|
|
35
|
+
expect(skipList.get(2)).toBeUndefined(); // 修改这里的断言
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should handle random data correctly', () => {
|
|
39
|
+
const randomData: Array<[number, string]> = [
|
|
40
|
+
[5, 'Five'],
|
|
41
|
+
[1, 'One'],
|
|
42
|
+
[3, 'Three'],
|
|
43
|
+
[2, 'Two'],
|
|
44
|
+
[4, 'Four']
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
for (const [key, value] of randomData) {
|
|
48
|
+
skipList.add(key, value);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
expect(skipList.get(3)).toBe('Three');
|
|
52
|
+
expect(skipList.get(5)).toBe('Five');
|
|
53
|
+
expect(skipList.get(4)).toBe('Four');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('SkipList', () => {
|
|
58
|
+
let skipList: SkipList<number, string>;
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
skipList = new SkipList();
|
|
62
|
+
skipList.add(1, 'One');
|
|
63
|
+
skipList.add(2, 'Two');
|
|
64
|
+
skipList.add(3, 'Three');
|
|
65
|
+
skipList.add(4, 'Four');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('getFirst() should return the getFirst element', () => {
|
|
69
|
+
expect(skipList.getFirst()).toBe('One');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('getLast() should return the getLast element', () => {
|
|
73
|
+
expect(skipList.getLast()).toBe('Four');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('higher(key) should return the getFirst element greater than the given key', () => {
|
|
77
|
+
expect(skipList.higher(2)).toBe('Three');
|
|
78
|
+
expect(skipList.higher(3)).toBe('Four');
|
|
79
|
+
expect(skipList.higher(4)).toBeUndefined();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('lower(key) should return the getLast element less than the given key', () => {
|
|
83
|
+
expect(skipList.lower(2)).toBe('One');
|
|
84
|
+
expect(skipList.lower(1)).toBe(null);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {MatrixNTI2D} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
describe('MatrixNTI2D', () => {
|
|
4
|
+
it('should initialize a matrix with rows and columns', () => {
|
|
5
|
+
const numRows = 3;
|
|
6
|
+
const numCols = 4;
|
|
7
|
+
const matrix = new MatrixNTI2D({row: numRows, col: numCols});
|
|
8
|
+
|
|
9
|
+
expect(matrix.toArray().length).toBe(numRows);
|
|
10
|
+
expect(matrix.toArray()[0].length).toBe(numCols);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should initialize all elements with the provided initial value', () => {
|
|
14
|
+
const numRows = 3;
|
|
15
|
+
const numCols = 4;
|
|
16
|
+
const initialValue = 42;
|
|
17
|
+
const matrix = new MatrixNTI2D({row: numRows, col: numCols, initialVal: initialValue});
|
|
18
|
+
|
|
19
|
+
const matrixArray = matrix.toArray();
|
|
20
|
+
for (let i = 0; i < numRows; i++) {
|
|
21
|
+
for (let j = 0; j < numCols; j++) {
|
|
22
|
+
expect(matrixArray[i][j]).toBe(initialValue);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should initialize all elements with 0 if no initial value is provided', () => {
|
|
28
|
+
const numRows = 3;
|
|
29
|
+
const numCols = 4;
|
|
30
|
+
const matrix = new MatrixNTI2D({row: numRows, col: numCols});
|
|
31
|
+
|
|
32
|
+
const matrixArray = matrix.toArray();
|
|
33
|
+
for (let i = 0; i < numRows; i++) {
|
|
34
|
+
for (let j = 0; j < numCols; j++) {
|
|
35
|
+
expect(matrixArray[i][j]).toBe(0);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should convert the matrix to a two-dimensional array', () => {
|
|
41
|
+
const numRows = 2;
|
|
42
|
+
const numCols = 3;
|
|
43
|
+
const matrix = new MatrixNTI2D({row: numRows, col: numCols, initialVal: 1});
|
|
44
|
+
|
|
45
|
+
const matrixArray = matrix.toArray();
|
|
46
|
+
expect(matrixArray.length).toBe(numRows);
|
|
47
|
+
for (let i = 0; i < numRows; i++) {
|
|
48
|
+
expect(matrixArray[i].length).toBe(numCols);
|
|
49
|
+
for (let j = 0; j < numCols; j++) {
|
|
50
|
+
expect(matrixArray[i][j]).toBe(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|