Re: [PATCH] another Storable test (Re: perl@16005)
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / compat06.t
CommitLineData
7a6a85bf 1#!./perl
2
b12202d0 3# $Id: compat-0.6.t,v 1.0.1.1 2001/02/17 12:26:21 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: compat-0.6.t,v $
b12202d0 11# Revision 1.0.1.1 2001/02/17 12:26:21 ram
12# patch8: added EBCDIC version of the test, from Peter Prymmer
13#
9e21b3d0 14# Revision 1.0 2000/09/01 19:40:41 ram
15# Baseline for first official release.
7a6a85bf 16#
17
18BEGIN {
0c384302 19 if ($ENV{PERL_CORE}){
20 chdir('t') if -d 't';
21 @INC = '.';
22 push @INC, '../lib';
23 }
9f233367 24 require Config; import Config;
0c384302 25 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 26 print "1..0 # Skip: Storable was not built\n";
27 exit 0;
28 }
7a6a85bf 29 require 'lib/st-dump.pl';
30}
31
32sub ok;
33
34print "1..8\n";
35
36use Storable qw(freeze nfreeze thaw);
37
38package TIED_HASH;
39
40sub TIEHASH {
41 my $self = bless {}, shift;
42 return $self;
43}
44
45sub FETCH {
46 my $self = shift;
47 my ($key) = @_;
48 $main::hash_fetch++;
49 return $self->{$key};
50}
51
52sub STORE {
53 my $self = shift;
54 my ($key, $val) = @_;
55 $self->{$key} = $val;
56}
57
58package SIMPLE;
59
60sub make {
61 my $self = bless [], shift;
62 my ($x) = @_;
63 $self->[0] = $x;
64 return $self;
65}
66
67package ROOT;
68
69sub make {
70 my $self = bless {}, shift;
71 my $h = tie %hash, TIED_HASH;
72 $self->{h} = $h;
73 $self->{ref} = \%hash;
74 my @pool;
75 for (my $i = 0; $i < 5; $i++) {
76 push(@pool, SIMPLE->make($i));
77 }
78 $self->{obj} = \@pool;
79 my @a = ('string', $h, $self);
80 $self->{a} = \@a;
81 $self->{num} = [1, 0, -3, -3.14159, 456, 4.5];
82 $h->{key1} = 'val1';
83 $h->{key2} = 'val2';
84 return $self;
85};
86
87sub num { $_[0]->{num} }
88sub h { $_[0]->{h} }
89sub ref { $_[0]->{ref} }
90sub obj { $_[0]->{obj} }
91
92package main;
93
b12202d0 94my $is_EBCDIC = (ord('A') == 193) ? 1 : 0;
95
7a6a85bf 96my $r = ROOT->make;
97
98my $data = '';
b12202d0 99if (!$is_EBCDIC) { # ASCII machine
d40f9568 100 while (<DATA>) {
b12202d0 101 next if /^#/;
b2010bf6 102 $data .= unpack("u", $_);
103 }
b12202d0 104} else {
d40f9568 105 while (<DATA>) {
b12202d0 106 next if /^#$/; # skip comments
107 next if /^#\s+/; # skip comments
108 next if /^[^#]/; # skip uuencoding for ASCII machines
109 s/^#//; # prepare uuencoded data for EBCDIC machines
110 $data .= unpack("u", $_);
b2010bf6 111 }
7a6a85bf 112}
113
b12202d0 114my $expected_length = $is_EBCDIC ? 217 : 278;
b2010bf6 115ok 1, length $data == $expected_length;
b12202d0 116
7a6a85bf 117my $y = thaw($data);
118ok 2, 1;
119ok 3, ref $y eq 'ROOT';
120
121$Storable::canonical = 1; # Prevent "used once" warning
122$Storable::canonical = 1;
44e147de 123# Allow for long double string conversions.
124$y->{num}->[3] += 0;
125$r->{num}->[3] += 0;
7a6a85bf 126ok 4, nfreeze($y) eq nfreeze($r);
127
128ok 5, $y->ref->{key1} eq 'val1';
129ok 6, $y->ref->{key2} eq 'val2';
130ok 7, $hash_fetch == 2;
131
132my $num = $r->num;
133my $ok = 1;
134for (my $i = 0; $i < @$num; $i++) {
135 do { $ok = 0; last } unless $num->[$i] == $y->num->[$i];
136}
137ok 8, $ok;
138
139__END__
140#
141# using Storable-0.6@11, output of: print pack("u", nfreeze(ROOT->make));
142# original size: 278 bytes
143#
144M`P,````%!`(````&"(%8"(!8"'U8"@@M,RXQ-#$U.5@)```!R%@*`S0N-5A8
145M6`````-N=6T$`P````(*!'9A;#%8````!&ME>3$*!'9A;#)8````!&ME>3)B
146M"51)141?2$%32%A8`````6@$`@````,*!G-T<FEN9U@$``````I8!```````
147M6%A8`````6$$`@````4$`@````$(@%AB!E-)35!,15A8!`(````!"(%88@93
148M24U03$586`0"`````0B"6&(&4TE-4$Q%6%@$`@````$(@UAB!E-)35!,15A8
149M!`(````!"(188@9324U03$586%A8`````V]B:@0,!``````*6%A8`````W)E
150(9F($4D]/5%@`
b2010bf6 151#
d40f9568 152# using Storable-0.6@11, output of: print '#' . pack("u", nfreeze(ROOT->make));
b2010bf6 153# on OS/390 (cp 1047) original size: 217 bytes
154#
155#M!0,1!-G6UN,#````!00,!!$)X\G%Q&W(P>+(`P````(*!*6!D_$````$DH6H
156#M\0H$I8&3\@````22A:CR`````YF%A@0"````!@B!"(`(?0H(8/-+\?3Q]?D)
157#M```!R`H#]$OU`````Y6DE`0"````!001!N+)U-?3Q0(````!"(`$$@("````
158#M`0B!!!("`@````$(@@02`@(````!"(,$$@("`````0B$`````Y:"D00`````
159#E!`````&(!`(````#"@:BHYF)E8<$``````0$```````````!@0``