Re: [PATCH] another Storable test (Re: perl@16005)
[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 = '.'; 
22         push @INC, '../lib';
23     }
24     require Config; import Config;
25     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
26         print "1..0 # Skip: Storable was not built\n";
27         exit 0;
28     }
29     require 'lib/st-dump.pl';
30     sub ok;
31 }
32
33 use Storable qw(freeze nfreeze thaw);
34
35 print "1..19\n";
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
49 print "not " unless defined ($f1 = freeze(\@a));
50 print "ok 1\n";
51
52 $dumped = &dump(\@a);
53 print "ok 2\n";
54
55 $root = thaw($f1);
56 print "not " unless defined $root;
57 print "ok 3\n";
58
59 $got = &dump($root);
60 print "ok 4\n";
61
62 print "not " unless $got eq $dumped; 
63 print "ok 5\n";
64
65 package FOO; @ISA = qw(Storable);
66
67 sub make {
68         my $self = bless {};
69         $self->{key} = \%main::a;
70         return $self;
71 };
72
73 package main;
74
75 $foo = FOO->make;
76 print "not " unless $f2 = $foo->freeze;
77 print "ok 6\n";
78
79 print "not " unless $f3 = $foo->nfreeze;
80 print "ok 7\n";
81
82 $root3 = thaw($f3);
83 print "not " unless defined $root3;
84 print "ok 8\n";
85
86 print "not " unless &dump($foo) eq &dump($root3);
87 print "ok 9\n";
88
89 $root = thaw($f2);
90 print "not " unless &dump($foo) eq &dump($root);
91 print "ok 10\n";
92
93 print "not " unless &dump($root3) eq &dump($root);
94 print "ok 11\n";
95
96 $other = freeze($root);
97 print "not " unless length($other) == length($f2);
98 print "ok 12\n";
99
100 $root2 = thaw($other);
101 print "not " unless &dump($root2) eq &dump($root);
102 print "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);
114 print "not " unless $VAR2->[3] eq $VAR1->[3];
115 print "ok 14\n";
116
117 # Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas
118 sub foo { $_[0] = 1 }
119 $foo = [];
120 foo($foo->[1]);
121 eval { freeze($foo) };
122 print "not " if $@;
123 print "ok 15\n";
124
125 # Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001
126 my $thaw_me = 'asdasdasdasd';
127
128 eval {
129         my $thawed = thaw $thaw_me;
130 };
131 ok 16, $@;
132
133 my %to_be_frozen = (foo => 'bar');
134 my $frozen;
135 eval {
136         $frozen = freeze \%to_be_frozen;
137 };
138 ok 17, !$@;
139
140 freeze {};
141 eval { thaw $thaw_me };
142 eval { $frozen = freeze { foo => {} } };
143 ok 18, !$@;
144
145 thaw $frozen;                   # used to segfault here
146 ok 19, 1;
147