1#!./perl
2#
3#  Copyright (c) 1995-2000, Raphael Manfredi
4#
5#  You may redistribute only under the same terms as Perl 5, as specified
6#  in the README file that comes with the distribution.
7#
8
9BEGIN {
10    unshift @INC, 't';
11    unshift @INC, 't/compat' if $] < 5.006002;
12    require Config; import Config;
13    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14        print "1..0 # Skip: Storable was not built\n";
15        exit 0;
16    }
17}
18
19use Test::More tests => 8;
20
21use Storable qw(freeze nfreeze thaw);
22
23package TIED_HASH;
24
25sub TIEHASH {
26	my $self = bless {}, shift;
27	return $self;
28}
29
30sub FETCH {
31	my $self = shift;
32	my ($key) = @_;
33	$main::hash_fetch++;
34	return $self->{$key};
35}
36
37sub STORE {
38	my $self = shift;
39	my ($key, $val) = @_;
40	$self->{$key} = $val;
41}
42
43package SIMPLE;
44
45sub make {
46	my $self = bless [], shift;
47	my ($x) = @_;
48	$self->[0] = $x;
49	return $self;
50}
51
52package ROOT;
53
54sub make {
55	my $self = bless {}, shift;
56	my $h = tie %hash, TIED_HASH;
57	$self->{h} = $h;
58	$self->{ref} = \%hash;
59	my @pool;
60	for (my $i = 0; $i < 5; $i++) {
61		push(@pool, SIMPLE->make($i));
62	}
63	$self->{obj} = \@pool;
64	my @a = ('string', $h, $self);
65	$self->{a} = \@a;
66	$self->{num} = [1, 0, -3, -3.14159, 456, 4.5];
67	$h->{key1} = 'val1';
68	$h->{key2} = 'val2';
69	return $self;
70};
71
72sub num { $_[0]->{num} }
73sub h   { $_[0]->{h} }
74sub ref { $_[0]->{ref} }
75sub obj { $_[0]->{obj} }
76
77package main;
78
79my $is_EBCDIC = (ord('A') == 193) ? 1 : 0;
80
81my $r = ROOT->make;
82
83my $data = '';
84if (!$is_EBCDIC) {			# ASCII machine
85	while (<DATA>) {
86		next if /^#/;
87	    $data .= unpack("u", $_);
88	}
89} else {
90	while (<DATA>) {
91		next if /^#$/;		# skip comments
92		next if /^#\s+/;	# skip comments
93		next if /^[^#]/;	# skip uuencoding for ASCII machines
94		s/^#//;				# prepare uuencoded data for EBCDIC machines
95		$data .= unpack("u", $_);
96	}
97}
98
99my $expected_length = $is_EBCDIC ? 217 : 278;
100is(length $data, $expected_length);
101
102my $y = thaw($data);
103isnt($y, undef);
104is(ref $y, 'ROOT');
105
106$Storable::canonical = 1;		# Prevent "used once" warning
107$Storable::canonical = 1;
108# Allow for long double string conversions.
109$y->{num}->[3] += 0;
110$r->{num}->[3] += 0;
111is(nfreeze($y), nfreeze($r));
112
113is($y->ref->{key1}, 'val1');
114is($y->ref->{key2}, 'val2');
115is($hash_fetch, 2);
116
117my $num = $r->num;
118my $ok = 1;
119for (my $i = 0; $i < @$num; $i++) {
120	do { $ok = 0; last } unless $num->[$i] == $y->num->[$i];
121}
122is($ok, 1);
123
124__END__
125#
126# using Storable-0.6@11, output of: print pack("u", nfreeze(ROOT->make));
127# original size: 278 bytes
128#
129M`P,````%!`(````&"(%8"(!8"'U8"@@M,RXQ-#$U.5@)```!R%@*`S0N-5A8
130M6`````-N=6T$`P````(*!'9A;#%8````!&ME>3$*!'9A;#)8````!&ME>3)B
131M"51)141?2$%32%A8`````6@$`@````,*!G-T<FEN9U@$``````I8!```````
132M6%A8`````6$$`@````4$`@````$(@%AB!E-)35!,15A8!`(````!"(%88@93
133M24U03$586`0"`````0B"6&(&4TE-4$Q%6%@$`@````$(@UAB!E-)35!,15A8
134M!`(````!"(188@9324U03$586%A8`````V]B:@0,!``````*6%A8`````W)E
135(9F($4D]/5%@`
136#
137# using Storable-0.6@11, output of: print '#' . pack("u", nfreeze(ROOT->make));
138# on OS/390 (cp 1047) original size: 217 bytes
139#
140#M!0,1!-G6UN,#````!00,!!$)X\G%Q&W(P>+(`P````(*!*6!D_$````$DH6H
141#M\0H$I8&3\@````22A:CR`````YF%A@0"````!@B!"(`(?0H(8/-+\?3Q]?D)
142#M```!R`H#]$OU`````Y6DE`0"````!001!N+)U-?3Q0(````!"(`$$@("````
143#M`0B!!!("`@````$(@@02`@(````!"(,$$@("`````0B$`````Y:"D00`````
144#E!`````&(!`(````#"@:BHYF)E8<$``````0$```````````!@0``
145