gmp_random_range.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. --TEST--
  2. gmp_random_range() basic tests
  3. --SKIPIF--
  4. <?php if (!extension_loaded("gmp")) print "skip"; ?>
  5. --FILE--
  6. <?php
  7. $minusTen = gmp_init(-10);
  8. $plusTen = gmp_init(10);
  9. $zero = gmp_init(0);
  10. var_dump(gmp_random_range());
  11. var_dump(gmp_random_range(10));
  12. var_dump(gmp_random_range(10, -10));
  13. var_dump(gmp_random_range($plusTen, $minusTen));
  14. var_dump(gmp_random_range($plusTen, $zero));
  15. // If these error the test fails.
  16. gmp_random_range(0, 10);
  17. gmp_random_range(1, 10);
  18. gmp_random_range(-1, 10);
  19. gmp_random_range(-10, 0);
  20. gmp_random_range(-10, -1);
  21. gmp_random_range(0, $plusTen);
  22. gmp_random_range(1, $plusTen);
  23. gmp_random_range(-1, $plusTen);
  24. gmp_random_range($zero, $plusTen);
  25. gmp_random_range($minusTen, $plusTen);
  26. // 0.5 seconds to make sure the numbers stay in range
  27. $start = microtime(true);
  28. while (1) {
  29. for ($i = 0; $i < 5000; $i++) {
  30. $result = gmp_random_range(0, 1000);
  31. if ($result < 0 || $result > 1000) {
  32. print "RANGE VIOLATION 1\n";
  33. var_dump($result);
  34. break 2;
  35. }
  36. $result = gmp_random_range(-1000, 0);
  37. if ($result < -1000 || $result > 0) {
  38. print "RANGE VIOLATION 2\n";
  39. var_dump($result);
  40. break 2;
  41. }
  42. $result = gmp_random_range(-500, 500);
  43. if ($result < -500 || $result > 500) {
  44. print "RANGE VIOLATION 3\n";
  45. var_dump($result);
  46. break 2;
  47. }
  48. }
  49. if (microtime(true) - $start > 0.5) {
  50. break;
  51. }
  52. }
  53. echo "Done\n";
  54. ?>
  55. --EXPECTF--
  56. Warning: gmp_random_range() expects exactly 2 parameters, 0 given in %s on line %d
  57. NULL
  58. Warning: gmp_random_range() expects exactly 2 parameters, 1 given in %s on line %d
  59. NULL
  60. Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
  61. bool(false)
  62. Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
  63. bool(false)
  64. Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
  65. bool(false)
  66. Done