| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::CheckLib; |
| 4 |
|
| 5 |
my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'; |
| 6 |
|
| 7 |
foreach my $api (0, 1, 2) |
| 8 |
{ |
| 9 |
|
| 10 |
subtest "api = $api" => sub { |
| 11 |
|
| 12 |
local $SIG{__WARN__} = sub { |
| 13 |
my $message = shift; |
| 14 |
return if $message =~ /^Subroutine main::.* redefined/; |
| 15 |
warn $message; |
| 16 |
}; |
| 17 |
|
| 18 |
my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef) ); |
| 19 |
$ffi->type('float *' => 'float_p'); |
| 20 |
$ffi->type('float [10]' => 'float_a'); |
| 21 |
$ffi->type('float []' => 'float_a2'); |
| 22 |
$ffi->type('(float)->float' => 'float_c'); |
| 23 |
|
| 24 |
$ffi->attach( [float_add => 'add'] => ['float', 'float'] => 'float'); |
| 25 |
$ffi->attach( [float_inc => 'inc'] => ['float_p', 'float'] => 'float_p'); |
| 26 |
$ffi->attach( [float_sum => 'sum'] => ['float_a'] => 'float'); |
| 27 |
$ffi->attach( [float_sum2 => 'sum2'] => ['float_a2','size_t'] => 'float'); |
| 28 |
$ffi->attach( [float_array_inc => 'array_inc'] => ['float_a'] => 'void'); |
| 29 |
$ffi->attach( [pointer_null => 'null'] => [] => 'float_p'); |
| 30 |
$ffi->attach( [pointer_is_null => 'is_null'] => ['float_p'] => 'int'); |
| 31 |
$ffi->attach( [float_static_array => 'static_array'] => [] => 'float_a'); |
| 32 |
$ffi->attach( [pointer_null => 'null2'] => [] => 'float_a'); |
| 33 |
|
| 34 |
if($api >= 2) |
| 35 |
{ |
| 36 |
$ffi->attach( [float_sum => 'sum3'] => ['float*'] => 'float'); |
| 37 |
$ffi->attach( [float_array_inc => 'array_inc2'] => ['float*'] => 'void'); |
| 38 |
} |
| 39 |
|
| 40 |
is add(1.5,2.5), 4, 'add(1.5,2.5) = 4'; |
| 41 |
is eval { no warnings; add() }, 0.0, 'add() = 0.0'; |
| 42 |
|
| 43 |
my $i = 3.5; |
| 44 |
is ${inc(\$i, 4.25)}, 7.75, 'inc(\$i,4.25) = \7.75'; |
| 45 |
|
| 46 |
is $i, 3.5+4.25, "i=3.5+4.25"; |
| 47 |
|
| 48 |
is ${inc(\3,4)}, 7, 'inc(\3,4) = \7'; |
| 49 |
|
| 50 |
my @list = (1,2,3,4,5,6,7,8,9,10); |
| 51 |
|
| 52 |
is sum(\@list), 55, 'sum([1..10]) = 55'; |
| 53 |
is sum2(\@list,scalar @list), 55, 'sum2([1..10],10) = 55'; |
| 54 |
|
| 55 |
if($api >= 2) |
| 56 |
{ |
| 57 |
is sum3(\@list), 55, 'sum([1..10]) = 55'; |
| 58 |
} |
| 59 |
|
| 60 |
array_inc(\@list); |
| 61 |
do { local $SIG{__WARN__} = sub {}; array_inc(); }; |
| 62 |
|
| 63 |
is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment'; |
| 64 |
|
| 65 |
if($api >= 2) |
| 66 |
{ |
| 67 |
array_inc2(\@list); |
| 68 |
is \@list, [3,4,5,6,7,8,9,10,11,12], 'array increment'; |
| 69 |
} |
| 70 |
|
| 71 |
is [null()], [$api >= 2 ? (undef) : ()], 'null() == undef'; |
| 72 |
is is_null(undef), 1, 'is_null(undef) == 1'; |
| 73 |
is is_null(), 1, 'is_null() == 1'; |
| 74 |
is is_null(\22), 0, 'is_null(22) == 0'; |
| 75 |
|
| 76 |
is static_array(), [-5.5, 5.5, -10, 10, -15.5, 15.5, 20, -20, 25.5, -25.5], 'static_array = [-5.5, 5.5, -10, 10, -15.5, 15.5, 20, -20, 25.5, -25.5]'; |
| 77 |
|
| 78 |
is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef'; |
| 79 |
|
| 80 |
my $closure = $ffi->closure(sub { $_[0]+2.25 }); |
| 81 |
$ffi->attach( [float_set_closure => 'set_closure'] => ['float_c'] => 'void'); |
| 82 |
$ffi->attach( [float_call_closure => 'call_closure'] => ['float'] => 'float'); |
| 83 |
|
| 84 |
set_closure($closure); |
| 85 |
is call_closure(2.5), 4.75, 'call_closure(2.5) = 4.75'; |
| 86 |
|
| 87 |
$closure = $ffi->closure(sub { undef }); |
| 88 |
set_closure($closure); |
| 89 |
is do { no warnings; call_closure(2.5) }, 0, 'call_closure(2.5) = 0'; |
| 90 |
|
| 91 |
subtest 'custom type input' => sub { |
| 92 |
$ffi->custom_type(type1 => { native_type => 'float', perl_to_native => sub { is $_[0], 1.25; $_[0]+0.25 } }); |
| 93 |
$ffi->attach( [float_add => 'custom_add'] => ['type1','float'] => 'float'); |
| 94 |
is custom_add(1.25,2.5), 4, 'custom_add(1.25,2.5) = 4'; |
| 95 |
}; |
| 96 |
|
| 97 |
subtest 'custom type output' => sub { |
| 98 |
$ffi->custom_type(type2 => { native_type => 'float', native_to_perl => sub { is $_[0], 2.0; $_[0]+0.25 } }); |
| 99 |
$ffi->attach( [float_add => 'custom_add2'] => ['float','float'] => 'type2'); |
| 100 |
is custom_add2(1,1), 2.25, 'custom_add2(1,1) = 2.25'; |
| 101 |
}; |
| 102 |
|
| 103 |
$ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int'); |
| 104 |
is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1'; |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
done_testing; |