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