]> git.vpit.fr Git - perl/modules/autovivification.git/blobdiff - t/44-multideref.t
Specify the lists in the iterator object
[perl/modules/autovivification.git] / t / 44-multideref.t
index 954b069ccae7b6577e878f2d0ed56fccb74b0dad..85f3c1cfcff839bbea6bf9d4e2703ad34fb2bd2e 100644 (file)
@@ -4,14 +4,17 @@ use strict;
 use warnings;
 
 use lib 't/lib';
-use Test::Leaner tests => 3 * 4 * (8 ** 3) * 2;
+use Test::Leaner tests => 4 * 4 * (8 ** 3) * 2;
 
 my $depth = 3;
 
+my $magic_val = 123;
+
 my @prefixes = (
- '',
- 'exists ',
- 'delete ',
+ sub { $_[0]                },
+ sub { "$_[0] = $magic_val" },
+ sub { "exists $_[0]"       },
+ sub { "delete $_[0]"       },
 );
 
 my  (@vlex, %vlex, $vrlex);
@@ -51,11 +54,30 @@ sub reset_vars {
  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 => \@_,
-   idx => [ (0) x $len ],
+   len   => $len,
+   max   => \@max,
+   lists => \@lists,
+   idx   => [ (0) x $len ],
   }, $class;
  }
 
@@ -78,20 +100,24 @@ sub reset_vars {
   return $i < $len;
  }
 
- sub pick {
+ sub items {
   my $self = shift;
 
-  my ($len, $idx) = @$self{qw<len idx>};
+  my ($len, $lists, $idx) = @$self{qw<len lists idx>};
 
-  return map $_[$_]->[$idx->[$_]], 0 .. ($len - 1);
+  return map $lists->[$_]->[$idx->[$_]], 0 .. ($len - 1);
  }
 }
 
-my $iterator = autovivification::TestIterator->new(3, 4, (8) x $depth);
+my $iterator = autovivification::TestIterator->new(
+ \@prefixes, \@heads, (\@derefs) x $depth,
+);
 do {
- my @elems = $iterator->pick(\@prefixes, \@heads, (\@derefs) x $depth);
- my $code  = join '', @elems;
- my $exp   = $elems[0] eq 'exists ' ? !1 : undef;
+ 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 @_ };