1use Test2::V0 -no_srand => 1;
2use FFI::Platypus;
3use FFI::Platypus::Memory qw( malloc free );
4
5skip_all 'test requires variadic function support'
6  unless eval { FFI::Platypus->new( lib => [undef] )->function(
7    sprintf => ['opaque', 'string'] => ['float'] ) };
8
9foreach my $api (0,1,2)
10{
11
12  subtest "api => $api" => sub {
13
14    our $ffi = FFI::Platypus->new( api => $api, lib => [undef], experimental => ($api >=2 ? $api : undef));
15
16    $ffi->type('float' => 'my_float');
17
18    sub callit
19    {
20      my($type) = @_;
21
22      my $ptr = malloc 1024;
23      $ffi->function( sprintf => ['opaque','string'] => [$type] )->call($ptr, "%f", 3.14);
24      my $string = $ffi->cast('opaque' => 'string', $ptr);
25      free $ptr;
26      return $string;
27    }
28
29    my $double = callit('double');
30    my $float  = callit('float');
31    note "double = $double";
32    note "float  = $float";
33    is $float, $double;
34
35    $float  = callit('my_float');
36    note "my_float = $float";
37    is $float, $double;
38
39  };
40}
41
42done_testing;
43