1--TEST-- 2Random: Randomizer: pickArrayKeys(): Basic functionality 3--FILE-- 4<?php 5 6use Random\Engine; 7use Random\Engine\Mt19937; 8use Random\Engine\PcgOneseq128XslRr64; 9use Random\Engine\Secure; 10use Random\Engine\Test\TestShaEngine; 11use Random\Engine\Xoshiro256StarStar; 12use Random\Randomizer; 13 14require __DIR__ . "/../../engines.inc"; 15 16$engines = []; 17$engines[] = new Mt19937(null, MT_RAND_MT19937); 18$engines[] = new Mt19937(null, MT_RAND_PHP); 19$engines[] = new PcgOneseq128XslRr64(); 20$engines[] = new Xoshiro256StarStar(); 21$engines[] = new Secure(); 22$engines[] = new TestShaEngine(); 23 24$array1 = []; // list 25$array2 = []; // associative array with only strings 26$array3 = []; // mixed key array 27for ($i = 0; $i < 500; $i++) { 28 $string = sha1((string)$i); 29 30 $array1[] = $i; 31 $array2[$string] = $i; 32 $array3[$string] = $i; 33 $array3[$i] = $string; 34} 35 36foreach ($engines as $engine) { 37 echo $engine::class, PHP_EOL; 38 39 $randomizer = new Randomizer($engine); 40 41 for ($i = 1; $i < 100; $i++) { 42 $result = $randomizer->pickArrayKeys($array1, $i); 43 44 if (array_unique($result) !== $result) { 45 die("failure: duplicates returned at {$i} for array1"); 46 } 47 48 if (array_diff($result, array_keys($array1)) !== []) { 49 die("failure: non-keys returned at {$i} for array1"); 50 } 51 52 $result = $randomizer->pickArrayKeys($array2, $i); 53 54 if (array_unique($result) !== $result) { 55 die("failure: duplicates returned at {$i} for array2"); 56 } 57 58 if (array_diff($result, array_keys($array2)) !== []) { 59 die("failure: non-keys returned at {$i} for array2"); 60 } 61 62 $result = $randomizer->pickArrayKeys($array3, $i); 63 64 if (array_unique($result) !== $result) { 65 die("failure: duplicates returned at {$i} for array3"); 66 } 67 68 if (array_diff($result, array_keys($array3)) !== []) { 69 die("failure: non-keys returned at {$i} for array3"); 70 } 71 } 72} 73 74die('success'); 75 76?> 77--EXPECT-- 78Random\Engine\Mt19937 79Random\Engine\Mt19937 80Random\Engine\PcgOneseq128XslRr64 81Random\Engine\Xoshiro256StarStar 82Random\Engine\Secure 83Random\Engine\Test\TestShaEngine 84success 85