data-structure-typed 1.33.7 → 1.33.9
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 +25 -14
- package/package.json +22 -22
- package/.prettierrc.js +0 -16
- package/coverage/coverage-final.json +0 -68
- package/coverage/coverage-summary.json +0 -69
- package/docs/.nojekyll +0 -1
- package/docs/index.html +0 -875
- package/docs/modules.html +0 -336
- package/jest.config.js +0 -8
- package/src/data-structures/binary-tree/aa-tree.ts +0 -1
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
- package/src/data-structures/binary-tree/avl-tree.ts +0 -307
- package/src/data-structures/binary-tree/b-tree.ts +0 -1
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
- package/src/data-structures/binary-tree/binary-tree.ts +0 -47
- package/src/data-structures/binary-tree/bst.ts +0 -537
- package/src/data-structures/binary-tree/index.ts +0 -12
- package/src/data-structures/binary-tree/rb-tree.ts +0 -366
- package/src/data-structures/binary-tree/segment-tree.ts +0 -260
- package/src/data-structures/binary-tree/splay-tree.ts +0 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
- package/src/data-structures/graph/abstract-graph.ts +0 -1040
- package/src/data-structures/graph/directed-graph.ts +0 -470
- package/src/data-structures/graph/index.ts +0 -4
- package/src/data-structures/graph/map-graph.ts +0 -129
- package/src/data-structures/graph/undirected-graph.ts +0 -274
- package/src/data-structures/hash/coordinate-map.ts +0 -67
- package/src/data-structures/hash/coordinate-set.ts +0 -56
- package/src/data-structures/hash/hash-map.ts +0 -203
- package/src/data-structures/hash/hash-table.ts +0 -277
- package/src/data-structures/hash/index.ts +0 -7
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/data-structures/hash/tree-map.ts +0 -1
- package/src/data-structures/hash/tree-set.ts +0 -1
- package/src/data-structures/heap/heap.ts +0 -212
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -31
- package/src/data-structures/heap/min-heap.ts +0 -32
- package/src/data-structures/index.ts +0 -11
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
- package/src/data-structures/linked-list/index.ts +0 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -27
- package/src/data-structures/matrix/matrix2d.ts +0 -213
- package/src/data-structures/matrix/navigator.ts +0 -121
- package/src/data-structures/matrix/vector2d.ts +0 -316
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
- package/src/data-structures/priority-queue/priority-queue.ts +0 -359
- package/src/data-structures/queue/deque.ts +0 -297
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -191
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -98
- package/src/data-structures/tree/index.ts +0 -1
- package/src/data-structures/tree/tree.ts +0 -69
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -225
- package/src/index.ts +0 -4
- package/src/interfaces/abstract-binary-tree.ts +0 -189
- package/src/interfaces/abstract-graph.ts +0 -31
- package/src/interfaces/avl-tree.ts +0 -25
- package/src/interfaces/binary-tree.ts +0 -6
- package/src/interfaces/bst.ts +0 -31
- package/src/interfaces/directed-graph.ts +0 -20
- package/src/interfaces/doubly-linked-list.ts +0 -1
- package/src/interfaces/heap.ts +0 -1
- package/src/interfaces/index.ts +0 -15
- package/src/interfaces/navigator.ts +0 -1
- package/src/interfaces/priority-queue.ts +0 -1
- package/src/interfaces/rb-tree.ts +0 -9
- package/src/interfaces/segment-tree.ts +0 -1
- package/src/interfaces/singly-linked-list.ts +0 -1
- package/src/interfaces/tree-multiset.ts +0 -7
- package/src/interfaces/undirected-graph.ts +0 -6
- package/src/types/data-structures/abstract-binary-tree.ts +0 -50
- package/src/types/data-structures/abstract-graph.ts +0 -11
- package/src/types/data-structures/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree.ts +0 -5
- package/src/types/data-structures/bst.ts +0 -13
- package/src/types/data-structures/directed-graph.ts +0 -8
- package/src/types/data-structures/doubly-linked-list.ts +0 -1
- package/src/types/data-structures/hash.ts +0 -1
- package/src/types/data-structures/heap.ts +0 -5
- package/src/types/data-structures/index.ts +0 -16
- package/src/types/data-structures/map-graph.ts +0 -1
- package/src/types/data-structures/navigator.ts +0 -13
- package/src/types/data-structures/priority-queue.ts +0 -9
- package/src/types/data-structures/rb-tree.ts +0 -8
- package/src/types/data-structures/segment-tree.ts +0 -1
- package/src/types/data-structures/singly-linked-list.ts +0 -1
- package/src/types/data-structures/tree-multiset.ts +0 -6
- package/src/types/helpers.ts +0 -1
- package/src/types/index.ts +0 -3
- package/src/types/utils/index.ts +0 -2
- package/src/types/utils/utils.ts +0 -6
- package/src/types/utils/validate-type.ts +0 -35
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -79
- package/test/integration/avl-tree.test.ts +0 -108
- package/test/integration/bst.test.ts +0 -380
- package/test/integration/heap.test.js +0 -16
- package/test/integration/index.html +0 -44
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
- package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
- package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
- package/test/unit/data-structures/graph/overall.test.ts +0 -49
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
- package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
- package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
- package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
- package/test/unit/data-structures/heap/heap.test.ts +0 -55
- package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
- package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
- package/test/unit/data-structures/linked-list/index.ts +0 -4
- package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
- package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
- package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
- package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
- package/test/unit/data-structures/queue/deque.test.ts +0 -130
- package/test/unit/data-structures/queue/queue.test.ts +0 -199
- package/test/unit/data-structures/stack/stack.test.ts +0 -67
- package/test/unit/data-structures/tree/tree.test.ts +0 -39
- package/test/unit/data-structures/trie/trie.test.ts +0 -95
- package/test/utils/index.ts +0 -2
- package/test/utils/magnitude.ts +0 -21
- package/test/utils/number.ts +0 -3
- package/tsconfig.json +0 -29
|
@@ -1,451 +0,0 @@
|
|
|
1
|
-
import {SinglyLinkedList} from '../../../../src';
|
|
2
|
-
import {bigO, magnitude} from '../../../utils';
|
|
3
|
-
|
|
4
|
-
describe('SinglyLinkedList Operation Test', () => {
|
|
5
|
-
let list: SinglyLinkedList<number>;
|
|
6
|
-
let objectList: SinglyLinkedList<{keyA: number}>;
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
list = new SinglyLinkedList<number>();
|
|
9
|
-
objectList = new SinglyLinkedList<{keyA: number}>();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
describe('push', () => {
|
|
13
|
-
it('should add elements to the end of the list', () => {
|
|
14
|
-
list.push(1);
|
|
15
|
-
list.push(2);
|
|
16
|
-
expect(list.toArray()).toEqual([1, 2]);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('pop', () => {
|
|
21
|
-
it('should remove and return the last element of the list', () => {
|
|
22
|
-
list.push(1);
|
|
23
|
-
list.push(2);
|
|
24
|
-
const popped = list.pop();
|
|
25
|
-
expect(popped).toBe(2);
|
|
26
|
-
expect(list.toArray()).toEqual([1]);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should return undefined if the list is empty', () => {
|
|
30
|
-
const popped = list.pop();
|
|
31
|
-
expect(popped).toBeUndefined();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe('shift', () => {
|
|
36
|
-
it('should remove and return the first element of the list', () => {
|
|
37
|
-
list.push(1);
|
|
38
|
-
list.push(2);
|
|
39
|
-
const shifted = list.shift();
|
|
40
|
-
expect(shifted).toBe(1);
|
|
41
|
-
expect(list.toArray()).toEqual([2]);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('should return undefined if the list is empty', () => {
|
|
45
|
-
const shifted = list.shift();
|
|
46
|
-
expect(shifted).toBeUndefined();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('unshift', () => {
|
|
51
|
-
it('should add elements to the beginning of the list', () => {
|
|
52
|
-
list.unshift(1);
|
|
53
|
-
list.unshift(2);
|
|
54
|
-
expect(list.toArray()).toEqual([2, 1]);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe('get', () => {
|
|
59
|
-
it('should return the element at the specified index', () => {
|
|
60
|
-
list.push(1);
|
|
61
|
-
list.push(2);
|
|
62
|
-
list.push(3);
|
|
63
|
-
const element = list.getAt(1);
|
|
64
|
-
expect(element).toBe(2);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should return undefined for an out-of-bounds index', () => {
|
|
68
|
-
list.push(1);
|
|
69
|
-
const element = list.getAt(1);
|
|
70
|
-
expect(element).toBeUndefined();
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe('insertAfter', () => {
|
|
75
|
-
it('should insert an element after an existing value', () => {
|
|
76
|
-
list.push(1);
|
|
77
|
-
list.push(2);
|
|
78
|
-
list.push(3);
|
|
79
|
-
list.insertAfter(2, 4);
|
|
80
|
-
expect(list.toArray()).toEqual([1, 2, 4, 3]);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should return false if the existing value is not found', () => {
|
|
84
|
-
list.push(1);
|
|
85
|
-
list.push(2);
|
|
86
|
-
list.push(3);
|
|
87
|
-
const result = list.insertAfter(5, 4);
|
|
88
|
-
expect(result).toBe(false);
|
|
89
|
-
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
describe('countOccurrences', () => {
|
|
94
|
-
it('should count occurrences of a value in the list', () => {
|
|
95
|
-
list.push(1);
|
|
96
|
-
list.push(2);
|
|
97
|
-
list.push(2);
|
|
98
|
-
list.push(3);
|
|
99
|
-
const count = list.countOccurrences(2);
|
|
100
|
-
expect(count).toBe(2);
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
it('should return 0 if the value is not found', () => {
|
|
104
|
-
list.push(1);
|
|
105
|
-
list.push(2);
|
|
106
|
-
const count = list.countOccurrences(3);
|
|
107
|
-
expect(count).toBe(0);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
describe('removeValue', () => {
|
|
112
|
-
it('should remove the first occurrence of a value from the list', () => {
|
|
113
|
-
list.push(1);
|
|
114
|
-
list.push(2);
|
|
115
|
-
list.push(3);
|
|
116
|
-
const removed = list.delete(2);
|
|
117
|
-
expect(removed).toBe(true);
|
|
118
|
-
expect(list.toArray()).toEqual([1, 3]);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should return false if the value is not found', () => {
|
|
122
|
-
list.push(1);
|
|
123
|
-
list.push(2);
|
|
124
|
-
list.push(3);
|
|
125
|
-
const removed = list.delete(4);
|
|
126
|
-
expect(removed).toBe(false);
|
|
127
|
-
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
describe('isEmpty', () => {
|
|
132
|
-
it('should return true for an empty list', () => {
|
|
133
|
-
expect(list.isEmpty()).toBe(true);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should return false for a non-empty list', () => {
|
|
137
|
-
list.push(1);
|
|
138
|
-
expect(list.isEmpty()).toBe(false);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('clear', () => {
|
|
143
|
-
it('should clear all elements from the list', () => {
|
|
144
|
-
list.push(1);
|
|
145
|
-
list.push(2);
|
|
146
|
-
list.push(3);
|
|
147
|
-
list.clear();
|
|
148
|
-
expect(list.toArray()).toEqual([]);
|
|
149
|
-
expect(list.length).toBe(0);
|
|
150
|
-
expect(list.isEmpty()).toBe(true);
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
describe('reverse', () => {
|
|
155
|
-
it('should reverse the order of elements in the list', () => {
|
|
156
|
-
list.push(1);
|
|
157
|
-
list.push(2);
|
|
158
|
-
list.push(3);
|
|
159
|
-
list.reverse();
|
|
160
|
-
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('should handle an empty list', () => {
|
|
164
|
-
list.reverse();
|
|
165
|
-
expect(list.toArray()).toEqual([]);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it('should handle a list with a single element', () => {
|
|
169
|
-
list.push(1);
|
|
170
|
-
list.reverse();
|
|
171
|
-
expect(list.toArray()).toEqual([1]);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
describe('indexOf', () => {
|
|
176
|
-
it('should return the index of the first occurrence of a value', () => {
|
|
177
|
-
list.push(1);
|
|
178
|
-
list.push(2);
|
|
179
|
-
list.push(3);
|
|
180
|
-
const index = list.indexOf(2);
|
|
181
|
-
expect(index).toBe(1);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('should return -1 if the value is not found', () => {
|
|
185
|
-
list.push(1);
|
|
186
|
-
list.push(2);
|
|
187
|
-
list.push(3);
|
|
188
|
-
const index = list.indexOf(4);
|
|
189
|
-
expect(index).toBe(-1);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe('toArray', () => {
|
|
194
|
-
it('should convert the list to an array', () => {
|
|
195
|
-
list.push(1);
|
|
196
|
-
list.push(2);
|
|
197
|
-
list.push(3);
|
|
198
|
-
const array = list.toArray();
|
|
199
|
-
expect(array).toEqual([1, 2, 3]);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it('should return an empty array for an empty list', () => {
|
|
203
|
-
const array = list.toArray();
|
|
204
|
-
expect(array).toEqual([]);
|
|
205
|
-
});
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
describe('insertBefore', () => {
|
|
209
|
-
it('should insert an element before an existing value', () => {
|
|
210
|
-
list.push(1);
|
|
211
|
-
list.push(2);
|
|
212
|
-
list.push(3);
|
|
213
|
-
list.insertBefore(2, 4);
|
|
214
|
-
expect(list.toArray()).toEqual([1, 4, 2, 3]);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it('should insert an element at the beginning', () => {
|
|
218
|
-
list.push(1);
|
|
219
|
-
list.push(2);
|
|
220
|
-
list.insertBefore(1, 3);
|
|
221
|
-
expect(list.toArray()).toEqual([3, 1, 2]);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should return false if the existing value is not found', () => {
|
|
225
|
-
list.push(1);
|
|
226
|
-
list.push(2);
|
|
227
|
-
list.push(3);
|
|
228
|
-
const result = list.insertBefore(5, 4);
|
|
229
|
-
expect(result).toBe(false);
|
|
230
|
-
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
describe('getLength', () => {
|
|
235
|
-
it('should return the correct length of the list', () => {
|
|
236
|
-
expect(list.length).toBe(0);
|
|
237
|
-
list.push(1);
|
|
238
|
-
list.push(2);
|
|
239
|
-
expect(list.length).toBe(2);
|
|
240
|
-
});
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
describe('remove', () => {
|
|
244
|
-
it('should remove and return the element at the specified index', () => {
|
|
245
|
-
list.push(1);
|
|
246
|
-
list.push(2);
|
|
247
|
-
list.push(3);
|
|
248
|
-
const removed = list.deleteAt(1);
|
|
249
|
-
expect(removed).toBe(2);
|
|
250
|
-
expect(list.toArray()).toEqual([1, 3]);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('should return undefined for an out-of-bounds index', () => {
|
|
254
|
-
list.push(1);
|
|
255
|
-
const removed = list.deleteAt(1);
|
|
256
|
-
expect(removed).toBeUndefined();
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it('should remove and return the first element', () => {
|
|
260
|
-
list.push(1);
|
|
261
|
-
list.push(2);
|
|
262
|
-
const removed = list.deleteAt(0);
|
|
263
|
-
expect(removed).toBe(1);
|
|
264
|
-
expect(list.toArray()).toEqual([2]);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
it('should remove and return the last element', () => {
|
|
268
|
-
list.push(1);
|
|
269
|
-
list.push(2);
|
|
270
|
-
const removed = list.deleteAt(1);
|
|
271
|
-
expect(removed).toBe(2);
|
|
272
|
-
expect(list.toArray()).toEqual([1]);
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
describe('push and pop', () => {
|
|
277
|
-
it('should push and pop elements correctly', () => {
|
|
278
|
-
list.push(1);
|
|
279
|
-
list.push(2);
|
|
280
|
-
expect(list.pop()).toBe(2);
|
|
281
|
-
expect(list.pop()).toBe(1);
|
|
282
|
-
expect(list.pop()).toBeUndefined();
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
describe('shift and unshift', () => {
|
|
287
|
-
it('should shift and unshift elements correctly', () => {
|
|
288
|
-
list.unshift(1);
|
|
289
|
-
list.unshift(2);
|
|
290
|
-
expect(list.shift()).toBe(2);
|
|
291
|
-
expect(list.shift()).toBe(1);
|
|
292
|
-
expect(list.shift()).toBeUndefined();
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
describe('insert and toArray', () => {
|
|
297
|
-
it('should insert elements and return array correctly', () => {
|
|
298
|
-
list.insertAt(0, 1);
|
|
299
|
-
list.insertAt(1, 3);
|
|
300
|
-
list.insertAt(1, 2);
|
|
301
|
-
expect(list.toArray()).toEqual([1, 2, 3]);
|
|
302
|
-
});
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
describe('find', () => {
|
|
306
|
-
it('should find elements using a callback function', () => {
|
|
307
|
-
list.push(1);
|
|
308
|
-
list.push(2);
|
|
309
|
-
list.push(3);
|
|
310
|
-
const result = list.find(data => data % 2 === 0);
|
|
311
|
-
expect(result).toBe(2);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
it('should return undefined if element is not found', () => {
|
|
315
|
-
list.push(1);
|
|
316
|
-
list.push(3);
|
|
317
|
-
const result = list.find(data => data % 2 === 0);
|
|
318
|
-
expect(result).toBeNull();
|
|
319
|
-
});
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
describe('reverse', () => {
|
|
323
|
-
it('should reverse the order of elements', () => {
|
|
324
|
-
list.push(1);
|
|
325
|
-
list.push(2);
|
|
326
|
-
list.push(3);
|
|
327
|
-
list.reverse();
|
|
328
|
-
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
329
|
-
});
|
|
330
|
-
});
|
|
331
|
-
|
|
332
|
-
describe('countOccurrences', () => {
|
|
333
|
-
it('should count occurrences of a value', () => {
|
|
334
|
-
list.push(1);
|
|
335
|
-
list.push(2);
|
|
336
|
-
list.push(2);
|
|
337
|
-
list.push(3);
|
|
338
|
-
const count = list.countOccurrences(2);
|
|
339
|
-
expect(count).toBe(2);
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('should return 0 if value is not found', () => {
|
|
343
|
-
list.push(1);
|
|
344
|
-
list.push(2);
|
|
345
|
-
const count = list.countOccurrences(3);
|
|
346
|
-
expect(count).toBe(0);
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('should insert and manipulate objects with numeric properties', () => {
|
|
351
|
-
const obj1 = {keyA: 1};
|
|
352
|
-
const obj2 = {keyA: 2};
|
|
353
|
-
const obj3 = {keyA: 3};
|
|
354
|
-
|
|
355
|
-
objectList.push(obj1);
|
|
356
|
-
objectList.push(obj2);
|
|
357
|
-
objectList.push(obj3);
|
|
358
|
-
|
|
359
|
-
expect(objectList.toArray()).toEqual([obj1, obj2, obj3]);
|
|
360
|
-
|
|
361
|
-
const newObj = {keyA: 2.5}; // Corrected newObj value
|
|
362
|
-
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
363
|
-
expect(insertSuccess).toBe(true);
|
|
364
|
-
|
|
365
|
-
const findNode = objectList.findNode(newObj); // Use newObj instead of obj2
|
|
366
|
-
expect(findNode?.val).toEqual(newObj);
|
|
367
|
-
|
|
368
|
-
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
369
|
-
expect(deleted).toBe(true);
|
|
370
|
-
|
|
371
|
-
const poppedObj = objectList.pop();
|
|
372
|
-
expect(poppedObj).toBe(obj3);
|
|
373
|
-
|
|
374
|
-
const shiftedObj = objectList.shift();
|
|
375
|
-
expect(shiftedObj).toBe(obj1);
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
describe('SinglyLinkedList Performance Test', () => {
|
|
380
|
-
it('should the push and pop methods adhere to a time complexity of O(n) and executed correctly under large scale data', () => {
|
|
381
|
-
const list = new SinglyLinkedList<number>();
|
|
382
|
-
|
|
383
|
-
const startPushTime = performance.now();
|
|
384
|
-
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
385
|
-
list.push(i);
|
|
386
|
-
}
|
|
387
|
-
expect(performance.now() - startPushTime).toBeLessThan(bigO.LINEAR * 10);
|
|
388
|
-
|
|
389
|
-
const startPopTime = performance.now();
|
|
390
|
-
|
|
391
|
-
for (let i = 0; i < magnitude.LINEAR; i++) {
|
|
392
|
-
list.pop();
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR);
|
|
396
|
-
expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 300);
|
|
397
|
-
});
|
|
398
|
-
});
|
|
399
|
-
describe('SinglyLinkedList', () => {
|
|
400
|
-
let list: SinglyLinkedList<number>;
|
|
401
|
-
|
|
402
|
-
beforeEach(() => {
|
|
403
|
-
list = new SinglyLinkedList<number>();
|
|
404
|
-
});
|
|
405
|
-
|
|
406
|
-
it('should initialize an empty list', () => {
|
|
407
|
-
expect(list.head).toBeNull();
|
|
408
|
-
expect(list.tail).toBeNull();
|
|
409
|
-
expect(list.length).toBe(0);
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
it('should push elements to the end of the list', () => {
|
|
413
|
-
list.push(1);
|
|
414
|
-
list.push(2);
|
|
415
|
-
expect(list.head!.val).toBe(1);
|
|
416
|
-
expect(list.tail!.val).toBe(2);
|
|
417
|
-
expect(list.length).toBe(2);
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
it('should pop elements from the end of the list', () => {
|
|
421
|
-
list.push(1);
|
|
422
|
-
list.push(2);
|
|
423
|
-
const popped = list.pop();
|
|
424
|
-
expect(popped).toBe(2);
|
|
425
|
-
expect(list.head!.val).toBe(1);
|
|
426
|
-
expect(list.tail!.val).toBe(1);
|
|
427
|
-
expect(list.length).toBe(1);
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
// Add more test cases for other methods like shift, unshift, getAt, deleteAt, and more.
|
|
431
|
-
|
|
432
|
-
it('should reverse the list', () => {
|
|
433
|
-
list.push(1);
|
|
434
|
-
list.push(2);
|
|
435
|
-
list.push(3);
|
|
436
|
-
list.reverse();
|
|
437
|
-
expect(list.head!.val).toBe(3);
|
|
438
|
-
expect(list.tail!.val).toBe(1);
|
|
439
|
-
// Add more assertions for reversed order.
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
// Add more test cases for other methods like find, indexOf, and more.
|
|
443
|
-
|
|
444
|
-
it('should convert the list to an array', () => {
|
|
445
|
-
list.push(1);
|
|
446
|
-
list.push(2);
|
|
447
|
-
list.push(3);
|
|
448
|
-
const array = list.toArray();
|
|
449
|
-
expect(array).toEqual([1, 2, 3]);
|
|
450
|
-
});
|
|
451
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
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 remove elements correctly', () => {
|
|
29
|
-
skipList.add(1, 'One');
|
|
30
|
-
skipList.add(2, 'Two');
|
|
31
|
-
skipList.add(3, 'Three');
|
|
32
|
-
|
|
33
|
-
skipList.remove(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
|
-
});
|
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import {Matrix2D, Vector2D} from '../../../../src';
|
|
2
|
-
|
|
3
|
-
describe('Matrix2D', () => {
|
|
4
|
-
it('should initialize with default identity matrix', () => {
|
|
5
|
-
const matrix = new Matrix2D();
|
|
6
|
-
const expectedMatrix = Matrix2D.identity;
|
|
7
|
-
|
|
8
|
-
expect(matrix.m).toEqual(expectedMatrix);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should initialize with provided 2D array', () => {
|
|
12
|
-
const inputMatrix = [
|
|
13
|
-
[2, 0, 0],
|
|
14
|
-
[0, 3, 0],
|
|
15
|
-
[0, 0, 1]
|
|
16
|
-
];
|
|
17
|
-
const matrix = new Matrix2D(inputMatrix);
|
|
18
|
-
|
|
19
|
-
expect(matrix.m).toEqual(inputMatrix);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should initialize with provided Vector2D', () => {
|
|
23
|
-
expect(true).toBeTruthy();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should add two matrices correctly', () => {
|
|
27
|
-
const matrix1 = new Matrix2D([
|
|
28
|
-
[1, 2, 3],
|
|
29
|
-
[4, 5, 6],
|
|
30
|
-
[7, 8, 9]
|
|
31
|
-
]);
|
|
32
|
-
const matrix2 = new Matrix2D([
|
|
33
|
-
[9, 8, 7],
|
|
34
|
-
[6, 5, 4],
|
|
35
|
-
[3, 2, 1]
|
|
36
|
-
]);
|
|
37
|
-
const expectedMatrix = [
|
|
38
|
-
[10, 10, 10],
|
|
39
|
-
[10, 10, 10],
|
|
40
|
-
[10, 10, 10]
|
|
41
|
-
];
|
|
42
|
-
|
|
43
|
-
const result = Matrix2D.add(matrix1, matrix2);
|
|
44
|
-
|
|
45
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should subtract two matrices correctly', () => {
|
|
49
|
-
const matrix1 = new Matrix2D([
|
|
50
|
-
[9, 8, 7],
|
|
51
|
-
[6, 5, 4],
|
|
52
|
-
[3, 2, 1]
|
|
53
|
-
]);
|
|
54
|
-
const matrix2 = new Matrix2D([
|
|
55
|
-
[1, 2, 3],
|
|
56
|
-
[4, 5, 6],
|
|
57
|
-
[7, 8, 9]
|
|
58
|
-
]);
|
|
59
|
-
const expectedMatrix = [
|
|
60
|
-
[8, 6, 4],
|
|
61
|
-
[2, 0, -2],
|
|
62
|
-
[-4, -6, -8]
|
|
63
|
-
];
|
|
64
|
-
|
|
65
|
-
const result = Matrix2D.subtract(matrix1, matrix2);
|
|
66
|
-
|
|
67
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should multiply two matrices correctly', () => {
|
|
71
|
-
const matrix1 = new Matrix2D([
|
|
72
|
-
[1, 2, 3],
|
|
73
|
-
[4, 5, 6],
|
|
74
|
-
[7, 8, 9]
|
|
75
|
-
]);
|
|
76
|
-
const matrix2 = new Matrix2D([
|
|
77
|
-
[9, 8, 7],
|
|
78
|
-
[6, 5, 4],
|
|
79
|
-
[3, 2, 1]
|
|
80
|
-
]);
|
|
81
|
-
const expectedMatrix = [
|
|
82
|
-
[30, 24, 18],
|
|
83
|
-
[84, 69, 54],
|
|
84
|
-
[138, 114, 90]
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
const result = Matrix2D.multiply(matrix1, matrix2);
|
|
88
|
-
|
|
89
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should multiply a matrix by a Vector2D correctly', () => {
|
|
93
|
-
expect(true).toBeTruthy();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('should scale a matrix by a value correctly', () => {
|
|
97
|
-
expect(true).toBeTruthy();
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should rotate a matrix by radians correctly', () => {
|
|
101
|
-
expect(true).toBeTruthy();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it('should translate a matrix by a Vector2D correctly', () => {
|
|
105
|
-
const translationVector = new Vector2D(2, 3);
|
|
106
|
-
const expectedMatrix = [
|
|
107
|
-
[1, 0, 2],
|
|
108
|
-
[0, 1, 3],
|
|
109
|
-
[0, 0, 1]
|
|
110
|
-
];
|
|
111
|
-
|
|
112
|
-
const result = Matrix2D.translate(translationVector);
|
|
113
|
-
|
|
114
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('should create a view matrix correctly', () => {
|
|
118
|
-
expect(true).toBeTruthy();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should multiply a matrix by a value correctly', () => {
|
|
122
|
-
const matrix = new Matrix2D([
|
|
123
|
-
[1, 2, 3],
|
|
124
|
-
[4, 5, 6],
|
|
125
|
-
[7, 8, 9]
|
|
126
|
-
]);
|
|
127
|
-
const value = 2;
|
|
128
|
-
const expectedMatrix = [
|
|
129
|
-
[2, 4, 6],
|
|
130
|
-
[8, 10, 12],
|
|
131
|
-
[14, 16, 18]
|
|
132
|
-
];
|
|
133
|
-
|
|
134
|
-
const result = Matrix2D.multiplyByValue(matrix, value);
|
|
135
|
-
|
|
136
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
137
|
-
});
|
|
138
|
-
});
|