10 use strict; use warnings;
14 use Hash::Util::FieldHash;
15 use Scalar::Util qw( weaken);
17 # The functions in Hash::Util::FieldHash
18 # _test_uvar_get, _test_uvar_get and _test_uvar_both
20 # _test_uvar_get( $anyref, \ $counter) makes the referent of $anyref
21 # "uvar"-magical with get magic only. $counter is reset if the magic
22 # could be established. $counter will be incremented each time the
23 # magic "get" function is called.
25 # _test_uvar_set does the same for "set" magic. _test_uvar_both
26 # sets both magic functions identically. Both use the same counter.
28 # magical weak ref (patch to sv.c)
30 my( $magref, $counter);
33 Hash::Util::FieldHash::_test_uvar_set( \ $magref, \ $counter);
34 is( $counter, 0, "got magical scalar");
38 is( $counter, 1, "store triggers magic");
41 is( $counter, 1, "weaken doesn't trigger magic");
44 is( $counter, 1, "read doesn't trigger magic");
47 is( $counter, 2, "ref expiry triggers magic (weakref patch worked)");
49 is( $magref, undef, "weak ref works normally");
51 # same, but overwrite weakref before expiry
53 weaken( $magref = $ref = []);
54 is( $counter, 1, "setup for overwrite");
56 $magref = my $other_ref = [];
57 is( $counter, 2, "overwrite triggers");
60 is( $counter, 2, "ref expiry doesn't trigger after overwrite");
62 is( $magref, $other_ref, "weak ref doesn't kill overwritten value");
64 BEGIN { $n_tests += 10 }
67 # magical hash (patches to mg.c and hv.c)
69 # the hook is only sensitive if the set function is NULL
72 Hash::Util::FieldHash::_test_uvar_get( \ %h, \ $counter);
73 is( $counter, 0, "got magical hash");
76 is( $counter, 1, "list assign triggers");
79 is( $counter, 3, "lvalue assign triggers twice");
82 is( $counter, 4, "good exists triggers");
85 is( $counter, 5, "bad exists triggers");
88 is( $counter, 6, "good delete triggers");
91 is( $counter, 7, "bad delete triggers");
94 is( $counter, 8, "good read triggers");
97 is( $counter, 9, "bad read triggers");
100 is( $counter, 9, "bless triggers(!)");
104 is( $counter, 9, "scalar keys doesn't trigger");
107 is( $counter, 9, "list keys doesn't trigger");
110 is( $counter, 9, "scalar values doesn't trigger");
113 is( $counter, 9, "list values doesn't trigger");
116 is( $counter, 9, "scalar each doesn't trigger");
119 is( $counter, 9, "list each doesn't trigger");
121 # see that normal set magic doesn't trigger (identity condition)
123 Hash::Util::FieldHash::_test_uvar_set( \ %i, \ $counter);
124 is( $counter, 0, "got magical hash");
141 is( $counter, 0, "normal set magic never triggers");
144 is( $counter, 1, "...except with bless");
146 # see that magic with both set and get doesn't trigger (identity condition)
149 Hash::Util::FieldHash::_test_uvar_same( \ %j, \ $counter);
150 is( $counter, 0, "got magical hash");
167 is( $counter, 0, "normal get magic never triggers");
170 is( $counter, 1, "...except for bless");
172 BEGIN { $n_tests += 22 }
175 BEGIN { plan tests => $n_tests }