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);
30 my $tests = $test + 6 + 2 * 6 * keys %::immortals;
35 sub make { bless [], shift }
37 package SHORT_NAME_WITH_HOOK;
39 sub make { bless [], shift }
50 die "STORABLE_thaw" unless $obj eq $self;
55 # Still less than 256 bytes, so long classname logic not fully exercised
56 # Wait until Perl removes the restriction on identifier lengths.
57 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
62 \@ISA = ("SHORT_NAME");
68 package ${name}_WITH_HOOK;
70 \@ISA = ("SHORT_NAME_WITH_HOOK");
74 # Construct a pool of objects
77 for (my $i = 0; $i < 10; $i++) {
78 push(@pool, SHORT_NAME->make);
79 push(@pool, SHORT_NAME_WITH_HOOK->make);
80 push(@pool, $name->make);
81 push(@pool, "${name}_WITH_HOOK"->make);
84 my $x = freeze \@pool;
88 ok 4, ref $y eq 'ARRAY';
91 ok 6, ref $y->[0] eq 'SHORT_NAME';
92 ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
93 ok 8, ref $y->[2] eq $name;
94 ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
97 for (my $i = 0; $i < 10; $i++) {
98 do { $good = 0; last } unless ref $y->[4*$i] eq 'SHORT_NAME';
99 do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
100 do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
101 do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
106 my $blessed_ref = bless \\[1,2,3], 'Foobar';
107 my $x = freeze $blessed_ref;
109 ok 11, ref $y eq 'Foobar';
110 ok 12, $$$y->[0] == 1;
113 package RETURNS_IMMORTALS;
115 sub make { my $self = shift; bless [@_], $self }
117 sub STORABLE_freeze {
118 # Some reference some number of times.
120 my ($what, $times) = @$self;
121 return ("$what$times", ($::immortals{$what}) x $times);
128 my ($what, $times) = $x =~ /(.)(\d+)/;
129 die "'$x' didn't match" unless defined $times;
130 main::ok ++$test, @refs == $times;
131 my $expect = $::immortals{$what};
132 die "'$x' did not give a reference" unless ref $expect;
135 $fail++ if $_ != $expect;
137 main::ok ++$test, !$fail;
142 # $Storable::DEBUGME = 1;
144 foreach $count (1..3) {
146 foreach $immortal (keys %::immortals) {
147 print "# $immortal x $count\n";
148 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
157 # Test automatic require of packages to find thaw hook.
168 sub STORABLE_freeze {
175 my $f = freeze (HAS_HOOK->make);
177 ok ++$test, $HAS_HOOK::loaded_count == 0;
178 ok ++$test, $HAS_HOOK::thawed_count == 0;
181 ok ++$test, $HAS_HOOK::loaded_count == 1;
182 ok ++$test, $HAS_HOOK::thawed_count == 1;
184 ok ++$test, ref $t eq 'HAS_HOOK';
186 # Can't do this because the method is still cached by UNIVERSAL::can
187 # delete $INC{"HAS_HOOK.pm"};
188 # undef &HAS_HOOK::STORABLE_thaw;
190 # warn HAS_HOOK->can('STORABLE_thaw');
192 # ok ++$test, $HAS_HOOK::loaded_count == 2;
193 # ok ++$test, $HAS_HOOK::thawed_count == 2;
195 # ok ++$test, ref $t eq 'HAS_HOOK';