1--TEST-- 2Test mt_rand() - basic function test mt_rand() 3--FILE-- 4<?php 5$default_max = mt_getrandmax(); 6 7echo "\nmt_rand() tests with default min and max value (i.e 0 thru ", $default_max, ")\n"; 8for ($i = 0; $i < 100; $i++) { 9 $res = mt_rand(); 10 11// By default RAND_MAX is 32768 although no constant is defined for it for user space apps 12 if (!is_int($res) || $res < 0 || $res > $default_max) { 13 break; 14 } 15} 16 17if ($i != 100) { 18 echo "FAILED: res = ", $res, " min = 0 max = ", $default_max, "\n"; 19} else { 20 echo "PASSED: range min = 0 max = ", $default_max, "\n"; 21} 22 23echo "\nmt_rand() tests with defined min and max value\n"; 24 25$min = array(10, 26 100, 27 10.5, 28 10.5e3, 29 0x10, 30 0400); 31 32$max = array(100, 33 1000, 34 19.5, 35 10.5e5, 36 0x10000, 37 0700); 38 39for ($x = 0; $x < count($min); $x++) { 40 for ($i = 0; $i < 100; $i++) { 41 $res = mt_rand($min[$x], $max[$x]); 42 43 if (!is_int($res) || $res < intval($min[$x]) || $res > intval($max[$x])) { 44 echo "FAILED: res = ", $res, " min = ", intval($min[$x]), " max = ", intval($max[$x]), "\n"; 45 break; 46 } 47 } 48 49 if ($i == 100) { 50 echo "PASSED: range min = ", intval($min[$x]), " max = ", intval($max[$x]), "\n"; 51 } 52} 53 54echo "\nNon-numeric cases\n"; 55$min = array(true, 56 false, 57 null, 58 "10", 59 "10.5"); 60 61// Expected numerical equivalent of above non-numerics 62$minval = array(1, 63 0, 64 0, 65 10, 66 10); 67for ($x = 0; $x < count($min); $x++) { 68 for ($i = 0; $i < 100; $i++) { 69 $res = mt_rand($min[$x], 100); 70 71 if (!is_int($res) || $res < intval($minval[$x]) || $res > 100) { 72 echo "FAILED: res = ", $res, " min = ", intval($min[$x]), " max = ", intval($max[$x]), "\n"; 73 break; 74 } 75 } 76 77 if ($i == 100) { 78 echo "PASSED range min = ", intval($min[$x]), " max = 100\n"; 79 } 80} 81?> 82--EXPECTF-- 83mt_rand() tests with default min and max value (i.e 0 thru 2147483647) 84PASSED: range min = 0 max = 2147483647 85 86mt_rand() tests with defined min and max value 87PASSED: range min = 10 max = 100 88PASSED: range min = 100 max = 1000 89PASSED: range min = 10 max = 19 90PASSED: range min = 10500 max = 1050000 91PASSED: range min = 16 max = 65536 92PASSED: range min = 256 max = 448 93 94Non-numeric cases 95PASSED range min = 1 max = 100 96PASSED range min = 0 max = 100 97PASSED range min = 0 max = 100 98PASSED range min = 10 max = 100 99PASSED range min = 10 max = 100 100