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 thaw);
21$Storable::flags = Storable::FLAGS_COMPAT;
22
23use Test::More tests => 25;
24
25($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
26
27package TIED_HASH;
28
29sub TIEHASH {
30	my $self = bless {}, shift;
31	return $self;
32}
33
34sub FETCH {
35	my $self = shift;
36	my ($key) = @_;
37	$main::hash_fetch++;
38	return $self->{$key};
39}
40
41sub STORE {
42	my $self = shift;
43	my ($key, $value) = @_;
44	$self->{$key} = $value;
45}
46
47sub FIRSTKEY {
48	my $self = shift;
49	scalar keys %{$self};
50	return each %{$self};
51}
52
53sub NEXTKEY {
54	my $self = shift;
55	return each %{$self};
56}
57
58package TIED_ARRAY;
59
60sub TIEARRAY {
61	my $self = bless [], shift;
62	return $self;
63}
64
65sub FETCH {
66	my $self = shift;
67	my ($idx) = @_;
68	$main::array_fetch++;
69	return $self->[$idx];
70}
71
72sub STORE {
73	my $self = shift;
74	my ($idx, $value) = @_;
75	$self->[$idx] = $value;
76}
77
78sub FETCHSIZE {
79	my $self = shift;
80	return @{$self};
81}
82
83package TIED_SCALAR;
84
85sub TIESCALAR {
86	my $scalar;
87	my $self = bless \$scalar, shift;
88	return $self;
89}
90
91sub FETCH {
92	my $self = shift;
93	$main::scalar_fetch++;
94	return $$self;
95}
96
97sub STORE {
98	my $self = shift;
99	my ($value) = @_;
100	$$self = $value;
101}
102
103package FAULT;
104
105$fault = 0;
106
107sub TIESCALAR {
108	my $pkg = shift;
109	return bless [@_], $pkg;
110}
111
112sub FETCH {
113	my $self = shift;
114	my ($href, $key) = @$self;
115	$fault++;
116	untie $href->{$key};
117	return $href->{$key} = 1;
118}
119
120package main;
121
122$a = 'toto';
123$b = \$a;
124
125$c = tie %hash, TIED_HASH;
126$d = tie @array, TIED_ARRAY;
127tie $scalar, TIED_SCALAR;
128
129#$scalar = 'foo';
130#$hash{'attribute'} = \$d;
131#$array[0] = $c;
132#$array[1] = \$scalar;
133
134### If I say
135###   $hash{'attribute'} = $d;
136### below, then dump() incorrectly dumps the hash value as a string the second
137### time it is reached. I have not investigated enough to tell whether it's
138### a bug in my dump() routine or in the Perl tieing mechanism.
139$scalar = 'foo';
140$hash{'attribute'} = 'plain value';
141$array[0] = \$scalar;
142$array[1] = $c;
143$array[2] = \@array;
144
145@tied = (\$scalar, \@array, \%hash);
146%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
147@a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
148	$b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
149
150my $f = freeze(\@a);
151isnt($f, undef);
152
153$dumped = &dump(\@a);
154isnt($dumped, undef);
155
156$root = thaw($f);
157isnt($root, undef);
158
159$got = &dump($root);
160isnt($got, undef);
161
162### Used to see the manifestation of the bug documented above.
163### print "original: $dumped";
164### print "--------\n";
165### print "got: $got";
166### print "--------\n";
167
168is($got, $dumped);
169
170$g = freeze($root);
171is(length $f, length $g);
172
173# Ensure the tied items in the retrieved image work
174@old = ($scalar_fetch, $array_fetch, $hash_fetch);
175@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
176@type = qw(SCALAR  ARRAY  HASH);
177
178is(ref tied $$tscalar, 'TIED_SCALAR');
179is(ref tied @$tarray, 'TIED_ARRAY');
180is(ref tied %$thash, 'TIED_HASH');
181
182@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
183@new = ($scalar_fetch, $array_fetch, $hash_fetch);
184
185# Tests 10..15
186for ($i = 0; $i < @new; $i++) {
187	is($new[$i], $old[$i] + 1);
188	is(ref $tied[$i], $type[$i]);
189}
190
191# Check undef ties
192my $h = {};
193tie $h->{'x'}, 'FAULT', $h, 'x';
194my $hf = freeze($h);
195isnt($hf, undef);
196is($FAULT::fault, 0);
197is($h->{'x'}, 1);
198is($FAULT::fault, 1);
199
200my $ht = thaw($hf);
201isnt($ht, undef);
202is($ht->{'x'}, 1);
203is($FAULT::fault, 2);
204
205{
206    package P;
207    use Storable qw(freeze thaw);
208    our ($a, $b);
209    $b = "not ok ";
210    sub TIESCALAR { bless \$a } sub FETCH { "ok " }
211    tie $a, P; my $r = thaw freeze \$a; $b = $$r;
212    main::is($b, "ok ");
213}
214
215{
216    # blessed ref to tied object should be thawed blessed
217    my @a;
218    tie @a, TIED_ARRAY;
219    my $r = bless \@a, 'FOO99';
220    my $f = freeze($r);
221    my $t = thaw($f);
222    isnt($t, undef);
223    like("$t", qr/^FOO99=ARRAY/);
224}
225