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 unshift @INC, 't/compat' if $] < 5.006002;
12 require Config; import Config;
13 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14 print "1..0 # Skip: Storable was not built\n";
21 use Storable qw(freeze thaw store retrieve);
31 REF => \(my $aref = []),
32 VSTRING => \(my $vstring = v1.2.3),
33 'long VSTRING' => \(my $vstring = eval "v" . 0 x 300),
34 LVALUE => \(my $substr = substr((my $str = "foo"), 0, 3)),
39 my $tests = $test + 23 + (2 * 6 * keys %::immortals) + (3 * keys %::weird_refs);
40 plan(tests => $tests);
44 sub make { bless [], shift }
46 package SHORT_NAME_WITH_HOOK;
48 sub make { bless [], shift }
59 die "STORABLE_thaw" unless $obj eq $self;
64 # Still less than 256 bytes, so long classname logic not fully exercised
65 # Wait until Perl removes the restriction on identifier lengths.
66 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
71 \@ISA = ("SHORT_NAME");
76 package ${name}_WITH_HOOK;
78 \@ISA = ("SHORT_NAME_WITH_HOOK");
82 # Construct a pool of objects
85 for (my $i = 0; $i < 10; $i++) {
86 push(@pool, SHORT_NAME->make);
87 push(@pool, SHORT_NAME_WITH_HOOK->make);
88 push(@pool, $name->make);
89 push(@pool, "${name}_WITH_HOOK"->make);
92 my $x = freeze \@pool;
93 pass("Freeze didn't crash");
97 is(scalar @{$y}, @pool);
99 is(ref $y->[0], 'SHORT_NAME');
100 is(ref $y->[1], 'SHORT_NAME_WITH_HOOK');
101 is(ref $y->[2], $name);
102 is(ref $y->[3], "${name}_WITH_HOOK");
105 for (my $i = 0; $i < 10; $i++) {
106 do { $good = 0; last } unless ref $y->[4*$i] eq 'SHORT_NAME';
107 do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
108 do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
109 do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
114 my $blessed_ref = bless \\[1,2,3], 'Foobar';
115 my $x = freeze $blessed_ref;
117 is(ref $y, 'Foobar');
121 package RETURNS_IMMORTALS;
123 sub make { my $self = shift; bless [@_], $self }
125 sub STORABLE_freeze {
126 # Some reference some number of times.
128 my ($what, $times) = @$self;
129 return ("$what$times", ($::immortals{$what}) x $times);
136 my ($what, $times) = $x =~ /(.)(\d+)/;
137 die "'$x' didn't match" unless defined $times;
138 main::is(scalar @refs, $times);
139 my $expect = $::immortals{$what};
140 die "'$x' did not give a reference" unless ref $expect;
143 $fail++ if $_ != $expect;
145 main::is($fail, undef);
150 # $Storable::DEBUGME = 1;
152 foreach $count (1..3) {
154 foreach $immortal (keys %::immortals) {
155 print "# $immortal x $count\n";
156 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
161 pass("thaw didn't crash");
165 # Test automatic require of packages to find thaw hook.
176 sub STORABLE_freeze {
183 my $f = freeze (HAS_HOOK->make);
185 is($HAS_HOOK::loaded_count, 0);
186 is($HAS_HOOK::thawed_count, 0);
189 is($HAS_HOOK::loaded_count, 1);
190 is($HAS_HOOK::thawed_count, 1);
192 is(ref $t, 'HAS_HOOK');
194 delete $INC{"HAS_HOOK.pm"};
195 delete $HAS_HOOK::{STORABLE_thaw};
198 is($HAS_HOOK::loaded_count, 2);
199 is($HAS_HOOK::thawed_count, 2);
201 is(ref $t, 'HAS_HOOK');
204 package STRESS_THE_STACK;
215 sub STORABLE_freeze {
218 return no_op(1..(++$stress * 2000)) ? die "can't happen" : '';
224 no_op(1..(++$stress * 2000)) && die "can't happen";
229 $STRESS_THE_STACK::freeze_count = 0;
230 $STRESS_THE_STACK::thaw_count = 0;
232 $f = freeze (STRESS_THE_STACK->make);
234 is($STRESS_THE_STACK::freeze_count, 1);
235 is($STRESS_THE_STACK::thaw_count, 0);
238 is($STRESS_THE_STACK::freeze_count, 1);
239 is($STRESS_THE_STACK::thaw_count, 1);
241 is(ref $t, 'STRESS_THE_STACK');
243 my $file = "storable-testfile.$$";
244 die "Temporary file '$file' already exists" if -e $file;
246 END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
248 $STRESS_THE_STACK::freeze_count = 0;
249 $STRESS_THE_STACK::thaw_count = 0;
251 store (STRESS_THE_STACK->make, $file);
253 is($STRESS_THE_STACK::freeze_count, 1);
254 is($STRESS_THE_STACK::thaw_count, 0);
256 $t = retrieve ($file);
257 is($STRESS_THE_STACK::freeze_count, 1);
258 is($STRESS_THE_STACK::thaw_count, 1);
260 is(ref $t, 'STRESS_THE_STACK');
263 package ModifyARG112358;
264 sub STORABLE_freeze { $_[0] = "foo"; }
265 my $o= {str=>bless {}};
267 ::is ref $o->{str}, __PACKAGE__,
268 'assignment to $_[0] in STORABLE_freeze does not corrupt things';
274 package WeirdRefHook;
275 sub STORABLE_freeze { () }
276 $INC{'WeirdRefHook.pm'} = __FILE__;
279 for my $weird (keys %weird_refs) {
280 my $obj = $weird_refs{$weird};
281 bless $obj, 'WeirdRefHook';
283 my $success = eval { $frozen = freeze($obj); 1 };
284 ok($success, "can freeze $weird objects")
285 || diag("freezing failed: $@");
286 my $thawn = thaw($frozen);
287 # is_deeply ignores blessings
288 is ref $thawn, ref $obj, "get the right blessing back for $weird";
289 if ($weird =~ 'VSTRING') {
290 # It is not just Storable that did not support vstrings. :-)
291 # See https://rt.cpan.org/Ticket/Display.html?id=78678
292 my $newver = "version"->can("new")
293 ? sub { "version"->new(shift) }
296 $$thawn eq $$obj && &$newver($$thawn) eq &$newver($$obj),
297 "get the right value back"
299 diag "$$thawn vs $$obj";
300 diag &$newver($$thawn) eq &$newver($$obj) if &$newver(1);
304 is_deeply($thawn, $obj, "get the right value back");