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