PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/hphp/test/zend/bad/ext/standard/tests/array/count_recursive.php

https://gitlab.com/iranjith4/hhvm
PHP | 126 lines | 93 code | 26 blank | 7 comment | 1 complexity | 839587680a8e40fd214850265ac9ef38 MD5 | raw file
  1. <?php
  2. /* Prototype: int count ( mixed $var [, int $mode] );
  3. Discription: Count elements in an array, or properties in an object
  4. */
  5. echo "*** Testing basic functionality of count() function ***\n";
  6. print "-- Testing NULL --\n";
  7. $arr = NULL;
  8. print "COUNT_NORMAL: should be 0, is ".count($arr, COUNT_NORMAL)."\n";
  9. print "COUNT_RECURSIVE: should be 0, is ".count($arr, COUNT_RECURSIVE)."\n";
  10. print "-- Testing arrays --\n";
  11. $arr = array(1, array(3, 4, array(6, array(8))));
  12. print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
  13. print "COUNT_RECURSIVE: should be 8, is ".count($arr, COUNT_RECURSIVE)."\n";
  14. print "-- Testing hashes --\n";
  15. $arr = array("a" => 1, "b" => 2, array("c" => 3, array("d" => 5)));
  16. print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
  17. print "COUNT_RECURSIVE: should be 6, is ".count($arr, COUNT_RECURSIVE)."\n";
  18. print "-- Testing strings --\n";
  19. print "COUNT_NORMAL: should be 1, is ".count("string", COUNT_NORMAL)."\n";
  20. print "COUNT_RECURSIVE: should be 1, is ".count("string", COUNT_RECURSIVE)."\n";
  21. print "-- Testing various types with no second argument --\n";
  22. print "COUNT_NORMAL: should be 1, is ".count("string")."\n";
  23. print "COUNT_NORMAL: should be 2, is ".count(array("a", array("b")))."\n";
  24. $arr = array('a'=>array(NULL, NULL, NULL), 1=>array(NULL=>1, 1=>NULL),
  25. array(array(array(array(array(NULL))))));
  26. print "-- Testing really cool arrays --\n";
  27. print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
  28. print "COUNT_RECURSIVE: should be 13, is ".count($arr, COUNT_RECURSIVE)."\n";
  29. echo "\n*** Testing possible variations of count() function on arrays ***";
  30. $count_array = array(
  31. array(),
  32. array( 1 => "string"),
  33. array( "" => "string", 0 => "a", NULL => "b", -1.00 => "c",
  34. array(array(array(NULL)))),
  35. array( -2.44444 => 12, array(array(1, 2, array(array("0"))))),
  36. array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
  37. array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,
  38. 1 => -2.344, array()),
  39. array( TRUE => TRUE, FALSE => FALSE, "" => "", " " => " ",
  40. NULL => NULL, "\x000" => "\x000", "\000" => "\000"),
  41. array( NULL, 1.23 => "Hi", "string" => "hello",
  42. array("" => "World", "-2.34" => "a", "0" => "b"))
  43. );
  44. $i = 0;
  45. foreach ($count_array as $count_value) {
  46. echo "\n-- Iteration $i --\n";
  47. print "COUNT_NORMAL is ".count($count_value, COUNT_NORMAL)."\n";
  48. print "COUNT_RECURSIVE is ".count($count_value, COUNT_RECURSIVE)."\n";
  49. $i++;
  50. }
  51. /* Testing count() by passing constant with no second argument */
  52. print "\n-- Testing count() on constants with no second argument --\n";
  53. print "COUNT_NORMAL: should be 1, is ".count(100)."\n";
  54. print "COUNT_NORMAL: should be 1, is ".count(-23.45)."\n";
  55. print "\n-- Testing count() on NULL and Unset variables --\n";
  56. print "COUNT_NORMAL: should be 0, is ".count(NULL)."\n";
  57. print "COUNT_NORMAL: should be 1, is ".count("")."\n";
  58. print "COUNT_NORMAL: should be 0, is ".@count($a)."\n";
  59. print "\n-- Testing count() on an empty sub-array --\n";
  60. $arr = array(1, array(3, 4, array()));
  61. print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
  62. print "COUNT_RECURSIVE: should be 5, is ".count($arr, COUNT_RECURSIVE)."\n";
  63. echo "\n-- Testing count() on objects with Countable interface --\n";
  64. class count_class implements Countable {
  65. private $var_private;
  66. public $var_public;
  67. protected $var_protected;
  68. public function count() {
  69. return 3;
  70. }
  71. }
  72. $obj = new count_class();
  73. print "COUNT_NORMAL: should be 3, is ".count($obj)."\n";
  74. echo "\n-- Testing count() on resource type --\n";
  75. $resource1 = fopen( __FILE__, "r" ); // Creating file(stream type) resource
  76. $resource2 = opendir( "." ); // Creating dir resource
  77. /* creating an array with resources as elements */
  78. $arr_resource = array("a" => $resource1, "b" => $resource2);
  79. var_dump(count($arr_resource));
  80. echo "\n-- Testing count() on arrays containing references --\n";
  81. $arr = array(1, array("a", "b", "c"));
  82. $arr[2] = &$arr[1];
  83. $mode_arr = array( COUNT_NORMAL, COUNT_RECURSIVE, 0, 1, -1, -1.45, 2, TRUE,
  84. FALSE, NULL);
  85. for( $i =0; $i < count( $mode_arr ); $i++) {
  86. echo "For mode '$mode_arr[$i]' count is => ";
  87. var_dump(count($arr, $mode_arr[$i]));
  88. }
  89. echo "\n-- Testing error conditions --";
  90. var_dump( count() ); // No. of args = 0
  91. var_dump( count(array(), COUNT_NORMAL, 100) ); // No. of args > expected
  92. /* Testing Invalid type arguments */
  93. var_dump( count("string", ABCD) );
  94. var_dump( count(100, "string") );
  95. var_dump( count(array(), "") );
  96. echo "\nDone";
  97. /* closing the resource handles */
  98. fclose( $resource1 );
  99. closedir( $resource2 );
  100. ?>