array_rand.phpt 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. --TEST--
  2. array_rand() tests
  3. --FILE--
  4. <?php
  5. try {
  6. var_dump(array_rand(array()));
  7. } catch (\ValueError $e) {
  8. echo $e->getMessage() . "\n";
  9. }
  10. try {
  11. var_dump(array_rand(array(), 0));
  12. } catch (\ValueError $e) {
  13. echo $e->getMessage() . "\n";
  14. }
  15. try {
  16. var_dump(array_rand(array(1,2,3), 0));
  17. } catch (\ValueError $e) {
  18. echo $e->getMessage() . "\n";
  19. }
  20. try {
  21. var_dump(array_rand(array(1,2,3), -1));
  22. } catch (\ValueError $e) {
  23. echo $e->getMessage() . "\n";
  24. }
  25. try {
  26. var_dump(array_rand(array(1,2,3), 10));
  27. } catch (\ValueError $e) {
  28. echo $e->getMessage() . "\n";
  29. }
  30. var_dump(array_rand(array(1,2,3), 3));
  31. var_dump(array_rand(array(1,2,3), 2));
  32. ?>
  33. --EXPECTF--
  34. array_rand(): Argument #1 ($array) cannot be empty
  35. array_rand(): Argument #1 ($array) cannot be empty
  36. array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array)
  37. array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array)
  38. array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array)
  39. array(3) {
  40. [0]=>
  41. int(%d)
  42. [1]=>
  43. int(%d)
  44. [2]=>
  45. int(%d)
  46. }
  47. array(2) {
  48. [0]=>
  49. int(%d)
  50. [1]=>
  51. int(%d)
  52. }