Debian Codegraph @

corpus/memory/arg_object.pl

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use Math::Complex;
4 use Test::LeakTrace qw( no_leaks_ok );
5
6 my @types = map { ( "sint$_" , "uint$_" ) }
7 qw( 8 16 32 64 );
8
9 {
10 package Foo;
11
12 sub new
13 {
14 my($class, $arg) = @_;
15 bless \$arg, $class;
16 }
17 }
18
19 foreach my $type (@types)
20 {
21 subtest $type => sub {
22 my $ffi = FFI::Platypus->new( api => 1 );
23 my $f = $ffi->function(0 => [ "object(Foo,$type)" ] => 'void' );
24 my $foo = Foo->new(129);
25 no_leaks_ok { $f->call($foo) };
26 }
27 }
28
29 subtest 'opaque' => sub {
30 my $ffi = FFI::Platypus->new( api => 1, lib => [undef] );
31 my $malloc = $ffi->function( malloc => [ 'size_t' ] => 'opaque' );
32 my $free = $ffi->function( free => [ 'opaque' ] => 'void' );
33 my $ptr = $malloc->call(200);
34 my $f = $ffi->function(0 => [ 'object(Foo)' ] => 'void' );
35
36 my $foo1 = Foo->new($ptr);
37
38 no_leaks_ok { $f->call($foo1) };
39 $free->call($ptr);
40
41 my $foo2 = Foo->new(undef);
42 no_leaks_ok { $f->call($foo2) };
43 };
44
45 done_testing;