Annotation of embedaddon/php/ext/standard/tests/array/count_recursive.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test 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:
11: echo "*** Testing basic functionality of count() function ***\n";
12: print "-- Testing NULL --\n";
13: $arr = NULL;
14: print "COUNT_NORMAL: should be 0, is ".count($arr, COUNT_NORMAL)."\n";
15: print "COUNT_RECURSIVE: should be 0, is ".count($arr, COUNT_RECURSIVE)."\n";
16:
17: print "-- Testing arrays --\n";
18: $arr = array(1, array(3, 4, array(6, array(8))));
19: print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
20: print "COUNT_RECURSIVE: should be 8, is ".count($arr, COUNT_RECURSIVE)."\n";
21:
22: print "-- Testing hashes --\n";
23: $arr = array("a" => 1, "b" => 2, array("c" => 3, array("d" => 5)));
24: print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
25: print "COUNT_RECURSIVE: should be 6, is ".count($arr, COUNT_RECURSIVE)."\n";
26:
27: print "-- Testing strings --\n";
28: print "COUNT_NORMAL: should be 1, is ".count("string", COUNT_NORMAL)."\n";
29: print "COUNT_RECURSIVE: should be 1, is ".count("string", COUNT_RECURSIVE)."\n";
30:
31: print "-- Testing various types with no second argument --\n";
32: print "COUNT_NORMAL: should be 1, is ".count("string")."\n";
33: print "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))))));
37: print "-- Testing really cool arrays --\n";
38: print "COUNT_NORMAL: should be 3, is ".count($arr, COUNT_NORMAL)."\n";
39: print "COUNT_RECURSIVE: should be 13, is ".count($arr, COUNT_RECURSIVE)."\n";
40:
41: echo "\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;
58: foreach ($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 */
67: print "\n-- Testing count() on constants with no second argument --\n";
68: print "COUNT_NORMAL: should be 1, is ".count(100)."\n";
69: print "COUNT_NORMAL: should be 1, is ".count(-23.45)."\n";
70:
71: print "\n-- Testing count() on NULL and Unset variables --\n";
72: print "COUNT_NORMAL: should be 0, is ".count(NULL)."\n";
73: print "COUNT_NORMAL: should be 1, is ".count("")."\n";
74: print "COUNT_NORMAL: should be 0, is ".@count($a)."\n";
75:
76:
77: print "\n-- Testing count() on an empty sub-array --\n";
78: $arr = array(1, array(3, 4, array()));
79: print "COUNT_NORMAL: should be 2, is ".count($arr, COUNT_NORMAL)."\n";
80: print "COUNT_RECURSIVE: should be 5, is ".count($arr, COUNT_RECURSIVE)."\n";
81:
82: echo "\n-- Testing count() on objects with Countable interface --\n";
83: class 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();
94: print "COUNT_NORMAL: should be 3, is ".count($obj)."\n";
95:
96:
97: echo "\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);
103: var_dump(count($arr_resource));
104:
105: echo "\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);
111: for( $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:
117: echo "\n-- Testing error conditions --";
118: var_dump( count() ); // No. of args = 0
119: var_dump( count(array(), COUNT_NORMAL, 100) ); // No. of args > expected
120:
121: /* Testing Invalid type arguments */
122: var_dump( count("string", ABCD) );
123: var_dump( count(100, "string") );
124: var_dump( count(array(), "") );
125:
126: echo "\nDone";
127:
128: /* closing the resource handles */
129: fclose( $resource1 );
130: closedir( $resource2 );
131: ?>
132: --EXPECTF--
133: *** Testing basic functionality of count() function ***
134: -- Testing NULL --
135: COUNT_NORMAL: should be 0, is 0
136: COUNT_RECURSIVE: should be 0, is 0
137: -- Testing arrays --
138: COUNT_NORMAL: should be 2, is 2
139: COUNT_RECURSIVE: should be 8, is 8
140: -- Testing hashes --
141: COUNT_NORMAL: should be 3, is 3
142: COUNT_RECURSIVE: should be 6, is 6
143: -- Testing strings --
144: COUNT_NORMAL: should be 1, is 1
145: COUNT_RECURSIVE: should be 1, is 1
146: -- Testing various types with no second argument --
147: COUNT_NORMAL: should be 1, is 1
148: COUNT_NORMAL: should be 2, is 2
149: -- Testing really cool arrays --
150: COUNT_NORMAL: should be 3, is 3
151: COUNT_RECURSIVE: should be 13, is 13
152:
153: *** Testing possible variations of count() function on arrays ***
154: -- Iteration 0 --
155: COUNT_NORMAL is 0
156: COUNT_RECURSIVE is 0
157:
158: -- Iteration 1 --
159: COUNT_NORMAL is 1
160: COUNT_RECURSIVE is 1
161:
162: -- Iteration 2 --
163: COUNT_NORMAL is 4
164: COUNT_RECURSIVE is 7
165:
166: -- Iteration 3 --
167: COUNT_NORMAL is 2
168: COUNT_RECURSIVE is 8
169:
170: -- Iteration 4 --
171: COUNT_NORMAL is 4
172: COUNT_RECURSIVE is 4
173:
174: -- Iteration 5 --
175: COUNT_NORMAL is 5
176: COUNT_RECURSIVE is 5
177:
178: -- Iteration 6 --
179: COUNT_NORMAL is 6
180: COUNT_RECURSIVE is 6
181:
182: -- Iteration 7 --
183: COUNT_NORMAL is 4
184: COUNT_RECURSIVE is 7
185:
186: -- Testing count() on constants with no second argument --
187: COUNT_NORMAL: should be 1, is 1
188: COUNT_NORMAL: should be 1, is 1
189:
190: -- Testing count() on NULL and Unset variables --
191: COUNT_NORMAL: should be 0, is 0
192: COUNT_NORMAL: should be 1, is 1
193: COUNT_NORMAL: should be 0, is 0
194:
195: -- Testing count() on an empty sub-array --
196: COUNT_NORMAL: should be 2, is 2
197: COUNT_RECURSIVE: should be 5, is 5
198:
199: -- Testing count() on objects with Countable interface --
200: COUNT_NORMAL: should be 3, is 3
201:
202: -- Testing count() on resource type --
203: int(2)
204:
205: -- Testing count() on arrays containing references --
206: For mode '0' count is => int(3)
207: For mode '1' count is => int(9)
208: For mode '0' count is => int(3)
209: For mode '1' count is => int(9)
210: For mode '-1' count is => int(3)
211: For mode '-1.45' count is => int(3)
212: For mode '2' count is => int(3)
213: For mode '1' count is => int(9)
214: For mode '' count is => int(3)
215: For mode '' count is => int(3)
216:
217: -- Testing error conditions --
218: Warning: count() expects at least 1 parameter, 0 given in %s on line %d
219: NULL
220:
221: Warning: count() expects at most 2 parameters, 3 given in %s on line %d
222: NULL
223:
224: Notice: Use of undefined constant ABCD - assumed 'ABCD' in %s on line %d
225:
226: Warning: count() expects parameter 2 to be long, %s given in %s on line %d
227: NULL
228:
229: Warning: count() expects parameter 2 to be long, %s given in %s on line %d
230: NULL
231:
232: Warning: count() expects parameter 2 to be long, %s given in %s on line %d
233: NULL
234:
235: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>