Debian Codegraph @

ext/XS-APItest/t/weaken.t

raw
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7
8 use_ok('XS::APItest');
9
10 # test sv_rvweaken() and sv_get_backrefs()
11 #
12 # weaken() maps to sv_rvweaken() and is the same as the one
13 # from Scalar::Utils - we recreate it in XS::APItest so
14 # we can test it even if we build without Scalar::Utils
15 #
16 # has_backrefs() maps to sv_get_backrefs(), which would not
17 # normally be useful to Perl code. (Er, maybe :-)
18
19 # has_backrefs is really an internal routine
20 # which would not normally have to worry about refs
21 # and things like that, but to use it from perl we cant
22 # have an AV/HV without having an RV wrapping it, so we
23 # mandate the ref always.
24
25 my $foo= "foo";
26 my $bar= "bar";
27
28 my $scalar_ref= \$foo;
29 my $array_ref= [ qw(this is an array) ];
30 my $hash_ref= { this => is => a => 'hash' };
31
32 my $nrml_scalar_ref= \$bar;
33 my $nrml_array_ref= [ qw( this is an array ) ];
34 my $nrml_hash_ref= { this => is => a => 'hash' };
35
36 # we could probably do other tests here, such as
37 # verify the refcount of the referents, but maybe
38 # another day.
39 apitest_weaken(my $weak_scalar_ref= $scalar_ref);
40 apitest_weaken(my $weak_array_ref= $array_ref);
41 apitest_weaken(my $weak_hash_ref= $hash_ref);
42
43 ok(has_backrefs($scalar_ref), "scalar with backrefs");
44 ok(has_backrefs($array_ref), "array with backrefs");
45 ok(has_backrefs($hash_ref), "hash with backrefs");
46
47 ok(!has_backrefs($nrml_scalar_ref), "scalar without backrefs");
48 ok(!has_backrefs($nrml_array_ref), "array without backrefs");
49 ok(!has_backrefs($nrml_hash_ref), "hash without backrefs");
50
51 1;
52