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