[PATCH] Re: Storable 2.0.0 fails on vendor perl on Mac OS X 10.1
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / tied.t
CommitLineData
7a6a85bf 1#!./perl
2
9e21b3d0 3# $Id: tied.t,v 1.0 2000/09/01 19:40:42 ram Exp $
7a6a85bf 4#
5# Copyright (c) 1995-2000, Raphael Manfredi
6#
9e21b3d0 7# You may redistribute only under the same terms as Perl 5, as specified
8# in the README file that comes with the distribution.
7a6a85bf 9#
10# $Log: tied.t,v $
9e21b3d0 11# Revision 1.0 2000/09/01 19:40:42 ram
12# Baseline for first official release.
7a6a85bf 13#
14
15sub BEGIN {
0c384302 16 if ($ENV{PERL_CORE}){
17 chdir('t') if -d 't';
7dadce44 18 @INC = ('.', '../lib', '../ext/Storable/t');
372cb964 19 } else {
20 unshift @INC, 't';
0c384302 21 }
9f233367 22 require Config; import Config;
0c384302 23 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 24 print "1..0 # Skip: Storable was not built\n";
25 exit 0;
26 }
372cb964 27 require 'st-dump.pl';
7a6a85bf 28}
29
30sub ok;
31
32use Storable qw(freeze thaw);
33
34print "1..22\n";
35
36($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
37
38package TIED_HASH;
39
40sub TIEHASH {
41 my $self = bless {}, shift;
42 return $self;
43}
44
45sub FETCH {
46 my $self = shift;
47 my ($key) = @_;
48 $main::hash_fetch++;
49 return $self->{$key};
50}
51
52sub STORE {
53 my $self = shift;
54 my ($key, $value) = @_;
55 $self->{$key} = $value;
56}
57
58sub FIRSTKEY {
59 my $self = shift;
60 scalar keys %{$self};
61 return each %{$self};
62}
63
64sub NEXTKEY {
65 my $self = shift;
66 return each %{$self};
67}
68
69package TIED_ARRAY;
70
71sub TIEARRAY {
72 my $self = bless [], shift;
73 return $self;
74}
75
76sub FETCH {
77 my $self = shift;
78 my ($idx) = @_;
79 $main::array_fetch++;
80 return $self->[$idx];
81}
82
83sub STORE {
84 my $self = shift;
85 my ($idx, $value) = @_;
86 $self->[$idx] = $value;
87}
88
89sub FETCHSIZE {
90 my $self = shift;
91 return @{$self};
92}
93
94package TIED_SCALAR;
95
96sub TIESCALAR {
97 my $scalar;
98 my $self = bless \$scalar, shift;
99 return $self;
100}
101
102sub FETCH {
103 my $self = shift;
104 $main::scalar_fetch++;
105 return $$self;
106}
107
108sub STORE {
109 my $self = shift;
110 my ($value) = @_;
111 $$self = $value;
112}
113
114package FAULT;
115
116$fault = 0;
117
118sub TIESCALAR {
119 my $pkg = shift;
120 return bless [@_], $pkg;
121}
122
123sub FETCH {
124 my $self = shift;
125 my ($href, $key) = @$self;
126 $fault++;
127 untie $href->{$key};
128 return $href->{$key} = 1;
129}
130
131package main;
132
133$a = 'toto';
134$b = \$a;
135
136$c = tie %hash, TIED_HASH;
137$d = tie @array, TIED_ARRAY;
138tie $scalar, TIED_SCALAR;
139
140#$scalar = 'foo';
141#$hash{'attribute'} = \$d;
142#$array[0] = $c;
143#$array[1] = \$scalar;
144
145### If I say
146### $hash{'attribute'} = $d;
147### below, then dump() incorectly dumps the hash value as a string the second
148### time it is reached. I have not investigated enough to tell whether it's
149### a bug in my dump() routine or in the Perl tieing mechanism.
150$scalar = 'foo';
151$hash{'attribute'} = 'plain value';
152$array[0] = \$scalar;
153$array[1] = $c;
154$array[2] = \@array;
155
156@tied = (\$scalar, \@array, \%hash);
157%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
158@a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
159 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
160
161ok 1, defined($f = freeze(\@a));
162
163$dumped = &dump(\@a);
164ok 2, 1;
165
166$root = thaw($f);
167ok 3, defined $root;
168
169$got = &dump($root);
170ok 4, 1;
171
172### Used to see the manifestation of the bug documented above.
173### print "original: $dumped";
174### print "--------\n";
175### print "got: $got";
176### print "--------\n";
177
178ok 5, $got eq $dumped;
179
180$g = freeze($root);
181ok 6, length($f) == length($g);
182
183# Ensure the tied items in the retrieved image work
184@old = ($scalar_fetch, $array_fetch, $hash_fetch);
185@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
186@type = qw(SCALAR ARRAY HASH);
187
188ok 7, tied $$tscalar;
189ok 8, tied @{$tarray};
190ok 9, tied %{$thash};
191
192@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
193@new = ($scalar_fetch, $array_fetch, $hash_fetch);
194
195# Tests 10..15
196for ($i = 0; $i < @new; $i++) {
197 print "not " unless $new[$i] == $old[$i] + 1;
198 printf "ok %d\n", 10 + 2*$i; # Tests 10,12,14
199 print "not " unless ref $tied[$i] eq $type[$i];
200 printf "ok %d\n", 11 + 2*$i; # Tests 11,13,15
201}
202
203# Check undef ties
204my $h = {};
205tie $h->{'x'}, 'FAULT', $h, 'x';
206my $hf = freeze($h);
207ok 16, defined $hf;
208ok 17, $FAULT::fault == 0;
209ok 18, $h->{'x'} == 1;
210ok 19, $FAULT::fault == 1;
211
212my $ht = thaw($hf);
213ok 20, defined $ht;
214ok 21, $ht->{'x'} == 1;
215ok 22, $FAULT::fault == 2;
216