count_recursive.phpt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. --TEST--
  2. Test count() function
  3. --FILE--
  4. <?php
  5. /* Prototype: int count ( mixed $var [, int $mode] );
  6. Discription: Count elements in an array, or properties in an object
  7. */
  8. echo "*** Testing basic functionality of count() function ***\n";
  9. print "-- Testing NULL --\n";
  10. $arr = NULL;
  11. print "COUNT_NORMAL: should be 0, is ".count($arr, COUNT_NORMAL)."\n";
  12. print "COUNT_RECURSIVE: should be 0, is ".count($arr, COUNT_RECURSIVE)."\n";
  13. print "-- Testing arrays --\n";
  14. $arr = array(1, array(3, 4, array(6, array(8))));
  15. print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
  16. print "COUNT_RECURSIVE: should be 8, is ".count($arr, COUNT_RECURSIVE)."\n";
  17. print "-- Testing hashes --\n";
  18. $arr = array("a" => 1, "b" => 2, array("c" => 3, array("d" => 5)));
  19. print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
  20. print "COUNT_RECURSIVE: should be 6, is ".count($arr, COUNT_RECURSIVE)."\n";
  21. print "-- Testing strings --\n";
  22. print "COUNT_NORMAL: should be 1, is ".count("string", COUNT_NORMAL)."\n";
  23. print "COUNT_RECURSIVE: should be 1, is ".count("string", COUNT_RECURSIVE)."\n";
  24. print "-- Testing various types with no second argument --\n";
  25. print "COUNT_NORMAL: should be 1, is ".count("string")."\n";
  26. print "COUNT_NORMAL: should be 2, is ".count(array("a", array("b")))."\n";
  27. $arr = array('a'=>array(NULL, NULL, NULL), 1=>array(NULL=>1, 1=>NULL),
  28. array(array(array(array(array(NULL))))));
  29. print "-- Testing really cool arrays --\n";
  30. print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
  31. print "COUNT_RECURSIVE: should be 13, is ".count($arr, COUNT_RECURSIVE)."\n";
  32. echo "\n*** Testing possible variations of count() function on arrays ***";
  33. $count_array = array(
  34. array(),
  35. array( 1 => "string"),
  36. array( "" => "string", 0 => "a", NULL => "b", -1.00 => "c",
  37. array(array(array(NULL)))),
  38. array( -2.44444 => 12, array(array(1, 2, array(array("0"))))),
  39. array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
  40. array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,
  41. 1 => -2.344, array()),
  42. array( TRUE => TRUE, FALSE => FALSE, "" => "", " " => " ",
  43. NULL => NULL, "\x000" => "\x000", "\000" => "\000"),
  44. array( NULL, 1.23 => "Hi", "string" => "hello",
  45. array("" => "World", "-2.34" => "a", "0" => "b"))
  46. );
  47. $i = 0;
  48. foreach ($count_array as $count_value) {
  49. echo "\n-- Iteration $i --\n";
  50. print "COUNT_NORMAL is ".count($count_value, COUNT_NORMAL)."\n";
  51. print "COUNT_RECURSIVE is ".count($count_value, COUNT_RECURSIVE)."\n";
  52. $i++;
  53. }
  54. /* Testing count() by passing constant with no second argument */
  55. print "\n-- Testing count() on constants with no second argument --\n";
  56. print "COUNT_NORMAL: should be 1, is ".count(100)."\n";
  57. print "COUNT_NORMAL: should be 1, is ".count(-23.45)."\n";
  58. print "\n-- Testing count() on NULL and Unset variables --\n";
  59. print "COUNT_NORMAL: should be 0, is ".count(NULL)."\n";
  60. print "COUNT_NORMAL: should be 1, is ".count("")."\n";
  61. print "COUNT_NORMAL: should be 0, is ".@count($a)."\n";
  62. print "\n-- Testing count() on an empty sub-array --\n";
  63. $arr = array(1, array(3, 4, array()));
  64. print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
  65. print "COUNT_RECURSIVE: should be 5, is ".count($arr, COUNT_RECURSIVE)."\n";
  66. echo "\n-- Testing count() on objects with Countable interface --\n";
  67. class count_class implements Countable {
  68. private $var_private;
  69. public $var_public;
  70. protected $var_protected;
  71. public function count() {
  72. return 3;
  73. }
  74. }
  75. $obj = new count_class();
  76. print "COUNT_NORMAL: should be 3, is ".count($obj)."\n";
  77. echo "\n-- Testing count() on resource type --\n";
  78. $resource1 = fopen( __FILE__, "r" ); // Creating file(stream type) resource
  79. $resource2 = opendir( "." ); // Creating dir resource
  80. /* creating an array with resources as elements */
  81. $arr_resource = array("a" => $resource1, "b" => $resource2);
  82. var_dump(count($arr_resource));
  83. echo "\n-- Testing count() on arrays containing references --\n";
  84. $arr = array(1, array("a", "b", "c"));
  85. $arr[2] = &$arr[1];
  86. $mode_arr = array( COUNT_NORMAL, COUNT_RECURSIVE, 0, 1, -1, -1.45, 2, TRUE,
  87. FALSE, NULL);
  88. for( $i =0; $i < count( $mode_arr ); $i++) {
  89. echo "For mode '$mode_arr[$i]' count is => ";
  90. var_dump(count($arr, $mode_arr[$i]));
  91. }
  92. echo "\n-- Testing error conditions --";
  93. var_dump( count() ); // No. of args = 0
  94. var_dump( count(array(), COUNT_NORMAL, 100) ); // No. of args > expected
  95. /* Testing Invalid type arguments */
  96. var_dump( count("string", ABCD) );
  97. var_dump( count(100, "string") );
  98. var_dump( count(array(), "") );
  99. echo "\nDone";
  100. /* closing the resource handles */
  101. fclose( $resource1 );
  102. closedir( $resource2 );
  103. ?>
  104. --EXPECTF--
  105. *** Testing basic functionality of count() function ***
  106. -- Testing NULL --
  107. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  108. COUNT_NORMAL: should be 0, is 0
  109. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  110. COUNT_RECURSIVE: should be 0, is 0
  111. -- Testing arrays --
  112. COUNT_NORMAL: should be 2, is 2
  113. COUNT_RECURSIVE: should be 8, is 8
  114. -- Testing hashes --
  115. COUNT_NORMAL: should be 3, is 3
  116. COUNT_RECURSIVE: should be 6, is 6
  117. -- Testing strings --
  118. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  119. COUNT_NORMAL: should be 1, is 1
  120. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  121. COUNT_RECURSIVE: should be 1, is 1
  122. -- Testing various types with no second argument --
  123. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  124. COUNT_NORMAL: should be 1, is 1
  125. COUNT_NORMAL: should be 2, is 2
  126. -- Testing really cool arrays --
  127. COUNT_NORMAL: should be 3, is 3
  128. COUNT_RECURSIVE: should be 13, is 13
  129. *** Testing possible variations of count() function on arrays ***
  130. -- Iteration 0 --
  131. COUNT_NORMAL is 0
  132. COUNT_RECURSIVE is 0
  133. -- Iteration 1 --
  134. COUNT_NORMAL is 1
  135. COUNT_RECURSIVE is 1
  136. -- Iteration 2 --
  137. COUNT_NORMAL is 4
  138. COUNT_RECURSIVE is 7
  139. -- Iteration 3 --
  140. COUNT_NORMAL is 2
  141. COUNT_RECURSIVE is 8
  142. -- Iteration 4 --
  143. COUNT_NORMAL is 4
  144. COUNT_RECURSIVE is 4
  145. -- Iteration 5 --
  146. COUNT_NORMAL is 5
  147. COUNT_RECURSIVE is 5
  148. -- Iteration 6 --
  149. COUNT_NORMAL is 6
  150. COUNT_RECURSIVE is 6
  151. -- Iteration 7 --
  152. COUNT_NORMAL is 4
  153. COUNT_RECURSIVE is 7
  154. -- Testing count() on constants with no second argument --
  155. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  156. COUNT_NORMAL: should be 1, is 1
  157. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  158. COUNT_NORMAL: should be 1, is 1
  159. -- Testing count() on NULL and Unset variables --
  160. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  161. COUNT_NORMAL: should be 0, is 0
  162. Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
  163. COUNT_NORMAL: should be 1, is 1
  164. COUNT_NORMAL: should be 0, is 0
  165. -- Testing count() on an empty sub-array --
  166. COUNT_NORMAL: should be 2, is 2
  167. COUNT_RECURSIVE: should be 5, is 5
  168. -- Testing count() on objects with Countable interface --
  169. COUNT_NORMAL: should be 3, is 3
  170. -- Testing count() on resource type --
  171. int(2)
  172. -- Testing count() on arrays containing references --
  173. For mode '0' count is => int(3)
  174. For mode '1' count is => int(9)
  175. For mode '0' count is => int(3)
  176. For mode '1' count is => int(9)
  177. For mode '-1' count is => int(3)
  178. For mode '-1.45' count is => int(3)
  179. For mode '2' count is => int(3)
  180. For mode '1' count is => int(9)
  181. For mode '' count is => int(3)
  182. For mode '' count is => int(3)
  183. -- Testing error conditions --
  184. Warning: count() expects at least 1 parameter, 0 given in %s on line %d
  185. NULL
  186. Warning: count() expects at most 2 parameters, 3 given in %s on line %d
  187. NULL
  188. Warning: Use of undefined constant ABCD - assumed 'ABCD' (this will throw an Error in a future version of PHP) in %s on line %d
  189. Warning: count() expects parameter 2 to be int, %s given in %s on line %d
  190. NULL
  191. Warning: count() expects parameter 2 to be int, %s given in %s on line %d
  192. NULL
  193. Warning: count() expects parameter 2 to be int, %s given in %s on line %d
  194. NULL
  195. Done