Skip to content

Commit d08088d

Browse files
author
Raghubansh Kumar
committed
New testcases for array_sum() function
1 parent 32da061 commit d08088d

9 files changed

+715
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Test array_sum() function : basic functionality
3+
--FILE--
4+
<?php
5+
/* Prototype : mixed array_sum(array &input)
6+
* Description: Returns the sum of the array entries
7+
* Source code: ext/standard/array.c
8+
*/
9+
10+
echo "*** Testing array_sum() : basic functionality ***\n";
11+
12+
// array with integer values
13+
$input = array(1, 2, 3, 4, 5);
14+
echo "-- array_sum() with integer array entries --\n";
15+
var_dump( array_sum($input) );
16+
17+
// array with float values
18+
$input = array(1.0, 2.2, 3.4, 4.6);
19+
echo "-- array_sum() with float array entries --\n";
20+
var_dump( array_sum($input) );
21+
22+
// array with integer and float values
23+
$input = array(1, 2.3, 4, 0.6, 10);
24+
echo "-- array_sum() with integer/float array entries --\n";
25+
var_dump( array_sum($input) );
26+
27+
echo "Done"
28+
?>
29+
--EXPECTF--
30+
*** Testing array_sum() : basic functionality ***
31+
-- array_sum() with integer array entries --
32+
int(15)
33+
-- array_sum() with float array entries --
34+
float(11.2)
35+
-- array_sum() with integer/float array entries --
36+
float(17.9)
37+
Done
38+
--UEXPECTF--
39+
*** Testing array_sum() : basic functionality ***
40+
-- array_sum() with integer array entries --
41+
int(15)
42+
-- array_sum() with float array entries --
43+
float(11.2)
44+
-- array_sum() with integer/float array entries --
45+
float(17.9)
46+
Done
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
Test array_sum() function : error conditions
3+
--FILE--
4+
<?php
5+
/* Prototype : mixed array_sum(array &input)
6+
* Description: Returns the sum of the array entries
7+
* Source code: ext/standard/array.c
8+
*/
9+
10+
echo "*** Testing array_sum() : error conditions ***\n";
11+
12+
// Zero arguments
13+
echo "-- Testing array_sum() function with zero arguments --\n";
14+
var_dump( array_sum() );
15+
16+
// One more than the expected number of arguments
17+
echo "-- Testing array_sum() function with more than expected no. of arguments --\n";
18+
$input = array(1, 2, 3, 4);
19+
$extra_arg = 10;
20+
var_dump( array_sum($input, $extra_arg) );
21+
22+
echo "Done"
23+
?>
24+
--EXPECTF--
25+
*** Testing array_sum() : error conditions ***
26+
-- Testing array_sum() function with zero arguments --
27+
28+
Warning: array_sum() expects exactly 1 parameter, 0 given in %s on line %d
29+
NULL
30+
-- Testing array_sum() function with more than expected no. of arguments --
31+
32+
Warning: array_sum() expects exactly 1 parameter, 2 given in %s on line %d
33+
NULL
34+
Done
35+
--UEXPECTF--
36+
*** Testing array_sum() : error conditions ***
37+
-- Testing array_sum() function with zero arguments --
38+
39+
Warning: array_sum() expects exactly 1 parameter, 0 given in %s on line %d
40+
NULL
41+
-- Testing array_sum() function with more than expected no. of arguments --
42+
43+
Warning: array_sum() expects exactly 1 parameter, 2 given in %s on line %d
44+
NULL
45+
Done
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
--TEST--
2+
Test array_sum() function : usage variations - unexpected values for 'input' argument
3+
--FILE--
4+
<?php
5+
/* Prototype : mixed array_sum(array $input)
6+
* Description: Returns the sum of the array entries
7+
* Source code: ext/standard/array.c
8+
*/
9+
10+
/*
11+
* Passing different scalar/nonscalar values as 'input' argument to array_sum()
12+
*/
13+
14+
echo "*** Testing array_sum() : unexpected values for 'input' ***\n";
15+
16+
// get an unset variable
17+
$unset_var = 10;
18+
unset ($unset_var);
19+
20+
// Class definition
21+
class MyClass
22+
{
23+
public function __toString()
24+
{
25+
return "object";
26+
}
27+
}
28+
29+
// different scalar/non scalar values for 'input' argument
30+
$input_values = array(
31+
32+
// int data
33+
/* 1 */ 0,
34+
1,
35+
12345,
36+
/* 4 */ -2345,
37+
38+
// float data
39+
/* 5 */ 10.5,
40+
-10.5,
41+
10.1234567e8,
42+
10.7654321E-8,
43+
/* 9 */ .5,
44+
45+
// null data
46+
/* 10*/ NULL,
47+
null,
48+
49+
// boolean data
50+
/* 12*/ true,
51+
false,
52+
TRUE,
53+
FALSE,
54+
55+
// empty data
56+
/* 16*/ "",
57+
'',
58+
59+
// string data
60+
/* 18*/ "string",
61+
'string',
62+
63+
// object data
64+
/* 20*/ new MyClass(),
65+
66+
// resource data
67+
$fp = fopen(__FILE__,'r'),
68+
69+
// undefined data
70+
@$undefined_var,
71+
72+
// unset data
73+
/* 23*/ @$unset_var,
74+
);
75+
76+
// loop through each element of the array for input
77+
for($count = 0; $count < count($input_values); $count++) {
78+
echo "-- Iteration ".($count + 1)." --\n";
79+
var_dump( array_sum($input_values[$count]) );
80+
};
81+
82+
fclose($fp);
83+
echo "Done"
84+
?>
85+
--EXPECTF--
86+
*** Testing array_sum() : unexpected values for 'input' ***
87+
-- Iteration 1 --
88+
89+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
90+
NULL
91+
-- Iteration 2 --
92+
93+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
94+
NULL
95+
-- Iteration 3 --
96+
97+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
98+
NULL
99+
-- Iteration 4 --
100+
101+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
102+
NULL
103+
-- Iteration 5 --
104+
105+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
106+
NULL
107+
-- Iteration 6 --
108+
109+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
110+
NULL
111+
-- Iteration 7 --
112+
113+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
114+
NULL
115+
-- Iteration 8 --
116+
117+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
118+
NULL
119+
-- Iteration 9 --
120+
121+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
122+
NULL
123+
-- Iteration 10 --
124+
125+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
126+
NULL
127+
-- Iteration 11 --
128+
129+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
130+
NULL
131+
-- Iteration 12 --
132+
133+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
134+
NULL
135+
-- Iteration 13 --
136+
137+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
138+
NULL
139+
-- Iteration 14 --
140+
141+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
142+
NULL
143+
-- Iteration 15 --
144+
145+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
146+
NULL
147+
-- Iteration 16 --
148+
149+
Warning: array_sum() expects parameter 1 to be array, string given in %s on line %d
150+
NULL
151+
-- Iteration 17 --
152+
153+
Warning: array_sum() expects parameter 1 to be array, string given in %s on line %d
154+
NULL
155+
-- Iteration 18 --
156+
157+
Warning: array_sum() expects parameter 1 to be array, string given in %s on line %d
158+
NULL
159+
-- Iteration 19 --
160+
161+
Warning: array_sum() expects parameter 1 to be array, string given in %s on line %d
162+
NULL
163+
-- Iteration 20 --
164+
165+
Warning: array_sum() expects parameter 1 to be array, object given in %s on line %d
166+
NULL
167+
-- Iteration 21 --
168+
169+
Warning: array_sum() expects parameter 1 to be array, resource given in %s on line %d
170+
NULL
171+
-- Iteration 22 --
172+
173+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
174+
NULL
175+
-- Iteration 23 --
176+
177+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
178+
NULL
179+
Done
180+
--UEXPECTF--
181+
*** Testing array_sum() : unexpected values for 'input' ***
182+
-- Iteration 1 --
183+
184+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
185+
NULL
186+
-- Iteration 2 --
187+
188+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
189+
NULL
190+
-- Iteration 3 --
191+
192+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
193+
NULL
194+
-- Iteration 4 --
195+
196+
Warning: array_sum() expects parameter 1 to be array, integer given in %s on line %d
197+
NULL
198+
-- Iteration 5 --
199+
200+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
201+
NULL
202+
-- Iteration 6 --
203+
204+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
205+
NULL
206+
-- Iteration 7 --
207+
208+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
209+
NULL
210+
-- Iteration 8 --
211+
212+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
213+
NULL
214+
-- Iteration 9 --
215+
216+
Warning: array_sum() expects parameter 1 to be array, double given in %s on line %d
217+
NULL
218+
-- Iteration 10 --
219+
220+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
221+
NULL
222+
-- Iteration 11 --
223+
224+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
225+
NULL
226+
-- Iteration 12 --
227+
228+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
229+
NULL
230+
-- Iteration 13 --
231+
232+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
233+
NULL
234+
-- Iteration 14 --
235+
236+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
237+
NULL
238+
-- Iteration 15 --
239+
240+
Warning: array_sum() expects parameter 1 to be array, boolean given in %s on line %d
241+
NULL
242+
-- Iteration 16 --
243+
244+
Warning: array_sum() expects parameter 1 to be array, Unicode string given in %s on line %d
245+
NULL
246+
-- Iteration 17 --
247+
248+
Warning: array_sum() expects parameter 1 to be array, Unicode string given in %s on line %d
249+
NULL
250+
-- Iteration 18 --
251+
252+
Warning: array_sum() expects parameter 1 to be array, Unicode string given in %s on line %d
253+
NULL
254+
-- Iteration 19 --
255+
256+
Warning: array_sum() expects parameter 1 to be array, Unicode string given in %s on line %d
257+
NULL
258+
-- Iteration 20 --
259+
260+
Warning: array_sum() expects parameter 1 to be array, object given in %s on line %d
261+
NULL
262+
-- Iteration 21 --
263+
264+
Warning: array_sum() expects parameter 1 to be array, resource given in %s on line %d
265+
NULL
266+
-- Iteration 22 --
267+
268+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
269+
NULL
270+
-- Iteration 23 --
271+
272+
Warning: array_sum() expects parameter 1 to be array, null given in %s on line %d
273+
NULL
274+
Done

0 commit comments

Comments
 (0)