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