This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove code in Storable's tests that handles perl < v5.6.1
[perl5.git] / dist / Storable / t / freeze.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 }
372cb964 17 require 'st-dump.pl';
7a6a85bf
RG
18}
19
7a6a85bf
RG
20use Storable qw(freeze nfreeze thaw);
21
c86b4700
TR
22$Storable::flags = Storable::FLAGS_COMPAT;
23
dddb60fc 24use Test::More tests => 21;
7a6a85bf
RG
25
26$a = 'toto';
27$b = \$a;
28$c = bless {}, CLASS;
29$c->{attribute} = $b;
30$d = {};
31$e = [];
32$d->{'a'} = $e;
33$e->[0] = $d;
34%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
35@a = ('first', undef, 3, -4, -3.14159, 456, 4.5, $d, \$d, \$e, $e,
36 $b, \$a, $a, $c, \$c, \%a);
37
dddb60fc
NC
38my $f1 = freeze(\@a);
39isnt($f1, undef);
7a6a85bf
RG
40
41$dumped = &dump(\@a);
dddb60fc 42isnt($dumped, undef);
7a6a85bf
RG
43
44$root = thaw($f1);
dddb60fc 45isnt($root, undef);
7a6a85bf
RG
46
47$got = &dump($root);
dddb60fc 48isnt($got, undef);
7a6a85bf 49
dddb60fc 50is($got, $dumped);
7a6a85bf
RG
51
52package FOO; @ISA = qw(Storable);
53
54sub make {
55 my $self = bless {};
56 $self->{key} = \%main::a;
57 return $self;
58};
59
60package main;
61
62$foo = FOO->make;
dddb60fc
NC
63my $f2 = $foo->freeze;
64isnt($f2, undef);
7a6a85bf 65
dddb60fc
NC
66my $f3 = $foo->nfreeze;
67isnt($f3, undef);
7a6a85bf
RG
68
69$root3 = thaw($f3);
dddb60fc 70isnt($root3, undef);
7a6a85bf 71
dddb60fc 72is(&dump($foo), &dump($root3));
7a6a85bf
RG
73
74$root = thaw($f2);
dddb60fc 75is(&dump($foo), &dump($root));
7a6a85bf 76
dddb60fc 77is(&dump($root3), &dump($root));
7a6a85bf
RG
78
79$other = freeze($root);
dddb60fc 80is(length$other, length $f2);
7a6a85bf
RG
81
82$root2 = thaw($other);
dddb60fc 83is(&dump($root2), &dump($root));
7a6a85bf
RG
84
85$VAR1 = [
86 'method',
87 1,
88 'prepare',
89 'SELECT table_name, table_owner, num_rows FROM iitables
90 where table_owner != \'$ingres\' and table_owner != \'DBA\''
91];
92
93$x = nfreeze($VAR1);
94$VAR2 = thaw($x);
dddb60fc 95is($VAR2->[3], $VAR1->[3]);
7a6a85bf
RG
96
97# Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas
98sub foo { $_[0] = 1 }
99$foo = [];
100foo($foo->[1]);
101eval { freeze($foo) };
dddb60fc 102is($@, '');
7a6a85bf 103
e993d95c
JH
104# Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001
105my $thaw_me = 'asdasdasdasd';
106
107eval {
108 my $thawed = thaw $thaw_me;
109};
dddb60fc 110isnt($@, '');
e993d95c
JH
111
112my %to_be_frozen = (foo => 'bar');
113my $frozen;
114eval {
115 $frozen = freeze \%to_be_frozen;
116};
dddb60fc 117is($@, '');
e993d95c
JH
118
119freeze {};
120eval { thaw $thaw_me };
121eval { $frozen = freeze { foo => {} } };
dddb60fc 122is($@, '');
e993d95c
JH
123
124thaw $frozen; # used to segfault here
dddb60fc 125pass("Didn't segfault");
20bb3f55 126
dddb60fc 127SKIP: {
dddb60fc 128 my (@a, @b);
fcaa57e7
AMS
129 eval '
130 $a = []; $#$a = 2; $a->[1] = undef;
131 $b = thaw freeze $a;
132 @a = map { ~~ exists $a->[$_] } 0 .. $#$a;
133 @b = map { ~~ exists $b->[$_] } 0 .. $#$b;
fcaa57e7 134 ';
dddb60fc
NC
135 is($@, '');
136 is("@a", "@b");
fcaa57e7 137}