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