Integrate perlio:
[p5sagit/p5-mst-13.2.git] / t / lib / st-recurse.t
CommitLineData
7a6a85bf 1#!./perl
2
b12202d0 3# $Id: recurse.t,v 1.0.1.3 2001/02/17 12:28:33 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: recurse.t,v $
b12202d0 11# Revision 1.0.1.3 2001/02/17 12:28:33 ram
12# patch8: ensure blessing occurs ASAP, specially designed for hooks
13#
90826881 14# Revision 1.0.1.2 2000/11/05 17:22:05 ram
15# patch6: stress hook a little more with refs to lexicals
16#
17# $Log: recurse.t,v $
dd19458b 18# Revision 1.0.1.1 2000/09/17 16:48:05 ram
19# patch1: added test case for store hook bug
20#
21# $Log: recurse.t,v $
9e21b3d0 22# Revision 1.0 2000/09/01 19:40:42 ram
23# Baseline for first official release.
7a6a85bf 24#
25
26sub BEGIN {
27 chdir('t') if -d 't';
20822f61 28 @INC = '.';
29 push @INC, '../lib';
9f233367 30 require Config; import Config;
31 if ($Config{'extensions'} !~ /\bStorable\b/) {
32 print "1..0 # Skip: Storable was not built\n";
33 exit 0;
34 }
7a6a85bf 35 require 'lib/st-dump.pl';
36}
37
38sub ok;
39
40use Storable qw(freeze thaw dclone);
41
b12202d0 42print "1..32\n";
7a6a85bf 43
44package OBJ_REAL;
45
46use Storable qw(freeze thaw);
47
48@x = ('a', 1);
49
50sub make { bless [], shift }
51
52sub STORABLE_freeze {
53 my $self = shift;
54 my $cloning = shift;
55 die "STORABLE_freeze" unless Storable::is_storing;
56 return (freeze(\@x), $self);
57}
58
59sub STORABLE_thaw {
60 my $self = shift;
61 my $cloning = shift;
62 my ($x, $obj) = @_;
63 die "STORABLE_thaw #1" unless $obj eq $self;
64 my $len = length $x;
65 my $a = thaw $x;
66 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
67 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
68 @$self = @$a;
69 die "STORABLE_thaw #4" unless Storable::is_retrieving;
70}
71
72package OBJ_SYNC;
73
74@x = ('a', 1);
75
76sub make { bless {}, shift }
77
78sub STORABLE_freeze {
79 my $self = shift;
80 my ($cloning) = @_;
81 return if $cloning;
82 return ("", \@x, $self);
83}
84
85sub STORABLE_thaw {
86 my $self = shift;
87 my ($cloning, $undef, $a, $obj) = @_;
88 die "STORABLE_thaw #1" unless $obj eq $self;
89 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
90 $self->{ok} = $self;
91}
92
93package OBJ_SYNC2;
94
95use Storable qw(dclone);
96
97sub make {
98 my $self = bless {}, shift;
99 my ($ext) = @_;
100 $self->{sync} = OBJ_SYNC->make;
101 $self->{ext} = $ext;
102 return $self;
103}
104
105sub STORABLE_freeze {
106 my $self = shift;
90826881 107 my %copy = %$self;
108 my $r = \%copy;
109 my $t = dclone($r->{sync});
110 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
7a6a85bf 111}
112
113sub STORABLE_thaw {
114 my $self = shift;
90826881 115 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
7a6a85bf 116 die "STORABLE_thaw #1" unless $obj eq $self;
117 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
90826881 118 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
119 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
7a6a85bf 120 $self->{ok} = $self;
121 ($self->{sync}, $self->{ext}) = @$a;
122}
123
124package OBJ_REAL2;
125
126use Storable qw(freeze thaw);
127
128$MAX = 20;
129$recursed = 0;
130$hook_called = 0;
131
132sub make { bless [], shift }
133
134sub STORABLE_freeze {
135 my $self = shift;
136 $hook_called++;
137 return (freeze($self), $self) if ++$recursed < $MAX;
138 return ("no", $self);
139}
140
141sub STORABLE_thaw {
142 my $self = shift;
143 my $cloning = shift;
144 my ($x, $obj) = @_;
145 die "STORABLE_thaw #1" unless $obj eq $self;
146 $self->[0] = thaw($x) if $x ne "no";
147 $recursed--;
148}
149
150package main;
151
152my $real = OBJ_REAL->make;
153my $x = freeze $real;
154ok 1, 1;
155
156my $y = thaw $x;
157ok 2, 1;
158ok 3, $y->[0] eq 'a';
159ok 4, $y->[1] == 1;
160
161my $sync = OBJ_SYNC->make;
162$x = freeze $sync;
163ok 5, 1;
164
165$y = thaw $x;
166ok 6, 1;
167ok 7, $y->{ok} == $y;
168
169my $ext = [1, 2];
170$sync = OBJ_SYNC2->make($ext);
171$x = freeze [$sync, $ext];
172ok 8, 1;
173
174my $z = thaw $x;
175$y = $z->[0];
176ok 9, 1;
177ok 10, $y->{ok} == $y;
178ok 11, ref $y->{sync} eq 'OBJ_SYNC';
179ok 12, $y->{ext} == $z->[1];
180
181$real = OBJ_REAL2->make;
182$x = freeze $real;
183ok 13, 1;
184ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
185ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
186
187$y = thaw $x;
188ok 16, 1;
189ok 17, $OBJ_REAL2::recursed == 0;
190
191$x = dclone $real;
192ok 18, 1;
193ok 19, ref $x eq 'OBJ_REAL2';
194ok 20, $OBJ_REAL2::recursed == 0;
195ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
196
197ok 22, !Storable::is_storing;
198ok 23, !Storable::is_retrieving;
dd19458b 199
200#
201# The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
202# sent me, along with a proposed fix.
203#
204
205package Foo;
206
207sub new {
208 my $class = shift;
209 my $dat = shift;
210 return bless {dat => $dat}, $class;
211}
212
213package Bar;
214sub new {
215 my $class = shift;
216 return bless {
217 a => 'dummy',
218 b => [
219 Foo->new(1),
220 Foo->new(2), # Second instance of a Foo
221 ]
222 }, $class;
223}
224
225sub STORABLE_freeze {
226 my($self,$clonning) = @_;
227 return "$self->{a}", $self->{b};
228}
229
230sub STORABLE_thaw {
231 my($self,$clonning,$dummy,$o) = @_;
232 $self->{a} = $dummy;
233 $self->{b} = $o;
234}
235
236package main;
237
238my $bar = new Bar;
239my $bar2 = thaw freeze $bar;
240
241ok 24, ref($bar2) eq 'Bar';
242ok 25, ref($bar->{b}[0]) eq 'Foo';
243ok 26, ref($bar->{b}[1]) eq 'Foo';
244ok 27, ref($bar2->{b}[0]) eq 'Foo';
245ok 28, ref($bar2->{b}[1]) eq 'Foo';
246
b12202d0 247#
248# The following attempts to make sure blessed objects are blessed ASAP
249# at retrieve time.
250#
251
252package CLASS_1;
253
254sub make {
255 my $self = bless {}, shift;
256 return $self;
257}
258
259package CLASS_2;
260
261sub make {
262 my $self = bless {}, shift;
263 my ($o) = @_;
264 $self->{c1} = CLASS_1->make();
265 $self->{o} = $o;
266 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
267 $o->set_c2($self);
268 return $self;
269}
270
271sub STORABLE_freeze {
272 my($self, $clonning) = @_;
273 return "", $self->{c1}, $self->{c3}, $self->{o};
274}
275
276sub STORABLE_thaw {
277 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
278 main::ok 29, ref $self eq "CLASS_2";
279 main::ok 30, ref $c1 eq "CLASS_1";
280 main::ok 31, ref $c3 eq "CLASS_3";
281 main::ok 32, ref $o eq "CLASS_OTHER";
282 $self->{c1} = $c1;
283 $self->{c3} = $c3;
284}
285
286package CLASS_OTHER;
287
288sub make {
289 my $self = bless {}, shift;
290 return $self;
291}
292
293sub set_c2 { $_[0]->{c2} = $_[1] }
294
295package main;
296
297my $o = CLASS_OTHER->make();
298my $c2 = CLASS_2->make($o);
299my $so = thaw freeze $o;
300