Debian Codegraph @

t/type_longdouble.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::Platypus::TypeParser;
4 use FFI::CheckLib;
5 use Config;
6
7 BEGIN {
8 skip_all 'test requires support for long double'
9 unless FFI::Platypus::TypeParser->have_type('longdouble');
10 }
11
12 subtest 'Math::LongDouble is loaded when needed for return type' => sub {
13 my $ffi = FFI::Platypus->new( api => 1 );
14 $ffi->lib(find_lib lib => 'test', libpath => 't/ffi');
15 is($INC{'Math/LongDouble.pm'}, undef, 'not pre-loaded');
16 $ffi->function( longdouble_add => ['longdouble','longdouble'] => 'longdouble' );
17
18 my $pm = $INC{'Math/LongDouble.pm'};
19
20 if(eval q{ use Math::LongDouble; 1 })
21 {
22 is($pm, $INC{'Math/LongDouble.pm'});
23 isnt $pm, undef;
24 }
25 else
26 {
27 is($pm, undef);
28 is($INC{'Math/LongDouble.pm'}, undef);
29 }
30 };
31
32 foreach my $api (0, 1, 2)
33 {
34
35 subtest "api = $api" => sub {
36
37 local $SIG{__WARN__} = sub {
38 my $message = shift;
39 return if $message =~ /^Subroutine main::.* redefined/;
40 warn $message;
41 };
42
43 my $ffi = FFI::Platypus->new( api => $api, experimental => ($api > 2 ? $api : undef) );
44 $ffi->lib(find_lib lib => 'test', libpath => 't/ffi');
45
46 $ffi->type('longdouble*' => 'longdouble_p');
47 $ffi->type('longdouble[3]' => 'longdouble_a3');
48 $ffi->type('longdouble[]' => 'longdouble_a');
49 $ffi->attach( [longdouble_add => 'add'] => ['longdouble','longdouble'] => 'longdouble');
50 $ffi->attach( longdouble_pointer_test => ['longdouble_p', 'longdouble_p'] => 'int');
51 $ffi->attach( longdouble_array_test => ['longdouble_a', 'int'] => 'int');
52 $ffi->attach( [longdouble_array_test => 'longdouble_array_test3'] => ['longdouble_a3', 'int'] => 'int');
53 $ffi->attach( [longdouble_array_test => 'longdouble_array_test_ptr'] => ['longdouble*', 'int'] => 'int') if $api >= 2;
54 $ffi->attach( longdouble_array_return_test => [] => 'longdouble_a3');
55 $ffi->attach( pointer_is_null => ['longdouble_p'] => 'int');
56 $ffi->attach( longdouble_pointer_return_test => ['longdouble'] => 'longdouble_p');
57 $ffi->attach( pointer_null => [] => 'longdouble_p');
58
59 subtest 'with Math::LongDouble' => sub {
60 skip_all 'test requires Math::LongDouble'
61 if $Config{uselongdouble} || !eval q{ use Math::LongDouble; 1 };
62
63 my $ld15 = Math::LongDouble->new(1.5);
64 my $ld25 = Math::LongDouble->new(2.5);
65 my $ld40 = Math::LongDouble->new(4.0);
66 my $ld80 = Math::LongDouble->new(8.0);
67
68 subtest 'scalar' => sub {
69 my $result = add($ld15, $ld25);
70 isa_ok $result, 'Math::LongDouble';
71 ok $result == $ld40, "add(1.5,2.5) = 4.0";
72 };
73
74 subtest 'pointer' => sub {
75 my $x = Math::LongDouble->new(1.5);
76 my $y = Math::LongDouble->new(2.5);
77 ok longdouble_pointer_test(\$x, \$y);
78 ok $x == $ld40;
79 ok $y == $ld80;
80 ok pointer_is_null(undef);
81
82 my $c = longdouble_pointer_return_test($ld15);
83 isa_ok $$c, 'Math::LongDouble';
84 ok $$c == $ld15;
85 };
86
87 my $ld10 = Math::LongDouble->new(1.0);
88 my $ld20 = Math::LongDouble->new(2.0);
89 my $ld30 = Math::LongDouble->new(3.0);
90
91 subtest 'array fixed' => sub {
92 my $list = [ map { Math::LongDouble->new($_) } qw( 25.0 25.0 50.0 )];
93
94 ok longdouble_array_test3($list, 3);
95 note "[", join(',', map { "$_" } @$list), "]";
96 ok $list->[0] == $ld10;
97 ok $list->[1] == $ld20;
98 ok $list->[2] == $ld30;
99 };
100
101 subtest 'array var' => sub {
102 my $list = [ map { Math::LongDouble->new($_) } qw( 25.0 25.0 50.0 )];
103
104 ok longdouble_array_test($list, 3);
105 note "[", join(',', map { "$_" } @$list), "]";
106 ok $list->[0] == $ld10;
107 ok $list->[1] == $ld20;
108 ok $list->[2] == $ld30;
109 };
110
111 subtest 'array var ptr' => sub {
112 skip_all 'for api = 2 and better only' unless $api >= 2;
113 my $list = [ map { Math::LongDouble->new($_) } qw( 25.0 25.0 50.0 )];
114
115 ok longdouble_array_test_ptr($list, 3);
116 note "[", join(',', map { "$_" } @$list), "]";
117 ok $list->[0] == $ld10;
118 ok $list->[1] == $ld20;
119 ok $list->[2] == $ld30;
120 };
121
122 subtest 'array return' => sub {
123 my $list = longdouble_array_return_test();
124 note "[", join(',', map { "$_" } @$list), "]";
125 ok $list->[0] == $ld10;
126 ok $list->[1] == $ld20;
127 ok $list->[2] == $ld30;
128 };
129 };
130
131 subtest 'without Math::LongDouble' => sub {
132 skip_all 'test requires Math::LongDouble'
133 if ! $Config{uselongdouble} || ! eval q{ use Math::LongDouble; 1 };
134
135 subtest 'scalar' => sub {
136 is add(1.5, 2.5), 4.0, "add(1.5,2.5) = 4";
137 };
138
139 subtest 'pointer' => sub {
140 my $x = 1.5;
141 my $y = 2.5;
142 ok longdouble_pointer_test(\$x, \$y);
143 ok $x == 4.0;
144 ok $y == 8.0;
145 ok pointer_is_null(undef);
146
147 my $c = longdouble_pointer_return_test(1.5);
148 ok $$c == 1.5;
149 };
150
151 subtest 'array fixed' => sub {
152 my $list = [ qw( 25.0 25.0 50.0 )];
153
154 ok longdouble_array_test3($list, 3);
155 note "[", join(',', map { "$_" } @$list), "]";
156 ok $list->[0] == 1.0;
157 ok $list->[1] == 2.0;
158 ok $list->[2] == 3.0;
159 };
160
161 subtest 'array var' => sub {
162 my $list = [ qw( 25.0 25.0 50.0 )];
163
164 ok longdouble_array_test($list, 3);
165 note "[", join(',', map { "$_" } @$list), "]";
166 ok $list->[0] == 1.0;
167 ok $list->[1] == 2.0;
168 ok $list->[2] == 3.0;
169 };
170
171 subtest 'array return' => sub {
172 my $list = longdouble_array_return_test();
173 note "[", join(',', map { "$_" } @$list), "]";
174 ok $list->[0] == 1.0;
175 ok $list->[1] == 2.0;
176 ok $list->[2] == 3.0;
177 };
178 };
179 };
180 }
181
182 done_testing;