Re: files not cleaned even by veryclean
[p5sagit/p5-mst-13.2.git] / t / lib / st-tiedhook.t
1 #!./perl
2
3 # $Id: tied_hook.t,v 0.7 2000/08/03 22:04:45 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_hook.t,v $
11 # Revision 0.7  2000/08/03 22:04:45  ram
12 # Baseline for second beta release.
13 #
14
15 sub BEGIN {
16     chdir('t') if -d 't';
17     unshift @INC, '../lib';
18     require Config; import Config;
19     if ($Config{'extensions'} !~ /\bStorable\b/) {
20         print "1..0 # Skip: Storable was not built\n";
21         exit 0;
22     }
23     require 'lib/st-dump.pl';
24 }
25
26 sub ok;
27
28 use Storable qw(freeze thaw);
29
30 print "1..21\n";
31
32 ($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
33
34 package TIED_HASH;
35
36 sub TIEHASH {
37         my $self = bless {}, shift;
38         return $self;
39 }
40
41 sub FETCH {
42         my $self = shift;
43         my ($key) = @_;
44         $main::hash_fetch++;
45         return $self->{$key};
46 }
47
48 sub STORE {
49         my $self = shift;
50         my ($key, $value) = @_;
51         $self->{$key} = $value;
52 }
53
54 sub FIRSTKEY {
55         my $self = shift;
56         scalar keys %{$self};
57         return each %{$self};
58 }
59
60 sub NEXTKEY {
61         my $self = shift;
62         return each %{$self};
63 }
64
65 sub STORABLE_freeze {
66         my $self = shift;
67         $main::hash_hook1++;
68         return join(":", keys %$self) . ";" . join(":", values %$self);
69 }
70
71 sub STORABLE_thaw {
72         my ($self, $cloning, $frozen) = @_;
73         my ($keys, $values) = split(/;/, $frozen);
74         my @keys = split(/:/, $keys);
75         my @values = split(/:/, $values);
76         for (my $i = 0; $i < @keys; $i++) {
77                 $self->{$keys[$i]} = $values[$i];
78         }
79         $main::hash_hook2++;
80 }
81
82 package TIED_ARRAY;
83
84 sub TIEARRAY {
85         my $self = bless [], shift;
86         return $self;
87 }
88
89 sub FETCH {
90         my $self = shift;
91         my ($idx) = @_;
92         $main::array_fetch++;
93         return $self->[$idx];
94 }
95
96 sub STORE {
97         my $self = shift;
98         my ($idx, $value) = @_;
99         $self->[$idx] = $value;
100 }
101
102 sub FETCHSIZE {
103         my $self = shift;
104         return @{$self};
105 }
106
107 sub STORABLE_freeze {
108         my $self = shift;
109         $main::array_hook1++;
110         return join(":", @$self);
111 }
112
113 sub STORABLE_thaw {
114         my ($self, $cloning, $frozen) = @_;
115         @$self = split(/:/, $frozen);
116         $main::array_hook2++;
117 }
118
119 package TIED_SCALAR;
120
121 sub TIESCALAR {
122         my $scalar;
123         my $self = bless \$scalar, shift;
124         return $self;
125 }
126
127 sub FETCH {
128         my $self = shift;
129         $main::scalar_fetch++;
130         return $$self;
131 }
132
133 sub STORE {
134         my $self = shift;
135         my ($value) = @_;
136         $$self = $value;
137 }
138
139 sub STORABLE_freeze {
140         my $self = shift;
141         $main::scalar_hook1++;
142         return $$self;
143 }
144
145 sub STORABLE_thaw {
146         my ($self, $cloning, $frozen) = @_;
147         $$self = $frozen;
148         $main::scalar_hook2++;
149 }
150
151 package main;
152
153 $a = 'toto';
154 $b = \$a;
155
156 $c = tie %hash, TIED_HASH;
157 $d = tie @array, TIED_ARRAY;
158 tie $scalar, TIED_SCALAR;
159
160 $scalar = 'foo';
161 $hash{'attribute'} = 'plain value';
162 $array[0] = \$scalar;
163 $array[1] = $c;
164 $array[2] = \@array;
165 $array[3] = "plaine scalaire";
166
167 @tied = (\$scalar, \@array, \%hash);
168 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
169 @a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
170         $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
171
172 ok 1, defined($f = freeze(\@a));
173
174 $dumped = &dump(\@a);
175 ok 2, 1;
176
177 $root = thaw($f);
178 ok 3, defined $root;
179
180 $got = &dump($root);
181 ok 4, 1;
182
183 ok 5, $got ne $dumped;          # our hooks did not handle refs in array
184
185 $g = freeze($root);
186 ok 6, length($f) == length($g);
187
188 # Ensure the tied items in the retrieved image work
189 @old = ($scalar_fetch, $array_fetch, $hash_fetch);
190 @tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
191 @type = qw(SCALAR  ARRAY  HASH);
192
193 ok 7, tied $$tscalar;
194 ok 8, tied @{$tarray};
195 ok 9, tied %{$thash};
196
197 @new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
198 @new = ($scalar_fetch, $array_fetch, $hash_fetch);
199
200 # Tests 10..15
201 for ($i = 0; $i < @new; $i++) {
202         ok 10 + 2*$i, $new[$i] == $old[$i] + 1;         # Tests 10,12,14
203         ok 11 + 2*$i, ref $tied[$i] eq $type[$i];       # Tests 11,13,15
204 }
205
206 ok 16, $$tscalar eq 'foo';
207 ok 17, $tarray->[3] eq 'plaine scalaire';
208 ok 18, $thash->{'attribute'} eq 'plain value';
209
210 # Ensure hooks were called
211 ok 19, ($scalar_hook1 && $scalar_hook2);
212 ok 20, ($array_hook1 && $array_hook2);
213 ok 21, ($hash_hook1 && $hash_hook2);
214