| 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('uint8 *' => 'uint8_p'); |
| 20 |
$ffi->type('uint8 [10]' => 'uint8_a'); |
| 21 |
$ffi->type('uint8 []' => 'uint8_a2'); |
| 22 |
$ffi->type('(uint8)->uint8' => 'uint8_c'); |
| 23 |
|
| 24 |
$ffi->attach( [uint8_add => 'add'] => ['uint8', 'uint8'] => 'uint8'); |
| 25 |
$ffi->attach( [uint8_inc => 'inc'] => ['uint8_p', 'uint8'] => 'uint8_p'); |
| 26 |
$ffi->attach( [uint8_sum => 'sum'] => ['uint8_a'] => 'uint8'); |
| 27 |
$ffi->attach( [uint8_sum2 => 'sum2'] => ['uint8_a2','size_t'] => 'uint8'); |
| 28 |
$ffi->attach( [uint8_array_inc => 'array_inc'] => ['uint8_a'] => 'void'); |
| 29 |
$ffi->attach( [pointer_null => 'null'] => [] => 'uint8_p'); |
| 30 |
$ffi->attach( [pointer_is_null => 'is_null'] => ['uint8_p'] => 'int'); |
| 31 |
$ffi->attach( [uint8_static_array => 'static_array'] => [] => 'uint8_a'); |
| 32 |
$ffi->attach( [pointer_null => 'null2'] => [] => 'uint8_a'); |
| 33 |
|
| 34 |
if($api >= 2) |
| 35 |
{ |
| 36 |
$ffi->attach( [uint8_sum => 'sum3'] => ['uint8*'] => 'uint8' ); |
| 37 |
$ffi->attach( [uint8_array_inc => 'array_inc2'] => ['uint8*'] => 'void'); |
| 38 |
} |
| 39 |
|
| 40 |
is add(1,2), 3, 'add(1,2) = 3'; |
| 41 |
is do { no warnings; add() }, 0, 'add() = 0'; |
| 42 |
|
| 43 |
my $i = 3; |
| 44 |
is ${inc(\$i, 4)}, 7, 'inc(\$i,4) = \7'; |
| 45 |
|
| 46 |
is $i, 3+4, "i=3+4"; |
| 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 (passed as pointer)'); |
| 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 |
@list = (1,2,3,4,5,6,7,8,9,10); |
| 68 |
array_inc2(\@list); |
| 69 |
is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment'; |
| 70 |
} |
| 71 |
|
| 72 |
is [null()], [$api >= 2 ? (undef) : ()], 'null() == undef'; |
| 73 |
is is_null(undef), 1, 'is_null(undef) == 1'; |
| 74 |
is is_null(), 1, 'is_null() == 1'; |
| 75 |
is is_null(\22), 0, 'is_null(22) == 0'; |
| 76 |
|
| 77 |
is static_array(), [1,4,6,8,10,12,14,16,18,20], 'static_array = [1,4,6,8,10,12,14,16,18,20]'; |
| 78 |
|
| 79 |
is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef'; |
| 80 |
|
| 81 |
my $closure = $ffi->closure(sub { $_[0]+2 }); |
| 82 |
$ffi->attach( [uint8_set_closure => 'set_closure'] => ['uint8_c'] => 'void'); |
| 83 |
$ffi->attach( [uint8_call_closure => 'call_closure'] => ['uint8'] => 'uint8'); |
| 84 |
|
| 85 |
set_closure($closure); |
| 86 |
is call_closure(2), 4, 'call_closure(2) = 4'; |
| 87 |
|
| 88 |
$closure = $ffi->closure(sub { undef }); |
| 89 |
set_closure($closure); |
| 90 |
is do { no warnings; call_closure(2) }, 0, 'call_closure(2) = 0'; |
| 91 |
|
| 92 |
subtest 'custom type input' => sub { |
| 93 |
$ffi->custom_type(type1 => { native_type => 'uint8', perl_to_native => sub { is $_[0], 2; $_[0]*2 } }); |
| 94 |
$ffi->attach( [uint8_add => 'custom_add'] => ['type1','uint8'] => 'uint8'); |
| 95 |
is custom_add(2,1), 5, 'custom_add(2,1) = 5'; |
| 96 |
}; |
| 97 |
|
| 98 |
subtest 'custom type output' => sub { |
| 99 |
$ffi->custom_type(type2 => { native_type => 'uint8', native_to_perl => sub { is $_[0], 2; $_[0]*2 } }); |
| 100 |
$ffi->attach( [uint8_add => 'custom_add2'] => ['uint8','uint8'] => 'type2'); |
| 101 |
is custom_add2(1,1), 4, 'custom_add2(1,1) = 4'; |
| 102 |
}; |
| 103 |
|
| 104 |
subtest 'custom type post' => sub { |
| 105 |
$ffi->custom_type(type3 => { native_type => 'uint8', perl_to_native_post => sub { is $_[0], 1 } }); |
| 106 |
$ffi->attach( [uint8_add => 'custom_add3'] => ['type3','uint8'] => 'uint8'); |
| 107 |
is custom_add3(1,2), 3, 'custom_add3(1,2) = 3'; |
| 108 |
}; |
| 109 |
|
| 110 |
$ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int'); |
| 111 |
is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1'; |
| 112 |
|
| 113 |
}; |
| 114 |
} |
| 115 |
|
| 116 |
foreach my $api (1,2) |
| 117 |
{ |
| 118 |
subtest 'object' => sub { |
| 119 |
|
| 120 |
{ package Roger } |
| 121 |
|
| 122 |
my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef ) ); |
| 123 |
$ffi->type('object(Roger,uint8)', 'roger_t'); |
| 124 |
|
| 125 |
my $int = 211; |
| 126 |
|
| 127 |
subtest 'argument' => sub { |
| 128 |
|
| 129 |
is $ffi->cast('roger_t' => 'uint8', bless(\$int, 'Roger')), $int; |
| 130 |
|
| 131 |
}; |
| 132 |
|
| 133 |
subtest 'return value' => sub { |
| 134 |
|
| 135 |
my $obj1 = $ffi->cast('uint8' => 'roger_t', $int); |
| 136 |
isa_ok $obj1, 'Roger'; |
| 137 |
is $$obj1, $int; |
| 138 |
|
| 139 |
}; |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
done_testing; |