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