Forgotten from #16656.
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / dclone.t
CommitLineData
7a6a85bf 1#!./perl
2
9e21b3d0 3# $Id: dclone.t,v 1.0 2000/09/01 19:40:41 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: dclone.t,v $
9e21b3d0 11# Revision 1.0 2000/09/01 19:40:41 ram
12# Baseline for first official release.
7a6a85bf 13#
14
15sub BEGIN {
0c384302 16 if ($ENV{PERL_CORE}){
17 chdir('t') if -d 't';
372cb964 18 @INC = ('.', '../lib', '../t/lib');
19 } else {
20 unshift @INC, 't';
0c384302 21 }
9f233367 22 require Config; import Config;
0c384302 23 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 24 print "1..0 # Skip: Storable was not built\n";
25 exit 0;
26 }
372cb964 27 require 'st-dump.pl';
7a6a85bf 28}
29
30
31use Storable qw(dclone);
32
14bff8b8 33print "1..10\n";
7a6a85bf 34
35$a = 'toto';
36$b = \$a;
37$c = bless {}, CLASS;
38$c->{attribute} = 'attrval';
39%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
40@a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
41 $b, \$a, $a, $c, \$c, \%a);
42
43print "not " unless defined ($aref = dclone(\@a));
44print "ok 1\n";
45
46$dumped = &dump(\@a);
47print "ok 2\n";
48
49$got = &dump($aref);
50print "ok 3\n";
51
52print "not " unless $got eq $dumped;
53print "ok 4\n";
54
55package FOO; @ISA = qw(Storable);
56
57sub make {
58 my $self = bless {};
59 $self->{key} = \%main::a;
60 return $self;
61};
62
63package main;
64
65$foo = FOO->make;
66print "not " unless defined($r = $foo->dclone);
67print "ok 5\n";
68
69print "not " unless &dump($foo) eq &dump($r);
70print "ok 6\n";
71
72# Ensure refs to "undef" values are properly shared during cloning
73my $hash;
74push @{$$hash{''}}, \$$hash{a};
75print "not " unless $$hash{''}[0] == \$$hash{a};
76print "ok 7\n";
77
78my $cloned = dclone(dclone($hash));
79print "not " unless $$cloned{''}[0] == \$$cloned{a};
80print "ok 8\n";
81
82$$cloned{a} = "blah";
83print "not " unless $$cloned{''}[0] == \$$cloned{a};
84print "ok 9\n";
85
14bff8b8 86# [ID 20020221.007] SEGV in Storable with empty string scalar object
87package TestString;
88sub new {
89 my ($type, $string) = @_;
90 return bless(\$string, $type);
91}
92package main;
93my $empty_string_obj = TestString->new('');
94my $clone = dclone($empty_string_obj);
95# If still here after the dclone the fix (#17543) worked.
96print ref $clone eq ref $empty_string_obj &&
97 $$clone eq $$empty_string_obj &&
98 $$clone eq '' ? "ok 10\n" : "not ok 10\n";
99