File: gh323.t

package info (click to toggle)
libffi-platypus-perl 2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,860 kB
  • sloc: perl: 7,388; ansic: 6,862; cpp: 53; sh: 19; makefile: 14
file content (42 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (2)
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;