Commit | Line | Data |
7a6a85bf |
1 | #!./perl |
2 | |
3 | # $Id: tied.t,v 0.7.1.1 2000/08/13 20:10:27 ram Exp $ |
4 | # |
5 | # Copyright (c) 1995-2000, Raphael Manfredi |
6 | # |
7 | # You may redistribute only under the terms of the Artistic License, |
8 | # as specified in the README file that comes with the distribution. |
9 | # |
10 | # $Log: tied.t,v $ |
11 | # Revision 0.7.1.1 2000/08/13 20:10:27 ram |
12 | # patch1: added test case for "undef" in hashes |
13 | # |
14 | # Revision 0.7 2000/08/03 22:04:45 ram |
15 | # Baseline for second beta release. |
16 | # |
17 | |
18 | sub BEGIN { |
19 | chdir('t') if -d 't'; |
20822f61 |
20 | @INC = '.'; |
21 | push @INC, '../lib'; |
9f233367 |
22 | require Config; import Config; |
23 | if ($Config{'extensions'} !~ /\bStorable\b/) { |
24 | print "1..0 # Skip: Storable was not built\n"; |
25 | exit 0; |
26 | } |
7a6a85bf |
27 | require 'lib/st-dump.pl'; |
28 | } |
29 | |
30 | sub ok; |
31 | |
32 | use Storable qw(freeze thaw); |
33 | |
34 | print "1..22\n"; |
35 | |
36 | ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0); |
37 | |
38 | package TIED_HASH; |
39 | |
40 | sub TIEHASH { |
41 | my $self = bless {}, shift; |
42 | return $self; |
43 | } |
44 | |
45 | sub FETCH { |
46 | my $self = shift; |
47 | my ($key) = @_; |
48 | $main::hash_fetch++; |
49 | return $self->{$key}; |
50 | } |
51 | |
52 | sub STORE { |
53 | my $self = shift; |
54 | my ($key, $value) = @_; |
55 | $self->{$key} = $value; |
56 | } |
57 | |
58 | sub FIRSTKEY { |
59 | my $self = shift; |
60 | scalar keys %{$self}; |
61 | return each %{$self}; |
62 | } |
63 | |
64 | sub NEXTKEY { |
65 | my $self = shift; |
66 | return each %{$self}; |
67 | } |
68 | |
69 | package TIED_ARRAY; |
70 | |
71 | sub TIEARRAY { |
72 | my $self = bless [], shift; |
73 | return $self; |
74 | } |
75 | |
76 | sub FETCH { |
77 | my $self = shift; |
78 | my ($idx) = @_; |
79 | $main::array_fetch++; |
80 | return $self->[$idx]; |
81 | } |
82 | |
83 | sub STORE { |
84 | my $self = shift; |
85 | my ($idx, $value) = @_; |
86 | $self->[$idx] = $value; |
87 | } |
88 | |
89 | sub FETCHSIZE { |
90 | my $self = shift; |
91 | return @{$self}; |
92 | } |
93 | |
94 | package TIED_SCALAR; |
95 | |
96 | sub TIESCALAR { |
97 | my $scalar; |
98 | my $self = bless \$scalar, shift; |
99 | return $self; |
100 | } |
101 | |
102 | sub FETCH { |
103 | my $self = shift; |
104 | $main::scalar_fetch++; |
105 | return $$self; |
106 | } |
107 | |
108 | sub STORE { |
109 | my $self = shift; |
110 | my ($value) = @_; |
111 | $$self = $value; |
112 | } |
113 | |
114 | package FAULT; |
115 | |
116 | $fault = 0; |
117 | |
118 | sub TIESCALAR { |
119 | my $pkg = shift; |
120 | return bless [@_], $pkg; |
121 | } |
122 | |
123 | sub FETCH { |
124 | my $self = shift; |
125 | my ($href, $key) = @$self; |
126 | $fault++; |
127 | untie $href->{$key}; |
128 | return $href->{$key} = 1; |
129 | } |
130 | |
131 | package main; |
132 | |
133 | $a = 'toto'; |
134 | $b = \$a; |
135 | |
136 | $c = tie %hash, TIED_HASH; |
137 | $d = tie @array, TIED_ARRAY; |
138 | tie $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 | |
161 | ok 1, defined($f = freeze(\@a)); |
162 | |
163 | $dumped = &dump(\@a); |
164 | ok 2, 1; |
165 | |
166 | $root = thaw($f); |
167 | ok 3, defined $root; |
168 | |
169 | $got = &dump($root); |
170 | ok 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 | |
178 | ok 5, $got eq $dumped; |
179 | |
180 | $g = freeze($root); |
181 | ok 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 | |
188 | ok 7, tied $$tscalar; |
189 | ok 8, tied @{$tarray}; |
190 | ok 9, tied %{$thash}; |
191 | |
192 | @new = ($$tscalar, $tarray->[0], $thash->{'attribute'}); |
193 | @new = ($scalar_fetch, $array_fetch, $hash_fetch); |
194 | |
195 | # Tests 10..15 |
196 | for ($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 |
204 | my $h = {}; |
205 | tie $h->{'x'}, 'FAULT', $h, 'x'; |
206 | my $hf = freeze($h); |
207 | ok 16, defined $hf; |
208 | ok 17, $FAULT::fault == 0; |
209 | ok 18, $h->{'x'} == 1; |
210 | ok 19, $FAULT::fault == 1; |
211 | |
212 | my $ht = thaw($hf); |
213 | ok 20, defined $ht; |
214 | ok 21, $ht->{'x'} == 1; |
215 | ok 22, $FAULT::fault == 2; |
216 | |