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