1#!./perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 7;
7
8use List::Util qw( reductions );
9
10is_deeply( [ reductions { } ], [],
11  'empty list'
12);
13
14is_deeply(
15  [ reductions { $a + $b } 1 .. 5 ],
16  [ 1, 3, 6, 10, 15 ],
17  'sum 1..5'
18);
19
20# We don't guarantee what this will return but it definitely shouldn't crash
21{
22  my $ret = reductions { $a + $b } 1 .. 3;
23  pass( 'reductions in scalar context does not crash' );
24}
25
26my $destroyed_count;
27sub Guardian::DESTROY { $destroyed_count++ }
28
29{
30  undef $destroyed_count;
31
32  my @ret = reductions { $b } map { bless [], "Guardian" } 1 .. 5;
33
34  ok( !$destroyed_count, 'nothing destroyed yet' );
35
36  @ret = ();
37
38  is( $destroyed_count, 5, 'all the items were destroyed' );
39}
40
41{
42  undef $destroyed_count;
43
44  ok( !defined eval {
45      reductions { die "stop" if $b == 4; bless [], "Guardian" } 1 .. 4;
46      1
47    }, 'die in BLOCK is propagated'
48  );
49
50  is( $destroyed_count, 2, 'intermediate temporaries are destroyed after exception' );
51}
52