Annotation of embedaddon/php/ext/standard/tests/array/bug24980.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Bug #24980 (array_reduce() uses first element as default running total)
! 3: --FILE--
! 4: <?php
! 5: /* test #1: numeric data */
! 6: function add_up($running_total, $current_value)
! 7: {
! 8: echo "running_total is ".(int)$running_total.", current_value is {$current_value}\n";
! 9: $running_total += $current_value * $current_value;
! 10: return $running_total;
! 11: }
! 12:
! 13: $numbers = array (2,3,5,7);
! 14: $total = array_reduce($numbers, 'add_up');
! 15: print "Total is $total\n";
! 16:
! 17: /* test #2: string data */
! 18: $a = array("a", "b", "c");
! 19: function foo ($a, $b)
! 20: {
! 21: return $a . $b;
! 22: }
! 23: var_dump(array_reduce($a, "foo"));
! 24:
! 25: /* test #3: basic test (used to leak memory) */
! 26: function rsum($v, $w)
! 27: {
! 28: $v += $w;
! 29: return $v;
! 30: }
! 31: function rmul($v, $w)
! 32: {
! 33: $v *= $w;
! 34: return $v;
! 35: }
! 36: $a = array(1, 2, 3, 4, 5);
! 37: $x = array();
! 38: $b = array_reduce($a, "rsum");
! 39: $c = array_reduce($a, "rmul", 10);
! 40: $d = array_reduce($x, "rsum", 1);
! 41:
! 42: var_dump($b, $c, $d);
! 43: ?>
! 44: --EXPECT--
! 45: running_total is 0, current_value is 2
! 46: running_total is 4, current_value is 3
! 47: running_total is 13, current_value is 5
! 48: running_total is 38, current_value is 7
! 49: Total is 87
! 50: string(3) "abc"
! 51: int(15)
! 52: int(1200)
! 53: int(1)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>