data-structure-typed 1.41.6 → 1.41.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +14 -11
- package/benchmark/report.html +14 -11
- package/benchmark/report.json +153 -202
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +5 -2
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +15 -2
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/abstract-graph.js +5 -5
- package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +5 -2
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +15 -2
- package/dist/mjs/src/data-structures/graph/abstract-graph.js +5 -5
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/rb-tree.ts +18 -2
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +110 -0
- package/test/integration/bst.test.ts +385 -0
- package/test/integration/heap.test.js +16 -0
- package/test/integration/index.html +51 -0
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +36 -0
- package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +45 -0
- package/test/performance/data-structures/binary-tree/bst.test.ts +36 -0
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +32 -0
- package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
- package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +49 -0
- package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/overall.test.ts +0 -0
- package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +30 -0
- package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
- package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +40 -0
- package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +34 -0
- package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +19 -0
- package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
- package/test/performance/data-structures/queue/deque.test.ts +21 -0
- package/test/performance/data-structures/queue/queue.test.ts +25 -0
- package/test/performance/data-structures/stack/stack.test.ts +0 -0
- package/test/performance/data-structures/tree/tree.test.ts +0 -0
- package/test/performance/data-structures/trie/trie.test.ts +22 -0
- package/test/performance/reportor.ts +185 -0
- package/test/performance/types/index.ts +1 -0
- package/test/performance/types/reportor.ts +3 -0
- package/test/types/index.ts +1 -0
- package/test/types/utils/big-o.ts +1 -0
- package/test/types/utils/index.ts +2 -0
- package/test/types/utils/json2html.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +269 -0
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +320 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +486 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +840 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +66 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +435 -0
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +542 -0
- package/test/unit/data-structures/graph/abstract-graph.test.ts +100 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +564 -0
- package/test/unit/data-structures/graph/map-graph.test.ts +126 -0
- package/test/unit/data-structures/graph/overall.test.ts +49 -0
- package/test/unit/data-structures/graph/salty-edges.json +1 -0
- package/test/unit/data-structures/graph/salty-vertexes.json +1 -0
- package/test/unit/data-structures/graph/undirected-graph.test.ts +167 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +74 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +66 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +103 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +186 -0
- package/test/unit/data-structures/heap/heap.test.ts +254 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +52 -0
- package/test/unit/data-structures/heap/min-heap.test.ts +52 -0
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +400 -0
- package/test/unit/data-structures/linked-list/linked-list.test.ts +8 -0
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +474 -0
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +13 -0
- package/test/unit/data-structures/linked-list/skip-list.test.ts +86 -0
- package/test/unit/data-structures/matrix/matrix.test.ts +54 -0
- package/test/unit/data-structures/matrix/matrix2d.test.ts +345 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +244 -0
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +73 -0
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +63 -0
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +53 -0
- package/test/unit/data-structures/queue/deque.test.ts +410 -0
- package/test/unit/data-structures/queue/queue.test.ts +207 -0
- package/test/unit/data-structures/stack/stack.test.ts +67 -0
- package/test/unit/data-structures/tree/tree.test.ts +39 -0
- package/test/unit/data-structures/trie/trie.test.ts +825 -0
- package/test/utils/array.ts +5514 -0
- package/test/utils/big-o.ts +207 -0
- package/test/utils/console.ts +31 -0
- package/test/utils/index.ts +7 -0
- package/test/utils/is.ts +56 -0
- package/test/utils/json2html.ts +322 -0
- package/test/utils/number.ts +13 -0
- package/test/utils/string.ts +1 -0
|
@@ -0,0 +1,345 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {Character, Navigator, NavigatorParams, Turning} from '../../../../src';
|
|
2
|
+
import {isDebugTest} from '../../../config';
|
|
3
|
+
|
|
4
|
+
const isDebug = isDebugTest;
|
|
5
|
+
const exampleMatrix: number[][] = [
|
|
6
|
+
[0, 0, 0, 0],
|
|
7
|
+
[0, 1, 1, 0],
|
|
8
|
+
[0, 0, 0, 0]
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
// Create a sample redirect object
|
|
12
|
+
const exampleTurning: Turning = {
|
|
13
|
+
up: 'right',
|
|
14
|
+
right: 'down',
|
|
15
|
+
down: 'left',
|
|
16
|
+
left: 'up'
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// Create a sample move callback function
|
|
20
|
+
const exampleOnMove = () => {
|
|
21
|
+
expect(true).toBeTruthy();
|
|
22
|
+
// isDebug && console.log(`Moved to position (${cur[0]}, ${cur[1]})`);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Create an initial parameter object for the example
|
|
26
|
+
const exampleInit: NavigatorParams<number>['init'] = {
|
|
27
|
+
cur: [0, 0],
|
|
28
|
+
charDir: 'right',
|
|
29
|
+
VISITED: -1
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Create a Navigator Params object
|
|
33
|
+
const exampleNavigatorParams: NavigatorParams<number> = {
|
|
34
|
+
matrix: exampleMatrix,
|
|
35
|
+
turning: exampleTurning,
|
|
36
|
+
onMove: exampleOnMove,
|
|
37
|
+
init: exampleInit
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
describe('Character class', () => {
|
|
41
|
+
it('should create a character with the correct direction', () => {
|
|
42
|
+
const character = new Character('up', exampleTurning);
|
|
43
|
+
expect(character.direction).toBe('up');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should turn the character in the correct direction', () => {
|
|
47
|
+
const character = new Character('up', exampleTurning);
|
|
48
|
+
const turnedCharacter = character.turn();
|
|
49
|
+
expect(turnedCharacter.direction).toBe('right');
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('Navigator class', () => {
|
|
54
|
+
let navigator: Navigator<number>;
|
|
55
|
+
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
navigator = new Navigator(exampleNavigatorParams);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should initialize with the correct matrix and current position', () => {
|
|
61
|
+
expect(navigator['_matrix']).toEqual(exampleMatrix);
|
|
62
|
+
expect(navigator['_cur']).toEqual(exampleInit.cur);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should move the character correctly', () => {
|
|
66
|
+
navigator.move('right');
|
|
67
|
+
expect(navigator['_cur']).toEqual([0, 1]);
|
|
68
|
+
expect(navigator['_matrix'][0][1]).toBe(exampleInit.VISITED);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should turn the character correctly', () => {
|
|
72
|
+
expect(navigator['_character'].direction).toBe('right');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should check for valid moves correctly', () => {
|
|
76
|
+
expect(navigator.check('up')).toBe(false); // Blocked by wall
|
|
77
|
+
expect(navigator.check('right')).toBe(true); // Open path
|
|
78
|
+
expect(navigator.check('down')).toBe(true); // Blocked by wall
|
|
79
|
+
expect(navigator.check('left')).toBe(false); // Open path
|
|
80
|
+
});
|
|
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
|
+
});
|