Debian Codegraph @

tests/phpunit/unit/includes/libs/ArrayUtilsTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests;
4
5 use ArrayUtils;
6 use MediaWikiCoversValidator;
7 use PHPUnit\Framework\TestCase;
8
9 /**
10 * @covers \ArrayUtils
11 */
12 class ArrayUtilsTest extends TestCase {
13
14 use MediaWikiCoversValidator;
15
16 /**
17 * @dataProvider provideFindLowerBound
18 */
19 public function testFindLowerBound(
20 $valueCallback, $valueCount, $comparisonCallback, $target, $expected
21 ) {
22 $this->assertSame(
23 $expected,
24 ArrayUtils::findLowerBound(
25 $valueCallback, $valueCount, $comparisonCallback, $target
26 )
27 );
28 }
29
30 public function provideFindLowerBound() {
31 $indexValueCallback = function ( $size ) {
32 return function ( $val ) use ( $size ) {
33 $this->assertTrue( $val >= 0 );
34 $this->assertTrue( $val < $size );
35 return $val;
36 };
37 };
38 $comparisonCallback = static function ( $a, $b ) {
39 return $a - $b;
40 };
41
42 return [
43 [
44 $indexValueCallback( 0 ),
45 0,
46 $comparisonCallback,
47 1,
48 false,
49 ],
50 [
51 $indexValueCallback( 1 ),
52 1,
53 $comparisonCallback,
54 -1,
55 false,
56 ],
57 [
58 $indexValueCallback( 1 ),
59 1,
60 $comparisonCallback,
61 0,
62 0,
63 ],
64 [
65 $indexValueCallback( 1 ),
66 1,
67 $comparisonCallback,
68 1,
69 0,
70 ],
71 [
72 $indexValueCallback( 2 ),
73 2,
74 $comparisonCallback,
75 -1,
76 false,
77 ],
78 [
79 $indexValueCallback( 2 ),
80 2,
81 $comparisonCallback,
82 0,
83 0,
84 ],
85 [
86 $indexValueCallback( 2 ),
87 2,
88 $comparisonCallback,
89 0.5,
90 0,
91 ],
92 [
93 $indexValueCallback( 2 ),
94 2,
95 $comparisonCallback,
96 1,
97 1,
98 ],
99 [
100 $indexValueCallback( 2 ),
101 2,
102 $comparisonCallback,
103 1.5,
104 1,
105 ],
106 [
107 $indexValueCallback( 3 ),
108 3,
109 $comparisonCallback,
110 1,
111 1,
112 ],
113 [
114 $indexValueCallback( 3 ),
115 3,
116 $comparisonCallback,
117 1.5,
118 1,
119 ],
120 [
121 $indexValueCallback( 3 ),
122 3,
123 $comparisonCallback,
124 2,
125 2,
126 ],
127 [
128 $indexValueCallback( 3 ),
129 3,
130 $comparisonCallback,
131 3,
132 2,
133 ],
134 ];
135 }
136
137 /**
138 * @dataProvider provideArrayDiffAssocRecursive
139 */
140 public function testArrayDiffAssocRecursive( $expected, ...$args ) {
141 $this->assertEquals( $expected, ArrayUtils::arrayDiffAssocRecursive( ...$args ) );
142 }
143
144 public static function provideArrayDiffAssocRecursive() {
145 return [
146 [
147 [],
148 [],
149 [],
150 ],
151 [
152 [],
153 [],
154 [],
155 [],
156 ],
157 [
158 [ 1 ],
159 [ 1 ],
160 [],
161 ],
162 [
163 [ 1 ],
164 [ 1 ],
165 [],
166 [],
167 ],
168 [
169 [],
170 [],
171 [ 1 ],
172 ],
173 [
174 [],
175 [],
176 [ 1 ],
177 [ 2 ],
178 ],
179 [
180 [ '' => 1 ],
181 [ '' => 1 ],
182 [],
183 ],
184 [
185 [],
186 [],
187 [ '' => 1 ],
188 ],
189 [
190 [ 1 ],
191 [ 1 ],
192 [ 2 ],
193 ],
194 [
195 [],
196 [ 1 ],
197 [ 2 ],
198 [ 1 ],
199 ],
200 [
201 [],
202 [ 1 ],
203 [ 1, 2 ],
204 ],
205 [
206 [ 1 => 1 ],
207 [ 1 => 1 ],
208 [ 1 ],
209 ],
210 [
211 [],
212 [ 1 => 1 ],
213 [ 1 ],
214 [ 1 => 1 ],
215 ],
216 [
217 [],
218 [ 1 => 1 ],
219 [ 1, 1, 1 ],
220 ],
221 [
222 [],
223 [ [] ],
224 [],
225 ],
226 [
227 [],
228 [ [ [] ] ],
229 [],
230 ],
231 [
232 [ 1, [ 1 ] ],
233 [ 1, [ 1 ] ],
234 [],
235 ],
236 [
237 [ 1 ],
238 [ 1, [ 1 ] ],
239 [ 2, [ 1 ] ],
240 ],
241 [
242 [],
243 [ 1, [ 1 ] ],
244 [ 2, [ 1 ] ],
245 [ 1, [ 2 ] ],
246 ],
247 [
248 [ 1 ],
249 [ 1, [] ],
250 [ 2 ],
251 ],
252 [
253 [],
254 [ 1, [] ],
255 [ 2 ],
256 [ 1 ],
257 ],
258 [
259 [ 1, [ 1 => 2 ] ],
260 [ 1, [ 1, 2 ] ],
261 [ 2, [ 1 ] ],
262 ],
263 [
264 [ 1 ],
265 [ 1, [ 1, 2 ] ],
266 [ 2, [ 1 ] ],
267 [ 2, [ 1 => 2 ] ],
268 ],
269 [
270 [ 1 => [ 1, 2 ] ],
271 [ 1, [ 1, 2 ] ],
272 [ 1, [ 2 ] ],
273 ],
274 [
275 [ 1 => [ [ 2, 3 ], 2 ] ],
276 [ 1, [ [ 2, 3 ], 2 ] ],
277 [ 1, [ 2 ] ],
278 ],
279 [
280 [ 1 => [ [ 2 ], 2 ] ],
281 [ 1, [ [ 2, 3 ], 2 ] ],
282 [ 1, [ [ 1 => 3 ] ] ],
283 ],
284 [
285 [ 1 => [ 1 => 2 ] ],
286 [ 1, [ [ 2, 3 ], 2 ] ],
287 [ 1, [ [ 1 => 3, 0 => 2 ] ] ],
288 ],
289 [
290 [ 1 => [ 1 => 2 ] ],
291 [ 1, [ [ 2, 3 ], 2 ] ],
292 [ 1, [ [ 1 => 3 ] ] ],
293 [ 1 => [ [ 2 ] ] ],
294 ],
295 [
296 [],
297 [ 1, [ [ 2, 3 ], 2 ] ],
298 [ 1 => [ 1 => 2, 0 => [ 1 => 3, 0 => 2 ] ], 0 => 1 ],
299 ],
300 [
301 [],
302 [ 1, [ [ 2, 3 ], 2 ] ],
303 [ 1 => [ 1 => 2 ] ],
304 [ 1 => [ [ 1 => 3 ] ] ],
305 [ 1 => [ [ 2 ] ] ],
306 [ 1 ],
307 ],
308 ];
309 }
310
311 /**
312 * @dataProvider provideCartesianProduct
313 */
314 public function testCartesianProduct( $inputs, $expected ) {
315 $result = ArrayUtils::cartesianProduct( ...$inputs );
316 $this->assertSame( $expected, $result );
317 }
318
319 public static function provideCartesianProduct() {
320 $ab = [ 'a', 'b' ];
321 $cd = [ 'c', 'd' ];
322 $ac = [ 'a', 'c' ];
323 $ad = [ 'a', 'd' ];
324 $bc = [ 'b', 'c' ];
325 $bd = [ 'b', 'd' ];
326
327 return [
328 'no inputs' => [
329 [],
330 []
331 ],
332 'one empty input' => [
333 [ [] ],
334 []
335 ],
336 'one non-empty input' => [
337 [ $ab ],
338 [ [ 'a' ], [ 'b' ] ]
339 ],
340 'non-empty times empty' => [
341 [ $ab, [] ],
342 []
343 ],
344 'empty times non-empty' => [
345 [ [], $ab ],
346 []
347 ],
348 'ab x cd' => [
349 [ $ab, $cd ],
350 [ $ac, $ad, $bc, $bd ]
351 ],
352 'keys are ignored' => [
353 [
354 [ 99 => 'a', 98 => 'b' ],
355 [ 97 => 'c', 96 => 'd' ],
356 ],
357 [ $ac, $ad, $bc, $bd ]
358 ],
359 ];
360 }
361
362 public static function provideConsistentSort() {
363 yield 'initial' => [
364 [
365 'tag1',
366 'tag2',
367 'tag3',
368 ],
369 'foobar',
370 [ 'tag2', 'tag3', 'tag1' ],
371 [ 1 => 'tag2', 2 => 'tag3', 0 => 'tag1' ],
372 ];
373 yield 'adding preserves order' => [
374 [
375 'tag1',
376 'tag2',
377 'tag3',
378 'tag4',
379 ],
380 'foobar',
381 [ 'tag2', 'tag4', 'tag3', 'tag1' ],
382 [ 1 => 'tag2', 3 => 'tag4', 2 => 'tag3', 0 => 'tag1' ],
383 ];
384 yield 'depool preserves order' => [
385 [
386 'tag1',
387 'tag3',
388 ],
389 'foobar',
390 [ 'tag3', 'tag1' ],
391 [ 1 => 'tag3', 0 => 'tag1' ],
392 ];
393 yield 'input order is not significant' => [
394 [
395 // Backwards
396 'tag3',
397 'tag2',
398 'tag1',
399 ],
400 'foobar',
401 [ 'tag2', 'tag3', 'tag1' ],
402 [ 1 => 'tag2', 0 => 'tag3', 2 => 'tag1' ],
403 ];
404 yield 'input keys are not significant' => [
405 [
406 'z' => 'tag2',
407 'y' => 'tag3',
408 'x' => 'tag1',
409 ],
410 'foobar',
411 [ 'tag2', 'tag3', 'tag1' ],
412 [ 'z' => 'tag2', 'y' => 'tag3', 'x' => 'tag1' ],
413 ];
414 }
415
416 /**
417 * @dataProvider provideConsistentSort
418 */
419 public function testConsistentSort( $array, string $key, array $expectList, array $expect ) {
420 $actual = $array;
421 ArrayUtils::consistentHashSort( $actual, $key );
422 $this->assertSame( $expect, $actual );
423 $this->assertSame( $expectList, array_values( $actual ) );
424 }
425
426 public static function provideConsistentSortStability() {
427 yield 'initial' => [
428 [ 'tag1', 'tag2', 'tag3', 'tag4' ],
429 [
430 'foo' => 'tag3',
431 'bar' => 'tag2',
432 'qux' => 'tag4',
433 'quux' => 'tag1',
434 'garply' => 'tag3',
435 ],
436 ];
437 yield 'depool tag2' => [
438 [ 'tag1', 'tag3', 'tag4' ],
439 [
440 'foo' => 'tag3',
441 'bar' => 'tag1', # changed from tag2
442 'qux' => 'tag4',
443 'quux' => 'tag1',
444 'garply' => 'tag3',
445 ],
446 ];
447 yield 'add tag5' => [
448 [ 'tag1', 'tag3', 'tag4', 'tag5' ],
449 [
450 'foo' => 'tag3',
451 'bar' => 'tag1',
452 'qux' => 'tag4',
453 'quux' => 'tag5', # changed from tag1
454 'garply' => 'tag3',
455 ],
456 ];
457 }
458
459 /**
460 * @dataProvider provideConsistentSortStability
461 */
462 public function testConsistentSortStability( $serverTags, $expected ) {
463 $actual = [];
464 foreach ( $expected as $key => $_ ) {
465 $sortedTags = $serverTags;
466 ArrayUtils::consistentHashSort( $sortedTags, $key );
467 $dest = $serverTags[array_key_first( $sortedTags )];
468
469 $actual[$key] = $dest;
470 }
471
472 $this->assertSame( $expected, $actual );
473 }
474 }