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
|
#!perl -w
# This test originally comes from the Hash-Util-FieldHash distribution
use strict;
use Test::More tests => 41;
use Hash::FieldHash qw(:all);
#########################
# define ref types to use with some tests
my @test_types;
BEGIN {
# skipping CODE refs, they are differently scoped
@test_types = qw( SCALAR ARRAY HASH GLOB);
}
### existence/retrieval/deletion
{
no warnings 'misc';
my $val = 123;
fieldhash my %h;
for ( [ ref => {}] ) {
my ( $keytype, $key) = @$_;
$h{ $key} = $val;
ok( exists $h{ $key}, "existence ($keytype)");
is( $h{ $key}, $val, "retrieval ($keytype)");
delete $h{ $key};
is( keys %h, 0, "deletion ($keytype)");
}
}
### id-action (stringification independent of bless)
{
my( %f, %g, %h, %i);
fieldhashes \(%f, %g);
my $val = 123;
my $key = [];
$f{ $key} = $val;
is( $f{ $key}, $val, "plain key set in field");
bless $key;
is( $f{ $key}, $val, "access through blessed");
$key = [];
$h{ $key} = $val;
is( $h{ $key}, $val, "plain key set in hash");
bless $key;
isnt( $h{ $key}, $val, "no access through blessed");
}
{
my %h;
fieldhash %h;
$h{ []} = 123;
is( keys %h, 0, "blip");
}
for my $preload ( [], [ map {}, 1 .. 3] ) {
my $pre = @$preload ? ' (preloaded)' : '';
my %f;
fieldhash %f;
my @preval = map "$_", @$preload;
@f{ @$preload} = @preval;
# Garbage collection separately
for my $type ( @test_types) {
{
my $ref = gen_ref( $type);
$f{ $ref} = $type;
my ( $val) = grep $_ eq $type, values %f;
is( $val, $type, "$type visible$pre");
}
is( keys %f, @$preload, "$type gone$pre");
}
# Garbage collection collectively
{
my @refs = map gen_ref( $_), @test_types;
@f{ @refs} = @test_types;
ok(
eq_set( [ values %f], [ @test_types, @preval]),
"all types present$pre",
);
}
die "preload gone" unless defined $preload;
ok( eq_set( [ values %f], \ @preval), "all types gone$pre");
}
# autovivified key
{
my %h;
fieldhash %h;
my $ref = {};
my $x = $h{ $ref}->[ 0];
is keys %h, 1, "autovivified key present";
undef $ref;
is keys %h, 0, "autovivified key collected";
}
# big key sets
{
my $size = 10_000;
my %f;
fieldhash %f;
{
my @refs = map [], 1 .. $size;
$f{ $_} = 1 for @refs;
is( keys %f, $size, "many keys singly");
}
is( keys %f, 0, "many keys singly gone");
{
my @refs = map [], 1 .. $size;
@f{ @refs } = ( 1) x @refs;
is( keys %f, $size, "many keys at once");
}
is( keys %f, 0, "many keys at once gone");
}
# many field hashes
{
my $n_fields = 1000;
my @fields = map {}, $n_fields;
fieldhashes @fields;
my @obs = map gen_ref( $_), @test_types;
my $n_obs = @obs;
for my $field ( @fields ) {
@{ $field }{ @obs} = map ref, @obs;
}
my $err = grep keys %$_ != @obs, @fields;
is( $err, 0, "$n_obs entries in $n_fields fields");
pop @obs;
$err = grep keys %$_ != @obs, @fields;
is( $err, 0, "one entry gone from $n_fields fields");
@obs = ();
$err = grep keys %$_ != @obs, @fields;
is( $err, 0, "all entries gone from $n_fields fields");
}
# direct hash assignment
{
fieldhashes \my( %f, %g, %h);
my $size = 6;
my @obs = map [], 1 .. $size;
@f{ @obs} = ( 1) x $size;
$g{ $_} = $f{ $_} for keys %f; # single assignment
%h = %f; # wholesale assignment
@obs = ();
is keys %f, 0, "orig garbage-collected";
is keys %g, 0, "single-copy garbage-collected";
is keys %h, 0, "wholesale-copy garbage-collected";
}
{
fieldhash my %h;
bless \ %h, 'abc'; # this bus-errors with a certain bug
ok( 1, "no bus error on bless")
}
#######################################################################
use Symbol qw( gensym);
BEGIN {
my %gen = (
SCALAR => sub { \ my $o },
ARRAY => sub { [] },
HASH => sub { {} },
GLOB => sub { gensym },
CODE => sub { sub {} },
);
sub gen_ref { $gen{ shift()}->() }
}
|