Blame t/dclone.t

Packit 14c646
#!./perl
Packit 14c646
#
Packit 14c646
#  Copyright (c) 1995-2000, Raphael Manfredi
Packit 14c646
#  
Packit 14c646
#  You may redistribute only under the same terms as Perl 5, as specified
Packit 14c646
#  in the README file that comes with the distribution.
Packit 14c646
#
Packit 14c646
Packit 14c646
sub BEGIN {
Packit 14c646
    unshift @INC, 't';
Packit 14c646
    unshift @INC, 't/compat' if $] < 5.006002;
Packit 14c646
    require Config; import Config;
Packit 14c646
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
Packit 14c646
        print "1..0 # Skip: Storable was not built\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
    require 'st-dump.pl';
Packit 14c646
}
Packit 14c646
Packit 14c646
Packit 14c646
use Storable qw(dclone);
Packit 14c646
Packit 14c646
use Test::More tests => 14;
Packit 14c646
Packit 14c646
$a = 'toto';
Packit 14c646
$b = \$a;
Packit 14c646
$c = bless {}, CLASS;
Packit 14c646
$c->{attribute} = 'attrval';
Packit 14c646
%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$c);
Packit 14c646
@a = ('first', undef, 3, -4, -3.14159, 456, 4.5,
Packit 14c646
	$b, \$a, $a, $c, \$c, \%a);
Packit 14c646
Packit 14c646
my $aref = dclone(\@a);
Packit 14c646
isnt($aref, undef);
Packit 14c646
Packit 14c646
$dumped = &dump(\@a);
Packit 14c646
isnt($dumped, undef);
Packit 14c646
Packit 14c646
$got = &dump($aref);
Packit 14c646
isnt($got, undef);
Packit 14c646
Packit 14c646
is($got, $dumped);
Packit 14c646
Packit 14c646
package FOO; @ISA = qw(Storable);
Packit 14c646
Packit 14c646
sub make {
Packit 14c646
	my $self = bless {};
Packit 14c646
	$self->{key} = \%main::a;
Packit 14c646
	return $self;
Packit 14c646
};
Packit 14c646
Packit 14c646
package main;
Packit 14c646
Packit 14c646
$foo = FOO->make;
Packit 14c646
my $r = $foo->dclone;
Packit 14c646
isnt($r, undef);
Packit 14c646
Packit 14c646
is(&dump($foo), &dump($r));
Packit 14c646
Packit 14c646
# Ensure refs to "undef" values are properly shared during cloning
Packit 14c646
my $hash;
Packit 14c646
push @{$$hash{''}}, \$$hash{a};
Packit 14c646
is($$hash{''}[0], \$$hash{a});
Packit 14c646
Packit 14c646
my $cloned = dclone(dclone($hash));
Packit 14c646
is($$cloned{''}[0], \$$cloned{a});
Packit 14c646
Packit 14c646
$$cloned{a} = "blah";
Packit 14c646
is($$cloned{''}[0], \$$cloned{a});
Packit 14c646
Packit 14c646
# [ID 20020221.007 (#8624)] SEGV in Storable with empty string scalar object
Packit 14c646
package TestString;
Packit 14c646
sub new {
Packit 14c646
    my ($type, $string) = @_;
Packit 14c646
    return bless(\$string, $type);
Packit 14c646
}
Packit 14c646
package main;
Packit 14c646
my $empty_string_obj = TestString->new('');
Packit 14c646
my $clone = dclone($empty_string_obj);
Packit 14c646
# If still here after the dclone the fix (#17543) worked.
Packit 14c646
is(ref $clone, ref $empty_string_obj);
Packit 14c646
is($$clone, $$empty_string_obj);
Packit 14c646
is($$clone, '');
Packit 14c646
Packit 14c646
Packit 14c646
SKIP: {
Packit 14c646
# Do not fail if Tie::Hash and/or Tie::StdHash is not available
Packit 14c646
    skip 'No Tie::StdHash available', 2
Packit 14c646
	unless eval { require Tie::Hash; scalar keys %Tie::StdHash:: };
Packit 14c646
    skip 'This version of perl has problems with Tie::StdHash', 2
Packit 14c646
	if $] eq "5.008";
Packit 14c646
    tie my %tie, "Tie::StdHash" or die $!;
Packit 14c646
    $tie{array} = [1,2,3,4];
Packit 14c646
    $tie{hash} = {1,2,3,4};
Packit 14c646
    my $clone_array = dclone $tie{array};
Packit 14c646
    is("@$clone_array", "@{$tie{array}}");
Packit 14c646
    my $clone_hash = dclone $tie{hash};
Packit 14c646
    is($clone_hash->{1}, $tie{hash}{1});
Packit 14c646
}