data-structure-typed 1.39.0 → 1.39.2
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/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/cjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/cjs/types/helpers.d.ts +1 -4
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/map-graph.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/mjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/mjs/types/helpers.d.ts +1 -4
- 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/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +289 -47
- package/test/unit/data-structures/binary-tree/bst.test.ts +391 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +73 -7
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +241 -186
- package/test/unit/data-structures/graph/directed-graph.test.ts +34 -4
- package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +82 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +69 -6
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +59 -12
- package/test/unit/data-structures/matrix/matrix2d.test.ts +207 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +166 -1
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/queue/deque.test.ts +283 -20
- package/test/unit/data-structures/queue/queue.test.ts +10 -8
|
@@ -57,3 +57,85 @@ describe('UndirectedGraph Operation Test', () => {
|
|
|
57
57
|
expect(Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key)).toEqual(['A', 'B', 'D']);
|
|
58
58
|
});
|
|
59
59
|
});
|
|
60
|
+
|
|
61
|
+
describe('UndirectedGraph', () => {
|
|
62
|
+
let undirectedGraph: UndirectedGraph<UndirectedVertex<string>, UndirectedEdge<string>>;
|
|
63
|
+
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
// Create a new UndirectedGraph instance before each test
|
|
66
|
+
undirectedGraph = new UndirectedGraph<UndirectedVertex<string>, UndirectedEdge<string>>();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Test adding vertices to the graph
|
|
70
|
+
it('should add vertices to the graph', () => {
|
|
71
|
+
const vertexA = new UndirectedVertex('A', 'Location A');
|
|
72
|
+
const vertexB = new UndirectedVertex('B', 'Location B');
|
|
73
|
+
|
|
74
|
+
undirectedGraph.addVertex(vertexA);
|
|
75
|
+
undirectedGraph.addVertex(vertexB);
|
|
76
|
+
|
|
77
|
+
expect(undirectedGraph.hasVertex('A')).toBe(true);
|
|
78
|
+
expect(undirectedGraph.hasVertex('B')).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Test adding edges to the graph
|
|
82
|
+
it('should add edges to the graph', () => {
|
|
83
|
+
const vertexA = new UndirectedVertex('A', 'Location A');
|
|
84
|
+
const vertexB = new UndirectedVertex('B', 'Location B');
|
|
85
|
+
const edgeAB = new UndirectedEdge('A', 'B', 1, 'Edge between A and B');
|
|
86
|
+
|
|
87
|
+
undirectedGraph.addVertex(vertexA);
|
|
88
|
+
undirectedGraph.addVertex(vertexB);
|
|
89
|
+
undirectedGraph.addEdge(edgeAB);
|
|
90
|
+
|
|
91
|
+
expect(undirectedGraph.hasEdge('A', 'B')).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Test getting neighbors of a vertex
|
|
95
|
+
it('should return the neighbors of a vertex', () => {
|
|
96
|
+
const vertexA = new UndirectedVertex('A', 'Location A');
|
|
97
|
+
const vertexB = new UndirectedVertex('B', 'Location B');
|
|
98
|
+
const vertexC = new UndirectedVertex('C', 'Location C');
|
|
99
|
+
const edgeAB = new UndirectedEdge('A', 'B', 1, 'Edge between A and B');
|
|
100
|
+
const edgeBC = new UndirectedEdge('B', 'C', 2, 'Edge between B and C');
|
|
101
|
+
|
|
102
|
+
undirectedGraph.addVertex(vertexA);
|
|
103
|
+
undirectedGraph.addVertex(vertexB);
|
|
104
|
+
undirectedGraph.addVertex(vertexC);
|
|
105
|
+
undirectedGraph.addEdge(edgeAB);
|
|
106
|
+
undirectedGraph.addEdge(edgeBC);
|
|
107
|
+
|
|
108
|
+
const neighborsOfA = undirectedGraph.getNeighbors('A');
|
|
109
|
+
const neighborsOfB = undirectedGraph.getNeighbors('B');
|
|
110
|
+
|
|
111
|
+
expect(neighborsOfA).toEqual([vertexB]);
|
|
112
|
+
expect(neighborsOfB).toEqual([vertexA, vertexC]);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// Test degree of a vertex
|
|
116
|
+
it('should return the degree of a vertex', () => {
|
|
117
|
+
const vertexA = new UndirectedVertex('A', 'Location A');
|
|
118
|
+
const vertexB = new UndirectedVertex('B', 'Location B');
|
|
119
|
+
const vertexC = new UndirectedVertex('C', 'Location C');
|
|
120
|
+
const edgeAB = new UndirectedEdge('A', 'B', 3, 'Edge between A and B');
|
|
121
|
+
const edgeBC = new UndirectedEdge('B', 'C', 4, 'Edge between B and C');
|
|
122
|
+
|
|
123
|
+
edgeAB.vertices = edgeAB.vertices;
|
|
124
|
+
expect(undirectedGraph.edges.size).toBe(0);
|
|
125
|
+
undirectedGraph.addVertex(vertexA);
|
|
126
|
+
undirectedGraph.addVertex(vertexB);
|
|
127
|
+
undirectedGraph.addVertex(vertexC);
|
|
128
|
+
undirectedGraph.addEdge(edgeAB);
|
|
129
|
+
undirectedGraph.addEdge(edgeBC);
|
|
130
|
+
|
|
131
|
+
const degreeOfA = undirectedGraph.degreeOf('A');
|
|
132
|
+
const degreeOfB = undirectedGraph.degreeOf('B');
|
|
133
|
+
const degreeOfC = undirectedGraph.degreeOf('C');
|
|
134
|
+
expect(undirectedGraph.edgeSet().length).toBe(2);
|
|
135
|
+
expect(undirectedGraph.getEndsOfEdge(edgeBC)?.length).toBe(2);
|
|
136
|
+
|
|
137
|
+
expect(degreeOfA).toBe(1);
|
|
138
|
+
expect(degreeOfB).toBe(2);
|
|
139
|
+
expect(degreeOfC).toBe(1);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -52,3 +52,23 @@ describe('CoordinateMap', () => {
|
|
|
52
52
|
expect(retrievedValue).toBe(value);
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe('CoordinateMap', () => {
|
|
57
|
+
class MyCoordinateMap<V = any> extends CoordinateMap<V> {
|
|
58
|
+
constructor(joint?: string) {
|
|
59
|
+
super(joint);
|
|
60
|
+
this._setJoint(joint += '-')
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const cMap = new MyCoordinateMap<number>('*');
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
cMap.set([0, 0], 0);
|
|
68
|
+
cMap.set([0, 1], 1);
|
|
69
|
+
cMap.set([1, 1], 11);
|
|
70
|
+
})
|
|
71
|
+
it('should joint to be *-', () => {
|
|
72
|
+
expect(cMap.joint).toBe('*-');
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -39,3 +39,28 @@ describe('CoordinateSet', () => {
|
|
|
39
39
|
expect(hasValue).toBe(true);
|
|
40
40
|
});
|
|
41
41
|
});
|
|
42
|
+
|
|
43
|
+
describe('MyCoordinateSet', () => {
|
|
44
|
+
class MyCoordinateSet extends CoordinateSet {
|
|
45
|
+
constructor(joint?: string) {
|
|
46
|
+
super(joint);
|
|
47
|
+
this._setJoint(joint += '-')
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const mySet = new MyCoordinateSet('*');
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
mySet.add([0, 0]);
|
|
55
|
+
mySet.add([0, 1]);
|
|
56
|
+
mySet.add([1, 1]);
|
|
57
|
+
})
|
|
58
|
+
it('should joint to be *-', () => {
|
|
59
|
+
expect(mySet.joint).toBe('*-');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should has, delete', () => {
|
|
63
|
+
mySet.delete([0, 1]);
|
|
64
|
+
expect(mySet.has([0, 1])).toBe(false);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -15,7 +15,9 @@ describe('HashNode', () => {
|
|
|
15
15
|
describe('HashTable', () => {
|
|
16
16
|
it('should initialize with default capacity', () => {
|
|
17
17
|
const hashTable = new HashTable<string, string>();
|
|
18
|
-
|
|
18
|
+
hashTable.capacity = hashTable.capacity;
|
|
19
|
+
hashTable.buckets = hashTable.buckets;
|
|
20
|
+
hashTable.hashFn = hashTable.hashFn;
|
|
19
21
|
expect(hashTable.capacity).toBe(16);
|
|
20
22
|
expect(hashTable.size).toBe(0);
|
|
21
23
|
expect(hashTable.buckets.length).toBe(16);
|
|
@@ -1,6 +1,69 @@
|
|
|
1
|
-
import {DoublyLinkedList} from '../../../../src';
|
|
1
|
+
import {DoublyLinkedList, DoublyLinkedListNode} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
|
+
describe('DoublyLinkedListNode', () => {
|
|
5
|
+
it('should DoublyLinkedListNode', () => {
|
|
6
|
+
const node1 = new DoublyLinkedListNode<number>(2);
|
|
7
|
+
expect(node1.val).toBe(2);
|
|
8
|
+
node1.val = 1;
|
|
9
|
+
expect(node1.val).toBe(1);
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('DoublyLinkedList Operation Test', () => {
|
|
14
|
+
let list: DoublyLinkedList<number>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
list = DoublyLinkedList.fromArray([1, 2, 3, 4, 5]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should out of bound index', () => {
|
|
21
|
+
expect(list.getNodeAt(-1)).toBe(null);
|
|
22
|
+
expect(list.getNodeAt(5)).toBe(null);
|
|
23
|
+
expect(list.insertAt(5, 6)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should insertBefore', () => {
|
|
27
|
+
expect(list.insertBefore(1, 0)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should deleteAt', () => {
|
|
31
|
+
expect(list.deleteAt(1)).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should delete tail', () => {
|
|
35
|
+
expect(list.delete(list.tail)).toBe(true);
|
|
36
|
+
expect(list.tail?.val).toBe(4);
|
|
37
|
+
expect(list.delete(6)).toBe(false);
|
|
38
|
+
expect(list.tail?.val).toBe(4);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
it('should find null', () => {
|
|
43
|
+
expect(list.find(val => val === 6)).toBe(null);
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should indexOf -1', () => {
|
|
48
|
+
expect(list.indexOf(6)).toBe(-1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should findBackward null', () => {
|
|
52
|
+
expect(list.findBackward(val => val === 0)).toBe(null);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should insertAfter tail', () => {
|
|
56
|
+
expect(list.insertAfter(list.tail!, 6)).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should insertAfter tail', () => {
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
expect([...list]).toEqual([1, 2, 3, 4, 5]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
|
|
4
67
|
describe('DoublyLinkedList Operation Test', () => {
|
|
5
68
|
let list: DoublyLinkedList<number>;
|
|
6
69
|
let objectList: DoublyLinkedList<{ keyA: number }>;
|
|
@@ -109,7 +172,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
109
172
|
list.reverse();
|
|
110
173
|
|
|
111
174
|
expect(list.toArray()).toEqual([3, 2, 1]);
|
|
112
|
-
expect(list.
|
|
175
|
+
expect(list.toArrayBackward()).toEqual([1, 2, 3]);
|
|
113
176
|
});
|
|
114
177
|
|
|
115
178
|
it('should map elements using a callback function', () => {
|
|
@@ -189,7 +252,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
189
252
|
list.push(3);
|
|
190
253
|
list.push(4);
|
|
191
254
|
|
|
192
|
-
const lastEven = list.
|
|
255
|
+
const lastEven = list.findBackward(val => val % 2 === 0);
|
|
193
256
|
|
|
194
257
|
expect(lastEven).toBe(4);
|
|
195
258
|
});
|
|
@@ -211,7 +274,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
211
274
|
list.push(2);
|
|
212
275
|
list.push(3);
|
|
213
276
|
|
|
214
|
-
const reversedArray = list.
|
|
277
|
+
const reversedArray = list.toArrayBackward();
|
|
215
278
|
|
|
216
279
|
expect(reversedArray).toEqual([3, 2, 1]);
|
|
217
280
|
});
|
|
@@ -327,8 +390,8 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
327
390
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
328
391
|
expect(insertSuccess).toBe(true);
|
|
329
392
|
|
|
330
|
-
const
|
|
331
|
-
expect(
|
|
393
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
394
|
+
expect(getNode?.val).toEqual(newObj);
|
|
332
395
|
|
|
333
396
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
334
397
|
expect(deleted).toBe(true);
|
|
@@ -11,7 +11,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
11
11
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
12
12
|
doublyList.push(i);
|
|
13
13
|
if (i === midIndex) {
|
|
14
|
-
midNode = doublyList.
|
|
14
|
+
midNode = doublyList.getNode(i);
|
|
15
15
|
} else if (i > midIndex && midNode) {
|
|
16
16
|
doublyList.insertBefore(midNode, i);
|
|
17
17
|
}
|
|
@@ -24,7 +24,7 @@ describe('LinkedList Performance Test', () => {
|
|
|
24
24
|
for (let i = 0; i < magnitude.SQUARED; i++) {
|
|
25
25
|
singlyList.push(i);
|
|
26
26
|
if (i === midIndex) {
|
|
27
|
-
midSinglyNode = singlyList.
|
|
27
|
+
midSinglyNode = singlyList.getNode(i);
|
|
28
28
|
} else if (i > midIndex && midSinglyNode) {
|
|
29
29
|
singlyList.insertBefore(midSinglyNode.val, i);
|
|
30
30
|
}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
import {SinglyLinkedList} from '../../../../src';
|
|
1
|
+
import {SinglyLinkedList, SinglyLinkedListNode} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
3
|
|
|
4
|
+
describe('SinglyLinkedListNode', () => {
|
|
5
|
+
it('should SinglyLinkedList', () => {
|
|
6
|
+
const node1 = new SinglyLinkedListNode<number>(2);
|
|
7
|
+
expect(node1.val).toBe(2);
|
|
8
|
+
node1.val = 1;
|
|
9
|
+
expect(node1.val).toBe(1);
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
|
|
4
13
|
describe('SinglyLinkedList Operation Test', () => {
|
|
5
14
|
let list: SinglyLinkedList<number>;
|
|
6
15
|
let objectList: SinglyLinkedList<{ keyA: number }>;
|
|
@@ -21,8 +30,10 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
21
30
|
it('should delete and return the last element of the list', () => {
|
|
22
31
|
list.push(1);
|
|
23
32
|
list.push(2);
|
|
33
|
+
list.push(3);
|
|
24
34
|
const popped = list.pop();
|
|
25
|
-
expect(popped).toBe(
|
|
35
|
+
expect(popped).toBe(3);
|
|
36
|
+
expect(list.popLast()).toBe(2);
|
|
26
37
|
expect(list.toArray()).toEqual([1]);
|
|
27
38
|
});
|
|
28
39
|
|
|
@@ -36,9 +47,11 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
36
47
|
it('should delete and return the first element of the list', () => {
|
|
37
48
|
list.push(1);
|
|
38
49
|
list.push(2);
|
|
50
|
+
list.push(3);
|
|
39
51
|
const shifted = list.shift();
|
|
40
52
|
expect(shifted).toBe(1);
|
|
41
|
-
expect(list.
|
|
53
|
+
expect(list.popFirst()).toBe(2);
|
|
54
|
+
expect(list.toArray()).toEqual([3]);
|
|
42
55
|
});
|
|
43
56
|
|
|
44
57
|
it('should return undefined if the list is empty', () => {
|
|
@@ -50,7 +63,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
50
63
|
describe('unshift', () => {
|
|
51
64
|
it('should add elements to the beginning of the list', () => {
|
|
52
65
|
list.unshift(1);
|
|
53
|
-
list.
|
|
66
|
+
list.addFirst(2);
|
|
54
67
|
expect(list.toArray()).toEqual([2, 1]);
|
|
55
68
|
});
|
|
56
69
|
});
|
|
@@ -62,6 +75,7 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
62
75
|
list.push(3);
|
|
63
76
|
const element = list.getAt(1);
|
|
64
77
|
expect(element).toBe(2);
|
|
78
|
+
expect(list.getNodeAt(2)?.val).toBe(3);
|
|
65
79
|
});
|
|
66
80
|
|
|
67
81
|
it('should return undefined for an out-of-bounds index', () => {
|
|
@@ -113,9 +127,14 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
113
127
|
list.push(1);
|
|
114
128
|
list.push(2);
|
|
115
129
|
list.push(3);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
expect(list.
|
|
130
|
+
list.push(4);
|
|
131
|
+
list.push(5);
|
|
132
|
+
expect(list.delete(2)).toBe(true);
|
|
133
|
+
expect(list.toArray()).toEqual([1, 3, 4, 5]);
|
|
134
|
+
expect(list.delete(1)).toBe(true);
|
|
135
|
+
expect(list.toArray()).toEqual([3, 4, 5]);
|
|
136
|
+
expect(list.delete(5)).toBe(true);
|
|
137
|
+
expect(list.toArray()).toEqual([3, 4]);
|
|
119
138
|
});
|
|
120
139
|
|
|
121
140
|
it('should return false if the value is not found', () => {
|
|
@@ -362,8 +381,8 @@ describe('SinglyLinkedList Operation Test', () => {
|
|
|
362
381
|
const insertSuccess = objectList.insertBefore(obj2, newObj);
|
|
363
382
|
expect(insertSuccess).toBe(true);
|
|
364
383
|
|
|
365
|
-
const
|
|
366
|
-
expect(
|
|
384
|
+
const getNode = objectList.getNode(newObj); // Use newObj instead of obj2
|
|
385
|
+
expect(getNode?.val).toEqual(newObj);
|
|
367
386
|
|
|
368
387
|
const deleted = objectList.delete(newObj); // Use newObj instead of obj2
|
|
369
388
|
expect(deleted).toBe(true);
|
|
@@ -393,7 +412,7 @@ describe('SinglyLinkedList Performance Test', () => {
|
|
|
393
412
|
}
|
|
394
413
|
|
|
395
414
|
// expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR);
|
|
396
|
-
expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR *
|
|
415
|
+
expect(performance.now() - startPopTime).toBeLessThan(bigO.LINEAR * 400);
|
|
397
416
|
});
|
|
398
417
|
});
|
|
399
418
|
describe('SinglyLinkedList', () => {
|
|
@@ -427,7 +446,6 @@ describe('SinglyLinkedList', () => {
|
|
|
427
446
|
expect(list.length).toBe(1);
|
|
428
447
|
});
|
|
429
448
|
|
|
430
|
-
// Add more test cases for other methods like shift, unshift, getAt, deleteAt, and more.
|
|
431
449
|
|
|
432
450
|
it('should reverse the list', () => {
|
|
433
451
|
list.push(1);
|
|
@@ -439,7 +457,6 @@ describe('SinglyLinkedList', () => {
|
|
|
439
457
|
// Add more assertions for reversed order.
|
|
440
458
|
});
|
|
441
459
|
|
|
442
|
-
// Add more test cases for other methods like find, indexOf, and more.
|
|
443
460
|
|
|
444
461
|
it('should convert the list to an array', () => {
|
|
445
462
|
list.push(1);
|
|
@@ -447,5 +464,35 @@ describe('SinglyLinkedList', () => {
|
|
|
447
464
|
list.push(3);
|
|
448
465
|
const array = list.toArray();
|
|
449
466
|
expect(array).toEqual([1, 2, 3]);
|
|
467
|
+
// @ts-ignore
|
|
468
|
+
expect([...list]).toEqual([1, 2, 3]);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('should filter the list', () => {
|
|
472
|
+
list.push(1);
|
|
473
|
+
list.push(2);
|
|
474
|
+
list.push(3);
|
|
475
|
+
expect(list.filter(val => val !== 2).toArray()).toEqual([1, 3]);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
it('should forEach the list', () => {
|
|
480
|
+
list.push(1);
|
|
481
|
+
list.push(2);
|
|
482
|
+
list.push(3);
|
|
483
|
+
list.forEach(val => val++);
|
|
484
|
+
expect(list.toArray()).toEqual([1, 2, 3])
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it('should map the list', () => {
|
|
488
|
+
list.addLast(1);
|
|
489
|
+
list.push(2);
|
|
490
|
+
list.push(3);
|
|
491
|
+
expect(list.map(val => val * 2).toArray()).toEqual([2, 4, 6]);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('should reduce the list', () => {
|
|
495
|
+
const list1 = SinglyLinkedList.fromArray([1, 2, 3]);
|
|
496
|
+
expect(list1.reduce((acc, val) => acc + val, 0)).toEqual(6);
|
|
450
497
|
});
|
|
451
498
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {Matrix2D, Vector2D} from '../../../../src';
|
|
2
|
+
import {isDebugTest} from '../../../config';
|
|
2
3
|
|
|
4
|
+
const isDebug = isDebugTest;
|
|
3
5
|
describe('Matrix2D', () => {
|
|
4
6
|
it('should initialize with default identity matrix', () => {
|
|
5
7
|
const matrix = new Matrix2D();
|
|
@@ -136,3 +138,208 @@ describe('Matrix2D', () => {
|
|
|
136
138
|
expect(result.m).toEqual(expectedMatrix);
|
|
137
139
|
});
|
|
138
140
|
});
|
|
141
|
+
|
|
142
|
+
describe('Vector2D', () => {
|
|
143
|
+
it('should create a vector with default values', () => {
|
|
144
|
+
const vector = new Vector2D();
|
|
145
|
+
expect(vector.x).toBe(0);
|
|
146
|
+
expect(vector.y).toBe(0);
|
|
147
|
+
expect(vector.w).toBe(1);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should correctly calculate vector length', () => {
|
|
151
|
+
const vector = new Vector2D(3, 4);
|
|
152
|
+
expect(vector.length).toBe(5);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('should correctly add two vectors', () => {
|
|
156
|
+
const vector1 = new Vector2D(2, 3);
|
|
157
|
+
const vector2 = new Vector2D(1, 2);
|
|
158
|
+
const result = Vector2D.add(vector1, vector2);
|
|
159
|
+
expect(result.x).toBe(3);
|
|
160
|
+
expect(result.y).toBe(5);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Add more test cases for Vector2D methods
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('Matrix2D', () => {
|
|
167
|
+
it('should create an identity matrix by default', () => {
|
|
168
|
+
const matrix = new Matrix2D();
|
|
169
|
+
expect(matrix.m).toEqual(Matrix2D.identity);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should correctly add two matrices', () => {
|
|
173
|
+
const matrix1 = new Matrix2D([
|
|
174
|
+
[1, 2, 3],
|
|
175
|
+
[4, 5, 6],
|
|
176
|
+
[7, 8, 9]
|
|
177
|
+
]);
|
|
178
|
+
const matrix2 = new Matrix2D([
|
|
179
|
+
[9, 8, 7],
|
|
180
|
+
[6, 5, 4],
|
|
181
|
+
[3, 2, 1]
|
|
182
|
+
]);
|
|
183
|
+
const result = Matrix2D.add(matrix1, matrix2);
|
|
184
|
+
expect(result.m).toEqual([
|
|
185
|
+
[10, 10, 10],
|
|
186
|
+
[10, 10, 10],
|
|
187
|
+
[10, 10, 10]
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Add more test cases for Matrix2D methods
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('Matrix2D', () => {
|
|
195
|
+
it('should create a matrix with default identity values', () => {
|
|
196
|
+
const matrix = new Matrix2D();
|
|
197
|
+
expect(matrix.m).toEqual(Matrix2D.identity);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should create a matrix from a Vector2D', () => {
|
|
201
|
+
const vector = new Vector2D(2, 3);
|
|
202
|
+
const matrix = new Matrix2D(vector);
|
|
203
|
+
expect(matrix.m).toEqual([
|
|
204
|
+
[2, 0, 0],
|
|
205
|
+
[3, 1, 0],
|
|
206
|
+
[1, 0, 1]
|
|
207
|
+
]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should correctly add two matrices', () => {
|
|
211
|
+
const matrix1 = new Matrix2D([
|
|
212
|
+
[1, 2, 3],
|
|
213
|
+
[4, 5, 6],
|
|
214
|
+
[7, 8, 9]
|
|
215
|
+
]);
|
|
216
|
+
const matrix2 = new Matrix2D([
|
|
217
|
+
[9, 8, 7],
|
|
218
|
+
[6, 5, 4],
|
|
219
|
+
[3, 2, 1]
|
|
220
|
+
]);
|
|
221
|
+
const result = Matrix2D.add(matrix1, matrix2);
|
|
222
|
+
expect(result.m).toEqual([
|
|
223
|
+
[10, 10, 10],
|
|
224
|
+
[10, 10, 10],
|
|
225
|
+
[10, 10, 10]
|
|
226
|
+
]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should correctly subtract two matrices', () => {
|
|
230
|
+
const matrix1 = new Matrix2D([
|
|
231
|
+
[5, 4, 3],
|
|
232
|
+
[2, 1, 0],
|
|
233
|
+
[0, 1, 2]
|
|
234
|
+
]);
|
|
235
|
+
const matrix2 = new Matrix2D([
|
|
236
|
+
[4, 3, 2],
|
|
237
|
+
[1, 0, 1],
|
|
238
|
+
[2, 1, 0]
|
|
239
|
+
]);
|
|
240
|
+
const result = Matrix2D.subtract(matrix1, matrix2);
|
|
241
|
+
expect(result.m).toEqual([
|
|
242
|
+
[1, 1, 1],
|
|
243
|
+
[1, 1, -1],
|
|
244
|
+
[-2, 0, 2]
|
|
245
|
+
]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should correctly multiply two matrices', () => {
|
|
249
|
+
const matrix1 = new Matrix2D([
|
|
250
|
+
[1, 2, 3],
|
|
251
|
+
[4, 5, 6],
|
|
252
|
+
[7, 8, 9]
|
|
253
|
+
]);
|
|
254
|
+
const matrix2 = new Matrix2D([
|
|
255
|
+
[9, 8, 7],
|
|
256
|
+
[6, 5, 4],
|
|
257
|
+
[3, 2, 1]
|
|
258
|
+
]);
|
|
259
|
+
const result = Matrix2D.multiply(matrix1, matrix2);
|
|
260
|
+
expect(result.m).toEqual([
|
|
261
|
+
[30, 24, 18],
|
|
262
|
+
[84, 69, 54],
|
|
263
|
+
[138, 114, 90]
|
|
264
|
+
]);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should correctly multiply a matrix by a value', () => {
|
|
268
|
+
const matrix = new Matrix2D([
|
|
269
|
+
[2, 3, 4],
|
|
270
|
+
[5, 6, 7],
|
|
271
|
+
[8, 9, 10]
|
|
272
|
+
]);
|
|
273
|
+
const value = 2;
|
|
274
|
+
const result = Matrix2D.multiplyByValue(matrix, value);
|
|
275
|
+
expect(result.m).toEqual([
|
|
276
|
+
[4, 6, 8],
|
|
277
|
+
[10, 12, 14],
|
|
278
|
+
[16, 18, 20]
|
|
279
|
+
]);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should correctly multiply a matrix by a vector', () => {
|
|
283
|
+
const matrix = new Matrix2D([
|
|
284
|
+
[2, 3, 4],
|
|
285
|
+
[5, 6, 7],
|
|
286
|
+
[8, 9, 10]
|
|
287
|
+
]);
|
|
288
|
+
const vector = new Vector2D(2, 3);
|
|
289
|
+
const result = Matrix2D.multiplyByVector(matrix, vector);
|
|
290
|
+
isDebug && console.log(JSON.stringify(result));
|
|
291
|
+
expect(result).toEqual({x: 17, y: 35, w: 1});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should correctly create a view matrix', () => {
|
|
295
|
+
const width = 800;
|
|
296
|
+
const height = 600;
|
|
297
|
+
const viewMatrix = Matrix2D.view(width, height);
|
|
298
|
+
expect(viewMatrix.m).toEqual([
|
|
299
|
+
[1, 0, 400],
|
|
300
|
+
[0, -1, 300],
|
|
301
|
+
[0, 0, 1]
|
|
302
|
+
]);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('should correctly scale a matrix', () => {
|
|
306
|
+
const factor = 3;
|
|
307
|
+
const scaledMatrix = Matrix2D.scale(factor);
|
|
308
|
+
expect(scaledMatrix.m).toEqual([
|
|
309
|
+
[3, 0, 0],
|
|
310
|
+
[0, 3, 0],
|
|
311
|
+
[0, 0, 3]
|
|
312
|
+
]);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('should correctly rotate a matrix', () => {
|
|
316
|
+
const radians = Math.PI / 4; // 45 degrees
|
|
317
|
+
const rotationMatrix = Matrix2D.rotate(radians);
|
|
318
|
+
console.log(JSON.stringify(rotationMatrix.m));
|
|
319
|
+
expect(rotationMatrix.m).toEqual([
|
|
320
|
+
[0.7071067811865476, -0.7071067811865475, 0],
|
|
321
|
+
[0.7071067811865475, 0.7071067811865476, 0],
|
|
322
|
+
[0, 0, 1]
|
|
323
|
+
]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('should correctly translate a matrix', () => {
|
|
327
|
+
const translationVector = new Vector2D(2, 3);
|
|
328
|
+
const translationMatrix = Matrix2D.translate(translationVector);
|
|
329
|
+
expect(translationMatrix.m).toEqual([
|
|
330
|
+
[1, 0, 2],
|
|
331
|
+
[0, 1, 3],
|
|
332
|
+
[0, 0, 1]
|
|
333
|
+
]);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('should correctly convert a matrix to a vector', () => {
|
|
337
|
+
const matrix = new Matrix2D([
|
|
338
|
+
[2, 3, 4],
|
|
339
|
+
[5, 6, 7],
|
|
340
|
+
[8, 9, 10]
|
|
341
|
+
]);
|
|
342
|
+
const vector = matrix.toVector();
|
|
343
|
+
expect(vector).toEqual(new Vector2D(2, 5));
|
|
344
|
+
});
|
|
345
|
+
});
|