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 --
135COUNT_NORMAL: should be 0, is 0
136COUNT_RECURSIVE: should be 0, is 0
137-- Testing arrays --
138COUNT_NORMAL: should be 2, is 2
139COUNT_RECURSIVE: should be 8, is 8
140-- Testing hashes --
141COUNT_NORMAL: should be 3, is 3
142COUNT_RECURSIVE: should be 6, is 6
143-- Testing strings --
144COUNT_NORMAL: should be 1, is 1
145COUNT_RECURSIVE: should be 1, is 1
146-- Testing various types with no second argument --
147COUNT_NORMAL: should be 1, is 1
148COUNT_NORMAL: should be 2, is 2
149-- Testing really cool arrays --
150COUNT_NORMAL: should be 3, is 3
151COUNT_RECURSIVE: should be 13, is 13
152
153*** Testing possible variations of count() function on arrays ***
154-- Iteration 0 --
155COUNT_NORMAL is 0
156COUNT_RECURSIVE is 0
157
158-- Iteration 1 --
159COUNT_NORMAL is 1
160COUNT_RECURSIVE is 1
161
162-- Iteration 2 --
163COUNT_NORMAL is 4
164COUNT_RECURSIVE is 7
165
166-- Iteration 3 --
167COUNT_NORMAL is 2
168COUNT_RECURSIVE is 8
169
170-- Iteration 4 --
171COUNT_NORMAL is 4
172COUNT_RECURSIVE is 4
173
174-- Iteration 5 --
175COUNT_NORMAL is 5
176COUNT_RECURSIVE is 5
177
178-- Iteration 6 --
179COUNT_NORMAL is 6
180COUNT_RECURSIVE is 6
181
182-- Iteration 7 --
183COUNT_NORMAL is 4
184COUNT_RECURSIVE is 7
185
186-- Testing count() on constants with no second argument --
187COUNT_NORMAL: should be 1, is 1
188COUNT_NORMAL: should be 1, is 1
189
190-- Testing count() on NULL and Unset variables --
191COUNT_NORMAL: should be 0, is 0
192COUNT_NORMAL: should be 1, is 1
193COUNT_NORMAL: should be 0, is 0
194
195-- Testing count() on an empty sub-array --
196COUNT_NORMAL: should be 2, is 2
197COUNT_RECURSIVE: should be 5, is 5
198
199-- Testing count() on objects with Countable interface --
200COUNT_NORMAL: should be 3, is 3
201
202-- Testing count() on resource type --
203int(2)
204
205-- Testing count() on arrays containing references --
206For mode '0' count is => int(3)
207For mode '1' count is => int(9)
208For mode '0' count is => int(3)
209For mode '1' count is => int(9)
210For mode '-1' count is => int(3)
211For mode '-1.45' count is => int(3)
212For mode '2' count is => int(3)
213For mode '1' count is => int(9)
214For mode '' count is => int(3)
215For mode '' count is => int(3)
216
217-- Testing error conditions --
218Warning: count() expects at least 1 parameter, 0 given in %s on line %d
219NULL
220
221Warning: count() expects at most 2 parameters, 3 given in %s on line %d
222NULL
223
224Notice: Use of undefined constant ABCD - assumed 'ABCD' in %s on line %d
225
226Warning: count() expects parameter 2 to be long, %s given in %s on line %d
227NULL
228
229Warning: count() expects parameter 2 to be long, %s given in %s on line %d
230NULL
231
232Warning: count() expects parameter 2 to be long, %s given in %s on line %d
233NULL
234
235Done
236