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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
#!./perl -w
BEGIN {
chdir 't' if -d 't';
require './test.pl';
set_up_inc('../lib');
}
use strict;
# This will crash perl if it fails
use constant PVBM => 'foo';
my $dummy = index 'foo', PVBM;
eval { my %h = (a => PVBM); 1 };
ok (!$@, 'fbm scalar can be inserted into a hash');
my $destroyed;
{ package Class; DESTROY { ++$destroyed; } }
$destroyed = 0;
{
my %h;
keys(%h) = 1;
$h{key} = bless({}, 'Class');
}
is($destroyed, 1, 'Timely hash destruction with lvalue keys');
# [perl #79178] Hash keys must not be stringified during compilation
# Run perl -MO=Concise -e '$a{\"foo"}' on a non-threaded pre-5.13.8 version
# to see why.
{
my $key;
package bar;
sub TIEHASH { bless {}, $_[0] }
sub FETCH { $key = $_[1] }
package main;
tie my %h, "bar";
() = $h{\'foo'};
is ref $key, SCALAR =>
'ref hash keys are not stringified during compilation';
use constant u => undef;
no warnings 'uninitialized'; # work around unfixed bug #105918
() = $h{+u};
is $key, undef,
'undef hash keys are not stringified during compilation, either';
}
# Part of RT #85026: Deleting the current iterator in void context does not
# free it.
{
my $gone;
no warnings 'once';
local *::DESTROY = sub { ++$gone };
my %a=(a=>bless[]);
each %a; # make the entry with the obj the current iterator
delete $a{a};
ok $gone, 'deleting the current iterator in void context frees the val'
}
# [perl #99660] Deleted hash element visible to destructor
{
my %h;
$h{k} = bless [];
my $normal_exit;
local *::DESTROY = sub { my $x = $h{k}; ++$normal_exit };
delete $h{k}; # must be in void context to trigger the bug
ok $normal_exit, 'freed hash elems are not visible to DESTROY';
}
# [perl #100340] Similar bug: freeing a hash elem during a delete
sub guard::DESTROY {
${$_[0]}->();
};
*guard = sub (&) {
my $callback = shift;
return bless \$callback, "guard"
};
{
my $ok;
my %t; %t = (
stash => {
guard => guard(sub{
$ok++;
delete $t{stash};
}),
foo => "bar",
bar => "baz",
},
);
ok eval { delete $t{stash}{guard}; # must be in void context
1 },
'freeing a hash elem from destructor called by delete does not die';
diag $@ if $@; # panic: free from wrong pool
is $ok, 1, 'the destructor was called';
}
# Weak references to pad hashes
{
my $ref;
no warnings 'experimental::builtin';
use builtin 'weaken';
{
my %hash;
weaken($ref = \%hash);
1; # the previous statement must not be the last
}
is $ref, undef, 'weak refs to pad hashes go stale on scope exit';
}
# [perl #107440]
sub A::DESTROY { $::ra = 0 }
$::ra = {a=>bless [], 'A'};
undef %$::ra;
pass 'no crash when freeing hash that is being undeffed';
$::ra = {a=>bless [], 'A'};
%$::ra = ('a'..'z');
pass 'no crash when freeing hash that is being exonerated, ahem, cleared';
# If I have these correct then removing any part of the lazy hash fill handling
# code in hv.c will cause some of these tests to start failing.
sub validate_hash {
my ($desc, $h) = @_;
local $::Level = $::Level + 1;
# test that scalar(%hash) works as expected, which as of perl 5.25 is
# the same as 0+keys %hash;
my $scalar= scalar %$h;
my $count= 0+keys %$h;
is($scalar, $count, "$desc scalar() should be the same as 0+keys() as of perl 5.25");
require Hash::Util;
sub Hash::Util::bucket_ratio (\%);
# back compat tests, via Hash::Util::bucket_ratio();
my $ratio = Hash::Util::bucket_ratio(%$h);
my $expect = qr!\A(\d+)/(\d+)\z!;
like($ratio, $expect, "$desc bucket_ratio matches pattern");
my ($used, $total)= (0,0);
($used, $total)= ($1,$2) if $ratio =~ /$expect/;
cmp_ok($total, '>', 0, "$desc has >0 array size ($total)");
cmp_ok($used, '>', 0, "$desc uses >0 heads ($used)");
cmp_ok($used, '<=', $total,
"$desc doesn't use more heads than are available");
return ($used, $total);
}
sub torture_hash {
my $desc = shift;
# Intentionally use an anon hash rather than a lexical, as lexicals default
# to getting reused on subsequent calls
my $h = {};
++$h->{$_} foreach @_;
my ($used0, $total0) = validate_hash($desc, $h);
# Remove half the keys each time round, until there are only 1 or 2 left
my @groups;
my ($h2, $h3, $h4);
while (keys %$h > 2) {
my $take = (keys %$h) / 2 - 1;
my @keys = (sort keys %$h)[0..$take];
my $scalar = %$h;
delete @$h{@keys};
push @groups, $scalar, \@keys;
my $count = keys %$h;
my ($used, $total) = validate_hash("$desc (-$count)", $h);
is($total, $total0, "$desc ($count) has same array size");
cmp_ok($used, '<=', $used0, "$desc ($count) has same or fewer heads");
++$h2->{$_} foreach @keys;
my (undef, $total2) = validate_hash("$desc (+$count)", $h2);
cmp_ok($total2, '<=', $total0, "$desc ($count) array size no larger");
# Each time this will get emptied then repopulated. If the fill isn't reset
# when the hash is emptied, the used count will likely exceed the array
%$h3 = %$h2;
is(join(",", sort keys %$h3),join(",",sort keys %$h2),"$desc (+$count copy) has same keys");
my (undef, $total3) = validate_hash("$desc (+$count copy)", $h3);
# We now only split when we collide on insert AND exceed the load factor
# when we did so. Building a hash via %x=%y means a pseudo-random key
# order inserting into %x, and we may end up encountering a collision
# at a different point in the load order, resulting in a possible power of
# two difference under the current load factor expectations. If this test
# fails then it is probably because DO_HSPLIT was changed, and this test
# needs to be adjusted accordingly.
ok( $total2 == $total3 || $total2*2==$total3 || $total2==$total3*2,
"$desc (+$count copy) array size within a power of 2 of each other");
# This might use fewer buckets than the original
%$h4 = %$h;
my (undef, $total4) = validate_hash("$desc ($count copy)", $h4);
cmp_ok($total4, '<=', $total0, "$desc ($count copy) array size no larger");
}
my $scalar = %$h;
my @keys = sort keys %$h;
delete @$h{@keys};
is(scalar %$h, 0, "scalar keys for empty $desc");
# Rebuild the original hash, and build a copy
# These will fail if hash key addition and deletion aren't handled correctly
my $h1;
foreach (@keys) {
++$h->{$_};
++$h1->{$_};
}
is(scalar %$h, $scalar, "scalar keys restored when rebuilding");
while (@groups) {
my $keys = pop @groups;
++$h->{$_} foreach @$keys;
my (undef, $total) = validate_hash($desc, $h);
ok($total == $total0 || $total == ($total0*2), "bucket count is expected size when rebuilding");
is(scalar %$h, pop @groups, "scalar keys is identical when rebuilding");
++$h1->{$_} foreach @$keys;
validate_hash("$desc copy", $h1);
}
# This will fail if the fill count isn't handled correctly on hash split
is(scalar %$h1, scalar %$h, "scalar keys is identical on copy and original");
}
if (is_miniperl) {
print "# skipping torture_hash tests on miniperl because no Hash::Util\n";
} else {
torture_hash('a .. zz', 'a' .. 'zz');
torture_hash('0 .. 9', 0 .. 9);
torture_hash("'Perl'", 'Rules');
}
{
my %h = qw(a x b y c z);
no warnings qw(misc uninitialized);
%h = $h{a};
is(join(':', %h), 'x:', 'hash self-assign');
}
# magic keys and values should be evaluated before the hash on the LHS is
# cleared
package Magic {
my %inner;
sub TIEHASH { bless [] }
sub FETCH { $inner{$_[1]} }
sub STORE { $inner{$_[1]} = $_[2]; }
sub CLEAR { %inner = () }
my (%t1, %t2);
tie %t1, 'Magic';
tie %t2, 'Magic';
%inner = qw(a x b y);
%t1 = (@t2{'a','b'});
::is(join( ':', %inner), "x:y", "magic keys");
}
# Make sure PL_sv_undef is copied, and not stored directly, when assigning
# to a hash. This failed on DEBUGGING + PERL_RC_STACK builds because the
# test for a lone SV assumed that an SV on the stack with a ref count of 1
# could be used directly rather than copied. However, PL_sv_undef and
# friends could reach a ref count of 1 but still not be stealable.
#
# A DEBUGGING build sets the initial ref count of the immortals to 1000
# rather than I32_MAX, making such problems easier to reproduce.
{
my $bad = 0;
for (1..1001) {
# Each iteration may leave the RC of PL_sv_undef one lower.
my %h = ('a', undef);
# this could fail with
# "Modification of non-creatable hash value attempted ..."
eval { $h{a} = 1; };
$bad = 1 if $@ ne "";
}
ok(!$bad, "PL_sv_undef RC 1");
}
done_testing();
|