data-structure-typed 1.49.4 → 1.49.5
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 +1 -3
- package/README_zh-CN.md +1 -1
- package/dist/cjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +19 -49
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +1 -43
- package/dist/cjs/data-structures/binary-tree/tree-multimap.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 +3 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +2 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/cjs/data-structures/matrix/index.js +0 -2
- package/dist/cjs/data-structures/matrix/index.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/cjs/data-structures/matrix/matrix.js +400 -15
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/cjs/data-structures/queue/deque.js +5 -7
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/cjs/utils/utils.d.ts +1 -0
- package/dist/cjs/utils/utils.js +6 -1
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +20 -50
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +2 -1
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +3 -44
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +4 -3
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +2 -2
- package/dist/mjs/data-structures/heap/heap.js +2 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/matrix/index.d.ts +0 -2
- package/dist/mjs/data-structures/matrix/index.js +0 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/mjs/data-structures/matrix/matrix.js +399 -16
- package/dist/mjs/data-structures/queue/deque.d.ts +2 -2
- package/dist/mjs/data-structures/queue/deque.js +7 -9
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/mjs/utils/utils.d.ts +1 -0
- package/dist/mjs/utils/utils.js +4 -0
- package/dist/umd/data-structure-typed.js +356 -524
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -3
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +15 -8
- package/src/data-structures/binary-tree/binary-tree.ts +57 -74
- package/src/data-structures/binary-tree/bst.ts +16 -13
- package/src/data-structures/binary-tree/rb-tree.ts +16 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +11 -48
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +7 -2
- package/src/types/common.ts +4 -4
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/test/integration/avl-tree.test.ts +18 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/performance/data-structures/comparison/comparison.test.ts +6 -11
- package/test/performance/data-structures/hash/hash-map.test.ts +3 -4
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/queue/deque.test.ts +6 -7
- package/test/performance/data-structures/queue/queue.test.ts +10 -9
- package/test/performance/reportor.ts +14 -15
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +59 -24
- package/test/unit/data-structures/binary-tree/bst.test.ts +52 -26
- package/test/unit/data-structures/binary-tree/overall.test.ts +16 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +17 -6
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -21
- package/test/unit/data-structures/graph/directed-graph.test.ts +104 -45
- package/test/unit/data-structures/graph/undirected-graph.test.ts +57 -40
- package/test/unit/data-structures/hash/hash-map.test.ts +48 -28
- package/test/unit/data-structures/hash/hash-table.test.ts +0 -4
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +6 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +10 -11
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +3 -4
- package/test/unit/data-structures/matrix/matrix.test.ts +345 -52
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -1
- package/test/unit/data-structures/queue/deque.test.ts +0 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/data-structures/stack/stack.test.ts +3 -4
- package/test/unit/unrestricted-interconversion.test.ts +50 -44
- package/test/utils/performanc.ts +1 -1
- package/typedoc.json +30 -0
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/cjs/data-structures/matrix/matrix2d.js +0 -200
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -291
- package/dist/cjs/data-structures/matrix/vector2d.js.map +0 -1
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/mjs/data-structures/matrix/matrix2d.js +0 -196
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -289
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
- package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -345
- package/test/unit/data-structures/matrix/vector2d.test.ts +0 -171
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
import { Matrix2D, Vector2D } from '../../../../src';
|
|
2
|
-
import { isDebugTest } from '../../../config';
|
|
3
|
-
|
|
4
|
-
const isDebug = isDebugTest;
|
|
5
|
-
describe('Matrix2D', () => {
|
|
6
|
-
it('should initialize with default identity matrix', () => {
|
|
7
|
-
const matrix = new Matrix2D();
|
|
8
|
-
const expectedMatrix = Matrix2D.identity;
|
|
9
|
-
|
|
10
|
-
expect(matrix.m).toEqual(expectedMatrix);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should initialize with provided 2D array', () => {
|
|
14
|
-
const inputMatrix = [
|
|
15
|
-
[2, 0, 0],
|
|
16
|
-
[0, 3, 0],
|
|
17
|
-
[0, 0, 1]
|
|
18
|
-
];
|
|
19
|
-
const matrix = new Matrix2D(inputMatrix);
|
|
20
|
-
|
|
21
|
-
expect(matrix.m).toEqual(inputMatrix);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('should initialize with provided Vector2D', () => {
|
|
25
|
-
expect(true).toBeTruthy();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should add two matrices correctly', () => {
|
|
29
|
-
const matrix1 = new Matrix2D([
|
|
30
|
-
[1, 2, 3],
|
|
31
|
-
[4, 5, 6],
|
|
32
|
-
[7, 8, 9]
|
|
33
|
-
]);
|
|
34
|
-
const matrix2 = new Matrix2D([
|
|
35
|
-
[9, 8, 7],
|
|
36
|
-
[6, 5, 4],
|
|
37
|
-
[3, 2, 1]
|
|
38
|
-
]);
|
|
39
|
-
const expectedMatrix = [
|
|
40
|
-
[10, 10, 10],
|
|
41
|
-
[10, 10, 10],
|
|
42
|
-
[10, 10, 10]
|
|
43
|
-
];
|
|
44
|
-
|
|
45
|
-
const result = Matrix2D.add(matrix1, matrix2);
|
|
46
|
-
|
|
47
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should subtract two matrices correctly', () => {
|
|
51
|
-
const matrix1 = new Matrix2D([
|
|
52
|
-
[9, 8, 7],
|
|
53
|
-
[6, 5, 4],
|
|
54
|
-
[3, 2, 1]
|
|
55
|
-
]);
|
|
56
|
-
const matrix2 = new Matrix2D([
|
|
57
|
-
[1, 2, 3],
|
|
58
|
-
[4, 5, 6],
|
|
59
|
-
[7, 8, 9]
|
|
60
|
-
]);
|
|
61
|
-
const expectedMatrix = [
|
|
62
|
-
[8, 6, 4],
|
|
63
|
-
[2, 0, -2],
|
|
64
|
-
[-4, -6, -8]
|
|
65
|
-
];
|
|
66
|
-
|
|
67
|
-
const result = Matrix2D.subtract(matrix1, matrix2);
|
|
68
|
-
|
|
69
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should multiply two matrices correctly', () => {
|
|
73
|
-
const matrix1 = new Matrix2D([
|
|
74
|
-
[1, 2, 3],
|
|
75
|
-
[4, 5, 6],
|
|
76
|
-
[7, 8, 9]
|
|
77
|
-
]);
|
|
78
|
-
const matrix2 = new Matrix2D([
|
|
79
|
-
[9, 8, 7],
|
|
80
|
-
[6, 5, 4],
|
|
81
|
-
[3, 2, 1]
|
|
82
|
-
]);
|
|
83
|
-
const expectedMatrix = [
|
|
84
|
-
[30, 24, 18],
|
|
85
|
-
[84, 69, 54],
|
|
86
|
-
[138, 114, 90]
|
|
87
|
-
];
|
|
88
|
-
|
|
89
|
-
const result = Matrix2D.multiply(matrix1, matrix2);
|
|
90
|
-
|
|
91
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should multiply a matrix by a Vector2D correctly', () => {
|
|
95
|
-
expect(true).toBeTruthy();
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should scale a matrix by a value correctly', () => {
|
|
99
|
-
expect(true).toBeTruthy();
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should rotate a matrix by radians correctly', () => {
|
|
103
|
-
expect(true).toBeTruthy();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should translate a matrix by a Vector2D correctly', () => {
|
|
107
|
-
const translationVector = new Vector2D(2, 3);
|
|
108
|
-
const expectedMatrix = [
|
|
109
|
-
[1, 0, 2],
|
|
110
|
-
[0, 1, 3],
|
|
111
|
-
[0, 0, 1]
|
|
112
|
-
];
|
|
113
|
-
|
|
114
|
-
const result = Matrix2D.translate(translationVector);
|
|
115
|
-
|
|
116
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should create a view matrix correctly', () => {
|
|
120
|
-
expect(true).toBeTruthy();
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should multiply a matrix by a value correctly', () => {
|
|
124
|
-
const matrix = new Matrix2D([
|
|
125
|
-
[1, 2, 3],
|
|
126
|
-
[4, 5, 6],
|
|
127
|
-
[7, 8, 9]
|
|
128
|
-
]);
|
|
129
|
-
const value = 2;
|
|
130
|
-
const expectedMatrix = [
|
|
131
|
-
[2, 4, 6],
|
|
132
|
-
[8, 10, 12],
|
|
133
|
-
[14, 16, 18]
|
|
134
|
-
];
|
|
135
|
-
|
|
136
|
-
const result = Matrix2D.multiplyByValue(matrix, value);
|
|
137
|
-
|
|
138
|
-
expect(result.m).toEqual(expectedMatrix);
|
|
139
|
-
});
|
|
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
|
-
isDebug && 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
|
-
});
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { Vector2D } from '../../../../src';
|
|
2
|
-
|
|
3
|
-
describe('Vector2D', () => {
|
|
4
|
-
it('should create a vector with default values', () => {
|
|
5
|
-
const vector = new Vector2D();
|
|
6
|
-
expect(vector.x).toBe(0);
|
|
7
|
-
expect(vector.y).toBe(0);
|
|
8
|
-
expect(vector.w).toBe(1);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should correctly check if a vector is zero', () => {
|
|
12
|
-
const nonZeroVector = new Vector2D(3, 4);
|
|
13
|
-
const zeroVector = new Vector2D(0, 0);
|
|
14
|
-
expect(nonZeroVector.isZero).toBe(false);
|
|
15
|
-
expect(zeroVector.isZero).toBe(true);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should correctly calculate vector length', () => {
|
|
19
|
-
const vector = new Vector2D(3, 4);
|
|
20
|
-
expect(vector.length).toBe(5);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should correctly calculate squared vector length', () => {
|
|
24
|
-
const vector = new Vector2D(3, 4);
|
|
25
|
-
expect(vector.lengthSq).toBe(25);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should correctly round vector components', () => {
|
|
29
|
-
const vector = new Vector2D(3.6, 4.3);
|
|
30
|
-
const roundedVector = vector.rounded;
|
|
31
|
-
expect(roundedVector.x).toBe(4);
|
|
32
|
-
expect(roundedVector.y).toBe(4);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should correctly add two vectors', () => {
|
|
36
|
-
const vector1 = new Vector2D(2, 3);
|
|
37
|
-
const vector2 = new Vector2D(1, 2);
|
|
38
|
-
const result = Vector2D.add(vector1, vector2);
|
|
39
|
-
expect(result.x).toBe(3);
|
|
40
|
-
expect(result.y).toBe(5);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should correctly subtract two vectors', () => {
|
|
44
|
-
const vector1 = new Vector2D(4, 5);
|
|
45
|
-
const vector2 = new Vector2D(1, 2);
|
|
46
|
-
const result = Vector2D.subtract(vector1, vector2);
|
|
47
|
-
expect(result.x).toBe(3);
|
|
48
|
-
expect(result.y).toBe(3);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should correctly subtract value from a vector', () => {
|
|
52
|
-
const vector = new Vector2D(5, 7);
|
|
53
|
-
const result = Vector2D.subtractValue(vector, 3);
|
|
54
|
-
expect(result.x).toBe(2);
|
|
55
|
-
expect(result.y).toBe(4);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should correctly multiply a vector by a value', () => {
|
|
59
|
-
const vector = new Vector2D(2, 3);
|
|
60
|
-
const result = Vector2D.multiply(vector, 4);
|
|
61
|
-
expect(result.x).toBe(8);
|
|
62
|
-
expect(result.y).toBe(12);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('should correctly divide a vector by a value', () => {
|
|
66
|
-
const vector = new Vector2D(6, 8);
|
|
67
|
-
const result = Vector2D.divide(vector, 2);
|
|
68
|
-
expect(result.x).toBe(3);
|
|
69
|
-
expect(result.y).toBe(4);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should correctly check if two vectors are equal', () => {
|
|
73
|
-
const vector1 = new Vector2D(3, 4);
|
|
74
|
-
const vector2 = new Vector2D(3, 4);
|
|
75
|
-
const vector3 = new Vector2D(4, 5);
|
|
76
|
-
expect(Vector2D.equals(vector1, vector2)).toBe(true);
|
|
77
|
-
expect(Vector2D.equals(vector1, vector3)).toBe(false);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('should correctly check if two vectors are equal within a rounding factor', () => {
|
|
81
|
-
const vector1 = new Vector2D(3, 4);
|
|
82
|
-
const vector2 = new Vector2D(2.9, 3.9);
|
|
83
|
-
const vector3 = new Vector2D(4, 5);
|
|
84
|
-
expect(Vector2D.equalsRounded(vector1, vector2, 0.2)).toBe(true);
|
|
85
|
-
expect(Vector2D.equalsRounded(vector1, vector3, 0.2)).toBe(false);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should correctly normalize a vector', () => {
|
|
89
|
-
const vector = new Vector2D(3, 4);
|
|
90
|
-
const normalized = Vector2D.normalize(vector);
|
|
91
|
-
expect(normalized.x).toBeCloseTo(0.6);
|
|
92
|
-
expect(normalized.y).toBeCloseTo(0.8);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should correctly truncate a vector', () => {
|
|
96
|
-
const vector = new Vector2D(3, 4);
|
|
97
|
-
const truncated = Vector2D.truncate(vector, 4);
|
|
98
|
-
expect(truncated.length).toBeLessThanOrEqual(4);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should correctly get the perpendicular vector', () => {
|
|
102
|
-
const vector = new Vector2D(3, 4);
|
|
103
|
-
const perpendicular = Vector2D.perp(vector);
|
|
104
|
-
expect(Vector2D.dot(vector, perpendicular)).toBe(0);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('should correctly reverse the vector', () => {
|
|
108
|
-
const vector = new Vector2D(3, 4);
|
|
109
|
-
const reversed = Vector2D.reverse(vector);
|
|
110
|
-
expect(reversed.x).toBe(-3);
|
|
111
|
-
expect(reversed.y).toBe(-4);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should correctly get the absolute vector', () => {
|
|
115
|
-
const vector = new Vector2D(-3, 4);
|
|
116
|
-
const absVector = Vector2D.abs(vector);
|
|
117
|
-
expect(absVector.x).toBe(3);
|
|
118
|
-
expect(absVector.y).toBe(4);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should correctly calculate the dot product of two vectors', () => {
|
|
122
|
-
const vector1 = new Vector2D(2, 3);
|
|
123
|
-
const vector2 = new Vector2D(4, 5);
|
|
124
|
-
const dotProduct = Vector2D.dot(vector1, vector2);
|
|
125
|
-
expect(dotProduct).toBe(23);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should correctly calculate the distance between two vectors', () => {
|
|
129
|
-
const vector1 = new Vector2D(1, 1);
|
|
130
|
-
const vector2 = new Vector2D(4, 5);
|
|
131
|
-
const distance = Vector2D.distance(vector1, vector2);
|
|
132
|
-
expect(distance).toBeCloseTo(5);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it('should correctly calculate the squared distance between two vectors', () => {
|
|
136
|
-
const vector1 = new Vector2D(1, 1);
|
|
137
|
-
const vector2 = new Vector2D(4, 5);
|
|
138
|
-
const distanceSq = Vector2D.distanceSq(vector1, vector2);
|
|
139
|
-
expect(distanceSq).toBe(25);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should correctly determine the sign of the cross product of two vectors', () => {
|
|
143
|
-
const vector1 = new Vector2D(2, 3);
|
|
144
|
-
const vector2 = new Vector2D(4, 5);
|
|
145
|
-
const sign = Vector2D.sign(vector1, vector2);
|
|
146
|
-
expect(sign).toBe(-1); // Assuming specific vector values, the result may vary
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('should correctly calculate the angle between a vector and the negative y-axis', () => {
|
|
150
|
-
const vector = new Vector2D(3, 4);
|
|
151
|
-
const angle = Vector2D.angle(vector);
|
|
152
|
-
expect(angle).toBeCloseTo(2.498091544796509, 3);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('should create a random vector within the specified range', () => {
|
|
156
|
-
const maxX = 10;
|
|
157
|
-
const maxY = 10;
|
|
158
|
-
const randomVector = Vector2D.random(maxX, maxY);
|
|
159
|
-
expect(randomVector.x).toBeGreaterThanOrEqual(-maxX / 2);
|
|
160
|
-
expect(randomVector.x).toBeLessThanOrEqual(maxX / 2);
|
|
161
|
-
expect(randomVector.y).toBeGreaterThanOrEqual(-maxY / 2);
|
|
162
|
-
expect(randomVector.y).toBeLessThanOrEqual(maxY / 2);
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
it('should zero the vector components', () => {
|
|
166
|
-
const vector = new Vector2D(3, 4);
|
|
167
|
-
vector.zero();
|
|
168
|
-
expect(vector.x).toBe(0);
|
|
169
|
-
expect(vector.y).toBe(0);
|
|
170
|
-
});
|
|
171
|
-
});
|