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