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