This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
tighten Storable's recognition of tied SVs
[perl5.git] / dist / Storable / t / compat06.t
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
9 BEGIN {
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
19 use Test::More tests => 8;
20
21 use Storable qw(freeze nfreeze thaw);
22
23 package TIED_HASH;
24
25 sub TIEHASH {
26         my $self = bless {}, shift;
27         return $self;
28 }
29
30 sub FETCH {
31         my $self = shift;
32         my ($key) = @_;
33         $main::hash_fetch++;
34         return $self->{$key};
35 }
36
37 sub STORE {
38         my $self = shift;
39         my ($key, $val) = @_;
40         $self->{$key} = $val;
41 }
42
43 package SIMPLE;
44
45 sub make {
46         my $self = bless [], shift;
47         my ($x) = @_;
48         $self->[0] = $x;
49         return $self;
50 }
51
52 package ROOT;
53
54 sub 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
72 sub num { $_[0]->{num} }
73 sub h   { $_[0]->{h} }
74 sub ref { $_[0]->{ref} }
75 sub obj { $_[0]->{obj} }
76
77 package main;
78
79 my $is_EBCDIC = (ord('A') == 193) ? 1 : 0;
80  
81 my $r = ROOT->make;
82
83 my $data = '';
84 if (!$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
99 my $expected_length = $is_EBCDIC ? 217 : 278;
100 is(length $data, $expected_length);
101   
102 my $y = thaw($data);
103 isnt($y, undef);
104 is(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;
111 is(nfreeze($y), nfreeze($r));
112
113 is($y->ref->{key1}, 'val1');
114 is($y->ref->{key2}, 'val2');
115 is($hash_fetch, 2);
116
117 my $num = $r->num;
118 my $ok = 1;
119 for (my $i = 0; $i < @$num; $i++) {
120         do { $ok = 0; last } unless $num->[$i] == $y->num->[$i];
121 }
122 is($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 #
129 M`P,````%!`(````&"(%8"(!8"'U8"@@M,RXQ-#$U.5@)```!R%@*`S0N-5A8
130 M6`````-N=6T$`P````(*!'9A;#%8````!&ME>3$*!'9A;#)8````!&ME>3)B
131 M"51)141?2$%32%A8`````6@$`@````,*!G-T<FEN9U@$``````I8!```````
132 M6%A8`````6$$`@````4$`@````$(@%AB!E-)35!,15A8!`(````!"(%88@93
133 M24U03$586`0"`````0B"6&(&4TE-4$Q%6%@$`@````$(@UAB!E-)35!,15A8
134 M!`(````!"(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``