| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::Platypus::TypeParser; |
| 4 |
use FFI::CheckLib; |
| 5 |
use Data::Dumper qw( Dumper ); |
| 6 |
|
| 7 |
BEGIN { |
| 8 |
skip_all 'Test requires support for float complex' |
| 9 |
unless FFI::Platypus::TypeParser->have_type('complex_float'); |
| 10 |
} |
| 11 |
|
| 12 |
foreach my $api (0, 1, 2) |
| 13 |
{ |
| 14 |
|
| 15 |
subtest "api = $api" => sub { |
| 16 |
|
| 17 |
local $SIG{__WARN__} = sub { |
| 18 |
my $message = shift; |
| 19 |
return if $message =~ /^Subroutine main::.* redefined/; |
| 20 |
warn $message; |
| 21 |
}; |
| 22 |
|
| 23 |
my $ffi = FFI::Platypus->new( api => $api, experimental => ($api > 2 ? $api : undef) ); |
| 24 |
$ffi->lib(find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi'); |
| 25 |
|
| 26 |
$ffi->attach(['complex_float_get_real' => 'creal'] => ['complex_float'] => 'float'); |
| 27 |
$ffi->attach(['complex_float_get_imag' => 'cimag'] => ['complex_float'] => 'float'); |
| 28 |
$ffi->attach(['complex_float_to_string' => 'to_string'] => ['complex_float'] => 'string'); |
| 29 |
|
| 30 |
subtest 'standard argument' => sub { |
| 31 |
subtest 'with a real number' => sub { |
| 32 |
note "to_string(10.5) = ", to_string(10.5); |
| 33 |
is creal(10.5), 10.5, "creal(10.5) = 10.5"; |
| 34 |
is cimag(10.5), 0.0, "cimag(10.5) = 0.0"; |
| 35 |
}; |
| 36 |
|
| 37 |
subtest 'with an array ref' => sub { |
| 38 |
note "to_string([10.5,20.5]) = ", to_string([10.5,20.5]); |
| 39 |
is creal([10.5,20.5]), 10.5, "creal([10.5,20.5]) = 10.5"; |
| 40 |
is cimag([10.5,20.5]), 20.5, "cimag([10.5,20.5]) = 20.5"; |
| 41 |
}; |
| 42 |
|
| 43 |
subtest 'with Math::Complex' => sub { |
| 44 |
skip_all 'test requires Math::Complex' |
| 45 |
unless eval q{ use Math::Complex (); 1 }; |
| 46 |
my $c = Math::Complex->make(10.5, 20.5); |
| 47 |
note "to_string(\$c) = ", to_string($c); |
| 48 |
is creal($c), 10.5, "creal(\$c) = 10.5"; |
| 49 |
is cimag($c), 20.5, "cimag(\$c) = 20.5"; |
| 50 |
}; |
| 51 |
}; |
| 52 |
|
| 53 |
$ffi->attach(['complex_float_ptr_get_real' => 'creal_ptr'] => ['complex_float *'] => 'float'); |
| 54 |
$ffi->attach(['complex_float_ptr_get_imag' => 'cimag_ptr'] => ['complex_float *'] => 'float'); |
| 55 |
$ffi->attach(['complex_float_ptr_set' => 'complex_set'] => ['complex_float *','float','float'] => 'void'); |
| 56 |
|
| 57 |
subtest 'pointer argument' => sub { |
| 58 |
subtest 'with a real number' => sub { |
| 59 |
note "to_string(10.5) = ", to_string(10.5); |
| 60 |
is creal_ptr(\10.5), 10.5, "creal_ptr(\\10.5) = 10.5"; |
| 61 |
is cimag_ptr(\10.5), 0.0, "cimag_ptr(\\10.5) = 0.0"; |
| 62 |
}; |
| 63 |
|
| 64 |
subtest 'with an array ref' => sub { |
| 65 |
note "to_string([10.5,20.5]) = ", to_string([10.5,20.5]); |
| 66 |
is creal_ptr(\[10.5,20.5]), 10.5, "creal_ptr(\\[10.5,20.5]) = 10.5"; |
| 67 |
is cimag_ptr(\[10.5,20.5]), 20.5, "cimag_ptr(\\[10.5,20.5]) = 20.5"; |
| 68 |
}; |
| 69 |
|
| 70 |
subtest 'with Math::Complex' => sub { |
| 71 |
skip_all 'test requires Math::Complex' |
| 72 |
unless eval q{ use Math::Complex (); 1 }; |
| 73 |
my $c = Math::Complex->make(10.5, 20.5); |
| 74 |
note "to_string(\$c) = ", to_string($c); |
| 75 |
is creal_ptr(\$c), 10.5, "creal_ptr(\\$c) = 10.5"; |
| 76 |
is cimag_ptr(\$c), 20.5, "cimag_ptr(\\$c) = 20.5"; |
| 77 |
}; |
| 78 |
|
| 79 |
subtest 'values set on out (array)' => sub { |
| 80 |
my @c; |
| 81 |
complex_set(\\@c, 1.0, 2.0); |
| 82 |
is \@c, [ 1.0, 2.0 ]; |
| 83 |
}; |
| 84 |
|
| 85 |
subtest 'values set on out (object)' => sub { |
| 86 |
skip_all 'test requires Math::Complex' |
| 87 |
unless eval q{ use Math::Complex (); 1 }; |
| 88 |
my $c = Math::Complex->make(0.0, 0.0); |
| 89 |
complex_set(\$c, 1.0, 2.0); |
| 90 |
is( [ $c->Re, $c->Im ], [1.0,2.0] ); |
| 91 |
}; |
| 92 |
|
| 93 |
subtest 'values set on out (other)' => sub { |
| 94 |
my $c; |
| 95 |
complex_set(\$c, 1.0, 2.0); |
| 96 |
is( $c, [1.0, 2.0]); |
| 97 |
}; |
| 98 |
|
| 99 |
}; |
| 100 |
|
| 101 |
$ffi->attach(['pointer_null' => 'complex_null'] => [] => 'complex_float*'); |
| 102 |
$ffi->attach(['complex_float_ret' => 'complex_ret'] => ['float','float'] => 'complex_float'); |
| 103 |
$ffi->attach(['complex_float_ptr_ret' => 'complex_ptr_ret'] => ['float','float'] => 'complex_float*'); |
| 104 |
|
| 105 |
subtest 'return value' => sub { |
| 106 |
|
| 107 |
is(complex_ret(1.0,2.0), [1.0,2.0], 'standard'); |
| 108 |
is(complex_ptr_ret(1.0,2.0), \[1.0,2.0], 'pointer'); |
| 109 |
is([complex_null()], $api >= 2 ? [undef] : [], 'null'); |
| 110 |
|
| 111 |
}; |
| 112 |
|
| 113 |
subtest 'complex array arg' => sub { |
| 114 |
|
| 115 |
my $f = $ffi->function(complex_float_array_get => ['complex_float[]','int'] => 'complex_float' ); |
| 116 |
|
| 117 |
my @a = ([0.0,0.0], [1.0,2.0], [3.0,4.0]); |
| 118 |
my $ret; |
| 119 |
is( $ret = $f->call(\@a, 0), [0.0,0.0] ) |
| 120 |
or diag Dumper($ret); |
| 121 |
is( $ret = $f->call(\@a, 1), [1.0,2.0] ) |
| 122 |
or diag Dumper($ret); |
| 123 |
is( $ret = $f->call(\@a, 2), [3.0,4.0] ) |
| 124 |
or diag Dumper($ret); |
| 125 |
|
| 126 |
}; |
| 127 |
|
| 128 |
subtest 'complex array arg' => sub { |
| 129 |
|
| 130 |
skip_all 'for api >= 2 only' unless $api >= 2; |
| 131 |
|
| 132 |
my $f = $ffi->function(complex_float_array_get => ['complex_float*','int'] => 'complex_float' ); |
| 133 |
|
| 134 |
my @a = ([0.0,0.0], [1.0,2.0], [3.0,4.0]); |
| 135 |
my $ret; |
| 136 |
is( $ret = $f->call(\@a, 0), [0.0,0.0] ) |
| 137 |
or diag Dumper($ret); |
| 138 |
is( $ret = $f->call(\@a, 1), [1.0,2.0] ) |
| 139 |
or diag Dumper($ret); |
| 140 |
is( $ret = $f->call(\@a, 2), [3.0,4.0] ) |
| 141 |
or diag Dumper($ret); |
| 142 |
|
| 143 |
}; |
| 144 |
|
| 145 |
subtest 'complex array arg set' => sub { |
| 146 |
|
| 147 |
my $f = $ffi->function(complex_float_array_set => ['complex_float[]','int','float','float'] => 'void' ); |
| 148 |
|
| 149 |
my @a = ([0.0,0.0], [1.0,2.0], [3.0,4.0]); |
| 150 |
$f->call(\@a, 1, 5.0, 6.0); |
| 151 |
is(\@a, [[0.0,0.0], [5.0,6.0], [3.0,4.0]]); |
| 152 |
|
| 153 |
}; |
| 154 |
|
| 155 |
subtest 'complex array arg set' => sub { |
| 156 |
|
| 157 |
skip_all 'for api >= 2 only' unless $api >= 2; |
| 158 |
|
| 159 |
my $f = $ffi->function(complex_float_array_set => ['complex_float*','int','float','float'] => 'void' ); |
| 160 |
|
| 161 |
my @a = ([0.0,0.0], [1.0,2.0], [3.0,4.0]); |
| 162 |
$f->call(\@a, 1, 5.0, 6.0); |
| 163 |
is(\@a, [[0.0,0.0], [5.0,6.0], [3.0,4.0]]); |
| 164 |
|
| 165 |
}; |
| 166 |
|
| 167 |
subtest 'complex array ret' => sub { |
| 168 |
|
| 169 |
my $f = $ffi->function(complex_float_array_ret => [] => 'complex_float[3]' ); |
| 170 |
|
| 171 |
my @a = ([0.0,0.0], [1.0,2.0], [3.0,4.0]); |
| 172 |
my $ret; |
| 173 |
|
| 174 |
is( |
| 175 |
$ret = $f->call( \@a ), |
| 176 |
\@a, |
| 177 |
) or diag Dumper($ret); |
| 178 |
|
| 179 |
}; |
| 180 |
}; |
| 181 |
} |
| 182 |
|
| 183 |
done_testing; |