Debian Codegraph @

t/type_uint16.t

raw
1 #
2 # DO NOT MODIFY THIS FILE.
3 # This file generated from similar file t/type_uint8.t
4 # all instances of "int8" have been changed to "int16"
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('uint16 *' => 'uint16_p');
25 $ffi->type('uint16 [10]' => 'uint16_a');
26 $ffi->type('uint16 []' => 'uint16_a2');
27 $ffi->type('(uint16)->uint16' => 'uint16_c');
28
29 $ffi->attach( [uint16_add => 'add'] => ['uint16', 'uint16'] => 'uint16');
30 $ffi->attach( [uint16_inc => 'inc'] => ['uint16_p', 'uint16'] => 'uint16_p');
31 $ffi->attach( [uint16_sum => 'sum'] => ['uint16_a'] => 'uint16');
32 $ffi->attach( [uint16_sum2 => 'sum2'] => ['uint16_a2','size_t'] => 'uint16');
33 $ffi->attach( [uint16_array_inc => 'array_inc'] => ['uint16_a'] => 'void');
34 $ffi->attach( [pointer_null => 'null'] => [] => 'uint16_p');
35 $ffi->attach( [pointer_is_null => 'is_null'] => ['uint16_p'] => 'int');
36 $ffi->attach( [uint16_static_array => 'static_array'] => [] => 'uint16_a');
37 $ffi->attach( [pointer_null => 'null2'] => [] => 'uint16_a');
38
39 if($api >= 2)
40 {
41 $ffi->attach( [uint16_sum => 'sum3'] => ['uint16*'] => 'uint16' );
42 $ffi->attach( [uint16_array_inc => 'array_inc2'] => ['uint16*'] => 'void');
43 }
44
45 is add(1,2), 3, 'add(1,2) = 3';
46 is do { no warnings; add() }, 0, 'add() = 0';
47
48 my $i = 3;
49 is ${inc(\$i, 4)}, 7, 'inc(\$i,4) = \7';
50
51 is $i, 3+4, "i=3+4";
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 (passed as pointer)');
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 @list = (1,2,3,4,5,6,7,8,9,10);
73 array_inc2(\@list);
74 is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment';
75 }
76
77 is [null()], [$api >= 2 ? (undef) : ()], 'null() == undef';
78 is is_null(undef), 1, 'is_null(undef) == 1';
79 is is_null(), 1, 'is_null() == 1';
80 is is_null(\22), 0, 'is_null(22) == 0';
81
82 is static_array(), [1,4,6,8,10,12,14,16,18,20], 'static_array = [1,4,6,8,10,12,14,16,18,20]';
83
84 is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef';
85
86 my $closure = $ffi->closure(sub { $_[0]+2 });
87 $ffi->attach( [uint16_set_closure => 'set_closure'] => ['uint16_c'] => 'void');
88 $ffi->attach( [uint16_call_closure => 'call_closure'] => ['uint16'] => 'uint16');
89
90 set_closure($closure);
91 is call_closure(2), 4, 'call_closure(2) = 4';
92
93 $closure = $ffi->closure(sub { undef });
94 set_closure($closure);
95 is do { no warnings; call_closure(2) }, 0, 'call_closure(2) = 0';
96
97 subtest 'custom type input' => sub {
98 $ffi->custom_type(type1 => { native_type => 'uint16', perl_to_native => sub { is $_[0], 2; $_[0]*2 } });
99 $ffi->attach( [uint16_add => 'custom_add'] => ['type1','uint16'] => 'uint16');
100 is custom_add(2,1), 5, 'custom_add(2,1) = 5';
101 };
102
103 subtest 'custom type output' => sub {
104 $ffi->custom_type(type2 => { native_type => 'uint16', native_to_perl => sub { is $_[0], 2; $_[0]*2 } });
105 $ffi->attach( [uint16_add => 'custom_add2'] => ['uint16','uint16'] => 'type2');
106 is custom_add2(1,1), 4, 'custom_add2(1,1) = 4';
107 };
108
109 subtest 'custom type post' => sub {
110 $ffi->custom_type(type3 => { native_type => 'uint16', perl_to_native_post => sub { is $_[0], 1 } });
111 $ffi->attach( [uint16_add => 'custom_add3'] => ['type3','uint16'] => 'uint16');
112 is custom_add3(1,2), 3, 'custom_add3(1,2) = 3';
113 };
114
115 $ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int');
116 is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1';
117
118 };
119 }
120
121 foreach my $api (1,2)
122 {
123 subtest 'object' => sub {
124
125 { package Roger }
126
127 my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef ) );
128 $ffi->type('object(Roger,uint16)', 'roger_t');
129
130 my $int = 211;
131
132 subtest 'argument' => sub {
133
134 is $ffi->cast('roger_t' => 'uint16', bless(\$int, 'Roger')), $int;
135
136 };
137
138 subtest 'return value' => sub {
139
140 my $obj1 = $ffi->cast('uint16' => 'roger_t', $int);
141 isa_ok $obj1, 'Roger';
142 is $$obj1, $int;
143
144 };
145 }
146 };
147
148 done_testing;