xref: /openbsd/gnu/usr.bin/perl/dist/Storable/t/tied.t (revision 891d7ab6)
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    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}
18
19sub ok;
20
21use Storable qw(freeze thaw);
22
23print "1..23\n";
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() incorectly 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
150ok 1, defined($f = freeze(\@a));
151
152$dumped = &dump(\@a);
153ok 2, 1;
154
155$root = thaw($f);
156ok 3, defined $root;
157
158$got = &dump($root);
159ok 4, 1;
160
161### Used to see the manifestation of the bug documented above.
162### print "original: $dumped";
163### print "--------\n";
164### print "got: $got";
165### print "--------\n";
166
167ok 5, $got eq $dumped;
168
169$g = freeze($root);
170ok 6, length($f) == length($g);
171
172# Ensure the tied items in the retrieved image work
173@old = ($scalar_fetch, $array_fetch, $hash_fetch);
174@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
175@type = qw(SCALAR  ARRAY  HASH);
176
177ok 7, tied $$tscalar;
178ok 8, tied @{$tarray};
179ok 9, tied %{$thash};
180
181@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
182@new = ($scalar_fetch, $array_fetch, $hash_fetch);
183
184# Tests 10..15
185for ($i = 0; $i < @new; $i++) {
186	print "not " unless $new[$i] == $old[$i] + 1;
187	printf "ok %d\n", 10 + 2*$i;	# Tests 10,12,14
188	print "not " unless ref $tied[$i] eq $type[$i];
189	printf "ok %d\n", 11 + 2*$i;	# Tests 11,13,15
190}
191
192# Check undef ties
193my $h = {};
194tie $h->{'x'}, 'FAULT', $h, 'x';
195my $hf = freeze($h);
196ok 16, defined $hf;
197ok 17, $FAULT::fault == 0;
198ok 18, $h->{'x'} == 1;
199ok 19, $FAULT::fault == 1;
200
201my $ht = thaw($hf);
202ok 20, defined $ht;
203ok 21, $ht->{'x'} == 1;
204ok 22, $FAULT::fault == 2;
205
206{
207    package P;
208    use Storable qw(freeze thaw);
209    use vars qw($a $b);
210    $b = "not ok ";
211    sub TIESCALAR { bless \$a } sub FETCH { "ok " }
212    tie $a, P; my $r = thaw freeze \$a; $b = $$r;
213    print $b , 23, "\n";
214}
215
216