array_rand_variation5.phpt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --TEST--
  2. Test array_rand() function : usage variation - invalid values for 'req_num' parameter
  3. --FILE--
  4. <?php
  5. /* Prototype : mixed array_rand(array $input [, int $num_req])
  6. * Description: Return key/keys for random entry/entries in the array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Test behaviour of array_rand() function when associative array and
  11. * various invalid values are passed to the 'input' and 'req_num'
  12. * parameters respectively
  13. */
  14. echo "*** Testing array_rand() : with invalid values for 'req_num' ***\n";
  15. // initialise associative arrays
  16. $input = array(
  17. 1 => 'one', 2.2 => 'float key', 0.9 => 'decimal key',
  18. 2e2 => 'exp key1', 2000e-3 => 'negative exp key',
  19. 0xabc => 2748, 0x12f => '303', 0xff => "255",
  20. 0123 => 83, 0129 => 10, 010 => "8"
  21. );
  22. // Testing array_rand() function with various invalid 'req_num' values
  23. // with valid num_req values
  24. echo"\n-- With default num_req value --\n";
  25. var_dump( array_rand($input) ); // with default $num_req value
  26. echo"\n-- With num_req = 1 --\n";
  27. var_dump( array_rand($input, 1) ); // with valid $num_req value
  28. // with invalid num_req value
  29. echo"\n-- With num_req = 0 --\n";
  30. var_dump( array_rand($input, 0) ); // with $num_req=0
  31. echo"\n-- With num_req = -1 --\n";
  32. var_dump( array_rand($input, -1) ); // with $num_req=-1
  33. echo"\n-- With num_req = -2 --\n";
  34. var_dump( array_rand($input, -2) ); // with $num_req=-2
  35. echo"\n-- With num_req more than number of members in 'input' array --\n";
  36. var_dump( array_rand($input, 13) ); // with $num_req=13
  37. echo "Done";
  38. ?>
  39. --EXPECTF--
  40. *** Testing array_rand() : with invalid values for 'req_num' ***
  41. -- With default num_req value --
  42. int(%d)
  43. -- With num_req = 1 --
  44. int(%d)
  45. -- With num_req = 0 --
  46. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  47. NULL
  48. -- With num_req = -1 --
  49. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  50. NULL
  51. -- With num_req = -2 --
  52. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  53. NULL
  54. -- With num_req more than number of members in 'input' array --
  55. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  56. NULL
  57. Done