3 # $Id: recurse.t,v 1.0.1.3 2001/02/17 12:28:33 ram Exp $
5 # Copyright (c) 1995-2000, Raphael Manfredi
7 # You may redistribute only under the same terms as Perl 5, as specified
8 # in the README file that comes with the distribution.
14 @INC = ('.', '../lib', '../ext/Storable/t');
18 require Config; import Config;
19 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
20 print "1..0 # Skip: Storable was not built\n";
28 use Storable qw(freeze thaw dclone);
34 use Storable qw(freeze thaw);
38 sub make { bless [], shift }
43 die "STORABLE_freeze" unless Storable::is_storing;
44 return (freeze(\@x), $self);
51 die "STORABLE_thaw #1" unless $obj eq $self;
54 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
55 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
57 die "STORABLE_thaw #4" unless Storable::is_retrieving;
64 sub make { bless {}, shift }
70 return ("", \@x, $self);
75 my ($cloning, $undef, $a, $obj) = @_;
76 die "STORABLE_thaw #1" unless $obj eq $self;
77 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
83 use Storable qw(dclone);
86 my $self = bless {}, shift;
88 $self->{sync} = OBJ_SYNC->make;
97 my $t = dclone($r->{sync});
98 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
103 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
104 die "STORABLE_thaw #1" unless $obj eq $self;
105 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
106 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
107 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
109 ($self->{sync}, $self->{ext}) = @$a;
114 use Storable qw(freeze thaw);
120 sub make { bless [], shift }
122 sub STORABLE_freeze {
125 return (freeze($self), $self) if ++$recursed < $MAX;
126 return ("no", $self);
133 die "STORABLE_thaw #1" unless $obj eq $self;
134 $self->[0] = thaw($x) if $x ne "no";
140 my $real = OBJ_REAL->make;
141 my $x = freeze $real;
146 ok 3, $y->[0] eq 'a';
149 my $sync = OBJ_SYNC->make;
155 ok 7, $y->{ok} == $y;
158 $sync = OBJ_SYNC2->make($ext);
159 $x = freeze [$sync, $ext];
165 ok 10, $y->{ok} == $y;
166 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
167 ok 12, $y->{ext} == $z->[1];
169 $real = OBJ_REAL2->make;
172 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
173 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
177 ok 17, $OBJ_REAL2::recursed == 0;
181 ok 19, ref $x eq 'OBJ_REAL2';
182 ok 20, $OBJ_REAL2::recursed == 0;
183 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
185 ok 22, !Storable::is_storing;
186 ok 23, !Storable::is_retrieving;
189 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
190 # sent me, along with a proposed fix.
198 return bless {dat => $dat}, $class;
208 Foo->new(2), # Second instance of a Foo
213 sub STORABLE_freeze {
214 my($self,$clonning) = @_;
215 return "$self->{a}", $self->{b};
219 my($self,$clonning,$dummy,$o) = @_;
227 my $bar2 = thaw freeze $bar;
229 ok 24, ref($bar2) eq 'Bar';
230 ok 25, ref($bar->{b}[0]) eq 'Foo';
231 ok 26, ref($bar->{b}[1]) eq 'Foo';
232 ok 27, ref($bar2->{b}[0]) eq 'Foo';
233 ok 28, ref($bar2->{b}[1]) eq 'Foo';
236 # The following attempts to make sure blessed objects are blessed ASAP
243 my $self = bless {}, shift;
250 my $self = bless {}, shift;
252 $self->{c1} = CLASS_1->make();
254 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
259 sub STORABLE_freeze {
260 my($self, $clonning) = @_;
261 return "", $self->{c1}, $self->{c3}, $self->{o};
265 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
266 main::ok 29, ref $self eq "CLASS_2";
267 main::ok 30, ref $c1 eq "CLASS_1";
268 main::ok 31, ref $c3 eq "CLASS_3";
269 main::ok 32, ref $o eq "CLASS_OTHER";
277 my $self = bless {}, shift;
281 sub set_c2 { $_[0]->{c2} = $_[1] }
285 my $o = CLASS_OTHER->make();
286 my $c2 = CLASS_2->make($o);
287 my $so = thaw freeze $o;