8 use strict; use warnings;
12 use Hash::Util::FieldHash qw( :all);
14 #########################
16 # define ref types to use with some tests
19 # skipping CODE refs, they are differently scoped
20 @test_types = qw( SCALAR ARRAY HASH GLOB);
25 BEGIN { $n_tests += 3 }
27 my $ob_reg = \ %Hash::Util::FieldHash::ob_reg;
34 is( keys %$ob_reg, 1, "one object registered");
36 # field hash stays alive until $obj dies
37 is( keys %$ob_reg, 1, "object still registered");
39 is( keys %$ob_reg, 0, "object unregistered");
42 ### existence/retrieval/deletion
43 BEGIN { $n_tests += 6 }
48 for ( [ str => 'abc'], [ ref => {}] ) {
49 my ( $keytype, $key) = @$_;
51 ok( exists $h{ $key}, "existence ($keytype)");
52 is( $h{ $key}, $val, "retrieval ($keytype)");
54 is( keys %h, 0, "deletion ($keytype)");
58 ### id-action (stringification independent of bless)
59 BEGIN { $n_tests += 4 }
67 is( $f{ $key}, $val, "plain key set in field");
69 is( $f{ $key}, $val, "access through blessed");
72 is( $h{ $key}, $val, "plain key set in hash");
74 isnt( $h{ $key}, $val, "no access through blessed");
78 BEGIN { $n_tests += 1 + 2*( 3*@test_types + 5) + 1 }
83 is( keys %h, 0, "blip");
86 for my $preload ( [], [ map {}, 1 .. 3] ) {
87 my $pre = @$preload ? ' (preloaded)' : '';
89 my @preval = map "$_", @$preload;
90 @f{ @$preload} = @preval;
91 # Garbage collection separately
92 for my $type ( @test_types) {
94 my $ref = gen_ref( $type);
96 my ( $val) = grep $_ eq $type, values %f;
97 is( $val, $type, "$type visible$pre");
99 keys %Hash::Util::FieldHash::ob_reg,
101 "$type obj registered$pre"
104 is( keys %f, @$preload, "$type gone$pre");
107 # Garbage collection collectively
108 is( keys %Hash::Util::FieldHash::ob_reg, @$preload, "no objs remaining$pre");
110 my @refs = map gen_ref( $_), @test_types;
111 @f{ @refs} = @test_types;
113 eq_set( [ values %f], [ @test_types, @preval]),
114 "all types present$pre",
117 keys %Hash::Util::FieldHash::ob_reg,
118 @test_types + @$preload,
119 "all types registered$pre",
122 die "preload gone" unless defined $preload;
123 ok( eq_set( [ values %f], \ @preval), "all types gone$pre");
124 is( keys %Hash::Util::FieldHash::ob_reg, @$preload, "all types unregistered$pre");
126 is( keys %Hash::Util::FieldHash::ob_reg, 0, "preload gone after loop");
129 BEGIN { $n_tests += 8 }
134 my @refs = map [], 1 .. $size;
135 $f{ $_} = 1 for @refs;
136 is( keys %f, $size, "many keys singly");
138 keys %Hash::Util::FieldHash::ob_reg,
140 "many objects singly",
143 is( keys %f, 0, "many keys singly gone");
145 keys %Hash::Util::FieldHash::ob_reg,
147 "many objects singly unregistered",
151 my @refs = map [], 1 .. $size;
152 $f{ $_} = 1 for @refs;
153 is( keys %f, $size, "many keys at once");
155 keys %Hash::Util::FieldHash::ob_reg,
157 "many objects at once",
160 is( keys %f, 0, "many keys at once gone");
162 keys %Hash::Util::FieldHash::ob_reg,
164 "many objects at once unregistered",
169 BEGIN { $n_tests += 6 }
172 my @fields = map &fieldhash( {}), 1 .. $n_fields;
173 my @obs = map gen_ref( $_), @test_types;
175 for my $field ( @fields ) {
176 @{ $field }{ @obs} = map ref, @obs;
178 my $err = grep keys %$_ != @obs, @fields;
179 is( $err, 0, "$n_obs entries in $n_fields fields");
180 is( keys %Hash::Util::FieldHash::ob_reg, @obs, "$n_obs obs registered");
182 $err = grep keys %$_ != @obs, @fields;
183 is( $err, 0, "one entry gone from $n_fields fields");
184 is( keys %Hash::Util::FieldHash::ob_reg, @obs, "one ob unregistered");
186 $err = grep keys %$_ != @obs, @fields;
187 is( $err, 0, "all entries gone from $n_fields fields");
188 is( keys %Hash::Util::FieldHash::ob_reg, @obs, "all obs unregistered");
193 BEGIN { $n_tests += 1 }
195 bless \ %h, 'abc'; # this bus-errors with a certain bug
196 ok( 1, "no bus error on bless")
199 BEGIN { plan tests => $n_tests }
201 #######################################################################
203 use Symbol qw( gensym);
207 SCALAR => sub { \ my $x },
210 GLOB => sub { gensym },
211 CODE => sub { sub {} },
214 sub gen_ref { $gen{ shift()}->() }