Debian Codegraph @

t/type_string.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::CheckLib;
4
5 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
6
7 foreach my $api (0, 1, 2)
8 {
9
10 subtest "api = $api" => sub {
11
12 local $SIG{__WARN__} = sub {
13 my $message = shift;
14 return if $message =~ /^Subroutine main::.* redefined/;
15 warn $message;
16 };
17
18 my $p = $api == 0 ? '' : '*';
19
20 my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef ) );
21 $ffi->type("string(10)$p" => 'string_10');
22 $ffi->type("string(5)$p" => 'string_5');
23
24 $ffi->attach( 'string_matches_foobarbaz' => ['string'] => 'int');
25 $ffi->attach( 'string_return_foobarbaz' => [] => 'string');
26 $ffi->attach( [pointer_null => 'null'] => [] => 'string');
27 $ffi->attach( [pointer_is_null => 'is_null'] => ['string'] => 'int');
28 $ffi->attach( 'string_write_to_string' => ['string','string'] => 'void');
29
30 ok string_matches_foobarbaz("foobarbaz"), "string_matches_foobarbaz(foobarbaz) = true";
31 ok !string_matches_foobarbaz("x"), "string_matches_foobarbaz(foobarbaz) = false";
32 is string_return_foobarbaz(), "foobarbaz", "string_return_foobarbaz() = foobarbaz";
33
34 is null(), undef, 'null() = undef';
35 is is_null(undef), 1, 'is_null(undef) = 1';
36 is is_null(), 1, 'is_null() = 1';
37 is is_null("foo"), 0, 'is_null("foo") = 0';
38
39 $ffi->attach( [string_set_closure => 'set_closure'] => ['(string)->void'] => 'void');
40 $ffi->attach( [string_call_closure => 'call_closure'] => ['string']=>'void');
41
42 my $save = 1;
43 my $closure = $ffi->closure(sub { $save = $_[0] });
44
45 set_closure($closure);
46 call_closure("hey there");
47 is $save, "hey there", "\$save = hey there";
48
49 call_closure(undef);
50 is $save, undef, "\$save = undef";
51
52
53 $ffi->attach( ['string_matches_foobarbaz' => 'fixed_input_test'] => ['string_10'] => 'int');
54 $ffi->attach( ['pointer_is_null' => 'fixed_input_is_null'] => ['string_10'] => 'int');
55
56 is fixed_input_test("foobarbaz\0"), 1, "fixed_input_test(foobarbaz\\0)";
57 is fixed_input_is_null(undef), 1, "fixed_input_is_null(undef)";
58
59 $ffi->attach( string_fixed_test => ['int'] => 'string_5');
60
61 is string_fixed_test(0), "zero ", "string_fixed_text(0) = zero";
62 is string_fixed_test(1), "one ", "string_fixed_text(1) = one";
63 is string_fixed_test(2), "two ", "string_fixed_text(2) = two";
64 is string_fixed_test(3), "three", "string_fixed_text(3) = three";
65
66 $ffi->attach( [pointer_null => 'fixed_output_null'] => [] => 'string_5');
67
68 is fixed_output_null(), undef, 'fixed_output_null()';
69
70 $ffi->attach( [string_set_closure => 'set_closure_fixed'] => ['(string_5)->void'] => 'void');
71
72 my $closure_fixed = $ffi->closure(sub { $save = $_[0] });
73
74 set_closure_fixed($closure_fixed);
75 call_closure("zero one two three");
76 is $save, "zero ", "save=zero ";
77
78 $ffi->attach( string_test_pointer_arg => [ 'string*' ] => 'string' );
79
80 {
81 my $arg = "foo";
82 is( string_test_pointer_arg(\$arg), "*arg==foo");
83 is( $arg, "out" );
84 }
85
86 {
87 my $arg;
88 is( string_test_pointer_arg(\$arg), "*arg==NULL");
89 is( $arg, "out" );
90 }
91
92 is( string_test_pointer_arg(undef), "arg==NULL");
93
94 $ffi->attach( string_test_pointer_ret => [ 'string' ] => 'string*' );
95 $ffi->attach( [ pointer_null => 'string_test_pointer_ret_null' ] => [] => 'string*' );
96
97 is( string_test_pointer_ret("foo"), \"foo" );
98 is( string_test_pointer_ret(undef), \undef );
99 is( [string_test_pointer_ret_null()], [$api >= 2 ? (undef) : ()] );
100
101 subtest 'fixed length input' => sub {
102
103 $ffi->type('string[5]' => 'string_5_undef');
104
105 my $a2 = $ffi->function(get_string_from_array => ['string_5_undef', 'int'] => 'string');
106
107 my @list = ( 'foo', 'bar', 'baz', undef, 'five', 'six' );
108
109 subtest 'with default' => sub {
110 is $a2->(\@list, 0), 'foo', 'a2(0) = foo';
111 is $a2->(\@list, 1), 'bar', 'a2(0) = bar';
112 is $a2->(\@list, 2), 'baz', 'a2(0) = baz';
113 is $a2->(\@list, 3), undef, 'a2(0) = undef';
114 is $a2->(\@list, 4), 'five', 'a2(0) = five';
115 };
116
117 };
118
119 subtest 'variable length input' => sub {
120
121 $ffi->type('string[]' => 'sa');
122
123 my $get_string_from_array = $ffi->function(get_string_from_array => ['sa','int'] => 'string');
124
125 my @list = ('foo', 'bar', 'baz', undef );
126
127 for(0..2)
128 {
129 is $get_string_from_array->(\@list, $_), $list[$_], "get_string_from_array(\@list, $_) = $list[$_]";
130 }
131
132 is $get_string_from_array->(\@list, 3), undef, "get_string_from_array(\@list, 3) = undef";
133 };
134
135 subtest 'variable length input' => sub {
136
137 skip_all 'test requires api >=2'
138 unless $api >= 2;
139
140 my $get_string_from_array = $ffi->function(get_string_from_array => ['string*','int'] => 'string');
141
142 my @list = ('foo', 'bar', 'baz', undef );
143
144 for(0..2)
145 {
146 is $get_string_from_array->(\@list, $_), $list[$_], "get_string_from_array(\@list, $_) = $list[$_]";
147 }
148
149 is $get_string_from_array->(\@list, 3), undef, "get_string_from_array(\@list, 3) = undef";
150 };
151
152 subtest 'fixed length return' => sub {
153
154 $ffi->type('string[3]' => 'sa3');
155
156 is(
157 $ffi->function(pointer_null => [] => 'sa3')->call,
158 undef,
159 'returns null',
160 );
161
162 is(
163 $ffi->function(onetwothree3 => [] => 'sa3')->call,
164 [ qw( one two three ) ],
165 'returns with just strings',
166 );
167
168 is(
169 $ffi->function(onenullthree3 => [] => 'sa3')->call,
170 [ 'one', undef, 'three' ],
171 'returns with NULL/undef in the middle',
172 );
173
174 };
175
176 subtest 'null terminated return' => sub {
177
178 is(
179 $ffi->function(pointer_null => [] => 'sa')->call,
180 undef,
181 'returns null',
182 );
183
184 is(
185 $ffi->function('onetwothree4', => [] => 'sa')->call,
186 [ qw( one two three ) ],
187 );
188
189 is(
190 $ffi->function('onenullthree3' => [] => 'sa')->call,
191 [ qw( one ) ],
192 );
193
194 is(
195 $ffi->function('ptrnull' => [] => 'sa')->call,
196 [],
197 );
198
199 };
200
201 subtest 'argument update' => sub {
202
203 my @args = ( undef, 'six', 'xx' );
204 $ffi->function( string_array_arg_update => [ 'string[3]' ] => 'void' )->call(\@args);
205 is(
206 \@args,
207 [ "one", "two", "xx" ],
208 );
209
210 };
211
212 subtest 'write to string' => sub {
213
214 my $src = 'hello world';
215 my $dst = ' ' x (length($src)+1);
216 string_write_to_string($dst, $src);
217 is($dst, "hello world\0");
218
219 };
220
221 is(
222 [$ffi->function( pointer_null => [] => 'string' )->call],
223 [$api >= 2 ? (undef) : ()],
224 );
225
226 };
227 }
228
229 done_testing;
230