This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Uncomment and fix up tests at the end of Storable's blessed.t
[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';
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';
e993d95c 17 sub ok;
7a6a85bf
RG
18}
19
7a6a85bf
RG
20use Storable qw(freeze nfreeze thaw);
21
20bb3f55 22print "1..20\n";
7a6a85bf
RG
23
24$a = 'toto';
25$b = \$a;
26$c = bless {}, CLASS;
27$c->{attribute} = $b;
28$d = {};
29$e = [];
30$d->{'a'} = $e;
31$e->[0] = $d;
32%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
33@a = ('first', undef, 3, -4, -3.14159, 456, 4.5, $d, \$d, \$e, $e,
34 $b, \$a, $a, $c, \$c, \%a);
35
36print "not " unless defined ($f1 = freeze(\@a));
37print "ok 1\n";
38
39$dumped = &dump(\@a);
40print "ok 2\n";
41
42$root = thaw($f1);
43print "not " unless defined $root;
44print "ok 3\n";
45
46$got = &dump($root);
47print "ok 4\n";
48
49print "not " unless $got eq $dumped;
50print "ok 5\n";
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;
63print "not " unless $f2 = $foo->freeze;
64print "ok 6\n";
65
66print "not " unless $f3 = $foo->nfreeze;
67print "ok 7\n";
68
69$root3 = thaw($f3);
70print "not " unless defined $root3;
71print "ok 8\n";
72
73print "not " unless &dump($foo) eq &dump($root3);
74print "ok 9\n";
75
76$root = thaw($f2);
77print "not " unless &dump($foo) eq &dump($root);
78print "ok 10\n";
79
80print "not " unless &dump($root3) eq &dump($root);
81print "ok 11\n";
82
83$other = freeze($root);
84print "not " unless length($other) == length($f2);
85print "ok 12\n";
86
87$root2 = thaw($other);
88print "not " unless &dump($root2) eq &dump($root);
89print "ok 13\n";
90
91$VAR1 = [
92 'method',
93 1,
94 'prepare',
95 'SELECT table_name, table_owner, num_rows FROM iitables
96 where table_owner != \'$ingres\' and table_owner != \'DBA\''
97];
98
99$x = nfreeze($VAR1);
100$VAR2 = thaw($x);
101print "not " unless $VAR2->[3] eq $VAR1->[3];
102print "ok 14\n";
103
104# Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas
105sub foo { $_[0] = 1 }
106$foo = [];
107foo($foo->[1]);
108eval { freeze($foo) };
109print "not " if $@;
110print "ok 15\n";
111
e993d95c
JH
112# Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001
113my $thaw_me = 'asdasdasdasd';
114
115eval {
116 my $thawed = thaw $thaw_me;
117};
118ok 16, $@;
119
120my %to_be_frozen = (foo => 'bar');
121my $frozen;
122eval {
123 $frozen = freeze \%to_be_frozen;
124};
125ok 17, !$@;
126
127freeze {};
128eval { thaw $thaw_me };
129eval { $frozen = freeze { foo => {} } };
130ok 18, !$@;
131
132thaw $frozen; # used to segfault here
133ok 19, 1;
20bb3f55 134
fcaa57e7
AMS
135if ($] >= 5.006) {
136 eval '
137 $a = []; $#$a = 2; $a->[1] = undef;
138 $b = thaw freeze $a;
139 @a = map { ~~ exists $a->[$_] } 0 .. $#$a;
140 @b = map { ~~ exists $b->[$_] } 0 .. $#$b;
141 ok 20, "@a" eq "@b";
142 ';
143}
144else {
145 print "ok 20 # skipped (no av_exists)\n";
146}