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.
11 require Config; import Config;
12 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
13 print "1..0 # Skip: Storable was not built\n";
21 use Storable qw(freeze thaw dclone);
27 use Storable qw(freeze thaw);
31 sub make { bless [], shift }
36 die "STORABLE_freeze" unless Storable::is_storing;
37 return (freeze(\@x), $self);
44 die "STORABLE_thaw #1" unless $obj eq $self;
47 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
48 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
50 die "STORABLE_thaw #4" unless Storable::is_retrieving;
57 sub make { bless {}, shift }
63 return ("", \@x, $self);
68 my ($cloning, $undef, $a, $obj) = @_;
69 die "STORABLE_thaw #1" unless $obj eq $self;
70 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
76 use Storable qw(dclone);
79 my $self = bless {}, shift;
81 $self->{sync} = OBJ_SYNC->make;
90 my $t = dclone($r->{sync});
91 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
96 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
97 die "STORABLE_thaw #1" unless $obj eq $self;
98 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
99 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
100 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
102 ($self->{sync}, $self->{ext}) = @$a;
107 use Storable qw(freeze thaw);
113 sub make { bless [], shift }
115 sub STORABLE_freeze {
118 return (freeze($self), $self) if ++$recursed < $MAX;
119 return ("no", $self);
126 die "STORABLE_thaw #1" unless $obj eq $self;
127 $self->[0] = thaw($x) if $x ne "no";
133 my $real = OBJ_REAL->make;
134 my $x = freeze $real;
138 ok 2, ref $y eq 'OBJ_REAL';
139 ok 3, $y->[0] eq 'a';
142 my $sync = OBJ_SYNC->make;
148 ok 7, $y->{ok} == $y;
151 $sync = OBJ_SYNC2->make($ext);
152 $x = freeze [$sync, $ext];
158 ok 10, $y->{ok} == $y;
159 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
160 ok 12, $y->{ext} == $z->[1];
162 $real = OBJ_REAL2->make;
165 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
166 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
170 ok 17, $OBJ_REAL2::recursed == 0;
174 ok 19, ref $x eq 'OBJ_REAL2';
175 ok 20, $OBJ_REAL2::recursed == 0;
176 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
178 ok 22, !Storable::is_storing;
179 ok 23, !Storable::is_retrieving;
182 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
183 # sent me, along with a proposed fix.
191 return bless {dat => $dat}, $class;
201 Foo->new(2), # Second instance of a Foo
206 sub STORABLE_freeze {
207 my($self,$clonning) = @_;
208 return "$self->{a}", $self->{b};
212 my($self,$clonning,$dummy,$o) = @_;
220 my $bar2 = thaw freeze $bar;
222 ok 24, ref($bar2) eq 'Bar';
223 ok 25, ref($bar->{b}[0]) eq 'Foo';
224 ok 26, ref($bar->{b}[1]) eq 'Foo';
225 ok 27, ref($bar2->{b}[0]) eq 'Foo';
226 ok 28, ref($bar2->{b}[1]) eq 'Foo';
229 # The following attempts to make sure blessed objects are blessed ASAP
236 my $self = bless {}, shift;
243 my $self = bless {}, shift;
245 $self->{c1} = CLASS_1->make();
247 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
252 sub STORABLE_freeze {
253 my($self, $clonning) = @_;
254 return "", $self->{c1}, $self->{c3}, $self->{o};
258 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
259 main::ok 29, ref $self eq "CLASS_2";
260 main::ok 30, ref $c1 eq "CLASS_1";
261 main::ok 31, ref $c3 eq "CLASS_3";
262 main::ok 32, ref $o eq "CLASS_OTHER";
270 my $self = bless {}, shift;
274 sub set_c2 { $_[0]->{c2} = $_[1] }
277 # Is the reference count of the extra references returned from a
278 # STORABLE_freeze hook correct? [ID 20020601.005]
283 my $self = bless {}, $_[0];
284 $self->{freezed} = "$self";
290 $::refcount_ok = 1 unless "$self" eq $self->{freezed};
299 sub STORABLE_freeze {
301 return ("", $obj, Foo2->new);
304 sub STORABLE_thaw { } # Not really used
307 use vars qw($refcount_ok);
309 my $o = CLASS_OTHER->make();
310 my $c2 = CLASS_2->make($o);
311 my $so = thaw freeze $o;
314 thaw freeze(Foo3->new);
315 ok 33, $refcount_ok == 1;