File: 02_weaken.t

package info (click to toggle)
libtask-weaken-perl 1.03-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 156 kB
  • ctags: 99
  • sloc: perl: 1,246; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 1,699 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

# Ensure that this platform really does have weak references and weaken.

use strict;
BEGIN {
	$|  = 1;
	$^W = 1;
}

use Test::More tests => 19;
use File::Spec::Functions ':ALL';
use Scalar::Util ();

# Ensure we can import weaken and isweak
BEGIN {
	Scalar::Util->import( 'weaken' );
	Scalar::Util->import( 'isweak' );
}
ok( defined(&weaken), '->import(weaken) worked' );
ok( defined(&isweak), '->import(isweak) worked' );





#####################################################################
# Functional Tests

# Test weaken, just in case someone tries to fake its existance.
# Code copied from Scalar::Util itself and stripped of non-essentials.
SCOPE: {
	package Dest;
	sub DESTROY {
		${$_[0]{Flag}} ++;
	}
}
my ($y,$z);
SCOPE: {
	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)) );
SCOPE: {
	my $x = "foo";
	$y = \$x;
}
ok( ref($y) );
weaken($y);
ok( not defined $y  );
my $flag = 0;
SCOPE: {
	my $y = bless {}, 'Dest';
	$y->{Self} = $y;
	$y->{Flag} = \$flag;
	weaken($y->{Self});
	ok( ref($y) );
}
ok( $flag == 1 );
undef $flag;
$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 );
SCOPE: {
	my $x = "foo";
	$y = \$x;
	$z = \$x;
}
weaken($y);
undef($y);
ok( not defined $y);
ok( ref($z) );
my $a = 5;
ok(!isweak($a));
my $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}));