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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
use Test2::V0 -no_srand => 1;
use FFI::Platypus;
use FFI::CheckLib;
use FFI::Platypus::ShareConfig;
my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
my @legal = qw( float double opaque );
push @legal, map { ("sint$_","uint$_") } qw( 8 16 32 64 );
my $return_ok = FFI::Platypus::ShareConfig->get('probe')->{recordvalue};
subtest 'legal custom types' => sub {
my $ffi = FFI::Platypus->new( api => 1 );
foreach my $type (@legal)
{
local $@ = "";
eval {
$ffi->custom_type( "foo_$type" => {
native_type => $type,
native_to_perl => sub {},
});
};
is "$@", "";
}
};
subtest 'illegal types' => sub {
my $ffi = FFI::Platypus->new( api => 1 );
foreach my $type (qw( sint8[32] sint8* ))
{
local $@ = "";
my $alias = "foo_$type";
$alias =~ s/[\*\[\]]/_/g;
note "alias = $alias";
eval {
$ffi->custom_type( $alias => {
native_type => $type,
native_to_perl => sub {},
});
};
like "$@", qr/\Q$type\E is not a legal basis for a custom type/;
}
};
subtest 'records' => sub {
{
package Foo;
use FFI::Platypus::Record;
record_layout qw(
string(16) name
sint32 value
);
}
subtest 'pointer' => sub {
my $ffi = FFI::Platypus->new( api => 2, lib => [@lib] );
local $@ = '';
eval {
$ffi->custom_type(
'foo_t' => {
native_type => 'record(Foo)*',
perl_to_native => sub {
my $var = shift;
return Foo->new(name => $var->[0], value => $var->[1]);
},
native_to_perl => sub {
my $rec = shift;
return defined $rec ? [$rec->name, $rec->value] : [];
},
},
);
};
is "$@", '' or return;
{
is(
$ffi->function( foo_get_name => [ 'foo_t' ] => 'string' )
->call( ["Graham", 47] ),
"Graham",
);
is(
$ffi->function( foo_get_value => [ 'foo_t' ] => 'sint32' )
->call( ["Graham", 47] ),
47,
);
is(
$ffi->function( foo_create => ['string','sint32'] => 'foo_t' )
->call("Adams", 42),
["Adams\0\0\0\0\0\0\0\0\0\0\0", 42],
);
is(
$ffi->function( pointer_null => [] => 'foo_t' )
->call,
[],
);
}
};
subtest 'by-value' => sub {
my $ffi = FFI::Platypus->new( api => 1, lib => [@lib] );
local $@ = '';
eval {
$ffi->custom_type(
'foo_t' => {
native_type => 'record(Foo)',
perl_to_native => sub {
my $var = shift;
return Foo->new(name => $var->[0], value => $var->[1]);
},
native_to_perl => sub {
my $rec = shift;
return [$rec->name, $rec->value];
},
},
);
};
is "$@", '' or return;
{
is(
$ffi->function( foo_value_get_name => [ 'foo_t' ] => 'string' )
->call( ["Graham", 47] ),
"Graham",
);
is(
$ffi->function( foo_value_get_value => [ 'foo_t' ] => 'sint32' )
->call( ["Graham", 47] ),
47,
);
subtest 'return-value' => sub {
skip_all 'test requires working return records-by-value'
unless $return_ok;
is(
$ffi->function( foo_value_create => ['string','sint32'] => 'foo_t' )
->call("Adams", 42),
["Adams\0\0\0\0\0\0\0\0\0\0\0", 42],
);
};
}
};
};
done_testing;
|