3 # Copyright (c) 1995-2000, Raphael Manfredi
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
12 @INC = ('.', '../lib', '../ext/Storable/t');
16 require Config; import Config;
17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18 print "1..0 # Skip: Storable was not built\n";
26 use Storable qw(freeze thaw dclone);
32 use Storable qw(freeze thaw);
36 sub make { bless [], shift }
41 die "STORABLE_freeze" unless Storable::is_storing;
42 return (freeze(\@x), $self);
49 die "STORABLE_thaw #1" unless $obj eq $self;
52 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
53 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
55 die "STORABLE_thaw #4" unless Storable::is_retrieving;
62 sub make { bless {}, shift }
68 return ("", \@x, $self);
73 my ($cloning, $undef, $a, $obj) = @_;
74 die "STORABLE_thaw #1" unless $obj eq $self;
75 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
81 use Storable qw(dclone);
84 my $self = bless {}, shift;
86 $self->{sync} = OBJ_SYNC->make;
95 my $t = dclone($r->{sync});
96 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
101 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
102 die "STORABLE_thaw #1" unless $obj eq $self;
103 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
104 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
105 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
107 ($self->{sync}, $self->{ext}) = @$a;
112 use Storable qw(freeze thaw);
118 sub make { bless [], shift }
120 sub STORABLE_freeze {
123 return (freeze($self), $self) if ++$recursed < $MAX;
124 return ("no", $self);
131 die "STORABLE_thaw #1" unless $obj eq $self;
132 $self->[0] = thaw($x) if $x ne "no";
138 my $real = OBJ_REAL->make;
139 my $x = freeze $real;
143 ok 2, ref $y eq 'OBJ_REAL';
144 ok 3, $y->[0] eq 'a';
147 my $sync = OBJ_SYNC->make;
153 ok 7, $y->{ok} == $y;
156 $sync = OBJ_SYNC2->make($ext);
157 $x = freeze [$sync, $ext];
163 ok 10, $y->{ok} == $y;
164 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
165 ok 12, $y->{ext} == $z->[1];
167 $real = OBJ_REAL2->make;
170 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
171 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
175 ok 17, $OBJ_REAL2::recursed == 0;
179 ok 19, ref $x eq 'OBJ_REAL2';
180 ok 20, $OBJ_REAL2::recursed == 0;
181 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
183 ok 22, !Storable::is_storing;
184 ok 23, !Storable::is_retrieving;
187 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
188 # sent me, along with a proposed fix.
196 return bless {dat => $dat}, $class;
206 Foo->new(2), # Second instance of a Foo
211 sub STORABLE_freeze {
212 my($self,$clonning) = @_;
213 return "$self->{a}", $self->{b};
217 my($self,$clonning,$dummy,$o) = @_;
225 my $bar2 = thaw freeze $bar;
227 ok 24, ref($bar2) eq 'Bar';
228 ok 25, ref($bar->{b}[0]) eq 'Foo';
229 ok 26, ref($bar->{b}[1]) eq 'Foo';
230 ok 27, ref($bar2->{b}[0]) eq 'Foo';
231 ok 28, ref($bar2->{b}[1]) eq 'Foo';
234 # The following attempts to make sure blessed objects are blessed ASAP
241 my $self = bless {}, shift;
248 my $self = bless {}, shift;
250 $self->{c1} = CLASS_1->make();
252 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
257 sub STORABLE_freeze {
258 my($self, $clonning) = @_;
259 return "", $self->{c1}, $self->{c3}, $self->{o};
263 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
264 main::ok 29, ref $self eq "CLASS_2";
265 main::ok 30, ref $c1 eq "CLASS_1";
266 main::ok 31, ref $c3 eq "CLASS_3";
267 main::ok 32, ref $o eq "CLASS_OTHER";
275 my $self = bless {}, shift;
279 sub set_c2 { $_[0]->{c2} = $_[1] }
282 # Is the reference count of the extra references returned from a
283 # STORABLE_freeze hook correct? [ID 20020601.005]
288 my $self = bless {}, $_[0];
289 $self->{freezed} = "$self";
295 $::refcount_ok = 1 unless "$self" eq $self->{freezed};
304 sub STORABLE_freeze {
306 return ("", $obj, Foo2->new);
309 sub STORABLE_thaw { } # Not really used
312 use vars qw($refcount_ok);
314 my $o = CLASS_OTHER->make();
315 my $c2 = CLASS_2->make($o);
316 my $so = thaw freeze $o;
319 thaw freeze(Foo3->new);
320 ok 33, $refcount_ok == 1;