This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In Storable.xs fix #80074, caused by the Perl stack moving when expanded.
[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';
9f233367 11 require Config; import Config;
0c384302 12 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367
PP
13 print "1..0 # Skip: Storable was not built\n";
14 exit 0;
15 }
372cb964 16 require 'st-dump.pl';
7a6a85bf
RG
17}
18
19sub ok;
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;
8e88cfee 30my $tests = $test + 22 + 2 * 6 * keys %::immortals;
dfd91409 31print "1..$tests\n";
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
64die $@ if $@;
65ok 1, $@ eq '';
66
67eval <<EOC;
68package ${name}_WITH_HOOK;
69
70\@ISA = ("SHORT_NAME_WITH_HOOK");
71EOC
72ok 2, $@ eq '';
73
74# Construct a pool of objects
75my @pool;
76
77for (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);
82}
83
84my $x = freeze \@pool;
85ok 3, 1;
86
87my $y = thaw $x;
88ok 4, ref $y eq 'ARRAY';
89ok 5, @{$y} == @pool;
90
91ok 6, ref $y->[0] eq 'SHORT_NAME';
92ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
93ok 8, ref $y->[2] eq $name;
94ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
95
96my $good = 1;
97for (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";
102}
103ok 10, $good;
87baa35a
SR
104
105{
106 my $blessed_ref = bless \\[1,2,3], 'Foobar';
107 my $x = freeze $blessed_ref;
108 my $y = thaw $x;
109 ok 11, ref $y eq 'Foobar';
110 ok 12, $$$y->[0] == 1;
111}
dfd91409
NC
112
113package RETURNS_IMMORTALS;
114
115sub make { my $self = shift; bless [@_], $self }
116
117sub STORABLE_freeze {
118 # Some reference some number of times.
119 my $self = shift;
120 my ($what, $times) = @$self;
121 return ("$what$times", ($::immortals{$what}) x $times);
122}
123
124sub STORABLE_thaw {
125 my $self = shift;
126 my $cloning = shift;
127 my ($x, @refs) = @_;
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;
133 my $fail;
134 foreach (@refs) {
135 $fail++ if $_ != $expect;
136 }
137 main::ok ++$test, !$fail;
138}
139
140package main;
141
142# $Storable::DEBUGME = 1;
143my $count;
144foreach $count (1..3) {
145 my $immortal;
146 foreach $immortal (keys %::immortals) {
147 print "# $immortal x $count\n";
148 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
149
150 my $f = freeze ($i);
151 ok ++$test, $f;
152 my $t = thaw $f;
153 ok ++$test, 1;
154 }
155}
754c00ca
NC
156
157# Test automatic require of packages to find thaw hook.
158
159package HAS_HOOK;
160
161$loaded_count = 0;
162$thawed_count = 0;
163
164sub make {
165 bless [];
166}
167
168sub STORABLE_freeze {
169 my $self = shift;
170 return '';
171}
172
173package main;
174
175my $f = freeze (HAS_HOOK->make);
176
177ok ++$test, $HAS_HOOK::loaded_count == 0;
178ok ++$test, $HAS_HOOK::thawed_count == 0;
179
180my $t = thaw $f;
181ok ++$test, $HAS_HOOK::loaded_count == 1;
182ok ++$test, $HAS_HOOK::thawed_count == 1;
183ok ++$test, $t;
184ok ++$test, ref $t eq 'HAS_HOOK';
185
9d021ad4
NC
186delete $INC{"HAS_HOOK.pm"};
187delete $HAS_HOOK::{STORABLE_thaw};
188
189$t = thaw $f;
190ok ++$test, $HAS_HOOK::loaded_count == 2;
191ok ++$test, $HAS_HOOK::thawed_count == 2;
192ok ++$test, $t;
193ok ++$test, ref $t eq 'HAS_HOOK';
8e88cfee
NC
194
195{
196 package STRESS_THE_STACK;
197
198 my $stress;
199 sub make {
200 bless [];
201 }
202
203 sub no_op {
204 0;
205 }
206
207 sub STORABLE_freeze {
208 my $self = shift;
209 ++$freeze_count;
210 return no_op(1..(++$stress * 2000)) ? die "can't happen" : '';
211 }
212
213 sub STORABLE_thaw {
214 my $self = shift;
215 ++$thaw_count;
216 no_op(1..(++$stress * 2000)) && die "can't happen";
217 return;
218 }
219}
220
221$STRESS_THE_STACK::freeze_count = 0;
222$STRESS_THE_STACK::thaw_count = 0;
223
224$f = freeze (STRESS_THE_STACK->make);
225
226ok ++$test, $STRESS_THE_STACK::freeze_count == 1;
227ok ++$test, $STRESS_THE_STACK::thaw_count == 0;
228
229$t = thaw $f;
230ok ++$test, $STRESS_THE_STACK::freeze_count == 1;
231ok ++$test, $STRESS_THE_STACK::thaw_count == 1;
232ok ++$test, $t;
233ok ++$test, ref $t eq 'STRESS_THE_STACK';
234
235my $file = "storable-testfile.$$";
236die "Temporary file '$file' already exists" if -e $file;
237
238END { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
239
240$STRESS_THE_STACK::freeze_count = 0;
241$STRESS_THE_STACK::thaw_count = 0;
242
243store (STRESS_THE_STACK->make, $file);
244
245ok ++$test, $STRESS_THE_STACK::freeze_count == 1;
246ok ++$test, $STRESS_THE_STACK::thaw_count == 0;
247
248$t = retrieve ($file);
249ok ++$test, $STRESS_THE_STACK::freeze_count == 1;
250ok ++$test, $STRESS_THE_STACK::thaw_count == 1;
251ok ++$test, $t;
252ok ++$test, ref $t eq 'STRESS_THE_STACK';