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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {Character, Navigator, NavigatorParams, Turning} from '../../../../src';
|
|
2
|
+
import {isDebugTest} from '../../../config';
|
|
2
3
|
|
|
4
|
+
const isDebug = isDebugTest;
|
|
3
5
|
const exampleMatrix: number[][] = [
|
|
4
6
|
[0, 0, 0, 0],
|
|
5
7
|
[0, 1, 1, 0],
|
|
@@ -17,7 +19,7 @@ const exampleTurning: Turning = {
|
|
|
17
19
|
// Create a sample move callback function
|
|
18
20
|
const exampleOnMove = () => {
|
|
19
21
|
expect(true).toBeTruthy();
|
|
20
|
-
// console.log(`Moved to position (${cur[0]}, ${cur[1]})`);
|
|
22
|
+
// isDebug && console.log(`Moved to position (${cur[0]}, ${cur[1]})`);
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
// Create an initial parameter object for the example
|
|
@@ -77,3 +79,166 @@ describe('Navigator class', () => {
|
|
|
77
79
|
expect(navigator.check('left')).toBe(false); // Open path
|
|
78
80
|
});
|
|
79
81
|
});
|
|
82
|
+
|
|
83
|
+
describe('Navigator', () => {
|
|
84
|
+
it('should correctly navigate the grid', () => {
|
|
85
|
+
// Define a sample grid and initial parameters
|
|
86
|
+
const matrix: number[][] = [
|
|
87
|
+
[0, 0, 1],
|
|
88
|
+
[0, 1, 0],
|
|
89
|
+
[0, 0, 0]
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
const turning: Turning = {
|
|
93
|
+
up: 'right',
|
|
94
|
+
right: 'down',
|
|
95
|
+
down: 'left',
|
|
96
|
+
left: 'up'
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const init: NavigatorParams<number>['init'] = {
|
|
100
|
+
cur: [0, 0], // Initial position
|
|
101
|
+
charDir: 'right', // Initial character direction
|
|
102
|
+
VISITED: 2 // Value to mark visited cells
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Initialize the navigator
|
|
106
|
+
const navigator = new Navigator({
|
|
107
|
+
matrix,
|
|
108
|
+
turning,
|
|
109
|
+
init,
|
|
110
|
+
onMove: cur => cur
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Define a function to track visited cells
|
|
114
|
+
const visitedCells: number[][] = [];
|
|
115
|
+
const onMove = (cur: number[]) => {
|
|
116
|
+
visitedCells.push([...cur]);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Attach the onMove function
|
|
120
|
+
navigator.onMove = onMove;
|
|
121
|
+
|
|
122
|
+
// Start the navigation
|
|
123
|
+
navigator.start();
|
|
124
|
+
|
|
125
|
+
// The character should have navigated the grid correctly
|
|
126
|
+
expect(visitedCells).toEqual([
|
|
127
|
+
[0, 1],
|
|
128
|
+
[0, 2],
|
|
129
|
+
[1, 2],
|
|
130
|
+
[2, 2],
|
|
131
|
+
[2, 1],
|
|
132
|
+
[2, 0],
|
|
133
|
+
[1, 0],
|
|
134
|
+
[1, 1]
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should not move if there are no valid moves', () => {
|
|
139
|
+
// Define a sample grid with no valid moves
|
|
140
|
+
const matrix: number[][] = [
|
|
141
|
+
[1, 1],
|
|
142
|
+
[1, 1]
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const turning: Turning = {
|
|
146
|
+
up: 'right',
|
|
147
|
+
right: 'down',
|
|
148
|
+
down: 'left',
|
|
149
|
+
left: 'up'
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const init: NavigatorParams<number>['init'] = {
|
|
153
|
+
cur: [0, 0], // Initial position
|
|
154
|
+
charDir: 'right', // Initial character direction
|
|
155
|
+
VISITED: 2 // Value to mark visited cells
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Initialize the navigator
|
|
159
|
+
const navigator = new Navigator({
|
|
160
|
+
matrix,
|
|
161
|
+
turning,
|
|
162
|
+
init,
|
|
163
|
+
onMove: cur => cur
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Define a function to track visited cells
|
|
167
|
+
const visitedCells: number[][] = [];
|
|
168
|
+
const onMove = (cur: number[]) => {
|
|
169
|
+
visitedCells.push([...cur]);
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// Attach the onMove function
|
|
173
|
+
navigator.onMove = onMove;
|
|
174
|
+
|
|
175
|
+
// Start the navigation
|
|
176
|
+
navigator.start();
|
|
177
|
+
|
|
178
|
+
// The character should not move
|
|
179
|
+
isDebug && console.log(visitedCells);
|
|
180
|
+
expect(visitedCells).toEqual([
|
|
181
|
+
[0, 1],
|
|
182
|
+
[1, 1],
|
|
183
|
+
[1, 0]
|
|
184
|
+
]);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should handle edge cases and turns correctly', () => {
|
|
188
|
+
// Define a sample grid with turns and edge cases
|
|
189
|
+
const matrix: number[][] = [
|
|
190
|
+
[1, 0, 0, 0],
|
|
191
|
+
[1, 0, 1, 0],
|
|
192
|
+
[0, 0, 1, 1]
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
const turning: Turning = {
|
|
196
|
+
up: 'right',
|
|
197
|
+
right: 'down',
|
|
198
|
+
down: 'left',
|
|
199
|
+
left: 'up'
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const init: NavigatorParams<number>['init'] = {
|
|
203
|
+
cur: [0, 0], // Initial position
|
|
204
|
+
charDir: 'right', // Initial character direction
|
|
205
|
+
VISITED: 2 // Value to mark visited cells
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// Initialize the navigator
|
|
209
|
+
const navigator = new Navigator({
|
|
210
|
+
matrix,
|
|
211
|
+
turning,
|
|
212
|
+
init,
|
|
213
|
+
onMove: cur => cur
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Define a function to track visited cells
|
|
217
|
+
const visitedCells: number[][] = [];
|
|
218
|
+
const onMove = (cur: number[]) => {
|
|
219
|
+
visitedCells.push([...cur]);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// Attach the onMove function
|
|
223
|
+
navigator.onMove = onMove;
|
|
224
|
+
|
|
225
|
+
// Start the navigation
|
|
226
|
+
navigator.start();
|
|
227
|
+
|
|
228
|
+
// The character should have navigated the grid, handled turns, and edge cases
|
|
229
|
+
isDebug && console.log(visitedCells);
|
|
230
|
+
expect(visitedCells).toEqual([
|
|
231
|
+
[0, 1],
|
|
232
|
+
[0, 2],
|
|
233
|
+
[0, 3],
|
|
234
|
+
[1, 3],
|
|
235
|
+
[2, 3],
|
|
236
|
+
[2, 2],
|
|
237
|
+
[2, 1],
|
|
238
|
+
[2, 0],
|
|
239
|
+
[1, 0],
|
|
240
|
+
[1, 1],
|
|
241
|
+
[1, 2]
|
|
242
|
+
]);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
});
|