Re: perl@16433
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / freeze.t
CommitLineData
7a6a85bf 1#!./perl
2
e993d95c 3# $Id: freeze.t,v 1.0.1.1 2001/07/01 11:25:16 ram Exp $
7a6a85bf 4#
5# Copyright (c) 1995-2000, Raphael Manfredi
6#
9e21b3d0 7# You may redistribute only under the same terms as Perl 5, as specified
8# in the README file that comes with the distribution.
7a6a85bf 9#
10# $Log: freeze.t,v $
e993d95c 11# Revision 1.0.1.1 2001/07/01 11:25:16 ram
12# patch12: added test cases for mem corruption during thaw()
13#
9e21b3d0 14# Revision 1.0 2000/09/01 19:40:41 ram
15# Baseline for first official release.
7a6a85bf 16#
17
18sub BEGIN {
0c384302 19 if ($ENV{PERL_CORE}){
20 chdir('t') if -d 't';
21 @INC = '.';
22 push @INC, '../lib';
23 }
9f233367 24 require Config; import Config;
0c384302 25 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 26 print "1..0 # Skip: Storable was not built\n";
27 exit 0;
28 }
7a6a85bf 29 require 'lib/st-dump.pl';
e993d95c 30 sub ok;
7a6a85bf 31}
32
7a6a85bf 33use Storable qw(freeze nfreeze thaw);
34
e993d95c 35print "1..19\n";
7a6a85bf 36
37$a = 'toto';
38$b = \$a;
39$c = bless {}, CLASS;
40$c->{attribute} = $b;
41$d = {};
42$e = [];
43$d->{'a'} = $e;
44$e->[0] = $d;
45%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
46@a = ('first', undef, 3, -4, -3.14159, 456, 4.5, $d, \$d, \$e, $e,
47 $b, \$a, $a, $c, \$c, \%a);
48
49print "not " unless defined ($f1 = freeze(\@a));
50print "ok 1\n";
51
52$dumped = &dump(\@a);
53print "ok 2\n";
54
55$root = thaw($f1);
56print "not " unless defined $root;
57print "ok 3\n";
58
59$got = &dump($root);
60print "ok 4\n";
61
62print "not " unless $got eq $dumped;
63print "ok 5\n";
64
65package FOO; @ISA = qw(Storable);
66
67sub make {
68 my $self = bless {};
69 $self->{key} = \%main::a;
70 return $self;
71};
72
73package main;
74
75$foo = FOO->make;
76print "not " unless $f2 = $foo->freeze;
77print "ok 6\n";
78
79print "not " unless $f3 = $foo->nfreeze;
80print "ok 7\n";
81
82$root3 = thaw($f3);
83print "not " unless defined $root3;
84print "ok 8\n";
85
86print "not " unless &dump($foo) eq &dump($root3);
87print "ok 9\n";
88
89$root = thaw($f2);
90print "not " unless &dump($foo) eq &dump($root);
91print "ok 10\n";
92
93print "not " unless &dump($root3) eq &dump($root);
94print "ok 11\n";
95
96$other = freeze($root);
97print "not " unless length($other) == length($f2);
98print "ok 12\n";
99
100$root2 = thaw($other);
101print "not " unless &dump($root2) eq &dump($root);
102print "ok 13\n";
103
104$VAR1 = [
105 'method',
106 1,
107 'prepare',
108 'SELECT table_name, table_owner, num_rows FROM iitables
109 where table_owner != \'$ingres\' and table_owner != \'DBA\''
110];
111
112$x = nfreeze($VAR1);
113$VAR2 = thaw($x);
114print "not " unless $VAR2->[3] eq $VAR1->[3];
115print "ok 14\n";
116
117# Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas
118sub foo { $_[0] = 1 }
119$foo = [];
120foo($foo->[1]);
121eval { freeze($foo) };
122print "not " if $@;
123print "ok 15\n";
124
e993d95c 125# Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001
126my $thaw_me = 'asdasdasdasd';
127
128eval {
129 my $thawed = thaw $thaw_me;
130};
131ok 16, $@;
132
133my %to_be_frozen = (foo => 'bar');
134my $frozen;
135eval {
136 $frozen = freeze \%to_be_frozen;
137};
138ok 17, !$@;
139
140freeze {};
141eval { thaw $thaw_me };
142eval { $frozen = freeze { foo => {} } };
143ok 18, !$@;
144
145thaw $frozen; # used to segfault here
146ok 19, 1;
147