use strict;
use warnings;
-use Fatal qw/open/;
+use Fatal qw/open close/;
use Text::Balanced qw/extract_bracketed/;
open my $hash_t, '<', 't/20-hash.t';
sub dump_array {
my ($a) = @_;
+
return 'undef' unless defined $a;
- return $a unless ref $a;
- die "Invalid argument" unless ref $a eq 'ARRAY';
- return '[ ' . join(', ', map dump_array($_), @$a) . ' ]';
+
+ if (ref $a) {
+ die "Invalid argument" unless ref $a eq 'ARRAY';
+ return '[ ' . join(', ', map dump_array($_), @$a) . ' ]';
+ } else {
+ $a = "'\Q$a\E'" if $a !~ /^\s*\d/;
+ return $a;
+ }
+}
+
+sub extract ($$) {
+ extract_bracketed $_[0], $_[1], qr/.*?(?<![\\@%])(?:\\\\)*(?=$_[1])/
}
-sub extract ($) { extract_bracketed $_[0], '{', qr/.*?(?<!\\)(?:\\\\)*(?={)/ }
+sub convert_testcase ($$) {
+ local $_ = $_[0];
+ my $fast = $_[1];
+
+ s!(\ba\b)?(\s*)HASH\b!($1 ? 'an': '') . "$2ARRAY"!eg;
+ s{
+ [\{\[]\s*(['"]?[a-z]['"]?(?:\s*,\s*['"]?[a-z]['"]?)*)\s*[\}\]]
+ }{
+ '[' . join(', ', map { my $n = num($_); $fast ? $n : "\$N[$n]" }
+ split /\s*,\s*/, $1) . ']'
+ }gex;
+ s!%(\{?)\$!\@$1\$!g;
+
+ my $buf;
+ my $suffix = $_;
+ my ($bracket, $prefix);
+ while (do { ($bracket, $suffix, $prefix) = extract($suffix, '{'); $bracket }) {
+ my $array = dump_array(hash2array(eval $bracket));
+ $buf .= $prefix . $array;
+ }
+ $buf .= $suffix;
+ $buf =~ s/\s+/ /g;
+ $buf =~ s/\s+$//;
+
+ return "$buf\n";
+}
my $in_data;
while (<$hash_t>) {
print $array_t $_;
print $array_fast_t $_;
} else {
- for my $file ([ 1, $array_t ], [ 0, $array_fast_t ]) {
- local $_ = $_;
- s!(\ba\b)?(\s*)HASH\b!($1 ? 'an': '') . "$2ARRAY"!eg;
- s{
- {\s*(['"]?[a-z]['"]?(?:\s*,\s*['"]?[a-z]['"]?)*)\s*}
- }{
- '[' . join(', ', map { my $n = num($_); $file->[0] ? "\$N[$n]" : $n }
- split /\s*,\s*/, $1) . ']'
- }gex;
- s!%(\{?)\$!\@$1\$!g;
- my $buf;
- my $suffix = $_;
- my ($bracket, $prefix);
- while (do { ($bracket, $suffix, $prefix) = extract($suffix); $bracket }) {
- $buf .= $prefix . dump_array(hash2array(eval $bracket));
- }
- $buf .= $suffix;
- $buf =~ s/\s+/ /g;
- $buf =~ s/\s+$//;
- print { $file->[1] } "$buf\n";
+ print $array_t convert_testcase($_, 0);
+ print $array_fast_t convert_testcase($_, 1);
+ }
+}
+
+close $hash_t;
+close $array_t;
+close $array_fast_t;
+
+open my $hash_specific_t, '<', 't/22-hash-kv.t';
+open my $array_specific_t, '>', 't/32-array-kv.t';
+
+$in_data = 0;
+while (<$hash_specific_t>) {
+ if (/^__DATA__$/) {
+ $in_data = 1;
+ } elsif (!$in_data) {
+ s{'%'}{'\@'};
+ if (/\bplan\s*[\s\(]\s*tests\b/) {
+ s/\s*;?\s*$//;
+ s/^(\s*)//;
+ $_ = "$1if (\$] >= 5.011) { $_ } else { plan skip_all => 'perl 5.11 required for keys/values \@array' }\n";
}
+ } else {
+ $_ = convert_testcase($_, 1);
}
+ print $array_specific_t $_;
}
use strict;
use warnings;
-use Test::More tests => 9 * 3 * 64;
+use Test::More;
+
+BEGIN {
+ plan tests => 9 * 3 * 64;
+}
use lib 't/lib';
use autovivification::TestCases;
$x # [ keys %$x ] # '', [ ], undef # +fetch
$x # [ keys %$x ] # '', [ ], { } # +exists +delete +store
-$x->{a} ='b' # [ keys %$x ] # '', [ 'a' ], { a => 'b' }
-$x->{a} ='b' # [ keys %$x ] # '', [ 'a' ], { a => 'b' } #
-$x->{a} ='b' # [ keys %$x ] # '', [ 'a' ], { a => 'b' } # +fetch
-$x->{a} ='b' # [ keys %$x ] # '', [ 'a' ], { a => 'b' } # +exists +delete +store
+$x->{a} = 1 # [ keys %$x ] # '', [ 'a' ], { a => 1 }
+$x->{a} = 1 # [ keys %$x ] # '', [ 'a' ], { a => 1 } #
+$x->{a} = 1 # [ keys %$x ] # '', [ 'a' ], { a => 1 } # +fetch
+$x->{a} = 1 # [ keys %$x ] # '', [ 'a' ], { a => 1 } # +exists +delete +store
$x # keys %{$x->{a}} # '', 0, { a => { } }
$x # keys %{$x->{a}} # '', 0, undef #
$x # [ values %$x ] # '', [ ], undef # +fetch
$x # [ values %$x ] # '', [ ], { } # +exists +delete +store
-$x->{a} ='b' # [ values %$x ] # '', [ 'b' ], { a=>'b' }
-$x->{a} ='b' # [ values %$x ] # '', [ 'b' ], { a=>'b' } #
-$x->{a} ='b' # [ values %$x ] # '', [ 'b' ], { a=>'b' } # +fetch
-$x->{a} ='b' # [ values %$x ] # '', [ 'b' ], { a=>'b' } # +exists +delete +store
+$x->{a} = 1 # [ values %$x ] # '', [ 1 ], { a => 1 }
+$x->{a} = 1 # [ values %$x ] # '', [ 1 ], { a => 1 } #
+$x->{a} = 1 # [ values %$x ] # '', [ 1 ], { a => 1 } # +fetch
+$x->{a} = 1 # [ values %$x ] # '', [ 1 ], { a => 1 } # +exists +delete +store
$x # values %{$x->{a}} # '', 0, { a => { } }
$x # values %{$x->{a}} # '', 0, undef #
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use Test::More;
+
+BEGIN {
+ if ($] >= 5.011) { plan tests => 9 * 3 * 64 } else { plan skip_all => 'perl 5.11 required for keys/values @array' }
+}
+
+use lib 't/lib';
+use autovivification::TestCases;
+
+while (<DATA>) {
+ 1 while chomp;
+ next unless /#/;
+ testcase_ok($_, '@');
+}
+
+__DATA__
+
+--- keys ---
+
+$x # keys @$x # '', 0, [ ]
+$x # keys @$x # '', 0, undef #
+$x # keys @$x # '', 0, undef # +fetch
+$x # keys @$x # '', 0, [ ] # +exists
+$x # keys @$x # '', 0, [ ] # +delete
+$x # keys @$x # '', 0, [ ] # +store
+
+$x # keys @$x # qr/^Reference vivification forbidden/, undef, undef # +strict +fetch
+$x # keys @$x # '', 0, [ ] # +strict +exists
+$x # keys @$x # '', 0, [ ] # +strict +delete
+$x # keys @$x # '', 0, [ ] # +strict +store
+
+$x # [ keys @$x ] # '', [ ], [ ]
+$x # [ keys @$x ] # '', [ ], undef #
+$x # [ keys @$x ] # '', [ ], undef # +fetch
+$x # [ keys @$x ] # '', [ ], [ ] # +exists +delete +store
+
+$x->[0] = 1 # [ keys @$x ] # '', [0], [ 1 ]
+$x->[0] = 1 # [ keys @$x ] # '', [0], [ 1 ] #
+$x->[0] = 1 # [ keys @$x ] # '', [0], [ 1 ] # +fetch
+$x->[0] = 1 # [ keys @$x ] # '', [0], [ 1 ] # +exists +delete +store
+
+$x # keys @{$x->[0]} # '', 0, [ [ ] ]
+$x # keys @{$x->[0]} # '', 0, undef #
+$x # keys @{$x->[0]} # '', 0, undef # +fetch
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +exists
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +delete
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +store
+
+$x # keys @{$x->[0]} # qr/^Reference vivification forbidden/, undef, undef # +strict +fetch
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +strict +exists
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +strict +delete
+$x # keys @{$x->[0]} # '', 0, [ [ ] ] # +strict +store
+
+$x # [ keys @{$x->[0]} ] # '', [ ], [ [ ] ]
+$x # [ keys @{$x->[0]} ] # '', [ ], undef #
+$x # [ keys @{$x->[0]} ] # '', [ ], undef # +fetch
+$x # [ keys @{$x->[0]} ] # '', [ ], [ [ ] ] # +exists +delete +store
+
+--- values ---
+
+$x # values @$x # '', 0, [ ]
+$x # values @$x # '', 0, undef #
+$x # values @$x # '', 0, undef # +fetch
+$x # values @$x # '', 0, [ ] # +exists
+$x # values @$x # '', 0, [ ] # +delete
+$x # values @$x # '', 0, [ ] # +store
+
+$x # values @$x # qr/^Reference vivification forbidden/, undef, undef # +strict +fetch
+$x # values @$x # '', 0, [ ] # +strict +exists
+$x # values @$x # '', 0, [ ] # +strict +delete
+$x # values @$x # '', 0, [ ] # +strict +store
+
+$x # [ values @$x ] # '', [ ], [ ]
+$x # [ values @$x ] # '', [ ], undef #
+$x # [ values @$x ] # '', [ ], undef # +fetch
+$x # [ values @$x ] # '', [ ], [ ] # +exists +delete +store
+
+$x->[0] = 1 # [ values @$x ] # '', [ 1 ], [ 1 ]
+$x->[0] = 1 # [ values @$x ] # '', [ 1 ], [ 1 ] #
+$x->[0] = 1 # [ values @$x ] # '', [ 1 ], [ 1 ] # +fetch
+$x->[0] = 1 # [ values @$x ] # '', [ 1 ], [ 1 ] # +exists +delete +store
+
+$x # values @{$x->[0]} # '', 0, [ [ ] ]
+$x # values @{$x->[0]} # '', 0, undef #
+$x # values @{$x->[0]} # '', 0, undef # +fetch
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +exists
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +delete
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +store
+
+$x # values @{$x->[0]} # qr/^Reference vivification forbidden/, undef, undef # +strict +fetch
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +strict +exists
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +strict +delete
+$x # values @{$x->[0]} # '', 0, [ [ ] ] # +strict +store
+
+$x # [ values @{$x->[0]} ] # '', [ ], [ [ ] ]
+$x # [ values @{$x->[0]} ] # '', [ ], undef #
+$x # [ values @{$x->[0]} ] # '', [ ], undef # +fetch
+$x # [ values @{$x->[0]} ] # '', [ ], [ [ ] ] # +exists +delete +store