Encode nits.
[p5sagit/p5-mst-13.2.git] / t / lib / st-recurse.t
CommitLineData
7a6a85bf 1#!./perl
2
9e21b3d0 3# $Id: recurse.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: recurse.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 dclone);
30
31print "1..23\n";
32
33package OBJ_REAL;
34
35use Storable qw(freeze thaw);
36
37@x = ('a', 1);
38
39sub make { bless [], shift }
40
41sub STORABLE_freeze {
42 my $self = shift;
43 my $cloning = shift;
44 die "STORABLE_freeze" unless Storable::is_storing;
45 return (freeze(\@x), $self);
46}
47
48sub STORABLE_thaw {
49 my $self = shift;
50 my $cloning = shift;
51 my ($x, $obj) = @_;
52 die "STORABLE_thaw #1" unless $obj eq $self;
53 my $len = length $x;
54 my $a = thaw $x;
55 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
56 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
57 @$self = @$a;
58 die "STORABLE_thaw #4" unless Storable::is_retrieving;
59}
60
61package OBJ_SYNC;
62
63@x = ('a', 1);
64
65sub make { bless {}, shift }
66
67sub STORABLE_freeze {
68 my $self = shift;
69 my ($cloning) = @_;
70 return if $cloning;
71 return ("", \@x, $self);
72}
73
74sub STORABLE_thaw {
75 my $self = shift;
76 my ($cloning, $undef, $a, $obj) = @_;
77 die "STORABLE_thaw #1" unless $obj eq $self;
78 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
79 $self->{ok} = $self;
80}
81
82package OBJ_SYNC2;
83
84use Storable qw(dclone);
85
86sub make {
87 my $self = bless {}, shift;
88 my ($ext) = @_;
89 $self->{sync} = OBJ_SYNC->make;
90 $self->{ext} = $ext;
91 return $self;
92}
93
94sub STORABLE_freeze {
95 my $self = shift;
96 my $t = dclone($self->{sync});
97 return ("", [$t, $self->{ext}], $self, $self->{ext});
98}
99
100sub STORABLE_thaw {
101 my $self = shift;
102 my ($cloning, $undef, $a, $obj, $ext) = @_;
103 die "STORABLE_thaw #1" unless $obj eq $self;
104 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
105 $self->{ok} = $self;
106 ($self->{sync}, $self->{ext}) = @$a;
107}
108
109package OBJ_REAL2;
110
111use Storable qw(freeze thaw);
112
113$MAX = 20;
114$recursed = 0;
115$hook_called = 0;
116
117sub make { bless [], shift }
118
119sub STORABLE_freeze {
120 my $self = shift;
121 $hook_called++;
122 return (freeze($self), $self) if ++$recursed < $MAX;
123 return ("no", $self);
124}
125
126sub STORABLE_thaw {
127 my $self = shift;
128 my $cloning = shift;
129 my ($x, $obj) = @_;
130 die "STORABLE_thaw #1" unless $obj eq $self;
131 $self->[0] = thaw($x) if $x ne "no";
132 $recursed--;
133}
134
135package main;
136
137my $real = OBJ_REAL->make;
138my $x = freeze $real;
139ok 1, 1;
140
141my $y = thaw $x;
142ok 2, 1;
143ok 3, $y->[0] eq 'a';
144ok 4, $y->[1] == 1;
145
146my $sync = OBJ_SYNC->make;
147$x = freeze $sync;
148ok 5, 1;
149
150$y = thaw $x;
151ok 6, 1;
152ok 7, $y->{ok} == $y;
153
154my $ext = [1, 2];
155$sync = OBJ_SYNC2->make($ext);
156$x = freeze [$sync, $ext];
157ok 8, 1;
158
159my $z = thaw $x;
160$y = $z->[0];
161ok 9, 1;
162ok 10, $y->{ok} == $y;
163ok 11, ref $y->{sync} eq 'OBJ_SYNC';
164ok 12, $y->{ext} == $z->[1];
165
166$real = OBJ_REAL2->make;
167$x = freeze $real;
168ok 13, 1;
169ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
170ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
171
172$y = thaw $x;
173ok 16, 1;
174ok 17, $OBJ_REAL2::recursed == 0;
175
176$x = dclone $real;
177ok 18, 1;
178ok 19, ref $x eq 'OBJ_REAL2';
179ok 20, $OBJ_REAL2::recursed == 0;
180ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
181
182ok 22, !Storable::is_storing;
183ok 23, !Storable::is_retrieving;