Commit | Line | Data |
1e73acc8 |
1 | #!perl |
2 | |
3 | BEGIN { |
df6ac08f |
4 | if ($ENV{PERL_CORE}) { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib'; |
7 | } |
1e73acc8 |
8 | } |
9 | |
10 | use strict; use warnings; |
11 | use Test::More; |
12 | my $n_tests; |
13 | |
14 | use Hash::Util::FieldHash; |
15 | use Scalar::Util qw( weaken); |
16 | |
17 | # The functions in Hash::Util::FieldHash |
18 | # _test_uvar_get, _test_uvar_get and _test_uvar_both |
19 | |
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. |
24 | |
25 | # _test_uvar_set does the same for "set" magic. _test_uvar_both |
26 | # sets both magic functions identically. Both use the same counter. |
27 | |
28 | # magical weak ref (patch to sv.c) |
29 | { |
30 | my( $magref, $counter); |
31 | |
32 | $counter = 123; |
33 | Hash::Util::FieldHash::_test_uvar_set( \ $magref, \ $counter); |
34 | is( $counter, 0, "got magical scalar"); |
35 | |
36 | my $ref = []; |
37 | $magref = $ref; |
38 | is( $counter, 1, "store triggers magic"); |
39 | |
40 | weaken $magref; |
41 | is( $counter, 1, "weaken doesn't trigger magic"); |
42 | |
43 | { my $x = $magref } |
44 | is( $counter, 1, "read doesn't trigger magic"); |
45 | |
46 | undef $ref; |
47 | is( $counter, 2, "ref expiry triggers magic (weakref patch worked)"); |
48 | |
49 | is( $magref, undef, "weak ref works normally"); |
50 | |
51 | # same, but overwrite weakref before expiry |
52 | $counter = 0; |
53 | weaken( $magref = $ref = []); |
54 | is( $counter, 1, "setup for overwrite"); |
55 | |
56 | $magref = my $other_ref = []; |
57 | is( $counter, 2, "overwrite triggers"); |
58 | |
59 | undef $ref; |
60 | is( $counter, 2, "ref expiry doesn't trigger after overwrite"); |
61 | |
62 | is( $magref, $other_ref, "weak ref doesn't kill overwritten value"); |
63 | |
64 | BEGIN { $n_tests += 10 } |
65 | } |
66 | |
67 | # magical hash (patches to mg.c and hv.c) |
68 | { |
69 | # the hook is only sensitive if the set function is NULL |
70 | my ( %h, $counter); |
71 | $counter = 123; |
72 | Hash::Util::FieldHash::_test_uvar_get( \ %h, \ $counter); |
73 | is( $counter, 0, "got magical hash"); |
74 | |
75 | %h = ( abc => 123); |
76 | is( $counter, 1, "list assign triggers"); |
77 | |
78 | $h{ def} = 456; |
79 | is( $counter, 3, "lvalue assign triggers twice"); |
80 | |
81 | exists $h{ def}; |
82 | is( $counter, 4, "good exists triggers"); |
83 | |
84 | exists $h{ xyz}; |
85 | is( $counter, 5, "bad exists triggers"); |
86 | |
87 | delete $h{ def}; |
88 | is( $counter, 6, "good delete triggers"); |
89 | |
90 | delete $h{ xyz}; |
91 | is( $counter, 7, "bad delete triggers"); |
92 | |
93 | my $x = $h{ abc}; |
94 | is( $counter, 8, "good read triggers"); |
95 | |
96 | $x = $h{ xyz}; |
97 | is( $counter, 9, "bad read triggers"); |
98 | |
99 | bless \ %h; |
100 | is( $counter, 9, "bless triggers(!)"); |
101 | |
102 | |
103 | $x = keys %h; |
104 | is( $counter, 9, "scalar keys doesn't trigger"); |
105 | |
106 | () = keys %h; |
107 | is( $counter, 9, "list keys doesn't trigger"); |
108 | |
109 | $x = values %h; |
110 | is( $counter, 9, "scalar values doesn't trigger"); |
111 | |
112 | () = values %h; |
113 | is( $counter, 9, "list values doesn't trigger"); |
114 | |
115 | $x = each %h; |
116 | is( $counter, 9, "scalar each doesn't trigger"); |
117 | |
118 | () = each %h; |
119 | is( $counter, 9, "list each doesn't trigger"); |
120 | |
ce809d1f |
121 | bless \ %h, 'xyz'; |
122 | is( $counter, 9, "bless doesn't trigger"); |
123 | |
1e73acc8 |
124 | # see that normal set magic doesn't trigger (identity condition) |
125 | my %i; |
126 | Hash::Util::FieldHash::_test_uvar_set( \ %i, \ $counter); |
127 | is( $counter, 0, "got magical hash"); |
128 | |
129 | %i = ( abc => 123); |
130 | $i{ def} = 456; |
131 | exists $i{ def}; |
132 | exists $i{ xyz}; |
133 | delete $i{ def}; |
134 | delete $i{ xyz}; |
135 | $x = $i{ abc}; |
136 | $x = $i{ xyz}; |
137 | $x = keys %i; |
138 | () = keys %i; |
139 | $x = values %i; |
140 | () = values %i; |
141 | $x = each %i; |
142 | () = each %i; |
143 | |
144 | is( $counter, 0, "normal set magic never triggers"); |
145 | |
146 | bless \ %i, 'abc'; |
147 | is( $counter, 1, "...except with bless"); |
148 | |
ce809d1f |
149 | # see that magic with both set and get doesn't trigger |
1e73acc8 |
150 | $counter = 123; |
151 | my %j; |
152 | Hash::Util::FieldHash::_test_uvar_same( \ %j, \ $counter); |
153 | is( $counter, 0, "got magical hash"); |
154 | |
155 | %j = ( abc => 123); |
156 | $j{ def} = 456; |
157 | exists $j{ def}; |
158 | exists $j{ xyz}; |
159 | delete $j{ def}; |
160 | delete $j{ xyz}; |
161 | $x = $j{ abc}; |
162 | $x = $j{ xyz}; |
163 | $x = keys %j; |
164 | () = keys %j; |
165 | $x = values %j; |
166 | () = values %j; |
167 | $x = each %j; |
168 | () = each %j; |
ce809d1f |
169 | |
170 | is( $counter, 0, "get/set magic never triggers"); |
1e73acc8 |
171 | |
172 | bless \ %j, 'abc'; |
173 | is( $counter, 1, "...except for bless"); |
174 | |
ce809d1f |
175 | BEGIN { $n_tests += 23 } |
1e73acc8 |
176 | } |
177 | |
178 | BEGIN { plan tests => $n_tests } |
179 | |