forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentTreeTest.php
347 lines (299 loc) · 11.6 KB
/
SegmentTreeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request #166
* https://github.com/TheAlgorithms/PHP/pull/166
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures;
require_once __DIR__ . '/../../DataStructures/SegmentTree/SegmentTree.php';
require_once __DIR__ . '/../../DataStructures/SegmentTree/SegmentTreeNode.php';
use DataStructures\SegmentTree\SegmentTree;
use InvalidArgumentException;
use OutOfBoundsException;
use PHPUnit\Framework\TestCase;
class SegmentTreeTest extends TestCase
{
private array $testArray;
protected function setUp(): void
{
$this->testArray = [1, 3, 5, 7, 9, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26];
}
public static function sumQueryProvider(): array
{
return [
// Format: [expectedResult, startIndex, endIndex]
[24, 1, 4],
[107, 5, 11],
[91, 2, 9],
[23, 15, 15],
];
}
/**
* Test sum queries using data provider.
* @dataProvider sumQueryProvider
*/
public function testSegmentTreeSumQuery(int $expected, int $startIndex, int $endIndex): void
{
// Test the default case: sum query
$segmentTree = new SegmentTree($this->testArray);
$this->assertEquals(
$expected,
$segmentTree->query($startIndex, $endIndex),
"Query sum between index $startIndex and $endIndex should return $expected."
);
}
public static function maxQueryProvider(): array
{
return [
[26, 0, 18],
[13, 2, 6],
[22, 8, 14],
[11, 5, 5],
];
}
/**
* Test max queries using data provider.
* @dataProvider maxQueryProvider
*/
public function testSegmentTreeMaxQuery(int $expected, int $startIndex, int $endIndex): void
{
$segmentTree = new SegmentTree($this->testArray, fn($a, $b) => max($a, $b));
$this->assertEquals(
$expected,
$segmentTree->query($startIndex, $endIndex),
"Max query between index $startIndex and $endIndex should return $expected."
);
}
public static function minQueryProvider(): array
{
return [
[1, 0, 18],
[5, 2, 7],
[18, 10, 17],
[17, 9, 9],
];
}
/**
* Test min queries using data provider.
* @dataProvider minQueryProvider
*/
public function testSegmentTreeMinQuery(int $expected, int $startIndex, int $endIndex): void
{
$segmentTree = new SegmentTree($this->testArray, function ($a, $b) {
return min($a, $b);
});
$this->assertEquals(
$expected,
$segmentTree->query($startIndex, $endIndex),
"Query min between index $startIndex and $endIndex should return $expected."
);
}
/**
* Test update functionality for different query types.
*/
public function testSegmentTreeUpdate(): void
{
// Sum Query
$segmentTreeSum = new SegmentTree($this->testArray);
$segmentTreeSum->update(2, 10); // Update index 2 to 10
$this->assertEquals(
29,
$segmentTreeSum->query(1, 4),
"After update, sum between index 1 and 4 should return 29."
);
// Max Query: with callback
$segmentTreeMax = new SegmentTree($this->testArray, fn($a, $b) => max($a, $b));
$segmentTreeMax->update(12, -1); // Update index 12 to -1
$this->assertEquals(
19,
$segmentTreeMax->query(5, 12),
"After update, max between index 5 and 12 should return 19."
);
// Min Query: with callback
$segmentTreeMin = new SegmentTree($this->testArray, fn($a, $b) => min($a, $b));
$segmentTreeMin->update(9, -5); // Update index 9 to -5
$this->assertEquals(
-5,
$segmentTreeMin->query(9, 13),
"After update, min between index 9 and 13 should return -5."
);
}
/**
* Test range update functionality for different query types.
*/
public function testSegmentTreeRangeUpdate(): void
{
// Sum Query
$segmentTreeSum = new SegmentTree($this->testArray);
$segmentTreeSum->rangeUpdate(3, 7, 0); // Set indices 3 to 7 to 0
$this->assertEquals(
55,
$segmentTreeSum->query(2, 10),
"After range update, sum between index 2 and 10 should return 55."
);
// Max Query: with callback
$segmentTreeMax = new SegmentTree($this->testArray, fn($a, $b) => max($a, $b));
$segmentTreeMax->rangeUpdate(3, 7, 0); // Set indices 3 to 7 to 0
$this->assertEquals(
5,
$segmentTreeMax->query(2, 7),
"After range update, max between index 2 and 7 should return 5."
);
// Min Query: with callback
$segmentTreeMin = new SegmentTree($this->testArray, fn($a, $b) => min($a, $b));
$segmentTreeMin->rangeUpdate(3, 9, 0); // Set indices 3 to 9 to 0
$this->assertEquals(
0,
$segmentTreeMin->query(2, 9),
"After range update, min between index 2 and 9 should return 0."
);
}
/**
* Test array updates reflections.
*/
public function testGetCurrentArray(): void
{
$segmentTree = new SegmentTree($this->testArray);
// Ensure the initial array matches the input array
$this->assertEquals(
$this->testArray,
$segmentTree->getCurrentArray(),
"getCurrentArray() should return the initial array."
);
// Perform an update and test again
$segmentTree->update(2, 10); // Update index 2 to 10
$updatedArray = $this->testArray;
$updatedArray[2] = 10;
$this->assertEquals(
$updatedArray,
$segmentTree->getCurrentArray(),
"getCurrentArray() should return the updated array."
);
}
/**
* Test serialization and deserialization of the segment tree.
*/
public function testSegmentTreeSerialization(): void
{
$segmentTree = new SegmentTree($this->testArray);
$serialized = $segmentTree->serialize();
$deserializedTree = SegmentTree::deserialize($serialized);
$this->assertEquals(
$segmentTree->query(1, 4),
$deserializedTree->query(1, 4),
"Serialized and deserialized trees should have the same query results."
);
}
/**
* Testing EdgeCases: first and last indices functionality on the Segment Tree
*/
public function testEdgeCases(): void
{
$segmentTree = new SegmentTree($this->testArray);
$firstIndex = 0;
$lastIndex = count($this->testArray) - 1;
// Test querying the first and last indices
$this->assertEquals(
$this->testArray[$firstIndex],
$segmentTree->query($firstIndex, $firstIndex),
"Query at the first index should return {$this->testArray[$firstIndex]}."
);
$this->assertEquals(
$this->testArray[$lastIndex],
$segmentTree->query($lastIndex, $lastIndex),
"Query at the last index should return {$this->testArray[$lastIndex]}."
);
// Test updating the first index
$segmentTree->update($firstIndex, 100); // Update first index to 100
$this->assertEquals(
100,
$segmentTree->query($firstIndex, $firstIndex),
"After update, query at the first index should return {$this->testArray[$firstIndex]}."
);
// Test updating the last index
$segmentTree->update($lastIndex, 200); // Update last index to 200
$this->assertEquals(
200,
$segmentTree->query($lastIndex, $lastIndex),
"After update, query at the last index should return {$this->testArray[$lastIndex]}."
);
// Test range update that includes the first index
$segmentTree->rangeUpdate($firstIndex, 2, 50); // Set indices 0 to 2 to 50
$this->assertEquals(
50,
$segmentTree->query($firstIndex, $firstIndex),
"After range update, query at index $firstIndex should return 50."
);
$this->assertEquals(50, $segmentTree->query(1, 1), "After range update, query at index 1 should return 50.");
$this->assertEquals(50, $segmentTree->query(2, 2), "After range update, query at index 2 should return 50.");
// Test range update that includes the last index
$segmentTree->rangeUpdate($lastIndex - 3, $lastIndex, 10); // Set indices to 10
$this->assertEquals(
10,
$segmentTree->query($lastIndex, $lastIndex),
"After range update, query at the last index should return 10."
);
$this->assertEquals(
10,
$segmentTree->query(count($this->testArray) - 2, count($this->testArray) - 2),
"After range update, query at the second last index should return 10."
);
}
/**
* Test empty or unsupported arrays.
*/
public function testUnsupportedOrEmptyArrayInitialization(): void
{
// Test empty array
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Array must not be empty, must contain numeric values
and must be non-associative.");
$segmentTreeEmpty = new SegmentTree([]); // expecting an exception
// Test unsupported array (e.g., with non-numeric values)
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Array must not be empty, must contain numeric values
and must be non-associative.");
$segmentTreeUnsupported = new SegmentTree([1, "two", 3]); // Mix of numeric and non-numeric
}
/**
* Test exception for invalid update index.
*/
public function testInvalidUpdateIndex(): void
{
$segmentTree = new SegmentTree($this->testArray);
$index = count($this->testArray) + 5;
// Expect an exception for range update with invalid indices
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage("Index out of bounds: $index. Must be between 0 and "
. (count($this->testArray) - 1));
$segmentTree->update($index, 100); // non-existing index, should trigger exception
}
/**
* Test exception for invalid update index.
*/
public function testOutOfBoundsQuery(): void
{
$segmentTree = new SegmentTree($this->testArray);
$start = 0;
$end = count($this->testArray);
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage("Index out of bounds: start = $start, end = $end.
Must be between 0 and " . (count($this->testArray) - 1));
$segmentTree->query(0, count($this->testArray)); // expecting an exception
}
/**
* Test exception for invalid range update.
*/
public function testInvalidRangeUpdate(): void
{
$segmentTree = new SegmentTree($this->testArray);
$start = -1;
$end = 5;
// Expect an exception for range update with invalid indices
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage("Invalid range: start = $start, end = $end.");
$segmentTree->rangeUpdate(-1, 5, 0); // Negative index, should trigger exception
}
}