Debian Codegraph @

t/ffi_platypus_typeparser_version1.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::Platypus::Internal;
4 use FFI::Platypus::TypeParser::Version1;
5 use Data::Dumper qw( Dumper );
6
7 my $tp = FFI::Platypus::TypeParser::Version1->new;
8 my $pointer_size = FFI::Platypus->new->sizeof('opaque');
9
10 subtest 'bad types' => sub {
11
12 eval { $tp->parse("bogus") };
13 like "$@", qr/^unknown type: bogus/;
14
15 eval { $tp->parse("*(^^%*%I(*&&^") };
16 like "$@", qr/^bad type name:/;
17
18 };
19
20 subtest 'basic types' => sub {
21
22 subtest 'void' => sub {
23
24 my $void = $tp->parse('void');
25 isa_ok $void, 'FFI::Platypus::Type';
26 is $void->type_code, FFI_PL_TYPE_VOID;
27
28 eval { $tp->parse('void*') };
29 like "$@", qr/^void pointer not allowed/;
30
31 eval { $tp->parse('void[]') };
32 like "$@", qr/^void array not allowed/;
33
34 };
35
36 subtest 'non-void' => sub {
37
38 is(
39 $tp->parse('sint8')->type_code,
40 FFI_PL_TYPE_SINT8,
41 );
42
43 is(
44 $tp->parse('sint8*')->type_code,
45 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER,
46 );
47
48 is(
49 $tp->parse('sint8[]')->type_code,
50 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
51 );
52
53 is(
54 $tp->parse('sint8[]')->meta->{size},
55 0,
56 );
57
58 is(
59 $tp->parse('sint8[10]')->type_code,
60 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
61 );
62
63 is(
64 $tp->parse('sint8[10]')->meta->{size},
65 10,
66 );
67
68 };
69
70 subtest 'string' => sub {
71
72 is(
73 $tp->parse('string')->type_code,
74 FFI_PL_TYPE_STRING,
75 );
76
77 is($tp->parse('string')->is_ro, 1);
78
79 is(
80 $tp->parse('string rw')->type_code,
81 FFI_PL_TYPE_STRING,
82 );
83
84 is(
85 $tp->parse('string ro')->type_code,
86 FFI_PL_TYPE_STRING,
87 );
88
89 is($tp->parse('string ro')->is_ro, 1);
90 is($tp->parse('string rw')->is_ro, 0);
91
92 is(
93 $tp->parse('string*')->type_code,
94 FFI_PL_TYPE_STRING | FFI_PL_SHAPE_POINTER,
95 );
96
97 is(
98 $tp->parse('string[]')->type_code,
99 FFI_PL_TYPE_STRING | FFI_PL_SHAPE_ARRAY,
100 );
101
102 is(
103 $tp->parse('string[]')->meta->{size},
104 0,
105 );
106
107 is(
108 $tp->parse('string[10]')->type_code,
109 FFI_PL_TYPE_STRING | FFI_PL_SHAPE_ARRAY,
110 );
111
112 is(
113 $tp->parse('string[10]')->meta->{size},
114 10 * $pointer_size,
115 );
116
117 };
118
119 subtest 'bogus' => sub {
120
121 eval { $tp->parse('sint8[0]') };
122 like "$@", qr/^array size must be larger than 0/;
123
124 };
125
126 };
127
128 subtest 'type map' => sub {
129
130 my $tp = FFI::Platypus::TypeParser::Version1->new;
131
132 $tp->type_map({
133 'char' => 'sint8',
134 'int' => 'sint32',
135 'unsigned int' => 'uint32',
136 'intptr' => 'sint32*',
137 });
138
139
140 is(
141 $tp->parse('char')->type_code,
142 FFI_PL_TYPE_SINT8,
143 );
144
145 is(
146 $tp->parse('int')->type_code,
147 FFI_PL_TYPE_SINT32,
148 );
149
150 is(
151 $tp->parse('unsigned int')->type_code,
152 FFI_PL_TYPE_UINT32,
153 );
154
155 is(
156 $tp->parse('char*')->type_code,
157 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER,
158 );
159
160 is(
161 $tp->parse('int*')->type_code,
162 FFI_PL_TYPE_SINT32 | FFI_PL_SHAPE_POINTER,
163 );
164
165 is(
166 $tp->parse('unsigned int *')->type_code,
167 FFI_PL_TYPE_UINT32 | FFI_PL_SHAPE_POINTER,
168 );
169
170 is(
171 $tp->parse('char[]')->type_code,
172 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
173 );
174
175 is(
176 $tp->parse('int[]')->type_code,
177 FFI_PL_TYPE_SINT32 | FFI_PL_SHAPE_ARRAY,
178 );
179
180 is(
181 $tp->parse('unsigned int []')->type_code,
182 FFI_PL_TYPE_UINT32 | FFI_PL_SHAPE_ARRAY,
183 );
184
185 is(
186 $tp->parse('char[]')->meta->{size},
187 0,
188 );
189
190 is(
191 $tp->parse('int[]')->meta->{size},
192 0,
193 );
194
195 is(
196 $tp->parse('unsigned int []')->meta->{size},
197 0,
198 );
199
200 is(
201 $tp->parse('char[22]')->type_code,
202 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
203 );
204
205 is(
206 $tp->parse('int[22]')->type_code,
207 FFI_PL_TYPE_SINT32 | FFI_PL_SHAPE_ARRAY,
208 );
209
210 is(
211 $tp->parse('unsigned int [22]')->type_code,
212 FFI_PL_TYPE_UINT32 | FFI_PL_SHAPE_ARRAY,
213 );
214
215 is(
216 $tp->parse('char[22]')->meta->{size},
217 22,
218 );
219
220 is(
221 $tp->parse('int[22]')->meta->{size},
222 88,
223 );
224
225 is(
226 $tp->parse('unsigned int [22]')->meta->{size},
227 88,
228 );
229
230 eval { $tp->parse('int[0]') };
231 like "$@", qr/^array size must be larger than 0/;
232
233 is(
234 $tp->parse('intptr')->type_code,
235 FFI_PL_TYPE_SINT32 | FFI_PL_SHAPE_POINTER,
236 );
237
238 eval { $tp->parse('intptr*') };
239 like "$@", qr/^bad type name: sint32\* \*/;
240
241 eval { $tp->parse('intptr[]') };
242 like "$@", qr/^bad type name: sint32\* \[\]/;
243
244 eval { $tp->parse('intptr[10]') };
245 like "$@", qr/^bad type name: sint32\* \[10\]/;
246
247 };
248
249 subtest 'fixed record / fixed string' => sub {
250
251 subtest 'zero bad' => sub {
252
253 my @bad = (
254 'string(0)',
255 'record(0)',
256 ' string(0)',
257 'string(0) ',
258 'string (0)',
259 ' string (0) ',
260 );
261
262 foreach my $bad (@bad)
263 {
264 eval { $tp->parse( $bad ) };
265 like "$@", qr{^fixed record / fixed string size must be larger than 0};
266 }
267
268 };
269
270 subtest 'ten good' => sub {
271
272 my @good = (
273 'string(10)*',
274 'record(10)*',
275 ' string(10)*',
276 'string(10) *',
277 'string (10)*',
278 ' string (10)* ',
279 );
280
281 foreach my $good (@good)
282 {
283 my $type = $tp->parse($good);
284 isa_ok $type, 'FFI::Platypus::Type';
285 is $type->type_code, FFI_PL_TYPE_RECORD;
286 is $type->meta->{size}, 10;
287 is $type->meta->{ref}, 0;
288 }
289
290 };
291
292 subtest 'ten pass-by-value' => sub {
293
294 my @good = (
295 'string(10)',
296 'record(10)',
297 ' string(10)',
298 'string(10) ',
299 'string (10)',
300 ' string (10) ',
301 );
302
303 foreach my $good (@good)
304 {
305 eval { $tp->parse($good) };
306 like "$@", qr{^fixed string / classless record not allowed as value type};
307 }
308
309 };
310
311 };
312
313 subtest 'record class' => sub {
314
315 {
316 package Foo::Bar1;
317 sub ffi_record_size { 220 };
318 }
319
320 {
321 package Foo::Bar2;
322 sub _ffi_record_size { 220 };
323 }
324
325 {
326 package Foo::Bar3;
327 }
328
329 subtest 'good with size' => sub {
330
331 my @good = qw(
332 record(Foo::Bar1)*
333 record(Foo::Bar2)*
334 );
335
336 foreach my $good (@good)
337 {
338 my $type = $tp->parse($good);
339 isa_ok $type, 'FFI::Platypus::Type';
340 }
341
342 };
343
344 subtest 'bad without size' => sub {
345
346 my @bad = qw(
347 record(Foo::Bar3)*
348 record(Foo::Bar4)*
349 );
350
351 foreach my $bad (@bad)
352 {
353 eval { $tp->parse($bad) };
354 like "$@", qr/^Foo::Bar[34] has no ffi_record_size or _ffi_record_size method/;
355 }
356
357 };
358
359 {
360 package Foo::Bar5;
361 use FFI::Platypus::Record;
362 record_layout(qw(
363 string(67) foo
364 ));
365 }
366
367 subtest 'pass-by-value' => sub {
368
369 my @bad = qw(
370 record(Foo::Bar5)
371 );
372
373 foreach my $bad (@bad)
374 {
375 my $type = $tp->parse($bad);
376 isa_ok $type, 'FFI::Platypus::Type';
377 }
378
379 };
380
381 subtest 'alias' => sub {
382
383 local $@ = '';
384 my $check = eval { $tp->check_alias('foo_bar5_t') };
385 is "$@", "";
386 is $check, 1;
387
388 eval { $tp->set_alias('foo_bar5_t', $tp->parse('record(Foo::Bar5)') ) };
389 is "$@", "";
390
391 is $tp->parse('foo_bar5_t')->type_code, FFI_PL_TYPE_RECORD_VALUE;
392
393 is $tp->parse('foo_bar5_t*')->type_code, FFI_PL_TYPE_RECORD;
394 is $tp->parse('foo_bar5_t*')->sizeof, 67;
395
396 };
397
398
399 };
400
401 subtest 'check alias' => sub {
402
403 is(
404 $tp->check_alias('foo_bar_baz_1239_XOR'),
405 1,
406 );
407
408 eval { $tp->check_alias('foo bar') };
409 like "$@", qr/^spaces not allowed in alias/;
410
411 eval { $tp->check_alias('!$#!$#') };
412 like "$@", qr/^allowed characters for alias: \[A-Za-z0-9_\]/;
413
414 eval { $tp->check_alias('void') };
415 like "$@", qr/^alias "void" conflicts with existing type/;
416
417 eval { $tp->check_alias('struct') };
418 like "$@", qr/^reserved world "struct" cannot be used as an alias/;
419
420 eval { $tp->check_alias('enum') };
421 like "$@", qr/^reserved world "enum" cannot be used as an alias/;
422
423 my $tp = FFI::Platypus::TypeParser::Version1->new;
424
425 $tp->type_map({
426 'foo_t' => 'sint16',
427 });
428
429 eval { $tp->check_alias('foo_t') };
430 like "$@", qr/^alias "foo_t" conflicts with existing type/;
431
432 $tp->set_alias('bar_t' => $tp->parse('sint32'));
433 eval { $tp->check_alias('bar_t') };
434 like "$@", qr/^alias "bar_t" conflicts with existing type/;
435
436 };
437
438 subtest 'use alias' => sub {
439
440 my $tp = FFI::Platypus::TypeParser::Version1->new;
441
442 $tp->set_alias('foo_t' => $tp->parse('sint8'));
443 $tp->set_alias('bar_t' => $tp->parse('sint8*'));
444
445 is(
446 $tp->parse('foo_t')->type_code,
447 FFI_PL_TYPE_SINT8,
448 );
449
450 is(
451 $tp->parse('foo_t*')->type_code,
452 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER,
453 );
454
455 is(
456 $tp->parse('foo_t[]')->type_code,
457 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
458 );
459
460 is(
461 $tp->parse('foo_t[]')->meta->{size},
462 0,
463 );
464
465 is(
466 $tp->parse('foo_t[99]')->type_code,
467 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY,
468 );
469
470 is(
471 $tp->parse('foo_t[99]')->meta->{size},
472 99,
473 );
474
475 is(
476 $tp->parse('bar_t')->type_code,
477 FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER,
478 );
479
480 eval { $tp->parse('bar_t*') };
481 like "$@", qr/^cannot make a pointer to bar_t/;
482
483 eval { $tp->parse('bar_t[]') };
484 like "$@", qr/^cannot make an array of bar_t/;
485
486 eval { $tp->parse('bar_t[200]') };
487 like "$@", qr/^cannot make an array of bar_t/;
488
489 };
490
491 subtest 'object' => sub {
492
493 { package Roger; }
494
495 is(
496 $tp->parse('object(Roger)')->type_code,
497 FFI_PL_SHAPE_OBJECT | FFI_PL_TYPE_OPAQUE,
498 );
499
500 is(
501 $tp->parse('object(Roger,sint32)')->type_code,
502 FFI_PL_SHAPE_OBJECT | FFI_PL_TYPE_SINT32,
503 );
504
505 local $@ = '';
506 eval { $tp->parse('object(Roger,float)') };
507 like "$@", qr/^cannot make an object of float/;
508
509 };
510
511 done_testing;