]> granicus.if.org Git - php/commitdiff
New testcases for array_sum() function
authorRaghubansh Kumar <kraghuba@php.net>
Tue, 27 Nov 2007 15:00:55 +0000 (15:00 +0000)
committerRaghubansh Kumar <kraghuba@php.net>
Tue, 27 Nov 2007 15:00:55 +0000 (15:00 +0000)
ext/standard/tests/array/array_sum_basic.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_error.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation1.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation2.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation3.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation4.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation5.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation6.phpt [new file with mode: 0644]
ext/standard/tests/array/array_sum_variation7.phpt [new file with mode: 0644]

diff --git a/ext/standard/tests/array/array_sum_basic.phpt b/ext/standard/tests/array/array_sum_basic.phpt
new file mode 100644 (file)
index 0000000..c178853
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+Test array_sum() function : basic functionality 
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array &input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+echo "*** Testing array_sum() : basic functionality ***\n";
+
+// array with integer values
+$input = array(1, 2, 3, 4, 5);
+echo "-- array_sum() with integer array entries --\n";
+var_dump( array_sum($input) );
+
+// array with float values
+$input = array(1.0, 2.2, 3.4, 4.6);
+echo "-- array_sum() with float array entries --\n";
+var_dump( array_sum($input) );
+
+// array with integer and float values
+$input = array(1, 2.3, 4, 0.6, 10);
+echo "-- array_sum() with integer/float array entries --\n";
+var_dump( array_sum($input) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : basic functionality ***
+-- array_sum() with integer array entries --
+int(15)
+-- array_sum() with float array entries --
+float(11.2)
+-- array_sum() with integer/float array entries --
+float(17.9)
+Done
diff --git a/ext/standard/tests/array/array_sum_error.phpt b/ext/standard/tests/array/array_sum_error.phpt
new file mode 100644 (file)
index 0000000..ffa7e5b
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Test array_sum() function : error conditions 
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array &input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+echo "*** Testing array_sum() : error conditions ***\n";
+
+// Zero arguments
+echo "-- Testing array_sum() function with zero arguments --\n";
+var_dump( array_sum() );
+
+// One more than the expected number of arguments
+echo "-- Testing array_sum() function with more than expected no. of arguments --\n";
+$input = array(1, 2, 3, 4);
+$extra_arg = 10;
+var_dump( array_sum($input, $extra_arg) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : error conditions ***
+-- Testing array_sum() function with zero arguments --
+
+Warning: Wrong parameter count for array_sum() in %s on line %d
+NULL
+-- Testing array_sum() function with more than expected no. of arguments --
+
+Warning: Wrong parameter count for array_sum() in %s on line %d
+NULL
+Done
diff --git a/ext/standard/tests/array/array_sum_variation1.phpt b/ext/standard/tests/array/array_sum_variation1.phpt
new file mode 100644 (file)
index 0000000..df62386
--- /dev/null
@@ -0,0 +1,179 @@
+--TEST--
+Test array_sum() function : usage variations - unexpected values for 'input' argument
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Passing different scalar/nonscalar values as 'input' argument to array_sum() 
+*/
+
+echo "*** Testing array_sum() : unexpected values for 'input' ***\n";
+
+// get an unset variable
+$unset_var = 10;
+unset ($unset_var);
+
+// Class definition
+class MyClass
+{
+  public function __toString()
+  {
+    return "object";
+  }
+}
+
+// different scalar/non scalar values for 'input' argument
+$input_values = array(
+
+         // int data
+/*1*/    0,
+         1,
+         12345,
+         -2345,
+
+         // float data
+/*5*/    10.5,
+         -10.5,
+         10.1234567e8,
+         10.7654321E-8,
+         .5,
+
+         // null data
+/*10*/   NULL,
+         null,
+
+         // boolean data
+/*12*/   true,
+         false,
+         TRUE,
+         FALSE,
+
+         // empty data
+/*16*/   "",
+         '',
+
+         // string data
+/*18*/   "string",
+         'string',
+
+         // object data
+/*20*/   new MyClass(),
+
+         // resource data
+/*21*/   $fp = fopen(__FILE__,'r'),
+
+         // undefined data
+/*22*/   @$undefined_var,
+
+         // unset data
+/*23*/   @$unset_var,
+);
+
+// loop through each element of the array for input
+for($count = 0; $count < count($input_values); $count++) {
+  echo "-- Iteration ".($count + 1)." --\n";
+  var_dump( array_sum($input_values[$count]) );
+};
+
+fclose($fp);
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : unexpected values for 'input' ***
+-- Iteration 1 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 2 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 3 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 4 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 5 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 6 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 7 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 8 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 9 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 10 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 11 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 12 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 13 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 14 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 15 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 16 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 17 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 18 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 19 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 20 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 21 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 22 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+-- Iteration 23 --
+
+Warning: array_sum(): The argument should be an array in %s on line %d
+NULL
+Done
diff --git a/ext/standard/tests/array/array_sum_variation2.phpt b/ext/standard/tests/array/array_sum_variation2.phpt
new file mode 100644 (file)
index 0000000..a697a15
--- /dev/null
@@ -0,0 +1,49 @@
+--TEST--
+Test array_sum() function : usage variations - array with different integer value
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array &input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Testing array_sum() with different types of integer arrays containing data of following type:
+*  integer, octal, hexadecimal, maximum and minimum integer values & mixed of all integers
+*/
+
+echo "*** Testing array_sum() : different integer array ***\n";
+
+// Int array
+$int_values = array(3, 2, 100, 150, 25, 350, 0, -3, -1200);
+echo "-- Sum of Integer array --\n";
+var_dump( array_sum($int_values) );
+
+// Octal array
+$octal_values = array(056, 023, 090, 015, -045, 01, -078);
+echo "-- Sum of Octal array --\n";
+var_dump( array_sum($octal_values) );
+
+// Hexadecimal array
+$hex_values = array(0xAE, 0x2B, 0X10, -0xCF, 0X12, -0XF2);
+echo "-- Sum of Hex array --\n";
+var_dump( array_sum($hex_values) );
+
+// Mixed values int, octal & hex
+$mixed_int_value = array(2, 5, -1, 054, 0X3E, 0, -014, -0x2A);
+echo "-- Sum of mixed integer values --\n";
+var_dump( array_sum($mixed_int_value) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : different integer array ***
+-- Sum of Integer array --
+int(-573)
+-- Sum of Octal array --
+int(35)
+-- Sum of Hex array --
+int(-198)
+-- Sum of mixed integer values --
+int(58)
+Done
diff --git a/ext/standard/tests/array/array_sum_variation3.phpt b/ext/standard/tests/array/array_sum_variation3.phpt
new file mode 100644 (file)
index 0000000..9d32c2c
--- /dev/null
@@ -0,0 +1,54 @@
+--TEST--
+Test array_sum() function : usage variations - array with different float values
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+ * sum of array containing different float values
+*/
+
+echo "*** Testing array_sum() : array with different float values ***\n";
+
+// Simple float array
+$float_input = array( 1.1, 2.3, 0.0, 0.5, -2.3, -0.8, .5);
+echo "-- simple float array --\n";
+var_dump( array_sum($float_input) );
+
+// float array with scientific notations
+$float_input = array( 1.2e2, 23.4e3, -4.1e2, 0.2e2, 2.1e-2, .5e3);
+echo "-- float array with scientific notations e and E --\n";
+var_dump( array_sum($float_input) );
+$float_input = array( 1.2E2, 23.4E3, -4.1E2, 0.2E2, 2.1E-2, .5E3);
+var_dump( array_sum($float_input) );
+
+// Mixed float array
+$float_input = array( 
+  1.2,
+  0.5
+  -5.8,
+  6.334,
+  -0.65,
+  1.2e3,
+  -2.3e2,
+  5.56E3,
+  -3.82E-2
+);
+echo "-- Mixed float array --\n";
+var_dump( array_sum($float_input) );
+                     
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : array with different float values ***
+-- simple float array --
+float(1.3)
+-- float array with scientific notations e and E --
+float(23630.021)
+float(23630.021)
+-- Mixed float array --
+float(6531.5458)
+Done
diff --git a/ext/standard/tests/array/array_sum_variation4.phpt b/ext/standard/tests/array/array_sum_variation4.phpt
new file mode 100644 (file)
index 0000000..4959dee
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Test array_sum() function : usage variations - array with duplicate values
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Checking array_sum() with integer and float array containing duplicate values 
+*/
+
+echo "*** Testing array_sum() : array with duplicate values ***\n";
+
+// integer array with duplicate values
+$int_input = array( 2, 5, 7, 5, 0, -4, 2, 100);
+echo "-- With integer array --\n";
+var_dump( array_sum($int_input) );
+
+// float array with duplicate values
+$float_input = array( 2.3, 1.9, -4.1, 0.5, 1.9, -4.1, 3.6, 0.5);
+echo "-- With float array --\n";
+var_dump( array_sum($float_input) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : array with duplicate values ***
+-- With integer array --
+int(117)
+-- With float array --
+float(2.5)
+Done
diff --git a/ext/standard/tests/array/array_sum_variation5.phpt b/ext/standard/tests/array/array_sum_variation5.phpt
new file mode 100644 (file)
index 0000000..9068c45
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+Test array_sum() function : usage variations - array with reference variables as elements
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Testing array_sum() with 'input' having reference variables as elements
+*/
+
+echo "*** Testing array_sum() : array with elements as reference ***\n";
+
+$value1 = -5;
+$value2 = 100;
+$value3 = 0;
+$value4 = &$value1;
+
+// input array containing elements as reference variables
+$input = array(
+  0 => 10,
+  1 => &$value4,
+  2 => &$value2,
+  3 => 200,
+  4 => &$value3,
+);
+
+var_dump( array_sum($input) );
+
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : array with elements as reference ***
+int(305)
+Done
diff --git a/ext/standard/tests/array/array_sum_variation6.phpt b/ext/standard/tests/array/array_sum_variation6.phpt
new file mode 100644 (file)
index 0000000..ba67cfd
--- /dev/null
@@ -0,0 +1,33 @@
+--TEST--
+Test array_sum() function : usage variations - associative array 
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Testing array_sum() with associative array as 'input' argument 
+*/
+
+echo "*** Testing array_sum() : with associative array ***\n";
+
+// array with numeric keys
+$input = array(0 => 1, 1 => 10, 2 => 0, 3 => -2, 4 => 23.56);
+echo "-- with numeric keys --\n";
+var_dump( array_sum($input) );
+
+// array with string keys
+$input = array('a' => 20, "b" => 50, 'c' => 0, 'd' => -30, "e" => 100);
+echo "-- with string keys --\n";
+var_dump( array_sum($input) );
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : with associative array ***
+-- with numeric keys --
+float(32.56)
+-- with string keys --
+int(140)
+Done
diff --git a/ext/standard/tests/array/array_sum_variation7.phpt b/ext/standard/tests/array/array_sum_variation7.phpt
new file mode 100644 (file)
index 0000000..d309875
--- /dev/null
@@ -0,0 +1,86 @@
+--TEST--
+Test array_sum() function : usage variations - 'input' array with unexpected values as array element
+--FILE--
+<?php
+/* Prototype  : mixed array_sum(array $input)
+ * Description: Returns the sum of the array entries 
+ * Source code: ext/standard/array.c
+*/
+
+/*
+* Testing array_sum() with array having other than numeric entries
+*    strings, bool, null, subarrays & objects
+*/
+
+echo "*** Testing array_sum() : array with unexpected entries ***\n";
+
+// empty array
+$input = array();
+echo "-- empty array --\n";
+var_dump( array_sum($input) );
+
+// string array
+$input = array('Apple', 'Banana', 'Carrot', 'Mango', 'Orange');
+echo "-- array with string values --\n";
+var_dump( array_sum($input) );
+
+// bool array
+$input = array( true, true, false, true, false);
+echo "-- array with bool values --\n";
+var_dump( array_sum($input) );
+
+// array with null entry
+$input = array(null, NULL);
+echo "-- array with null values --\n";
+var_dump( array_sum($input) );
+
+// array with subarray
+$input = array(
+  array(1, 2),
+  array(),
+  array(0)
+);
+echo "-- array with subarrays --\n";
+var_dump( array_sum($input) );
+
+class MyClass
+{
+  public $value;
+  public function __construct($value)
+  {
+    $this->value = $value;
+  }
+}
+// array of objects
+$input = array(
+  new MyClass(2),
+  new MyClass(5),
+  new MyClass(10),
+  new MyClass(0)
+);
+echo "-- array with object values --\n";
+var_dump( array_sum($input) );
+
+// Mixed values
+$input = array( 5, -8, 7.2, -1.2, "10", "apple", 'Mango', true, false, null, NULL, array( array(1,2), array(0), array()));
+echo "-- array with mixed values --\n";
+var_dump( array_sum($input) );
+echo "Done"
+?>
+--EXPECTF--
+*** Testing array_sum() : array with unexpected entries ***
+-- empty array --
+int(0)
+-- array with string values --
+int(0)
+-- array with bool values --
+int(3)
+-- array with null values --
+int(0)
+-- array with subarrays --
+int(0)
+-- array with object values --
+int(0)
+-- array with mixed values --
+float(14)
+Done