1--TEST-- 2Test rand() - basic function test rand() 3--FILE-- 4<?php 5$default_max = getrandmax(); 6 7echo "\nrand() tests with default min and max value (i.e 0 thru ", $default_max, ")\n"; 8for ($i = 0; $i < 100; $i++) { 9 $res = 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 "\nrand() 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 = 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// Eexepcted 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 = 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-- 83 84rand() tests with default min and max value (i.e 0 thru %i) 85PASSED: range min = 0 max = %i 86 87rand() tests with defined min and max value 88PASSED: range min = 10 max = 100 89PASSED: range min = 100 max = 1000 90PASSED: range min = 10 max = 19 91PASSED: range min = 10500 max = 1050000 92PASSED: range min = 16 max = 65536 93PASSED: range min = 256 max = 448 94 95Non-numeric cases 96PASSED range min = 1 max = 100 97PASSED range min = 0 max = 100 98PASSED range min = 0 max = 100 99PASSED range min = 10 max = 100 100PASSED range min = 10 max = 100 101