Debian Codegraph @

t/type_sint64.t

raw
1 #
2 # DO NOT MODIFY THIS FILE.
3 # This file generated from similar file t/type_sint8.t
4 # all instances of "int8" have been changed to "int64"
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('sint64 *' => 'sint64_p');
25 $ffi->type('sint64 [10]' => 'sint64_a');
26 $ffi->type('sint64 []' => 'sint64_a2');
27 $ffi->type('(sint64)->sint64' => 'sint64_c');
28
29 $ffi->attach( [sint64_add => 'add'] => ['sint64', 'sint64'] => 'sint64');
30 $ffi->attach( [sint64_inc => 'inc'] => ['sint64_p', 'sint64'] => 'sint64_p');
31 $ffi->attach( [sint64_sum => 'sum'] => ['sint64_a'] => 'sint64');
32 $ffi->attach( [sint64_sum2 => 'sum2'] => ['sint64_a2','size_t'] => 'sint64');
33 $ffi->attach( [sint64_array_inc => 'array_inc'] => ['sint64_a'] => 'void');
34 $ffi->attach( [pointer_null => 'null'] => [] => 'sint64_p');
35 $ffi->attach( [pointer_is_null => 'is_null'] => ['sint64_p'] => 'int');
36 $ffi->attach( [sint64_static_array => 'static_array'] => [] => 'sint64_a');
37 $ffi->attach( [pointer_null => 'null2'] => [] => 'sint64_a');
38
39 if($api >= 2)
40 {
41 $ffi->attach( [sint64_sum => 'sum3'] => ['sint64*'] => 'sint64' );
42 $ffi->attach( [sint64_array_inc => 'array_inc2'] => ['sint64*'] => 'void');
43 }
44
45 is add(-1,2), 1, 'add(-1,2) = 1';
46 is do { no warnings; add() }, 0, 'add() = 0';
47
48 my $i = -3;
49 is ${inc(\$i, 4)}, 1, 'inc(\$i,4) = \1';
50
51 is $i, 1, "i=1";
52
53 is ${inc(\-3,4)}, 1, 'inc(\-3,4) = \1';
54
55 my @list = (-5,-4,-3,-2,-1,0,1,2,3,4);
56
57 is sum(\@list), -5, 'sum([-5..4]) = -5';
58 is sum2(\@list,scalar @list), -5, 'sum([-5..4],10) = -5';
59
60 if($api >= 2)
61 {
62 is(sum3(\@list), -5, 'sum([-5..4]) = -5 (passed as pointer)');
63 }
64
65 array_inc(\@list);
66 do { local $SIG{__WARN__} = sub {}; array_inc() };
67
68 is \@list, [-4,-3,-2,-1,0,1,2,3,4,5], 'array increment';
69
70 if($api >= 2)
71 {
72 @list = (-5,-4,-3,-2,-1,0,1,2,3,4);
73 array_inc2(\@list);
74 is \@list, [-4,-3,-2,-1,0,1,2,3,4,5], '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,2,-3,4,-5,6,-7,8,-9,10], 'static_array = [-1,2,-3,4,-5,6,-7,8,-9,10]';
83
84 is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef';
85
86 my $closure = $ffi->closure(sub { $_[0]-2 });
87 $ffi->attach( [sint64_set_closure => 'set_closure'] => ['sint64_c'] => 'void');
88 $ffi->attach( [sint64_call_closure => 'call_closure'] => ['sint64'] => 'sint64');
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 => 'uint64', perl_to_native => sub { is $_[0], -2; $_[0]*2 } });
99 $ffi->attach( [sint64_add => 'custom_add'] => ['type1','sint64'] => 'sint64');
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 => 'sint64', native_to_perl => sub { is $_[0], -3; $_[0]*2 } });
105 $ffi->attach( [sint64_add => 'custom_add2'] => ['sint64','sint64'] => 'type2');
106 is custom_add2(-2,-1), -6, 'custom_add2(-2,-1) = -6';
107 };
108
109 $ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int');
110 is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1';
111 };
112 }
113
114 foreach my $api (1,2)
115 {
116 subtest 'object' => sub {
117
118 { package Roger }
119
120 my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef) );
121 $ffi->type('object(Roger,sint64)', 'roger_t');
122
123 my $int = -22;
124
125 subtest 'argument' => sub {
126
127 is $ffi->cast('roger_t' => 'sint64', bless(\$int, 'Roger')), $int;
128
129 };
130
131 subtest 'return value' => sub {
132
133 my $obj1 = $ffi->cast('sint64' => 'roger_t', $int);
134 isa_ok $obj1, 'Roger';
135 is $$obj1, $int;
136 };
137
138 };
139
140 };
141
142 done_testing;