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