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