Commit | Line | Data |
7a6a85bf |
1 | #!./perl |
7a6a85bf |
2 | # |
3 | # Copyright (c) 1995-2000, Raphael Manfredi |
4 | # |
9e21b3d0 |
5 | # You may redistribute only under the same terms as Perl 5, as specified |
6 | # in the README file that comes with the distribution. |
7a6a85bf |
7 | # |
7a6a85bf |
8 | |
9 | sub BEGIN { |
48c887dd |
10 | unshift @INC, 't'; |
9f233367 |
11 | require Config; import Config; |
0c384302 |
12 | if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) { |
9f233367 |
13 | print "1..0 # Skip: Storable was not built\n"; |
14 | exit 0; |
15 | } |
372cb964 |
16 | require 'st-dump.pl'; |
7a6a85bf |
17 | } |
18 | |
19 | sub ok; |
20 | |
21 | use Storable qw(freeze thaw); |
22 | |
b12202d0 |
23 | print "1..25\n"; |
7a6a85bf |
24 | |
25 | ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0); |
26 | |
27 | package TIED_HASH; |
28 | |
29 | sub TIEHASH { |
30 | my $self = bless {}, shift; |
31 | return $self; |
32 | } |
33 | |
34 | sub FETCH { |
35 | my $self = shift; |
36 | my ($key) = @_; |
37 | $main::hash_fetch++; |
38 | return $self->{$key}; |
39 | } |
40 | |
41 | sub STORE { |
42 | my $self = shift; |
43 | my ($key, $value) = @_; |
44 | $self->{$key} = $value; |
45 | } |
46 | |
47 | sub FIRSTKEY { |
48 | my $self = shift; |
49 | scalar keys %{$self}; |
50 | return each %{$self}; |
51 | } |
52 | |
53 | sub NEXTKEY { |
54 | my $self = shift; |
55 | return each %{$self}; |
56 | } |
57 | |
58 | sub STORABLE_freeze { |
59 | my $self = shift; |
60 | $main::hash_hook1++; |
61 | return join(":", keys %$self) . ";" . join(":", values %$self); |
62 | } |
63 | |
64 | sub STORABLE_thaw { |
65 | my ($self, $cloning, $frozen) = @_; |
66 | my ($keys, $values) = split(/;/, $frozen); |
67 | my @keys = split(/:/, $keys); |
68 | my @values = split(/:/, $values); |
69 | for (my $i = 0; $i < @keys; $i++) { |
70 | $self->{$keys[$i]} = $values[$i]; |
71 | } |
72 | $main::hash_hook2++; |
73 | } |
74 | |
75 | package TIED_ARRAY; |
76 | |
77 | sub TIEARRAY { |
78 | my $self = bless [], shift; |
79 | return $self; |
80 | } |
81 | |
82 | sub FETCH { |
83 | my $self = shift; |
84 | my ($idx) = @_; |
85 | $main::array_fetch++; |
86 | return $self->[$idx]; |
87 | } |
88 | |
89 | sub STORE { |
90 | my $self = shift; |
91 | my ($idx, $value) = @_; |
92 | $self->[$idx] = $value; |
93 | } |
94 | |
95 | sub FETCHSIZE { |
96 | my $self = shift; |
97 | return @{$self}; |
98 | } |
99 | |
100 | sub STORABLE_freeze { |
101 | my $self = shift; |
102 | $main::array_hook1++; |
103 | return join(":", @$self); |
104 | } |
105 | |
106 | sub STORABLE_thaw { |
107 | my ($self, $cloning, $frozen) = @_; |
108 | @$self = split(/:/, $frozen); |
109 | $main::array_hook2++; |
110 | } |
111 | |
112 | package TIED_SCALAR; |
113 | |
114 | sub TIESCALAR { |
115 | my $scalar; |
116 | my $self = bless \$scalar, shift; |
117 | return $self; |
118 | } |
119 | |
120 | sub FETCH { |
121 | my $self = shift; |
122 | $main::scalar_fetch++; |
123 | return $$self; |
124 | } |
125 | |
126 | sub STORE { |
127 | my $self = shift; |
128 | my ($value) = @_; |
129 | $$self = $value; |
130 | } |
131 | |
132 | sub STORABLE_freeze { |
133 | my $self = shift; |
134 | $main::scalar_hook1++; |
135 | return $$self; |
136 | } |
137 | |
138 | sub STORABLE_thaw { |
139 | my ($self, $cloning, $frozen) = @_; |
140 | $$self = $frozen; |
141 | $main::scalar_hook2++; |
142 | } |
143 | |
144 | package main; |
145 | |
146 | $a = 'toto'; |
147 | $b = \$a; |
148 | |
149 | $c = tie %hash, TIED_HASH; |
150 | $d = tie @array, TIED_ARRAY; |
151 | tie $scalar, TIED_SCALAR; |
152 | |
153 | $scalar = 'foo'; |
154 | $hash{'attribute'} = 'plain value'; |
155 | $array[0] = \$scalar; |
156 | $array[1] = $c; |
157 | $array[2] = \@array; |
158 | $array[3] = "plaine scalaire"; |
159 | |
160 | @tied = (\$scalar, \@array, \%hash); |
161 | %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar); |
162 | @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d, |
163 | $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied); |
164 | |
165 | ok 1, defined($f = freeze(\@a)); |
166 | |
167 | $dumped = &dump(\@a); |
168 | ok 2, 1; |
169 | |
170 | $root = thaw($f); |
171 | ok 3, defined $root; |
172 | |
173 | $got = &dump($root); |
174 | ok 4, 1; |
175 | |
176 | ok 5, $got ne $dumped; # our hooks did not handle refs in array |
177 | |
178 | $g = freeze($root); |
179 | ok 6, length($f) == length($g); |
180 | |
181 | # Ensure the tied items in the retrieved image work |
182 | @old = ($scalar_fetch, $array_fetch, $hash_fetch); |
183 | @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]}; |
184 | @type = qw(SCALAR ARRAY HASH); |
185 | |
186 | ok 7, tied $$tscalar; |
187 | ok 8, tied @{$tarray}; |
188 | ok 9, tied %{$thash}; |
189 | |
190 | @new = ($$tscalar, $tarray->[0], $thash->{'attribute'}); |
191 | @new = ($scalar_fetch, $array_fetch, $hash_fetch); |
192 | |
193 | # Tests 10..15 |
194 | for ($i = 0; $i < @new; $i++) { |
195 | ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14 |
196 | ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15 |
197 | } |
198 | |
199 | ok 16, $$tscalar eq 'foo'; |
200 | ok 17, $tarray->[3] eq 'plaine scalaire'; |
201 | ok 18, $thash->{'attribute'} eq 'plain value'; |
202 | |
203 | # Ensure hooks were called |
204 | ok 19, ($scalar_hook1 && $scalar_hook2); |
205 | ok 20, ($array_hook1 && $array_hook2); |
206 | ok 21, ($hash_hook1 && $hash_hook2); |
207 | |
b12202d0 |
208 | # |
209 | # And now for the "blessed ref to tied hash" with "store hook" test... |
210 | # |
211 | |
212 | my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook |
213 | my $bx = thaw freeze $bc; |
214 | |
215 | ok 22, ref $bx eq 'FOO'; |
216 | my $old_hash_fetch = $hash_fetch; |
217 | my $v = $bx->{attribute}; |
218 | ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied |
219 | |
220 | package TIED_HASH_REF; |
221 | |
222 | |
223 | sub STORABLE_freeze { |
224 | my ($self, $cloning) = @_; |
225 | return if $cloning; |
226 | return('ref lost'); |
227 | } |
228 | |
229 | sub STORABLE_thaw { |
230 | my ($self, $cloning, $data) = @_; |
231 | return if $cloning; |
232 | } |
233 | |
234 | package main; |
235 | |
236 | $bc = bless \%hash, 'TIED_HASH_REF'; |
237 | $bx = thaw freeze $bc; |
238 | |
239 | ok 24, ref $bx eq 'TIED_HASH_REF'; |
240 | $old_hash_fetch = $hash_fetch; |
241 | $v = $bx->{attribute}; |
242 | ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied |