data-structure-typed 1.38.9 → 1.39.1
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 +9 -9
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +86 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +8 -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/rb-tree.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 +4 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js +5 -5
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
- package/dist/cjs/data-structures/graph/directed-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/graph/undirected-graph.d.ts +3 -3
- package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/cjs/data-structures/heap/heap.js +10 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/max-heap.js +9 -7
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/cjs/data-structures/heap/min-heap.js +9 -7
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.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/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- 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-indexed-tree.d.ts +8 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +86 -32
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +8 -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 +4 -4
- package/dist/mjs/data-structures/graph/abstract-graph.js +5 -5
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
- 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/graph/undirected-graph.d.ts +3 -3
- package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
- package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
- package/dist/mjs/data-structures/heap/heap.js +10 -10
- package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/max-heap.js +9 -7
- package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/mjs/data-structures/heap/min-heap.js +9 -7
- 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/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- 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 +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +21 -1
- package/src/data-structures/binary-tree/binary-tree.ts +37 -93
- package/src/data-structures/binary-tree/bst.ts +11 -17
- package/src/data-structures/binary-tree/rb-tree.ts +2 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +16 -15
- package/src/data-structures/graph/directed-graph.ts +8 -7
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/graph/undirected-graph.ts +9 -8
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +12 -12
- package/src/data-structures/heap/max-heap.ts +8 -6
- package/src/data-structures/heap/min-heap.ts +8 -6
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +1 -4
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -7
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +269 -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 +37 -7
- package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- package/test/unit/data-structures/graph/undirected-graph.test.ts +84 -2
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
- 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/priority-queue/max-priority-queue.test.ts +3 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
- package/test/unit/data-structures/queue/deque.test.ts +264 -1
- package/test/unit/data-structures/queue/queue.test.ts +3 -1
|
@@ -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
|
+
});
|
|
@@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
|
20
|
-
const priorityQueue = new MaxPriorityQueue<{keyA: number}>((a, b) => b.keyA - a.keyA);
|
|
20
|
+
const priorityQueue = new MaxPriorityQueue<{keyA: number}>({comparator: (a, b) => b.keyA - a.keyA});
|
|
21
21
|
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
|
22
22
|
priorityQueue.add({keyA: 7});
|
|
23
23
|
|
|
@@ -53,7 +53,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
53
53
|
|
|
54
54
|
it('should correctly heapify an array', () => {
|
|
55
55
|
const array = [5, 3, 7, 1];
|
|
56
|
-
const heap = MaxPriorityQueue.heapify<number>(array, (a, b) => b - a);
|
|
56
|
+
const heap = MaxPriorityQueue.heapify<number>({nodes: array, comparator: (a, b) => b - a});
|
|
57
57
|
heap.refill(array);
|
|
58
58
|
|
|
59
59
|
expect(heap.poll()).toBe(7);
|
|
@@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|
|
64
64
|
|
|
65
65
|
it('should correctly heapify an object array', () => {
|
|
66
66
|
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
|
67
|
-
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA);
|
|
67
|
+
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>({nodes: nodes, comparator: (a, b) => b.keyA - a.keyA});
|
|
68
68
|
|
|
69
69
|
expect(maxPQ.poll()?.keyA).toBe(7);
|
|
70
70
|
expect(maxPQ.poll()?.keyA).toBe(5);
|
|
@@ -3,7 +3,7 @@ import {getRandomInt} from '../../../utils';
|
|
|
3
3
|
|
|
4
4
|
describe('PriorityQueue Operation Test', () => {
|
|
5
5
|
it('should PriorityQueue poll, pee, heapify, toArray work well', function () {
|
|
6
|
-
const minPQ = new PriorityQueue<number>((a, b) => a - b);
|
|
6
|
+
const minPQ = new PriorityQueue<number>({comparator: (a, b) => a - b});
|
|
7
7
|
minPQ.refill([5, 2, 3, 4, 6, 1]);
|
|
8
8
|
expect(minPQ.toArray()).toEqual([1, 2, 3, 4, 6, 5]);
|
|
9
9
|
minPQ.poll();
|
|
@@ -11,13 +11,13 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
11
11
|
minPQ.poll();
|
|
12
12
|
expect(minPQ.toArray()).toEqual([4, 5, 6]);
|
|
13
13
|
expect(minPQ.peek()).toBe(4);
|
|
14
|
-
expect(PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], (a, b) => a - b).toArray()).toEqual(
|
|
15
|
-
1, 2, 3, 5, 6, 7, 8, 9, 10
|
|
16
|
-
|
|
14
|
+
expect(PriorityQueue.heapify({nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], comparator: (a, b) => a - b}).toArray()).toEqual(
|
|
15
|
+
[1, 2, 3, 5, 6, 7, 8, 9, 10]
|
|
16
|
+
);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
it('should Max PriorityQueue poll, peek, heapify, toArray work well', function () {
|
|
20
|
-
const maxPriorityQueue = new PriorityQueue<number>((a, b) => b - a);
|
|
20
|
+
const maxPriorityQueue = new PriorityQueue<number>({comparator: (a, b) => b - a});
|
|
21
21
|
maxPriorityQueue.refill([5, 2, 3, 4, 6, 1]);
|
|
22
22
|
expect(maxPriorityQueue.toArray()).toEqual([6, 5, 3, 4, 2, 1]);
|
|
23
23
|
maxPriorityQueue.poll();
|
|
@@ -25,13 +25,13 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
25
25
|
maxPriorityQueue.poll();
|
|
26
26
|
expect(maxPriorityQueue.toArray()).toEqual([3, 2, 1]);
|
|
27
27
|
expect(maxPriorityQueue.peek()).toBe(3);
|
|
28
|
-
expect(PriorityQueue.heapify([3, 2, 1, 5, 6, 7, 8, 9, 10], (a, b) => a - b).toArray()).toEqual(
|
|
29
|
-
1, 2, 3, 5, 6, 7, 8, 9, 10
|
|
30
|
-
|
|
28
|
+
expect(PriorityQueue.heapify({nodes: [3, 2, 1, 5, 6, 7, 8, 9, 10], comparator: (a, b) => a - b}).toArray()).toEqual(
|
|
29
|
+
[1, 2, 3, 5, 6, 7, 8, 9, 10]
|
|
30
|
+
);
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
it('should PriorityQueue clone, sort, getNodes, dfs work well', function () {
|
|
34
|
-
const minPQ1 = new PriorityQueue<number>((a, b) => a - b);
|
|
34
|
+
const minPQ1 = new PriorityQueue<number>({comparator: (a, b) => a - b});
|
|
35
35
|
minPQ1.refill([2, 5, 8, 3, 1, 6, 7, 4]);
|
|
36
36
|
const clonedPriorityQueue = minPQ1.clone();
|
|
37
37
|
expect(clonedPriorityQueue.getNodes()).toEqual(minPQ1.getNodes());
|
|
@@ -45,7 +45,7 @@ describe('PriorityQueue Operation Test', () => {
|
|
|
45
45
|
describe('Priority Queue Performance Test', () => {
|
|
46
46
|
it('should numeric heap work well', function () {
|
|
47
47
|
const values = Array.from(new Array(10000), () => getRandomInt(1, 10000000));
|
|
48
|
-
const minPriorityQueue = new PriorityQueue<number>((a, b) => a - b);
|
|
48
|
+
const minPriorityQueue = new PriorityQueue<number>({comparator: (a, b) => a - b});
|
|
49
49
|
minPriorityQueue.refill(values);
|
|
50
50
|
const sorted = minPriorityQueue.sort();
|
|
51
51
|
expect(sorted).toEqual(values.sort((a, b) => a - b));
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {ArrayDeque, Deque, ObjectDeque} from '../../../../src';
|
|
2
2
|
import {bigO} from '../../../utils';
|
|
3
|
+
import {isDebugTest} from '../../../config';
|
|
3
4
|
|
|
5
|
+
const isDebug = isDebugTest;
|
|
4
6
|
describe('Deque Tests', () => {
|
|
5
7
|
// Test cases for the Deque class (DoublyLinkedList-based)
|
|
6
8
|
describe('Deque (DoublyLinkedList-based)', () => {
|
|
@@ -141,7 +143,268 @@ describe('Deque Performance Test', () => {
|
|
|
141
143
|
for (let i = 0; i < dataSize; i++) {
|
|
142
144
|
queue.pop();
|
|
143
145
|
}
|
|
144
|
-
console.log(`Queue Deque Test: ${performance.now() - startTime} ms`);
|
|
146
|
+
isDebug && console.log(`Queue Deque Test: ${performance.now() - startTime} ms`);
|
|
145
147
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
146
148
|
});
|
|
147
149
|
});
|
|
150
|
+
|
|
151
|
+
describe('Deque', () => {
|
|
152
|
+
let deque: Deque<number>;
|
|
153
|
+
|
|
154
|
+
beforeEach(() => {
|
|
155
|
+
deque = new Deque<number>();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('should initialize an empty deque', () => {
|
|
159
|
+
expect(deque.size).toBe(0);
|
|
160
|
+
expect(deque.isEmpty()).toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('should add elements to the front and back', () => {
|
|
164
|
+
deque.addFirst(1);
|
|
165
|
+
deque.addLast(2);
|
|
166
|
+
|
|
167
|
+
expect(deque.size).toBe(2);
|
|
168
|
+
expect(deque.peekFirst()).toBe(1);
|
|
169
|
+
expect(deque.peekLast()).toBe(2);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('should remove elements from the front and back', () => {
|
|
173
|
+
deque.addFirst(1);
|
|
174
|
+
deque.addLast(2);
|
|
175
|
+
|
|
176
|
+
const firstElement = deque.pollFirst();
|
|
177
|
+
const lastElement = deque.pollLast();
|
|
178
|
+
|
|
179
|
+
expect(deque.size).toBe(0);
|
|
180
|
+
expect(firstElement).toBe(1);
|
|
181
|
+
expect(lastElement).toBe(2);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('should get elements by index', () => {
|
|
185
|
+
deque.addLast(1);
|
|
186
|
+
deque.addLast(2);
|
|
187
|
+
deque.addLast(3);
|
|
188
|
+
|
|
189
|
+
expect(deque.getAt(0)).toBe(1);
|
|
190
|
+
expect(deque.getAt(1)).toBe(2);
|
|
191
|
+
expect(deque.getAt(2)).toBe(3);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('should return null for out-of-bounds index', () => {
|
|
195
|
+
expect(deque.getAt(0)).toBe(undefined);
|
|
196
|
+
expect(deque.getAt(1)).toBe(undefined);
|
|
197
|
+
expect(deque.getAt(-1)).toBe(undefined);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('should check if the deque is empty', () => {
|
|
201
|
+
expect(deque.isEmpty()).toBe(true);
|
|
202
|
+
|
|
203
|
+
deque.addLast(1);
|
|
204
|
+
expect(deque.isEmpty()).toBe(false);
|
|
205
|
+
|
|
206
|
+
deque.pollFirst();
|
|
207
|
+
expect(deque.isEmpty()).toBe(true);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe('ArrayDeque', () => {
|
|
212
|
+
let deque: ArrayDeque<number>;
|
|
213
|
+
|
|
214
|
+
beforeEach(() => {
|
|
215
|
+
deque = new ArrayDeque<number>();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('should initialize an empty deque', () => {
|
|
219
|
+
expect(deque.size).toBe(0);
|
|
220
|
+
expect(deque.isEmpty()).toBe(true);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('should add elements to the front and back', () => {
|
|
224
|
+
deque.addFirst(1);
|
|
225
|
+
deque.addLast(2);
|
|
226
|
+
|
|
227
|
+
expect(deque.size).toBe(2);
|
|
228
|
+
expect(deque.peekFirst()).toBe(1);
|
|
229
|
+
expect(deque.peekLast()).toBe(2);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('should remove elements from the front and back', () => {
|
|
233
|
+
deque.addFirst(1);
|
|
234
|
+
deque.addLast(2);
|
|
235
|
+
|
|
236
|
+
const firstElement = deque.pollFirst();
|
|
237
|
+
const lastElement = deque.pollLast();
|
|
238
|
+
|
|
239
|
+
expect(deque.size).toBe(0);
|
|
240
|
+
expect(firstElement).toBe(1);
|
|
241
|
+
expect(lastElement).toBe(2);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('should get elements by index', () => {
|
|
245
|
+
deque.addLast(1);
|
|
246
|
+
deque.addLast(2);
|
|
247
|
+
deque.addLast(3);
|
|
248
|
+
|
|
249
|
+
expect(deque.get(0)).toBe(1);
|
|
250
|
+
expect(deque.get(1)).toBe(2);
|
|
251
|
+
expect(deque.get(2)).toBe(3);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('should return null for out-of-bounds index', () => {
|
|
255
|
+
expect(deque.get(0)).toBe(null);
|
|
256
|
+
expect(deque.get(1)).toBe(null);
|
|
257
|
+
expect(deque.get(-1)).toBe(null);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
test('should check if the deque is empty', () => {
|
|
261
|
+
expect(deque.isEmpty()).toBe(true);
|
|
262
|
+
|
|
263
|
+
deque.addLast(1);
|
|
264
|
+
expect(deque.isEmpty()).toBe(false);
|
|
265
|
+
|
|
266
|
+
deque.pollFirst();
|
|
267
|
+
expect(deque.isEmpty()).toBe(true);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('should set elements at a specific index', () => {
|
|
271
|
+
deque.addLast(1);
|
|
272
|
+
deque.addLast(2);
|
|
273
|
+
deque.addLast(3);
|
|
274
|
+
|
|
275
|
+
deque.set(1, 4);
|
|
276
|
+
|
|
277
|
+
expect(deque.get(0)).toBe(1);
|
|
278
|
+
expect(deque.get(1)).toBe(4);
|
|
279
|
+
expect(deque.get(2)).toBe(3);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('should insert elements at a specific index', () => {
|
|
283
|
+
deque.addLast(1);
|
|
284
|
+
deque.addLast(2);
|
|
285
|
+
deque.addLast(3);
|
|
286
|
+
|
|
287
|
+
deque.insert(1, 4);
|
|
288
|
+
|
|
289
|
+
expect(deque.size).toBe(4);
|
|
290
|
+
expect(deque.get(0)).toBe(1);
|
|
291
|
+
expect(deque.get(1)).toBe(4);
|
|
292
|
+
expect(deque.get(2)).toBe(2);
|
|
293
|
+
expect(deque.get(3)).toBe(3);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('should delete elements at a specific index', () => {
|
|
297
|
+
deque.addLast(1);
|
|
298
|
+
deque.addLast(2);
|
|
299
|
+
deque.addLast(3);
|
|
300
|
+
|
|
301
|
+
const deletedElement = deque.delete(1);
|
|
302
|
+
|
|
303
|
+
expect(deque.size).toBe(2);
|
|
304
|
+
expect(deletedElement[0]).toBe(2);
|
|
305
|
+
expect(deque.get(0)).toBe(1);
|
|
306
|
+
expect(deque.get(1)).toBe(3);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
describe('ObjectDeque', () => {
|
|
311
|
+
let deque: ObjectDeque<number>;
|
|
312
|
+
|
|
313
|
+
beforeEach(() => {
|
|
314
|
+
deque = new ObjectDeque<number>();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('should add elements to the front of the deque', () => {
|
|
318
|
+
deque.addFirst(1);
|
|
319
|
+
deque.addFirst(2);
|
|
320
|
+
|
|
321
|
+
expect(deque.size).toBe(2);
|
|
322
|
+
expect(deque.peekFirst()).toBe(2);
|
|
323
|
+
expect(deque.peekLast()).toBe(1);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('should add elements to the end of the deque', () => {
|
|
327
|
+
deque.addLast(1);
|
|
328
|
+
deque.addLast(2);
|
|
329
|
+
|
|
330
|
+
expect(deque.size).toBe(2);
|
|
331
|
+
expect(deque.peekFirst()).toBe(1);
|
|
332
|
+
expect(deque.peekLast()).toBe(2);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
test('should remove elements from the front of the deque', () => {
|
|
336
|
+
deque.addLast(1);
|
|
337
|
+
deque.addLast(2);
|
|
338
|
+
|
|
339
|
+
const removedElement = deque.pollFirst();
|
|
340
|
+
|
|
341
|
+
expect(deque.size).toBe(1);
|
|
342
|
+
expect(removedElement).toBe(1);
|
|
343
|
+
expect(deque.peekFirst()).toBe(2);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('should remove elements from the end of the deque', () => {
|
|
347
|
+
deque.addLast(1);
|
|
348
|
+
deque.addLast(2);
|
|
349
|
+
|
|
350
|
+
const removedElement = deque.pollFirst();
|
|
351
|
+
|
|
352
|
+
expect(deque.size).toBe(1);
|
|
353
|
+
expect(removedElement).toBe(1);
|
|
354
|
+
expect(deque.peekLast()).toBe(2);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test('should return the element at the front of the deque without removing it', () => {
|
|
358
|
+
deque.addFirst(1);
|
|
359
|
+
deque.addFirst(2);
|
|
360
|
+
|
|
361
|
+
expect(deque.peekFirst()).toBe(2);
|
|
362
|
+
expect(deque.size).toBe(2);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test('should return the element at the end of the deque without removing it', () => {
|
|
366
|
+
deque.addLast(1);
|
|
367
|
+
deque.addLast(2);
|
|
368
|
+
|
|
369
|
+
expect(deque.peekLast()).toBe(2);
|
|
370
|
+
expect(deque.size).toBe(2);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test('should return the correct size of the deque', () => {
|
|
374
|
+
deque.addFirst(1);
|
|
375
|
+
deque.addLast(2);
|
|
376
|
+
deque.addLast(3);
|
|
377
|
+
|
|
378
|
+
expect(deque.size).toBe(3);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test('should check if the deque is empty', () => {
|
|
382
|
+
expect(deque.isEmpty()).toBe(true);
|
|
383
|
+
|
|
384
|
+
deque.addFirst(1);
|
|
385
|
+
|
|
386
|
+
expect(deque.isEmpty()).toBe(false);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
test('should set elements at a specific index', () => {
|
|
390
|
+
deque.addFirst(1);
|
|
391
|
+
deque.addLast(2);
|
|
392
|
+
deque.addLast(3);
|
|
393
|
+
|
|
394
|
+
expect(deque.peekFirst()).toBe(1);
|
|
395
|
+
expect(deque.get(1)).toBe(2);
|
|
396
|
+
expect(deque.peekLast()).toBe(3);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
test('should insert elements at a specific index', () => {
|
|
400
|
+
deque.addFirst(1);
|
|
401
|
+
deque.addLast(2);
|
|
402
|
+
deque.addLast(3);
|
|
403
|
+
|
|
404
|
+
expect(deque.size).toBe(3);
|
|
405
|
+
expect(deque.peekFirst()).toBe(1);
|
|
406
|
+
expect(deque.get(1)).toBe(2);
|
|
407
|
+
expect(deque.get(2)).toBe(3);
|
|
408
|
+
expect(deque.peekLast()).toBe(3);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import {LinkedListQueue, Queue} from '../../../../src';
|
|
2
2
|
import {bigO, magnitude} from '../../../utils';
|
|
3
|
+
import {isDebugTest} from '../../../config';
|
|
3
4
|
|
|
5
|
+
const isDebug = isDebugTest;
|
|
4
6
|
describe('Queue Operation Test', () => {
|
|
5
7
|
it('should validate a queue', () => {
|
|
6
8
|
const queue = new Queue<number>();
|
|
@@ -209,7 +211,7 @@ describe('Queue Performance Test', () => {
|
|
|
209
211
|
for (let i = 0; i < dataSize; i++) {
|
|
210
212
|
queue.dequeue();
|
|
211
213
|
}
|
|
212
|
-
console.log(`Queue Performance Test: ${performance.now() - startTime} ms`);
|
|
214
|
+
isDebug && console.log(`Queue Performance Test: ${performance.now() - startTime} ms`);
|
|
213
215
|
expect(performance.now() - startTime).toBeLessThan(bigO.LINEAR * 100);
|
|
214
216
|
});
|
|
215
217
|
|