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