Commit | Line | Data |
---|---|---|
7a6a85bf | 1 | #!./perl |
7a6a85bf RG |
2 | # |
3 | # Copyright (c) 1995-2000, Raphael Manfredi | |
4 | # | |
9e21b3d0 JH |
5 | # You may redistribute only under the same terms as Perl 5, as specified |
6 | # in the README file that comes with the distribution. | |
7a6a85bf | 7 | # |
7a6a85bf RG |
8 | |
9 | sub BEGIN { | |
48c887dd | 10 | unshift @INC, 't'; |
1afdebce | 11 | unshift @INC, 't/compat' if $] < 5.006002; |
9f233367 | 12 | require Config; import Config; |
0c384302 | 13 | if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) { |
9f233367 PP |
14 | print "1..0 # Skip: Storable was not built\n"; |
15 | exit 0; | |
16 | } | |
372cb964 | 17 | require 'st-dump.pl'; |
7a6a85bf RG |
18 | } |
19 | ||
7a6a85bf RG |
20 | use Storable qw(freeze nfreeze thaw); |
21 | ||
c86b4700 TR |
22 | $Storable::flags = Storable::FLAGS_COMPAT; |
23 | ||
dddb60fc | 24 | use Test::More tests => 21; |
7a6a85bf RG |
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 | ||
dddb60fc NC |
38 | my $f1 = freeze(\@a); |
39 | isnt($f1, undef); | |
7a6a85bf RG |
40 | |
41 | $dumped = &dump(\@a); | |
dddb60fc | 42 | isnt($dumped, undef); |
7a6a85bf RG |
43 | |
44 | $root = thaw($f1); | |
dddb60fc | 45 | isnt($root, undef); |
7a6a85bf RG |
46 | |
47 | $got = &dump($root); | |
dddb60fc | 48 | isnt($got, undef); |
7a6a85bf | 49 | |
dddb60fc | 50 | is($got, $dumped); |
7a6a85bf RG |
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; | |
dddb60fc NC |
63 | my $f2 = $foo->freeze; |
64 | isnt($f2, undef); | |
7a6a85bf | 65 | |
dddb60fc NC |
66 | my $f3 = $foo->nfreeze; |
67 | isnt($f3, undef); | |
7a6a85bf RG |
68 | |
69 | $root3 = thaw($f3); | |
dddb60fc | 70 | isnt($root3, undef); |
7a6a85bf | 71 | |
dddb60fc | 72 | is(&dump($foo), &dump($root3)); |
7a6a85bf RG |
73 | |
74 | $root = thaw($f2); | |
dddb60fc | 75 | is(&dump($foo), &dump($root)); |
7a6a85bf | 76 | |
dddb60fc | 77 | is(&dump($root3), &dump($root)); |
7a6a85bf RG |
78 | |
79 | $other = freeze($root); | |
dddb60fc | 80 | is(length$other, length $f2); |
7a6a85bf RG |
81 | |
82 | $root2 = thaw($other); | |
dddb60fc | 83 | is(&dump($root2), &dump($root)); |
7a6a85bf RG |
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); | |
dddb60fc | 95 | is($VAR2->[3], $VAR1->[3]); |
7a6a85bf RG |
96 | |
97 | # Test the workaround for LVALUE bug in perl 5.004_04 -- from Gisle Aas | |
98 | sub foo { $_[0] = 1 } | |
99 | $foo = []; | |
100 | foo($foo->[1]); | |
101 | eval { freeze($foo) }; | |
dddb60fc | 102 | is($@, ''); |
7a6a85bf | 103 | |
e993d95c JH |
104 | # Test cleanup bug found by Claudio Garcia -- RAM, 08/06/2001 |
105 | my $thaw_me = 'asdasdasdasd'; | |
106 | ||
107 | eval { | |
108 | my $thawed = thaw $thaw_me; | |
109 | }; | |
dddb60fc | 110 | isnt($@, ''); |
e993d95c JH |
111 | |
112 | my %to_be_frozen = (foo => 'bar'); | |
113 | my $frozen; | |
114 | eval { | |
115 | $frozen = freeze \%to_be_frozen; | |
116 | }; | |
dddb60fc | 117 | is($@, ''); |
e993d95c JH |
118 | |
119 | freeze {}; | |
120 | eval { thaw $thaw_me }; | |
121 | eval { $frozen = freeze { foo => {} } }; | |
dddb60fc | 122 | is($@, ''); |
e993d95c JH |
123 | |
124 | thaw $frozen; # used to segfault here | |
dddb60fc | 125 | pass("Didn't segfault"); |
20bb3f55 | 126 | |
dddb60fc | 127 | SKIP: { |
dddb60fc | 128 | my (@a, @b); |
fcaa57e7 AMS |
129 | eval ' |
130 | $a = []; $#$a = 2; $a->[1] = undef; | |
131 | $b = thaw freeze $a; | |
132 | @a = map { ~~ exists $a->[$_] } 0 .. $#$a; | |
133 | @b = map { ~~ exists $b->[$_] } 0 .. $#$b; | |
fcaa57e7 | 134 | '; |
dddb60fc NC |
135 | is($@, ''); |
136 | is("@a", "@b"); | |
fcaa57e7 | 137 | } |