This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document string- and number-specific bitops in perlop
[perl5.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     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
20 use Storable qw(freeze nfreeze thaw);
21
22 use Test::More tests => 21;
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 my $f1 = freeze(\@a);
37 isnt($f1, undef);
38
39 $dumped = &dump(\@a);
40 isnt($dumped, undef);
41
42 $root = thaw($f1);
43 isnt($root, undef);
44
45 $got = &dump($root);
46 isnt($got, undef);
47
48 is($got, $dumped);
49
50 package FOO; @ISA = qw(Storable);
51
52 sub make {
53         my $self = bless {};
54         $self->{key} = \%main::a;
55         return $self;
56 };
57
58 package main;
59
60 $foo = FOO->make;
61 my $f2 = $foo->freeze;
62 isnt($f2, undef);
63
64 my $f3 = $foo->nfreeze;
65 isnt($f3, undef);
66
67 $root3 = thaw($f3);
68 isnt($root3, undef);
69
70 is(&dump($foo), &dump($root3));
71
72 $root = thaw($f2);
73 is(&dump($foo), &dump($root));
74
75 is(&dump($root3), &dump($root));
76
77 $other = freeze($root);
78 is(length$other, length $f2);
79
80 $root2 = thaw($other);
81 is(&dump($root2), &dump($root));
82
83 $VAR1 = [
84         'method',
85         1,
86         'prepare',
87         'SELECT table_name, table_owner, num_rows FROM iitables
88                   where table_owner != \'$ingres\' and table_owner != \'DBA\''
89 ];
90
91 $x = nfreeze($VAR1);
92 $VAR2 = thaw($x);
93 is($VAR2->[3], $VAR1->[3]);
94
95 # Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas
96 sub foo { $_[0] = 1 }
97 $foo = [];
98 foo($foo->[1]);
99 eval { freeze($foo) };
100 is($@, '');
101
102 # Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001
103 my $thaw_me = 'asdasdasdasd';
104
105 eval {
106         my $thawed = thaw $thaw_me;
107 };
108 isnt($@, '');
109
110 my %to_be_frozen = (foo => 'bar');
111 my $frozen;
112 eval {
113         $frozen = freeze \%to_be_frozen;
114 };
115 is($@, '');
116
117 freeze {};
118 eval { thaw $thaw_me };
119 eval { $frozen = freeze { foo => {} } };
120 is($@, '');
121
122 thaw $frozen;                   # used to segfault here
123 pass("Didn't segfault");
124
125 SKIP: {
126     skip 'no av_exists', 2 unless $] >= 5.006;
127     my (@a, @b);
128     eval '
129         $a = []; $#$a = 2; $a->[1] = undef;
130         $b = thaw freeze $a;
131         @a = map { ~~ exists $a->[$_] } 0 .. $#$a;
132         @b = map { ~~ exists $b->[$_] } 0 .. $#$b;
133     ';
134     is($@, '');
135     is("@a", "@b");
136 }