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