rand_basic.phpt 2.3 KB

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