Fix up .gitignore files some more
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / dclone.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
25 use Storable qw(dclone);
26
27 print "1..12\n";
28
29 $a = 'toto';
30 $b = \$a;
31 $c = bless {}, CLASS;
32 $c->{attribute} = 'attrval';
33 %a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
34 @a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
35         $b, \$a, $a, $c, \$c, \%a);
36
37 print "not " unless defined ($aref = dclone(\@a));
38 print "ok 1\n";
39
40 $dumped = &dump(\@a);
41 print "ok 2\n";
42
43 $got = &dump($aref);
44 print "ok 3\n";
45
46 print "not " unless $got eq $dumped; 
47 print "ok 4\n";
48
49 package FOO; @ISA = qw(Storable);
50
51 sub make {
52         my $self = bless {};
53         $self->{key} = \%main::a;
54         return $self;
55 };
56
57 package main;
58
59 $foo = FOO->make;
60 print "not " unless defined($r = $foo->dclone);
61 print "ok 5\n";
62
63 print "not " unless &dump($foo) eq &dump($r);
64 print "ok 6\n";
65
66 # Ensure refs to "undef" values are properly shared during cloning
67 my $hash;
68 push @{$$hash{''}}, \$$hash{a};
69 print "not " unless $$hash{''}[0] == \$$hash{a};
70 print "ok 7\n";
71
72 my $cloned = dclone(dclone($hash));
73 print "not " unless $$cloned{''}[0] == \$$cloned{a};
74 print "ok 8\n";
75
76 $$cloned{a} = "blah";
77 print "not " unless $$cloned{''}[0] == \$$cloned{a};
78 print "ok 9\n";
79
80 # [ID 20020221.007] SEGV in Storable with empty string scalar object
81 package TestString;
82 sub new {
83     my ($type, $string) = @_;
84     return bless(\$string, $type);
85 }
86 package main;
87 my $empty_string_obj = TestString->new('');
88 my $clone = dclone($empty_string_obj);
89 # If still here after the dclone the fix (#17543) worked.
90 print ref $clone eq ref $empty_string_obj &&
91       $$clone eq $$empty_string_obj &&
92       $$clone eq '' ? "ok 10\n" : "not ok 10\n";
93
94
95 # Do not fail if Tie::Hash and/or Tie::StdHash is not available
96 if (eval { require Tie::Hash; scalar keys %Tie::StdHash:: }) {
97     tie my %tie, "Tie::StdHash" or die $!;
98     $tie{array} = [1,2,3,4];
99     $tie{hash} = {1,2,3,4};
100     my $clone_array = dclone $tie{array};
101     print "not " unless "@$clone_array" eq "@{$tie{array}}";
102     print "ok 11\n";
103     my $clone_hash = dclone $tie{hash};
104     print "not " unless $clone_hash->{1} eq $tie{hash}{1};
105     print "ok 12\n";
106 } else {
107     print <<EOF;
108 ok 11 # skip No Tie::StdHash available
109 ok 12 # skip No Tie::StdHash available
110 EOF
111 }