File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array / bug24980.phpt
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:48:03 2012 UTC (12 years, 4 months ago) by misho
Branches: php, MAIN
CVS tags: v5_4_3elwix, v5_4_29p0, v5_4_29, v5_4_20p0, v5_4_20, v5_4_17p0, v5_4_17, v5_3_10, HEAD
php

--TEST--
Bug #24980 (array_reduce() uses first element as default running total)
--FILE--
<?php
/* test #1: numeric data */
function add_up($running_total, $current_value)
{
	echo "running_total is ".(int)$running_total.", current_value is {$current_value}\n";
	$running_total += $current_value * $current_value;
	return $running_total;
}
                        
$numbers = array (2,3,5,7);
$total = array_reduce($numbers, 'add_up');
print "Total is $total\n";

/* test #2: string data */
$a = array("a", "b", "c");
function foo ($a, $b)
{
	return $a . $b;
}
var_dump(array_reduce($a, "foo"));
                        
/* test #3: basic test (used to leak memory) */
function rsum($v, $w)
{
	$v += $w;
	return $v;
}
function rmul($v, $w)
{
	$v *= $w;
	return $v;
}
$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1);

var_dump($b, $c, $d);
?>
--EXPECT--
running_total is 0, current_value is 2
running_total is 4, current_value is 3
running_total is 13, current_value is 5
running_total is 38, current_value is 7
Total is 87
string(3) "abc"
int(15)
int(1200)
int(1)

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>