xref: /openbsd-src/gnu/usr.bin/perl/dist/Storable/t/tied_hook.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
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);
21use Test::More tests => 28;
22
23($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
24
25package TIED_HASH;
26
27sub TIEHASH {
28	my $self = bless {}, shift;
29	return $self;
30}
31
32sub FETCH {
33	my $self = shift;
34	my ($key) = @_;
35	$main::hash_fetch++;
36	return $self->{$key};
37}
38
39sub STORE {
40	my $self = shift;
41	my ($key, $value) = @_;
42	$self->{$key} = $value;
43}
44
45sub FIRSTKEY {
46	my $self = shift;
47	scalar keys %{$self};
48	return each %{$self};
49}
50
51sub NEXTKEY {
52	my $self = shift;
53	return each %{$self};
54}
55
56sub STORABLE_freeze {
57	my $self = shift;
58	$main::hash_hook1++;
59	return join(":", keys %$self) . ";" . join(":", values %$self);
60}
61
62sub STORABLE_thaw {
63	my ($self, $cloning, $frozen) = @_;
64	my ($keys, $values) = split(/;/, $frozen);
65	my @keys = split(/:/, $keys);
66	my @values = split(/:/, $values);
67	for (my $i = 0; $i < @keys; $i++) {
68		$self->{$keys[$i]} = $values[$i];
69	}
70	$main::hash_hook2++;
71}
72
73package TIED_ARRAY;
74
75sub TIEARRAY {
76	my $self = bless [], shift;
77	return $self;
78}
79
80sub FETCH {
81	my $self = shift;
82	my ($idx) = @_;
83	$main::array_fetch++;
84	return $self->[$idx];
85}
86
87sub STORE {
88	my $self = shift;
89	my ($idx, $value) = @_;
90	$self->[$idx] = $value;
91}
92
93sub FETCHSIZE {
94	my $self = shift;
95	return @{$self};
96}
97
98sub STORABLE_freeze {
99	my $self = shift;
100	$main::array_hook1++;
101	return join(":", @$self);
102}
103
104sub STORABLE_thaw {
105	my ($self, $cloning, $frozen) = @_;
106	@$self = split(/:/, $frozen);
107	$main::array_hook2++;
108}
109
110package TIED_SCALAR;
111
112sub TIESCALAR {
113	my $scalar;
114	my $self = bless \$scalar, shift;
115	return $self;
116}
117
118sub FETCH {
119	my $self = shift;
120	$main::scalar_fetch++;
121	return $$self;
122}
123
124sub STORE {
125	my $self = shift;
126	my ($value) = @_;
127	$$self = $value;
128}
129
130sub STORABLE_freeze {
131	my $self = shift;
132	$main::scalar_hook1++;
133	return $$self;
134}
135
136sub STORABLE_thaw {
137	my ($self, $cloning, $frozen) = @_;
138	$$self = $frozen;
139	$main::scalar_hook2++;
140}
141
142package main;
143
144$a = 'toto';
145$b = \$a;
146
147$c = tie %hash, TIED_HASH;
148$d = tie @array, TIED_ARRAY;
149tie $scalar, TIED_SCALAR;
150
151$scalar = 'foo';
152$hash{'attribute'} = 'plain value';
153$array[0] = \$scalar;
154$array[1] = $c;
155$array[2] = \@array;
156$array[3] = "plaine scalaire";
157
158@tied = (\$scalar, \@array, \%hash);
159%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
160@a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
161	$b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
162
163my $f = freeze(\@a);
164isnt($f, undef);
165$dumped = &dump(\@a);
166isnt($dumped, undef);
167
168$root = thaw($f);
169isnt($root, undef);
170
171$got = &dump($root);
172isnt($got, undef);
173
174isnt($got, $dumped);		# our hooks did not handle refs in array
175
176$g = freeze($root);
177is(length $f, length $g);
178
179# Ensure the tied items in the retrieved image work
180@old = ($scalar_fetch, $array_fetch, $hash_fetch);
181@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
182@type = qw(SCALAR  ARRAY  HASH);
183
184is(ref tied $$tscalar, 'TIED_SCALAR');
185is(ref tied @$tarray, 'TIED_ARRAY');
186is(ref tied %$thash, 'TIED_HASH');
187
188@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
189@new = ($scalar_fetch, $array_fetch, $hash_fetch);
190
191# Tests 10..15
192for ($i = 0; $i < @new; $i++) {
193    is($new[$i], $old[$i] + 1);		# Tests 10,12,14
194    is(ref $tied[$i], $type[$i]);	# Tests 11,13,15
195}
196
197is($$tscalar, 'foo');
198is($tarray->[3], 'plaine scalaire');
199is($thash->{'attribute'}, 'plain value');
200
201# Ensure hooks were called
202is($scalar_hook1, 2);
203is($scalar_hook2, 1);
204is($array_hook1, 2);
205is($array_hook2, 1);
206is($hash_hook1, 2);
207is($hash_hook2, 1);
208
209#
210# And now for the "blessed ref to tied hash" with "store hook" test...
211#
212
213my $bc = bless \%hash, 'FOO';		# FOO does not exist -> no hook
214my $bx = thaw freeze $bc;
215
216is(ref $bx, 'FOO');
217my $old_hash_fetch = $hash_fetch;
218my $v = $bx->{attribute};
219is($hash_fetch, $old_hash_fetch + 1, 'Still tied');
220
221package TIED_HASH_REF;
222
223
224sub STORABLE_freeze {
225        my ($self, $cloning) = @_;
226        return if $cloning;
227        return('ref lost');
228}
229
230sub STORABLE_thaw {
231        my ($self, $cloning, $data) = @_;
232        return if $cloning;
233}
234
235package main;
236
237$bc = bless \%hash, 'TIED_HASH_REF';
238$bx = thaw freeze $bc;
239
240is(ref $bx, 'TIED_HASH_REF');
241$old_hash_fetch = $hash_fetch;
242$v = $bx->{attribute};
243is($hash_fetch, $old_hash_fetch + 1, 'Still tied');
244