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