array_rand_variation1.phpt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. --TEST--
  2. Test array_rand() function : usage variations - unexpected values for 'input' 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 arrays passed to the 'input' parameter
  11. * to see that function works with unexpeced data and generates warning message as required.
  12. */
  13. echo "*** Testing array_rand() : unexpected values for 'input' parameter ***\n";
  14. // Initialise function arguments
  15. $num_req = 10;
  16. //get an unset variable
  17. $unset_var = 10;
  18. unset ($unset_var);
  19. //get a resource variable
  20. $fp = fopen(__FILE__, "r");
  21. //define a class
  22. class test
  23. {
  24. var $t = 10;
  25. function __toString()
  26. {
  27. return "object";
  28. }
  29. }
  30. //array of different values for 'input' parameter
  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. // resource data
  60. /*21*/ $fp,
  61. // undefined data
  62. /*22*/ @$undefined_var,
  63. // unset data
  64. /*23*/ @$unset_var,
  65. );
  66. /* loop through each element of the array to test array_rand() function
  67. * for different values for 'input' argument
  68. */
  69. $count = 1;
  70. foreach($values as $value) {
  71. echo "\n-- Iteration $count --\n";
  72. var_dump( array_rand($value,$num_req) );
  73. $count++;
  74. };
  75. // closing the resource
  76. fclose($fp);
  77. echo "Done";
  78. ?>
  79. --EXPECTF--
  80. *** Testing array_rand() : unexpected values for 'input' parameter ***
  81. -- Iteration 1 --
  82. Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
  83. NULL
  84. -- Iteration 2 --
  85. Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
  86. NULL
  87. -- Iteration 3 --
  88. Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
  89. NULL
  90. -- Iteration 4 --
  91. Warning: array_rand() expects parameter 1 to be array, integer given in %s on line %d
  92. NULL
  93. -- Iteration 5 --
  94. Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
  95. NULL
  96. -- Iteration 6 --
  97. Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
  98. NULL
  99. -- Iteration 7 --
  100. Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
  101. NULL
  102. -- Iteration 8 --
  103. Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
  104. NULL
  105. -- Iteration 9 --
  106. Warning: array_rand() expects parameter 1 to be array, double given in %s on line %d
  107. NULL
  108. -- Iteration 10 --
  109. Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
  110. NULL
  111. -- Iteration 11 --
  112. Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
  113. NULL
  114. -- Iteration 12 --
  115. Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
  116. NULL
  117. -- Iteration 13 --
  118. Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
  119. NULL
  120. -- Iteration 14 --
  121. Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
  122. NULL
  123. -- Iteration 15 --
  124. Warning: array_rand() expects parameter 1 to be array, boolean given in %s on line %d
  125. NULL
  126. -- Iteration 16 --
  127. Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
  128. NULL
  129. -- Iteration 17 --
  130. Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
  131. NULL
  132. -- Iteration 18 --
  133. Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
  134. NULL
  135. -- Iteration 19 --
  136. Warning: array_rand() expects parameter 1 to be array, string given in %s on line %d
  137. NULL
  138. -- Iteration 20 --
  139. Warning: array_rand() expects parameter 1 to be array, object given in %s on line %d
  140. NULL
  141. -- Iteration 21 --
  142. Warning: array_rand() expects parameter 1 to be array, resource given in %s on line %d
  143. NULL
  144. -- Iteration 22 --
  145. Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
  146. NULL
  147. -- Iteration 23 --
  148. Warning: array_rand() expects parameter 1 to be array, null given in %s on line %d
  149. NULL
  150. Done