array_rand_variation2.phpt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. --TEST--
  2. Test array_rand() function : usage variations - unexpected values for 'num_req' 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 array_rand() with different types of values other than int passed to 'num_req' argument
  11. * to see that function works with unexpeced data and generates warning message as required.
  12. */
  13. echo "*** Testing array_rand() : unexpected values for 'num_req' parameter ***\n";
  14. // Initialise function arguments
  15. $input = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
  16. //get an unset variable
  17. $unset_var = 10;
  18. unset ($unset_var);
  19. //define a class
  20. class test
  21. {
  22. var $t = 10;
  23. function __toString()
  24. {
  25. return "3object";
  26. }
  27. }
  28. //array of values to iterate over
  29. $values = array(
  30. // int data
  31. /*1*/ 0,
  32. 1,
  33. 12345,
  34. -2345,
  35. // float data
  36. /*5*/ 10.5,
  37. -10.5,
  38. 12.3456789000e10,
  39. 12.3456789000E-10,
  40. .5,
  41. // null data
  42. /*10*/ NULL,
  43. null,
  44. // boolean data
  45. /*12*/ true,
  46. false,
  47. TRUE,
  48. FALSE,
  49. // empty data
  50. /*16*/ "",
  51. '',
  52. // string data
  53. /*18*/ "string",
  54. 'string',
  55. // object data
  56. /*20*/ new test(),
  57. // undefined data
  58. /*21*/ @$undefined_var,
  59. // unset data
  60. /*22*/ @$unset_var,
  61. );
  62. // loop through each element of the array for different values for 'num_req' argument
  63. $count = 1;
  64. foreach($values as $value) {
  65. echo "\n-- Iteration $count --\n";
  66. var_dump( array_rand($input,$value) );
  67. $count++;
  68. };
  69. echo "Done";
  70. ?>
  71. --EXPECTF--
  72. *** Testing array_rand() : unexpected values for 'num_req' parameter ***
  73. -- Iteration 1 --
  74. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  75. NULL
  76. -- Iteration 2 --
  77. int(%d)
  78. -- Iteration 3 --
  79. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  80. NULL
  81. -- Iteration 4 --
  82. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  83. NULL
  84. -- Iteration 5 --
  85. array(10) {
  86. [0]=>
  87. int(%d)
  88. [1]=>
  89. int(%d)
  90. [2]=>
  91. int(%d)
  92. [3]=>
  93. int(%d)
  94. [4]=>
  95. int(%d)
  96. [5]=>
  97. int(%d)
  98. [6]=>
  99. int(%d)
  100. [7]=>
  101. int(%d)
  102. [8]=>
  103. int(%d)
  104. [9]=>
  105. int(%d)
  106. }
  107. -- Iteration 6 --
  108. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  109. NULL
  110. -- Iteration 7 --
  111. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  112. NULL
  113. -- Iteration 8 --
  114. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  115. NULL
  116. -- Iteration 9 --
  117. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  118. NULL
  119. -- Iteration 10 --
  120. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  121. NULL
  122. -- Iteration 11 --
  123. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  124. NULL
  125. -- Iteration 12 --
  126. int(%d)
  127. -- Iteration 13 --
  128. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  129. NULL
  130. -- Iteration 14 --
  131. int(%d)
  132. -- Iteration 15 --
  133. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  134. NULL
  135. -- Iteration 16 --
  136. Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
  137. NULL
  138. -- Iteration 17 --
  139. Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
  140. NULL
  141. -- Iteration 18 --
  142. Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
  143. NULL
  144. -- Iteration 19 --
  145. Warning: array_rand() expects parameter 2 to be long, string given in %s on line %d
  146. NULL
  147. -- Iteration 20 --
  148. Warning: array_rand() expects parameter 2 to be long, object given in %s on line %d
  149. NULL
  150. -- Iteration 21 --
  151. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  152. NULL
  153. -- Iteration 22 --
  154. Warning: array_rand(): Second argument has to be between 1 and the number of elements in the array in %s on line %d
  155. NULL
  156. Done