2 use strict; use warnings;
6 use Hash::Util::FieldHash;
7 use Scalar::Util qw( weaken);
9 # The functions in Hash::Util::FieldHash
10 # _test_uvar_get, _test_uvar_get and _test_uvar_both
12 # _test_uvar_get( $anyref, \ $counter) makes the referent of $anyref
13 # "uvar"-magical with get magic only. $counter is reset if the magic
14 # could be established. $counter will be incremented each time the
15 # magic "get" function is called.
17 # _test_uvar_set does the same for "set" magic. _test_uvar_both
18 # sets both magic functions identically. Both use the same counter.
20 # magical weak ref (patch to sv.c)
22 my( $magref, $counter);
25 Hash::Util::FieldHash::_test_uvar_set( \ $magref, \ $counter);
26 is( $counter, 0, "got magical scalar");
30 is( $counter, 1, "store triggers magic");
33 is( $counter, 1, "weaken doesn't trigger magic");
36 is( $counter, 1, "read doesn't trigger magic");
39 is( $counter, 2, "ref expiry triggers magic (weakref patch worked)");
41 is( $magref, undef, "weak ref works normally");
43 # same, but overwrite weakref before expiry
45 weaken( $magref = $ref = []);
46 is( $counter, 1, "setup for overwrite");
48 $magref = my $other_ref = [];
49 is( $counter, 2, "overwrite triggers");
52 is( $counter, 2, "ref expiry doesn't trigger after overwrite");
54 is( $magref, $other_ref, "weak ref doesn't kill overwritten value");
56 BEGIN { $n_tests += 10 }
59 # magical hash (patches to mg.c and hv.c)
61 # the hook is only sensitive if the set function is NULL
64 Hash::Util::FieldHash::_test_uvar_get( \ %h, \ $counter);
65 is( $counter, 0, "got magical hash");
68 is( $counter, 1, "list assign triggers");
72 is( $counter, 1, "scalar keys doesn't trigger");
73 is( $x, 1, "there is one key");
76 is( $counter, 1, "list keys doesn't trigger");
77 is( "@x", "abc", "key is correct");
80 is( $counter, 1, "scalar values doesn't trigger");
81 is( $x, 1, "the value is correct");
84 is( $counter, 1, "list values doesn't trigger");
85 is( "@x", "123", "the value is correct");
88 is( $counter, 1, "scalar each doesn't trigger");
89 is( $x, "abc", "the return is correct");
92 is( $counter, 1, "scalar each doesn't trigger");
93 is( $x, undef, "the return is correct");
96 is( $counter, 1, "list each doesn't trigger");
97 is( "@x", "abc 123", "the return is correct");
100 is( $counter, 1, "hash in scalar context doesn't trigger");
101 like( $x, qr!^\d+/\d+$!, "correct result");
104 is( $counter, 1, "hash in list context doesn't trigger");
105 is( "@x", "abc 123", "correct result");
109 is( $counter, 2, "lvalue assign triggers");
112 is( $counter, 2, "hash in list context doesn't trigger");
113 is( "@x", "123 456 abc def", "correct result");
116 is( $counter, 3, "good exists triggers");
119 is( $counter, 4, "bad exists triggers");
122 is( $counter, 5, "good delete triggers");
125 is( $counter, 5, "hash in list context doesn't trigger");
126 is( "@x", "123 abc", "correct result");
129 is( $counter, 6, "bad delete triggers");
132 is( $counter, 6, "hash in list context doesn't trigger");
133 is( "@x", "123 abc", "correct result");
136 is( $counter, 7, "good read triggers");
139 is( $counter, 8, "bad read triggers");
142 is( $counter, 8, "hash in list context doesn't trigger");
143 is( "@x", "123 abc", "correct result");
147 is( $counter, 8, "bless doesn't trigger");
150 is( $counter, 8, "bless doesn't trigger");
152 # see that normal set magic doesn't trigger (identity condition)
154 Hash::Util::FieldHash::_test_uvar_set( \ %i, \ $counter);
155 is( $counter, 0, "got magical hash");
172 is( $counter, 0, "normal set magic never triggers");
175 is( $counter, 1, "...except with bless");
177 # see that magic with both set and get doesn't trigger
180 Hash::Util::FieldHash::_test_uvar_same( \ %j, \ $counter);
181 is( $counter, 0, "got magical hash");
198 is( $counter, 0, "get/set magic never triggers");
201 is( $counter, 1, "...except for bless");
203 BEGIN { $n_tests += 43 }
206 BEGIN { plan tests => $n_tests }