array_fill_variation2.phpt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. --TEST--
  2. Test array_fill() function : usage variations - unexpected values for 'num' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : proto array array_fill(int start_key, int num, mixed val)
  6. * Description: Create an array containing num elements starting with index start_key each initialized to val
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * testing array_fill() by passing different unexpected values for 'num' argument
  11. */
  12. echo "*** Testing array_fill() : usage variations ***\n";
  13. // Initialise function arguments not being substituted
  14. $start_key = 0;
  15. $val = 100;
  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 "testObject";
  26. }
  27. }
  28. //array of different values for 'num' argument
  29. $values = array(
  30. // float values
  31. /* 1 */ 2.5,
  32. -2.5,
  33. 0.5e1,
  34. 0.5E-1,
  35. .5,
  36. // array values
  37. /* 6 */ array(),
  38. array(0),
  39. array(1),
  40. array(1, 2),
  41. array('color' => 'red', 'item' => 'pen'),
  42. // null values
  43. /* 11 */ NULL,
  44. null,
  45. // boolean values
  46. /* 13 */ true,
  47. false,
  48. TRUE,
  49. FALSE,
  50. // empty string
  51. /* 17 */ "",
  52. '',
  53. // string values
  54. /* 19 */ "string",
  55. 'string',
  56. // objects
  57. /* 21 */ new test(),
  58. // undefined variable
  59. @$undefined_var,
  60. // unset variable
  61. /* 24 */ @$unset_var,
  62. );
  63. // loop through each element of the array for num
  64. // check the working of array_fill
  65. echo "--- Testing array_fill() with different values for 'num' arg ---\n";
  66. $counter = 1;
  67. for($index = 0; $index < count($values); $index ++)
  68. {
  69. echo "-- Iteration $counter --\n";
  70. $num = $values[$index];
  71. var_dump( array_fill($start_key,$num,$val) );
  72. $counter ++;
  73. }
  74. echo "Done";
  75. ?>
  76. --EXPECTF--
  77. *** Testing array_fill() : usage variations ***
  78. --- Testing array_fill() with different values for 'num' arg ---
  79. -- Iteration 1 --
  80. array(2) {
  81. [0]=>
  82. int(100)
  83. [1]=>
  84. int(100)
  85. }
  86. -- Iteration 2 --
  87. Warning: array_fill(): Number of elements can't be negative in %s on line %d
  88. bool(false)
  89. -- Iteration 3 --
  90. array(5) {
  91. [0]=>
  92. int(100)
  93. [1]=>
  94. int(100)
  95. [2]=>
  96. int(100)
  97. [3]=>
  98. int(100)
  99. [4]=>
  100. int(100)
  101. }
  102. -- Iteration 4 --
  103. array(0) {
  104. }
  105. -- Iteration 5 --
  106. array(0) {
  107. }
  108. -- Iteration 6 --
  109. Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
  110. NULL
  111. -- Iteration 7 --
  112. Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
  113. NULL
  114. -- Iteration 8 --
  115. Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
  116. NULL
  117. -- Iteration 9 --
  118. Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
  119. NULL
  120. -- Iteration 10 --
  121. Warning: array_fill() expects parameter 2 to be long, array given in %s on line %d
  122. NULL
  123. -- Iteration 11 --
  124. array(0) {
  125. }
  126. -- Iteration 12 --
  127. array(0) {
  128. }
  129. -- Iteration 13 --
  130. array(1) {
  131. [0]=>
  132. int(100)
  133. }
  134. -- Iteration 14 --
  135. array(0) {
  136. }
  137. -- Iteration 15 --
  138. array(1) {
  139. [0]=>
  140. int(100)
  141. }
  142. -- Iteration 16 --
  143. array(0) {
  144. }
  145. -- Iteration 17 --
  146. Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
  147. NULL
  148. -- Iteration 18 --
  149. Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
  150. NULL
  151. -- Iteration 19 --
  152. Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
  153. NULL
  154. -- Iteration 20 --
  155. Warning: array_fill() expects parameter 2 to be long, string given in %s on line %d
  156. NULL
  157. -- Iteration 21 --
  158. Warning: array_fill() expects parameter 2 to be long, object given in %s on line %d
  159. NULL
  160. -- Iteration 22 --
  161. array(0) {
  162. }
  163. -- Iteration 23 --
  164. array(0) {
  165. }
  166. Done