Debian Codegraph @

t/type_double.t

raw
1 #
2 # DO NOT MODIFY THIS FILE.
3 # This file generated from similar file t/type_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::CheckLib;
9
10 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
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, lib => [@lib], experimental => ($api > 2 ? $api : undef) );
24 $ffi->type('double *' => 'double_p');
25 $ffi->type('double [10]' => 'double_a');
26 $ffi->type('double []' => 'double_a2');
27 $ffi->type('(double)->double' => 'double_c');
28
29 $ffi->attach( [double_add => 'add'] => ['double', 'double'] => 'double');
30 $ffi->attach( [double_inc => 'inc'] => ['double_p', 'double'] => 'double_p');
31 $ffi->attach( [double_sum => 'sum'] => ['double_a'] => 'double');
32 $ffi->attach( [double_sum2 => 'sum2'] => ['double_a2','size_t'] => 'double');
33 $ffi->attach( [double_array_inc => 'array_inc'] => ['double_a'] => 'void');
34 $ffi->attach( [pointer_null => 'null'] => [] => 'double_p');
35 $ffi->attach( [pointer_is_null => 'is_null'] => ['double_p'] => 'int');
36 $ffi->attach( [double_static_array => 'static_array'] => [] => 'double_a');
37 $ffi->attach( [pointer_null => 'null2'] => [] => 'double_a');
38
39 if($api >= 2)
40 {
41 $ffi->attach( [double_sum => 'sum3'] => ['double*'] => 'double');
42 $ffi->attach( [double_array_inc => 'array_inc2'] => ['double*'] => 'void');
43 }
44
45 is add(1.5,2.5), 4, 'add(1.5,2.5) = 4';
46 is eval { no warnings; add() }, 0.0, 'add() = 0.0';
47
48 my $i = 3.5;
49 is ${inc(\$i, 4.25)}, 7.75, 'inc(\$i,4.25) = \7.75';
50
51 is $i, 3.5+4.25, "i=3.5+4.25";
52
53 is ${inc(\3,4)}, 7, 'inc(\3,4) = \7';
54
55 my @list = (1,2,3,4,5,6,7,8,9,10);
56
57 is sum(\@list), 55, 'sum([1..10]) = 55';
58 is sum2(\@list,scalar @list), 55, 'sum2([1..10],10) = 55';
59
60 if($api >= 2)
61 {
62 is sum3(\@list), 55, 'sum([1..10]) = 55';
63 }
64
65 array_inc(\@list);
66 do { local $SIG{__WARN__} = sub {}; array_inc(); };
67
68 is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment';
69
70 if($api >= 2)
71 {
72 array_inc2(\@list);
73 is \@list, [3,4,5,6,7,8,9,10,11,12], 'array increment';
74 }
75
76 is [null()], [$api >= 2 ? (undef) : ()], 'null() == undef';
77 is is_null(undef), 1, 'is_null(undef) == 1';
78 is is_null(), 1, 'is_null() == 1';
79 is is_null(\22), 0, 'is_null(22) == 0';
80
81 is static_array(), [-5.5, 5.5, -10, 10, -15.5, 15.5, 20, -20, 25.5, -25.5], 'static_array = [-5.5, 5.5, -10, 10, -15.5, 15.5, 20, -20, 25.5, -25.5]';
82
83 is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef';
84
85 my $closure = $ffi->closure(sub { $_[0]+2.25 });
86 $ffi->attach( [double_set_closure => 'set_closure'] => ['double_c'] => 'void');
87 $ffi->attach( [double_call_closure => 'call_closure'] => ['double'] => 'double');
88
89 set_closure($closure);
90 is call_closure(2.5), 4.75, 'call_closure(2.5) = 4.75';
91
92 $closure = $ffi->closure(sub { undef });
93 set_closure($closure);
94 is do { no warnings; call_closure(2.5) }, 0, 'call_closure(2.5) = 0';
95
96 subtest 'custom type input' => sub {
97 $ffi->custom_type(type1 => { native_type => 'double', perl_to_native => sub { is $_[0], 1.25; $_[0]+0.25 } });
98 $ffi->attach( [double_add => 'custom_add'] => ['type1','double'] => 'double');
99 is custom_add(1.25,2.5), 4, 'custom_add(1.25,2.5) = 4';
100 };
101
102 subtest 'custom type output' => sub {
103 $ffi->custom_type(type2 => { native_type => 'double', native_to_perl => sub { is $_[0], 2.0; $_[0]+0.25 } });
104 $ffi->attach( [double_add => 'custom_add2'] => ['double','double'] => 'type2');
105 is custom_add2(1,1), 2.25, 'custom_add2(1,1) = 2.25';
106 };
107
108 $ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int');
109 is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1';
110 };
111 }
112
113 done_testing;