| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use lib 't/lib'; |
| 3 |
use Test::FauxAttach; |
| 4 |
use FFI::Platypus; |
| 5 |
use Math::Complex; |
| 6 |
use Test::LeakTrace qw( no_leaks_ok ); |
| 7 |
use FFI::Platypus::Record::Meta; |
| 8 |
|
| 9 |
my @types = ( 'float', 'double', 'longdouble', |
| 10 |
map { ( "sint$_" , "uint$_" ) } |
| 11 |
qw( 8 16 32 64 )); |
| 12 |
|
| 13 |
foreach my $type (@types) |
| 14 |
{ |
| 15 |
subtest $type => sub { |
| 16 |
my $ffi = FFI::Platypus->new; |
| 17 |
my $f = $ffi->function(0 => [ $type ] => 'void' ); |
| 18 |
no_leaks_ok { $f->call(129) }; |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
subtest 'opaque' => sub { |
| 23 |
my $ffi = FFI::Platypus->new( lib => [undef] ); |
| 24 |
my $malloc = $ffi->function( malloc => [ 'size_t' ] => 'opaque' ); |
| 25 |
my $free = $ffi->function( free => [ 'opaque' ] => 'void' ); |
| 26 |
my $ptr = $malloc->call(200); |
| 27 |
my $f = $ffi->function(0 => [ 'opaque' ] => 'void' ); |
| 28 |
|
| 29 |
no_leaks_ok { $f->call($ptr) }; |
| 30 |
$free->call($ptr); |
| 31 |
no_leaks_ok { $f->call(undef) }; |
| 32 |
}; |
| 33 |
|
| 34 |
subtest 'string' => sub { |
| 35 |
my $ffi = FFI::Platypus->new; |
| 36 |
my $f = $ffi->function(0 => [ 'string' ] => 'void' ); |
| 37 |
no_leaks_ok { $f->call("hello world") }; |
| 38 |
no_leaks_ok { $f->call(undef) }; |
| 39 |
}; |
| 40 |
|
| 41 |
subtest 'complex' => sub { |
| 42 |
|
| 43 |
foreach my $type (qw( complex_float complex_double )) |
| 44 |
{ |
| 45 |
subtest $type => sub { |
| 46 |
my $ffi = FFI::Platypus->new; |
| 47 |
my $f = $ffi->function(0 => [ $type ] => 'void' ); |
| 48 |
|
| 49 |
{ |
| 50 |
my $c = [1.0,2.0]; |
| 51 |
no_leaks_ok { $f->call($c) }; |
| 52 |
} |
| 53 |
|
| 54 |
{ |
| 55 |
my $c = Math::Complex->make(1.0,2.0); |
| 56 |
no_leaks_ok { $f->call($c) }; |
| 57 |
} |
| 58 |
}; |
| 59 |
} |
| 60 |
|
| 61 |
}; |
| 62 |
|
| 63 |
subtest 'record' => sub { |
| 64 |
|
| 65 |
my $ffi = FFI::Platypus->new( api => 1 ); |
| 66 |
|
| 67 |
our $meta = FFI::Platypus::Record::Meta->new(['sint32']); |
| 68 |
|
| 69 |
{ |
| 70 |
package Foo; |
| 71 |
sub new |
| 72 |
{ |
| 73 |
my $value = "\0" x 4; |
| 74 |
return bless \$value, 'Foo'; |
| 75 |
} |
| 76 |
sub _ffi_record_size { 4 } |
| 77 |
sub _ffi_meta { $meta } |
| 78 |
} |
| 79 |
|
| 80 |
$ffi->type('record(Foo)' => 'foo_t'); |
| 81 |
my $foo = Foo->new; |
| 82 |
|
| 83 |
foreach my $type (qw( foo_t foo_t* )) |
| 84 |
{ |
| 85 |
subtest $type => sub { |
| 86 |
my $f = $ffi->function(0 => [ $type ] => 'void' ); |
| 87 |
no_leaks_ok { $f->call($foo) }; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
subtest 'record(4)*' => sub { |
| 92 |
my $f = $ffi->function(0 => [ 'record(4)*' ] => 'void' ); |
| 93 |
my $str = "\0" x 4; |
| 94 |
no_leaks_ok { $f->call($str) }; |
| 95 |
}; |
| 96 |
|
| 97 |
undef $meta; |
| 98 |
|
| 99 |
}; |
| 100 |
|
| 101 |
subtest 'closure' => sub { |
| 102 |
|
| 103 |
my $ffi = FFI::Platypus->new; |
| 104 |
$ffi->type('()->void' => 'closure_t'); |
| 105 |
my $f = $ffi->function(0 => [ 'closure_t' ] => 'void' ); |
| 106 |
|
| 107 |
no_leaks_ok { $f->call(undef) }; |
| 108 |
no_leaks_ok { |
| 109 |
my $closure = $ffi->closure(sub {}); |
| 110 |
$f->call($closure); |
| 111 |
}; |
| 112 |
|
| 113 |
{ |
| 114 |
my $closure = $ffi->closure(sub {}); |
| 115 |
$f->call($closure); |
| 116 |
no_leaks_ok { |
| 117 |
$f->call($closure); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
done_testing; |