This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #112358] Storable: Don’t create RV with no refcnt
[perl5.git] / dist / Storable / t / blessed.t
CommitLineData
7a6a85bf 1#!./perl
7a6a85bf
RG
2#
3# Copyright (c) 1995-2000, Raphael Manfredi
4#
9e21b3d0
JH
5# You may redistribute only under the same terms as Perl 5, as specified
6# in the README file that comes with the distribution.
7a6a85bf 7#
7a6a85bf
RG
8
9sub BEGIN {
48c887dd 10 unshift @INC, 't';
1afdebce 11 unshift @INC, 't/compat' if $] < 5.006002;
9f233367 12 require Config; import Config;
0c384302 13 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367
PP
14 print "1..0 # Skip: Storable was not built\n";
15 exit 0;
16 }
7a6a85bf
RG
17}
18
dddb60fc 19use Test::More;
7a6a85bf 20
8e88cfee 21use Storable qw(freeze thaw store retrieve);
7a6a85bf 22
dfd91409
NC
23%::immortals
24 = (u => \undef,
25 'y' => \(1 == 1),
26 n => \(1 == 0)
27);
28
29my $test = 12;
27cc3b5a 30my $tests = $test + 23 + 2 * 6 * keys %::immortals;
dddb60fc 31plan(tests => $tests);
7a6a85bf
RG
32
33package SHORT_NAME;
34
35sub make { bless [], shift }
36
37package SHORT_NAME_WITH_HOOK;
38
39sub make { bless [], shift }
40
41sub STORABLE_freeze {
42 my $self = shift;
43 return ("", $self);
44}
45
46sub STORABLE_thaw {
47 my $self = shift;
48 my $cloning = shift;
49 my ($x, $obj) = @_;
50 die "STORABLE_thaw" unless $obj eq $self;
51}
52
53package main;
54
55# Still less than 256 bytes, so long classname logic not fully exercised
56# Wait until Perl removes the restriction on identifier lengths.
57my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
58
59eval <<EOC;
60package $name;
61
62\@ISA = ("SHORT_NAME");
63EOC
dddb60fc 64is($@, '');
7a6a85bf
RG
65
66eval <<EOC;
67package ${name}_WITH_HOOK;
68
69\@ISA = ("SHORT_NAME_WITH_HOOK");
70EOC
dddb60fc 71is($@, '');
7a6a85bf
RG
72
73# Construct a pool of objects
74my @pool;
75
76for (my $i = 0; $i < 10; $i++) {
77 push(@pool, SHORT_NAME->make);
78 push(@pool, SHORT_NAME_WITH_HOOK->make);
79 push(@pool, $name->make);
80 push(@pool, "${name}_WITH_HOOK"->make);
81}
82
83my $x = freeze \@pool;
dddb60fc 84pass("Freeze didn't crash");
7a6a85bf
RG
85
86my $y = thaw $x;
dddb60fc
NC
87is(ref $y, 'ARRAY');
88is(scalar @{$y}, @pool);
7a6a85bf 89
dddb60fc
NC
90is(ref $y->[0], 'SHORT_NAME');
91is(ref $y->[1], 'SHORT_NAME_WITH_HOOK');
92is(ref $y->[2], $name);
93is(ref $y->[3], "${name}_WITH_HOOK");
7a6a85bf
RG
94
95my $good = 1;
96for (my $i = 0; $i < 10; $i++) {
97 do { $good = 0; last } unless ref $y->[4*$i] eq 'SHORT_NAME';
98 do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
99 do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
100 do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
101}
dddb60fc 102is($good, 1);
87baa35a
SR
103
104{
105 my $blessed_ref = bless \\[1,2,3], 'Foobar';
106 my $x = freeze $blessed_ref;
107 my $y = thaw $x;
dddb60fc
NC
108 is(ref $y, 'Foobar');
109 is($$$y->[0], 1);
87baa35a 110}
dfd91409
NC
111
112package RETURNS_IMMORTALS;
113
114sub make { my $self = shift; bless [@_], $self }
115
116sub STORABLE_freeze {
117 # Some reference some number of times.
118 my $self = shift;
119 my ($what, $times) = @$self;
120 return ("$what$times", ($::immortals{$what}) x $times);
121}
122
123sub STORABLE_thaw {
124 my $self = shift;
125 my $cloning = shift;
126 my ($x, @refs) = @_;
127 my ($what, $times) = $x =~ /(.)(\d+)/;
128 die "'$x' didn't match" unless defined $times;
dddb60fc 129 main::is(scalar @refs, $times);
dfd91409
NC
130 my $expect = $::immortals{$what};
131 die "'$x' did not give a reference" unless ref $expect;
132 my $fail;
133 foreach (@refs) {
134 $fail++ if $_ != $expect;
135 }
dddb60fc 136 main::is($fail, undef);
dfd91409
NC
137}
138
139package main;
140
141# $Storable::DEBUGME = 1;
142my $count;
143foreach $count (1..3) {
144 my $immortal;
145 foreach $immortal (keys %::immortals) {
146 print "# $immortal x $count\n";
147 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
148
149 my $f = freeze ($i);
dddb60fc 150 isnt($f, undef);
dfd91409 151 my $t = thaw $f;
dddb60fc 152 pass("thaw didn't crash");
dfd91409
NC
153 }
154}
754c00ca
NC
155
156# Test automatic require of packages to find thaw hook.
157
158package HAS_HOOK;
159
160$loaded_count = 0;
161$thawed_count = 0;
162
163sub make {
164 bless [];
165}
166
167sub STORABLE_freeze {
168 my $self = shift;
169 return '';
170}
171
172package main;
173
174my $f = freeze (HAS_HOOK->make);
175
dddb60fc
NC
176is($HAS_HOOK::loaded_count, 0);
177is($HAS_HOOK::thawed_count, 0);
754c00ca
NC
178
179my $t = thaw $f;
dddb60fc
NC
180is($HAS_HOOK::loaded_count, 1);
181is($HAS_HOOK::thawed_count, 1);
182isnt($t, undef);
183is(ref $t, 'HAS_HOOK');
754c00ca 184
9d021ad4
NC
185delete $INC{"HAS_HOOK.pm"};
186delete $HAS_HOOK::{STORABLE_thaw};
187
188$t = thaw $f;
dddb60fc
NC
189is($HAS_HOOK::loaded_count, 2);
190is($HAS_HOOK::thawed_count, 2);
191isnt($t, undef);
192is(ref $t, 'HAS_HOOK');
8e88cfee
NC
193
194{
195 package STRESS_THE_STACK;
196
197 my $stress;
198 sub make {
199 bless [];
200 }
201
202 sub no_op {
203 0;
204 }
205
206 sub STORABLE_freeze {
207 my $self = shift;
208 ++$freeze_count;
209 return no_op(1..(++$stress * 2000)) ? die "can't happen" : '';
210 }
211
212 sub STORABLE_thaw {
213 my $self = shift;
214 ++$thaw_count;
215 no_op(1..(++$stress * 2000)) && die "can't happen";
216 return;
217 }
218}
219
220$STRESS_THE_STACK::freeze_count = 0;
221$STRESS_THE_STACK::thaw_count = 0;
222
223$f = freeze (STRESS_THE_STACK->make);
224
dddb60fc
NC
225is($STRESS_THE_STACK::freeze_count, 1);
226is($STRESS_THE_STACK::thaw_count, 0);
8e88cfee
NC
227
228$t = thaw $f;
dddb60fc
NC
229is($STRESS_THE_STACK::freeze_count, 1);
230is($STRESS_THE_STACK::thaw_count, 1);
231isnt($t, undef);
232is(ref $t, 'STRESS_THE_STACK');
8e88cfee
NC
233
234my $file = "storable-testfile.$$";
235die "Temporary file '$file' already exists" if -e $file;
236
237END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
238
239$STRESS_THE_STACK::freeze_count = 0;
240$STRESS_THE_STACK::thaw_count = 0;
241
242store (STRESS_THE_STACK->make, $file);
243
dddb60fc
NC
244is($STRESS_THE_STACK::freeze_count, 1);
245is($STRESS_THE_STACK::thaw_count, 0);
8e88cfee
NC
246
247$t = retrieve ($file);
dddb60fc
NC
248is($STRESS_THE_STACK::freeze_count, 1);
249is($STRESS_THE_STACK::thaw_count, 1);
250isnt($t, undef);
251is(ref $t, 'STRESS_THE_STACK');
27cc3b5a
FC
252
253{
254 package ModifyARG112358;
255 sub STORABLE_freeze { $_[0] = "foo"; }
256 my $o= {str=>bless {}};
257 my $f= ::freeze($o);
258 ::is ref $o->{str}, __PACKAGE__,
259 'assignment to $_[0] in STORABLE_freeze does not corrupt things';
260}