| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::CheckLib; |
| 3 |
use FFI::Platypus; |
| 4 |
|
| 5 |
my $libtest = find_lib lib => 'test', libpath => 't/ffi'; |
| 6 |
|
| 7 |
my $ffi = FFI::Platypus->new; |
| 8 |
$ffi->lib($libtest); |
| 9 |
|
| 10 |
subtest 'fixed length input' => sub { |
| 11 |
|
| 12 |
$ffi->load_custom_type('::StringArray' => 'string_5_hey' => 5, "hey"); |
| 13 |
$ffi->load_custom_type('::StringArray' => 'string_5_undef' => 5, undef); |
| 14 |
|
| 15 |
my $a1 = $ffi->function(get_string_from_array => ['string_5_hey', 'int'] => 'string'); |
| 16 |
my $a2 = $ffi->function(get_string_from_array => ['string_5_undef', 'int'] => 'string'); |
| 17 |
|
| 18 |
my @list = ( 'foo', 'bar', 'baz', undef, 'five', 'six' ); |
| 19 |
|
| 20 |
subtest 'with default' => sub { |
| 21 |
is $a1->(\@list, 0), 'foo', 'a1(0) = foo'; |
| 22 |
is $a1->(\@list, 1), 'bar', 'a1(0) = bar'; |
| 23 |
is $a1->(\@list, 2), 'baz', 'a1(0) = baz'; |
| 24 |
is $a1->(\@list, 3), 'hey', 'a1(0) = hey'; |
| 25 |
is $a1->(\@list, 4), 'five', 'a1(0) = five'; |
| 26 |
is $a1->(\@list, 5), undef, 'a1(0) = undef'; |
| 27 |
}; |
| 28 |
|
| 29 |
subtest 'with default' => sub { |
| 30 |
is $a2->(\@list, 0), 'foo', 'a2(0) = foo'; |
| 31 |
is $a2->(\@list, 1), 'bar', 'a2(0) = bar'; |
| 32 |
is $a2->(\@list, 2), 'baz', 'a2(0) = baz'; |
| 33 |
is $a2->(\@list, 3), undef, 'a2(0) = undef'; |
| 34 |
is $a2->(\@list, 4), 'five', 'a2(0) = five'; |
| 35 |
is $a2->(\@list, 5), undef, 'a2(0) = undef'; |
| 36 |
}; |
| 37 |
|
| 38 |
}; |
| 39 |
|
| 40 |
subtest 'variable length input' => sub { |
| 41 |
|
| 42 |
$ffi->load_custom_type('::StringArray' => 'sa'); |
| 43 |
|
| 44 |
my $get_string_from_array = $ffi->function(get_string_from_array => ['sa','int'] => 'string'); |
| 45 |
|
| 46 |
my @list = qw( foo bar baz ); |
| 47 |
|
| 48 |
for(0..2) |
| 49 |
{ |
| 50 |
is $get_string_from_array->(\@list, $_), $list[$_], "get_string_from_array(\@list, $_) = $list[$_]"; |
| 51 |
} |
| 52 |
|
| 53 |
is $get_string_from_array->(\@list, 3), undef, "get_string_from_array(\@list, 3) = undef"; |
| 54 |
}; |
| 55 |
|
| 56 |
subtest 'fixed length return' => sub { |
| 57 |
|
| 58 |
$ffi->load_custom_type('::StringArray' => 'sa3' => 3); |
| 59 |
$ffi->load_custom_type('::StringArray' => 'sa3x' => 3, 'x'); |
| 60 |
|
| 61 |
is( |
| 62 |
$ffi->function(pointer_null => [] => 'sa3')->call, |
| 63 |
undef, |
| 64 |
'returns null', |
| 65 |
); |
| 66 |
|
| 67 |
is( |
| 68 |
$ffi->function(onetwothree3 => [] => 'sa3')->call, |
| 69 |
[ qw( one two three ) ], |
| 70 |
'returns with just strings', |
| 71 |
); |
| 72 |
|
| 73 |
is( |
| 74 |
$ffi->function(onenullthree3 => [] => 'sa3')->call, |
| 75 |
[ 'one', undef, 'three' ], |
| 76 |
'returns with NULL/undef in the middle', |
| 77 |
); |
| 78 |
|
| 79 |
is( |
| 80 |
$ffi->function(onenullthree3 => [] => 'sa3x')->call, |
| 81 |
[ 'one', 'x', 'three' ], |
| 82 |
'returns with NULL/undef in the middle with default', |
| 83 |
); |
| 84 |
|
| 85 |
}; |
| 86 |
|
| 87 |
subtest 'null terminated return' => sub { |
| 88 |
|
| 89 |
#$ffi->load_custom_type('::StringArray' => 'sa'); |
| 90 |
|
| 91 |
is( |
| 92 |
$ffi->function(pointer_null => [] => 'sa')->call, |
| 93 |
undef, |
| 94 |
'returns null', |
| 95 |
); |
| 96 |
|
| 97 |
is( |
| 98 |
$ffi->function('onetwothree4', => [] => 'sa')->call, |
| 99 |
[ qw( one two three ) ], |
| 100 |
); |
| 101 |
|
| 102 |
is( |
| 103 |
$ffi->function('onenullthree3' => [] => 'sa')->call, |
| 104 |
[ qw( one ) ], |
| 105 |
); |
| 106 |
|
| 107 |
is( |
| 108 |
$ffi->function('ptrnull' => [] => 'sa')->call, |
| 109 |
[], |
| 110 |
); |
| 111 |
|
| 112 |
}; |
| 113 |
|
| 114 |
done_testing; |