1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#!./perl
use strict;
use warnings;
use Config;
use Scalar::Util ();
use Test::More ((grep { /weaken/ } @Scalar::Util::EXPORT_FAIL) and !$ENV{PERL_CORE})
? (skip_all => 'weaken requires XS version')
: (tests => 28);
Scalar::Util->import(qw(weaken unweaken isweak));
# two references, one is weakened, the other is then undef'ed.
{
my ($y,$z);
{
my $x = "foo";
$y = \$x;
$z = \$x;
}
ok(ref($y) and ref($z));
weaken($y);
ok(ref($y) and ref($z));
undef($z);
ok(not(defined($y) and defined($z)));
undef($y);
ok(not(defined($y) and defined($z)));
}
# one reference, which is weakened
{
my $y;
{
my $x = "foo";
$y = \$x;
}
ok(ref($y));
weaken($y);
ok(not defined $y);
}
my $flag;
# a circular structure
{
$flag = 0;
{
my $y = bless {}, 'Dest';
$y->{Self} = $y;
$y->{Flag} = \$flag;
weaken($y->{Self});
ok( ref($y) );
}
ok( $flag == 1 );
undef $flag;
}
# a more complicated circular structure
{
$flag = 0;
{
my $y = bless {}, 'Dest';
my $x = bless {}, 'Dest';
$x->{Ref} = $y;
$y->{Ref} = $x;
$x->{Flag} = \$flag;
$y->{Flag} = \$flag;
weaken($x->{Ref});
}
ok( $flag == 2 );
}
# deleting a weakref before the other one
{
my ($y,$z);
{
my $x = "foo";
$y = \$x;
$z = \$x;
}
weaken($y);
undef($y);
ok(not defined $y);
ok(ref($z) );
}
# isweakref
{
$a = 5;
ok(!isweak($a));
$b = \$a;
ok(!isweak($b));
weaken($b);
ok(isweak($b));
$b = \$a;
ok(!isweak($b));
my $x = {};
weaken($x->{Y} = \$a);
ok(isweak($x->{Y}));
ok(!isweak($x->{Z}));
}
# unweaken
{
my ($y,$z);
{
my $x = "foo";
$y = \$x;
$z = \$x;
}
weaken($y);
ok(isweak($y), '$y is weak after weaken()');
is($$y, "foo", '$y points at \"foo" after weaken()');
unweaken($y);
is(ref $y, "SCALAR", '$y is still a SCALAR ref after unweaken()');
ok(!isweak($y), '$y is not weak after unweaken()');
is($$y, "foo", '$y points at \"foo" after unweaken()');
undef $z;
ok(defined $y, '$y still defined after undef $z');
}
# test weaken on a read only ref
SKIP: {
# Doesn't work for older perls, see bug [perl #24506]
skip("Test does not work with perl < 5.8.3", 5) if $] < 5.008003;
# in a MAD build, constants have refcnt 2, not 1
skip("Test does not work with MAD", 5) if exists $Config{mad};
$a = eval '\"hello"';
ok(ref($a)) or print "# didn't get a ref from eval\n";
$b = $a;
eval { weaken($b) };
# we didn't die
is($@, "");
ok(isweak($b));
is($$b, "hello");
$a="";
ok(not $b) or diag("b did not go away");
}
package Dest;
sub DESTROY {
${$_[0]{Flag}} ++;
}
|