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