]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/85-stress-unwind.t
5a59e7a01af26eae18b5be2d8682d2f2811d6561
[perl/modules/Scope-Upper.git] / t / 85-stress-unwind.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7
8 use Scope::Upper qw/unwind/;
9
10 our ($call, @args, $args);
11
12 $call = sub {
13  my ($height, $level, $i) = @_;
14  return [ [ "unwind(\@args => $level)\n", '' ] ];
15 };
16
17 sub list { @_ }
18
19 my @blocks = (
20  [ 'sub {',     '}->()' ],
21  [ 'eval {',    '}' ],
22 );
23
24 my @contexts = (
25  [ '',        '; ()' ],
26  [ 'scalar(', ')' ],
27  [ 'list(',   ')' ],
28 );
29
30 @blocks   = map [ map "$_\n", @$_ ], @blocks;
31 @contexts = map [ map "$_\n", @$_ ], @contexts;
32
33 sub gen {
34  my ($height, $level, $i) = @_;
35  push @_, $i = 0 if @_ == 2;
36  my @res;
37  my $up = $i == $height + 1 ? $call->(@_) : gen($height, $level, $i + 1);
38  if ($i + $level == $height + 1) {
39   for (@$up) {
40    $_->[1] = "return($args)\n";
41   }
42  }
43  for my $base (@$up) {
44   my ($code, $exp) = @$base;
45   for my $blk (@blocks) {
46    for my $cx (@contexts) {
47     push @res, [
48      $blk->[0] . $cx->[0] . $code . $cx->[1] . $blk->[1],
49      $blk->[0] . $cx->[0] . $exp .  $cx->[1] . $blk->[1],
50     ];
51     my $list = join ', ', map { int rand 10 } 0 .. rand 3;
52     push @res, [
53      $blk->[0] . $cx->[0] . "($list, " . $code . ')' . $cx->[1] . $blk->[1],
54      $blk->[0] . $cx->[0] . "($list, " . $exp .  ')' . $cx->[1] . $blk->[1],
55     ];
56    }
57   }
58  }
59  return \@res;
60 }
61
62 sub linearize { join ', ', map { defined($_) ? $_ : '(undef)' } @_ }
63
64 sub runtests {
65  my ($height, $level) = @_;
66  my $i;
67  my $tests = gen @_;
68  for (@$tests) {
69   ++$i;
70   no warnings 'void';
71   my $res = linearize eval $_->[0];
72   $res = '*TEST DID NOT COMPILE*' if $@;
73   my $exp;
74   unless ($@) {
75    $exp = linearize eval $_->[1];
76    $exp = '*REFERENCE DID NOT COMPILE*' if $@;
77   }
78   if ($@ || $res ne $exp) {
79    diag <<DIAG;
80 === This testcase failed ===
81 $_->[0];
82 ==== vvvvv Errors vvvvvv ===
83 DIAG
84   }
85   is $res, $exp, "stress unwind $height $level $i";
86  }
87 }
88
89 for ([ ], [ 'A' ], [ qw/B C/ ]) {
90  @args = @$_;
91  $args = '(' . join(', ', map "'$_'", @args) . ')';
92  runtests 0, 0;
93  runtests 0, 1;
94  runtests 1, 0;
95  runtests 1, 1;
96 }