File: 44-multideref.t

package info (click to toggle)
libautovivification-perl 0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 440 kB
  • sloc: perl: 3,573; ansic: 1,507; makefile: 8
file content (133 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!perl -T

use strict;
use warnings;

use lib 't/lib';
use Test::Leaner tests => 4 * 4 * (8 ** 3) * 2;

my $depth = 3;

my $magic_val = 123;

my @prefixes = (
 sub { $_[0]                },
 sub { "$_[0] = $magic_val" },
 sub { "exists $_[0]"       },
 sub { "delete $_[0]"       },
);

my  (@vlex, %vlex, $vrlex);
our (@vgbl, %vgbl, $vrgbl);

my @heads = (
 '$vlex',    # lexical array/hash
 '$vgbl',    # global array/hash
 '$vrlex->', # lexical array/hash reference
 '$vrgbl->', # global array/hash reference
);

my  $lex;
our $gbl;

my @derefs = (
 '[0]',      # array const (aelemfast)
 '[$lex]',   # array lexical
 '[$gbl]',   # array global
 '[$lex+1]', # array complex
 '{foo}',    # hash const
 '{$lex}',   # hash lexical
 '{$gbl}',   # hash global
 '{"x$lex"}' # hash complex
);

sub reset_vars {
 (@vlex, %vlex, $vrlex) = ();
 (@vgbl, %vgbl, $vrgbl) = ();
 $lex = 1;
 $gbl = 2;
}

{
 package autovivification::TestIterator;

 sub new {
  my $class = shift;

  my (@lists, @max);
  for my $arg (@_) {
   next unless defined $arg;
   my $type = ref $arg;
   my $list;
   if ($type eq 'ARRAY') {
    $list = $arg;
   } elsif ($type eq '') {
    $list = [ 1 .. $arg ];
   } else {
    die "Invalid argument of type $type";
   }
   my $max = @$list;
   die "Empty list" unless $max;
   push @lists, $list;
   push @max,   $max;
  }

  my $len = @_;
  bless {
   len   => $len,
   max   => \@max,
   lists => \@lists,
   idx   => [ (0) x $len ],
  }, $class;
 }

 sub next {
  my $self = shift;

  my ($len, $max, $idx) = @$self{qw<len max idx>};

  my $i;
  ++$idx->[0];
  for ($i = 0; $i < $len; ++$i) {
   if ($idx->[$i] == $max->[$i]) {
    $idx->[$i] = 0;
    ++$idx->[$i + 1] unless $i == $len - 1;
   } else {
    last;
   }
  }

  return $i < $len;
 }

 sub items {
  my $self = shift;

  my ($len, $lists, $idx) = @$self{qw<len lists idx>};

  return map $lists->[$_]->[$idx->[$_]], 0 .. ($len - 1);
 }
}

my $iterator = autovivification::TestIterator->new(
 \@prefixes, \@heads, (\@derefs) x $depth,
);
do {
 my ($prefix, @elems) = $iterator->items;
 my $code = $prefix->(join '', @elems);
 my $exp  = ($code =~ /^\s*exists/) ? !1
                                    : (($code =~ /=\s*$magic_val/) ? $magic_val
                                                                   : undef);
 reset_vars();
 my ($res, $err) = do {
  local $SIG{__WARN__} = sub { die @_ };
  local $@;
  my $r = eval <<" CODE";
  no autovivification;
  $code
 CODE
  ($r, $@)
 };
 is $err, '',   "$code: no exception";
 is $res, $exp, "$code: value";
} while ($iterator->next);