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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Builder::Tester;
use Test::Refcount;
my $anon = [];
test_out( "ok 1 - anon ARRAY ref" );
is_refcount( $anon, 1, 'anon ARRAY ref' );
test_test( "anon ARRAY ref succeeds" );
test_out( "not ok 1 - not ref" );
test_fail( +2 );
test_err( "# expected a reference, was not given one" );
is_refcount( "hello", 1, 'not ref' );
test_test( "not ref fails" );
my $object = bless {}, "Some::Class";
test_out( "ok 1 - object" );
is_refcount( $object, 1, 'object' );
test_test( "normal object succeeds" );
my $newref = $object;
test_out( "ok 1 - two refs" );
is_refcount( $object, 2, 'two refs' );
test_test( "two refs to object succeeds" );
test_out( "not ok 1 - one ref" );
test_fail( +10 );
test_err( "# expected 1 references, found 2" );
if( Test::Refcount::HAVE_DEVEL_MAT_DUMPER ) {
test_err( qr/^# SV address is 0x[0-9a-f]+\n/ );
test_err( qr/^# Writing heap dump to \S+\n/ );
}
if( Test::Refcount::HAVE_DEVEL_FINDREF ) {
test_err( qr/^# Some::Class=HASH\(0x[0-9a-f]+\) (?:\[refcount 2\] )?is\n/ );
test_err( qr/(?:^#.*\n){1,}/m ); # Don't be sensitive on what Devel::FindRef actually prints
}
is_refcount( $object, 1, 'one ref' );
test_test( "two refs to object fails to be 1" );
undef $newref;
$object->{self} = $object;
test_out( "ok 1 - circular" );
is_refcount( $object, 2, 'circular' );
test_test( "circular object succeeds" );
undef $object->{self};
my $otherobject = bless { firstobject => $object }, "Other::Class";
test_out( "ok 1 - other ref to object" );
is_refcount( $object, 2, 'other ref to object' );
test_test( "object with another reference succeeds" );
undef $otherobject;
test_out( "ok 1 - undefed other ref to object" );
is_refcount( $object, 1, 'undefed other ref to object' );
test_test( "object with another reference undefed succeeds" );
END {
# Clean up Devel::MAT dumpfile
my $pmat = $0;
$pmat =~ s/\.t$/-1.pmat/;
unlink $pmat if -f $pmat;
}
done_testing;
|