1--TEST--
2Test count() function
3--FILE--
4<?php
5echo "*** Testing basic functionality of count() function ***\n";
6
7print "-- Testing arrays --\n";
8$arr = array(1, array(3, 4, array(6, array(8))));
9print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
10print "COUNT_RECURSIVE: should be 8, is ".count($arr, COUNT_RECURSIVE)."\n";
11
12print "-- Testing hashes --\n";
13$arr = array("a" => 1, "b" => 2, array("c" => 3, array("d" => 5)));
14print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
15print "COUNT_RECURSIVE: should be 6, is ".count($arr, COUNT_RECURSIVE)."\n";
16
17print "-- Testing various types with no second argument --\n";
18print "COUNT_NORMAL: should be 2, is ".count(array("a", array("b")))."\n";
19
20$arr = array('a'=>array(NULL, NULL, NULL), 1=>array(NULL=>1, 1=>NULL),
21    array(array(array(array(array(NULL))))));
22print "-- Testing really cool arrays --\n";
23print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
24print "COUNT_RECURSIVE: should be 13, is ".count($arr, COUNT_RECURSIVE)."\n";
25
26echo "\n*** Testing possible variations of count() function on arrays ***";
27$count_array = array(
28  array(),
29  array( 1 => "string"),
30  array( "" => "string", 0 => "a", NULL => "b", -1 => "c",
31         array(array(array(NULL)))),
32  array( -2 => 12, array(array(1, 2, array(array("0"))))),
33  array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
34  array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,
35         1 => -2.344, array()),
36  array( TRUE => TRUE, FALSE => FALSE, "" => "", " " => " ",
37     NULL => NULL, "\x000" => "\x000", "\000" => "\000"),
38  array( NULL, 1 => "Hi", "string" => "hello",
39         array("" => "World", "-2.34" => "a", "0" => "b"))
40);
41
42$i = 0;
43foreach ($count_array as $count_value) {
44  echo "\n-- Iteration $i --\n";
45  print "COUNT_NORMAL is ".count($count_value, COUNT_NORMAL)."\n";
46  print "COUNT_RECURSIVE is ".count($count_value, COUNT_RECURSIVE)."\n";
47  $i++;
48}
49
50print "\n-- Testing count() on an empty sub-array --\n";
51$arr = array(1, array(3, 4, array()));
52print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
53print "COUNT_RECURSIVE: should be 5, is ".count($arr, COUNT_RECURSIVE)."\n";
54
55echo "\n-- Testing count() on objects with Countable interface --\n";
56class count_class implements Countable {
57  private $var_private;
58  public $var_public;
59  protected $var_protected;
60
61  public function count(): int {
62    return 3;
63  }
64}
65
66$obj = new count_class();
67print "COUNT_NORMAL: should be 3, is ".count($obj)."\n";
68
69
70echo "\n-- Testing count() on resource type --\n";
71$resource1 = fopen( __FILE__, "r" );  // Creating file(stream type) resource
72$resource2 = opendir( "." );  // Creating dir resource
73
74/* creating an array with resources as elements */
75$arr_resource = array("a" => $resource1, "b" => $resource2);
76var_dump(count($arr_resource));
77
78echo "\n-- Testing count() on arrays containing references --\n";
79$arr = array(1, array("a", "b", "c"));
80$arr[2] = &$arr[1];
81
82echo "Count normal" . \PHP_EOL;
83var_dump(count($arr, COUNT_NORMAL));
84echo "Count recursive" . \PHP_EOL;
85var_dump(count($arr, COUNT_RECURSIVE));
86
87/* closing the resource handles */
88fclose( $resource1 );
89closedir( $resource2 );
90?>
91--EXPECT--
92*** Testing basic functionality of count() function ***
93-- Testing arrays --
94COUNT_NORMAL: should be 2, is 2
95COUNT_RECURSIVE: should be 8, is 8
96-- Testing hashes --
97COUNT_NORMAL: should be 3, is 3
98COUNT_RECURSIVE: should be 6, is 6
99-- Testing various types with no second argument --
100COUNT_NORMAL: should be 2, is 2
101-- Testing really cool arrays --
102COUNT_NORMAL: should be 3, is 3
103COUNT_RECURSIVE: should be 13, is 13
104
105*** Testing possible variations of count() function on arrays ***
106-- Iteration 0 --
107COUNT_NORMAL is 0
108COUNT_RECURSIVE is 0
109
110-- Iteration 1 --
111COUNT_NORMAL is 1
112COUNT_RECURSIVE is 1
113
114-- Iteration 2 --
115COUNT_NORMAL is 4
116COUNT_RECURSIVE is 7
117
118-- Iteration 3 --
119COUNT_NORMAL is 2
120COUNT_RECURSIVE is 8
121
122-- Iteration 4 --
123COUNT_NORMAL is 4
124COUNT_RECURSIVE is 4
125
126-- Iteration 5 --
127COUNT_NORMAL is 5
128COUNT_RECURSIVE is 5
129
130-- Iteration 6 --
131COUNT_NORMAL is 6
132COUNT_RECURSIVE is 6
133
134-- Iteration 7 --
135COUNT_NORMAL is 4
136COUNT_RECURSIVE is 7
137
138-- Testing count() on an empty sub-array --
139COUNT_NORMAL: should be 2, is 2
140COUNT_RECURSIVE: should be 5, is 5
141
142-- Testing count() on objects with Countable interface --
143COUNT_NORMAL: should be 3, is 3
144
145-- Testing count() on resource type --
146int(2)
147
148-- Testing count() on arrays containing references --
149Count normal
150int(3)
151Count recursive
152int(9)
153