xref: /openbsd/gnu/usr.bin/perl/dist/Storable/t/dclone.t (revision f3efcd01)
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
9sub BEGIN {
10    unshift @INC, 't';
11    unshift @INC, 't/compat' if $] < 5.006002;
12    require Config; import Config;
13    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14        print "1..0 # Skip: Storable was not built\n";
15        exit 0;
16    }
17    require 'st-dump.pl';
18}
19
20
21use Storable qw(dclone);
22
23use Test::More tests => 14;
24
25$a = 'toto';
26$b = \$a;
27$c = bless {}, CLASS;
28$c->{attribute} = 'attrval';
29%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
30@a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
31	$b, \$a, $a, $c, \$c, \%a);
32
33my $aref = dclone(\@a);
34isnt($aref, undef);
35
36$dumped = &dump(\@a);
37isnt($dumped, undef);
38
39$got = &dump($aref);
40isnt($got, undef);
41
42is($got, $dumped);
43
44package FOO; @ISA = qw(Storable);
45
46sub make {
47	my $self = bless {};
48	$self->{key} = \%main::a;
49	return $self;
50};
51
52package main;
53
54$foo = FOO->make;
55my $r = $foo->dclone;
56isnt($r, undef);
57
58is(&dump($foo), &dump($r));
59
60# Ensure refs to "undef" values are properly shared during cloning
61my $hash;
62push @{$$hash{''}}, \$$hash{a};
63is($$hash{''}[0], \$$hash{a});
64
65my $cloned = dclone(dclone($hash));
66is($$cloned{''}[0], \$$cloned{a});
67
68$$cloned{a} = "blah";
69is($$cloned{''}[0], \$$cloned{a});
70
71# [ID 20020221.007 (#8624)] SEGV in Storable with empty string scalar object
72package TestString;
73sub new {
74    my ($type, $string) = @_;
75    return bless(\$string, $type);
76}
77package main;
78my $empty_string_obj = TestString->new('');
79my $clone = dclone($empty_string_obj);
80# If still here after the dclone the fix (#17543) worked.
81is(ref $clone, ref $empty_string_obj);
82is($$clone, $$empty_string_obj);
83is($$clone, '');
84
85
86SKIP: {
87# Do not fail if Tie::Hash and/or Tie::StdHash is not available
88    skip 'No Tie::StdHash available', 2
89	unless eval { require Tie::Hash; scalar keys %Tie::StdHash:: };
90    skip 'This version of perl has problems with Tie::StdHash', 2
91	if $] eq "5.008";
92    tie my %tie, "Tie::StdHash" or die $!;
93    $tie{array} = [1,2,3,4];
94    $tie{hash} = {1,2,3,4};
95    my $clone_array = dclone $tie{array};
96    is("@$clone_array", "@{$tie{array}}");
97    my $clone_hash = dclone $tie{hash};
98    is($clone_hash->{1}, $tie{hash}{1});
99}
100