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
|
use Test2::V0 -no_srand => 1;
use FFI::Platypus;
use FFI::Platypus::Memory qw( malloc free );
skip_all 'test requires variadic function support'
unless eval { FFI::Platypus->new( lib => [undef] )->function(
sprintf => ['opaque', 'string'] => ['float'] ) };
foreach my $api (0,1,2)
{
subtest "api => $api" => sub {
our $ffi = FFI::Platypus->new( api => $api, lib => [undef], experimental => ($api > 2 ? $api : undef));
$ffi->type('float' => 'my_float');
sub callit
{
my($type) = @_;
my $ptr = malloc 1024;
$ffi->function( sprintf => ['opaque','string'] => [$type] )->call($ptr, "%f", 3.14);
my $string = $ffi->cast('opaque' => 'string', $ptr);
free $ptr;
return $string;
}
my $double = callit('double');
my $float = callit('float');
note "double = $double";
note "float = $float";
is $float, $double;
$float = callit('my_float');
note "my_float = $float";
is $float, $double;
};
}
done_testing;
|