Debian Codegraph @

corpus/memory/arg_custom.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 { "$_" } ( 'float', 'double',
7 map { ( "sint$_" , "uint$_" ) }
8 qw( 8 16 32 64 ));
9
10 foreach my $type (@types)
11 {
12 subtest $type => sub {
13 my $ffi = FFI::Platypus->new;
14 $ffi->custom_type( foo_t => {
15 native_type => $type,
16 native_to_perl => sub { $_[0] },
17 perl_to_native => sub { $_[0] },
18 perl_to_native_post => sub { $_[0] },
19 });
20 my $f = $ffi->function(0 => [ "foo_t" ] => 'void' );
21 no_leaks_ok { $f->call(129) };
22 }
23 }
24
25 subtest 'opaque' => sub {
26 my $ffi = FFI::Platypus->new( lib => [undef] );
27 my $malloc = $ffi->function( malloc => [ 'size_t' ] => 'opaque' );
28 my $free = $ffi->function( free => [ 'opaque' ] => 'void' );
29 my $ptr = $malloc->call(200);
30
31 $ffi->custom_type( foo_t => {
32 native_type => 'opaque',
33 native_to_perl => sub { $_[0] },
34 perl_to_native => sub { $_[0] },
35 perl_to_native_post => sub { $_[0] },
36 });
37
38 my $f = $ffi->function(0 => [ 'foo_t' ] => 'void' );
39
40 no_leaks_ok { $f->call($ptr) };
41 $free->call($ptr);
42 no_leaks_ok { $f->call(undef) };
43 };
44
45 done_testing;