array_rand_variation2.phpt 4.4 KB

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