Storable and uninitalized array values
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / blessed.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 sub BEGIN {
10     if ($ENV{PERL_CORE}){
11         chdir('t') if -d 't';
12         @INC = ('.', '../lib', '../ext/Storable/t');
13     } else {
14         unshift @INC, 't';
15     }
16     require Config; import Config;
17     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18         print "1..0 # Skip: Storable was not built\n";
19         exit 0;
20     }
21     require 'st-dump.pl';
22 }
23
24 sub ok;
25
26 use Storable qw(freeze thaw);
27
28 print "1..12\n";
29
30 package SHORT_NAME;
31
32 sub make { bless [], shift }
33
34 package SHORT_NAME_WITH_HOOK;
35
36 sub make { bless [], shift }
37
38 sub STORABLE_freeze {
39         my $self = shift;
40         return ("", $self);
41 }
42
43 sub STORABLE_thaw {
44         my $self = shift;
45         my $cloning = shift;
46         my ($x, $obj) = @_;
47         die "STORABLE_thaw" unless $obj eq $self;
48 }
49
50 package main;
51
52 # Still less than 256 bytes, so long classname logic not fully exercised
53 # Wait until Perl removes the restriction on identifier lengths.
54 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
55
56 eval <<EOC;
57 package $name;
58
59 \@ISA = ("SHORT_NAME");
60 EOC
61 die $@ if $@;
62 ok 1, $@ eq '';
63
64 eval <<EOC;
65 package ${name}_WITH_HOOK;
66
67 \@ISA = ("SHORT_NAME_WITH_HOOK");
68 EOC
69 ok 2, $@ eq '';
70
71 # Construct a pool of objects
72 my @pool;
73
74 for (my $i = 0; $i < 10; $i++) {
75         push(@pool, SHORT_NAME->make);
76         push(@pool, SHORT_NAME_WITH_HOOK->make);
77         push(@pool, $name->make);
78         push(@pool, "${name}_WITH_HOOK"->make);
79 }
80
81 my $x = freeze \@pool;
82 ok 3, 1;
83
84 my $y = thaw $x;
85 ok 4, ref $y eq 'ARRAY';
86 ok 5, @{$y} == @pool;
87
88 ok 6, ref $y->[0] eq 'SHORT_NAME';
89 ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
90 ok 8, ref $y->[2] eq $name;
91 ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
92
93 my $good = 1;
94 for (my $i = 0; $i < 10; $i++) {
95         do { $good = 0; last } unless ref $y->[4*$i]   eq 'SHORT_NAME';
96         do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
97         do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
98         do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
99 }
100 ok 10, $good;
101
102 {
103         my $blessed_ref = bless \\[1,2,3], 'Foobar';
104         my $x = freeze $blessed_ref;
105         my $y = thaw $x;
106         ok 11, ref $y eq 'Foobar';
107         ok 12, $$$y->[0] == 1;
108 }