Skip to content

Commit 963b001

Browse files
committed
New tests and update function
1 parent 0c0b7d9 commit 963b001

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/lib/segment-tree/segment-tree.spec.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ test('updates specific indexes and returns correct sums', t => {
152152
}
153153
});
154154

155-
test('random updates', t => {
155+
test('random updates (sum)', t => {
156156
const largeInputCopy = largeInput.slice();
157157
const tree = new SegmentTree(largeInput, (a, b) => a + b);
158158

159-
const testCases = 1e7;
159+
const testCases = 1e6;
160160

161161
for (let i = 0; i < testCases; ++i) {
162162
const randomIndex = generateNumber(0, largeInput.length - 1);
@@ -170,3 +170,41 @@ test('random updates', t => {
170170
findSumLinear(largeInputCopy, 0, largeInputCopy.length - 1)
171171
);
172172
});
173+
174+
test('random updates (max)', t => {
175+
const largeInputCopy = largeInput.slice();
176+
const tree = new SegmentTree(largeInput, Math.max);
177+
178+
const testCases = 1e6;
179+
180+
for (let i = 0; i < testCases; ++i) {
181+
const randomIndex = generateNumber(0, largeInput.length - 1);
182+
const randomValue = generateNumber(-1e7, 1e7);
183+
tree.update(randomIndex, randomValue, false);
184+
largeInputCopy[randomIndex] = randomValue;
185+
}
186+
187+
t.is(
188+
tree.query(),
189+
findMaxLinear(largeInputCopy, 0, largeInputCopy.length - 1)
190+
);
191+
});
192+
193+
test('random updates (min)', t => {
194+
const largeInputCopy = largeInput.slice();
195+
const tree = new SegmentTree(largeInput, Math.min);
196+
197+
const testCases = 1e6;
198+
199+
for (let i = 0; i < testCases; ++i) {
200+
const randomIndex = generateNumber(0, largeInput.length - 1);
201+
const randomValue = generateNumber(-1e7, 1e7);
202+
tree.update(randomIndex, randomValue, false);
203+
largeInputCopy[randomIndex] = randomValue;
204+
}
205+
206+
t.is(
207+
tree.query(),
208+
findMinLinear(largeInputCopy, 0, largeInputCopy.length - 1)
209+
);
210+
});

0 commit comments

Comments
 (0)