Remove duplicately applied patch shards.
[p5sagit/p5-mst-13.2.git] / t / lib / st-tiedhook.t
CommitLineData
7a6a85bf 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
15sub BEGIN {
16 chdir('t') if -d 't';
8268a68b 17 unshift @INC, '../lib';
9f233367 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 }
7a6a85bf 23 require 'lib/st-dump.pl';
24}
25
26sub ok;
27
28use Storable qw(freeze thaw);
29
30print "1..21\n";
31
32($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
33
34package TIED_HASH;
35
36sub TIEHASH {
37 my $self = bless {}, shift;
38 return $self;
39}
40
41sub FETCH {
42 my $self = shift;
43 my ($key) = @_;
44 $main::hash_fetch++;
45 return $self->{$key};
46}
47
48sub STORE {
49 my $self = shift;
50 my ($key, $value) = @_;
51 $self->{$key} = $value;
52}
53
54sub FIRSTKEY {
55 my $self = shift;
56 scalar keys %{$self};
57 return each %{$self};
58}
59
60sub NEXTKEY {
61 my $self = shift;
62 return each %{$self};
63}
64
65sub STORABLE_freeze {
66 my $self = shift;
67 $main::hash_hook1++;
68 return join(":", keys %$self) . ";" . join(":", values %$self);
69}
70
71sub 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
82package TIED_ARRAY;
83
84sub TIEARRAY {
85 my $self = bless [], shift;
86 return $self;
87}
88
89sub FETCH {
90 my $self = shift;
91 my ($idx) = @_;
92 $main::array_fetch++;
93 return $self->[$idx];
94}
95
96sub STORE {
97 my $self = shift;
98 my ($idx, $value) = @_;
99 $self->[$idx] = $value;
100}
101
102sub FETCHSIZE {
103 my $self = shift;
104 return @{$self};
105}
106
107sub STORABLE_freeze {
108 my $self = shift;
109 $main::array_hook1++;
110 return join(":", @$self);
111}
112
113sub STORABLE_thaw {
114 my ($self, $cloning, $frozen) = @_;
115 @$self = split(/:/, $frozen);
116 $main::array_hook2++;
117}
118
119package TIED_SCALAR;
120
121sub TIESCALAR {
122 my $scalar;
123 my $self = bless \$scalar, shift;
124 return $self;
125}
126
127sub FETCH {
128 my $self = shift;
129 $main::scalar_fetch++;
130 return $$self;
131}
132
133sub STORE {
134 my $self = shift;
135 my ($value) = @_;
136 $$self = $value;
137}
138
139sub STORABLE_freeze {
140 my $self = shift;
141 $main::scalar_hook1++;
142 return $$self;
143}
144
145sub STORABLE_thaw {
146 my ($self, $cloning, $frozen) = @_;
147 $$self = $frozen;
148 $main::scalar_hook2++;
149}
150
151package main;
152
153$a = 'toto';
154$b = \$a;
155
156$c = tie %hash, TIED_HASH;
157$d = tie @array, TIED_ARRAY;
158tie $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
172ok 1, defined($f = freeze(\@a));
173
174$dumped = &dump(\@a);
175ok 2, 1;
176
177$root = thaw($f);
178ok 3, defined $root;
179
180$got = &dump($root);
181ok 4, 1;
182
183ok 5, $got ne $dumped; # our hooks did not handle refs in array
184
185$g = freeze($root);
186ok 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
193ok 7, tied $$tscalar;
194ok 8, tied @{$tarray};
195ok 9, tied %{$thash};
196
197@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
198@new = ($scalar_fetch, $array_fetch, $hash_fetch);
199
200# Tests 10..15
201for ($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
206ok 16, $$tscalar eq 'foo';
207ok 17, $tarray->[3] eq 'plaine scalaire';
208ok 18, $thash->{'attribute'} eq 'plain value';
209
210# Ensure hooks were called
211ok 19, ($scalar_hook1 && $scalar_hook2);
212ok 20, ($array_hook1 && $array_hook2);
213ok 21, ($hash_hook1 && $hash_hook2);
214