| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::Platypus::Internal; |
| 4 |
use FFI::Platypus::TypeParser::Version2; |
| 5 |
use Data::Dumper qw( Dumper ); |
| 6 |
|
| 7 |
my $tp = FFI::Platypus::TypeParser::Version2->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::Version2->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 |
' string(10)', |
| 297 |
'string(10) ', |
| 298 |
'string (10)', |
| 299 |
' string (10) ', |
| 300 |
); |
| 301 |
|
| 302 |
foreach my $good (@good) |
| 303 |
{ |
| 304 |
my $type = $tp->parse($good); |
| 305 |
isa_ok $type, 'FFI::Platypus::Type'; |
| 306 |
is $type->type_code, FFI_PL_TYPE_RECORD; |
| 307 |
is $type->meta->{size}, 10; |
| 308 |
is $type->meta->{ref}, 0; |
| 309 |
} |
| 310 |
|
| 311 |
}; |
| 312 |
|
| 313 |
subtest 'ten record pass-by-value' => sub { |
| 314 |
|
| 315 |
local $@; |
| 316 |
eval { $tp->parse("record(10)") }; |
| 317 |
like "$@", qr/^classless record not allowed as value type/; |
| 318 |
|
| 319 |
}; |
| 320 |
|
| 321 |
}; |
| 322 |
|
| 323 |
subtest 'record class' => sub { |
| 324 |
|
| 325 |
{ |
| 326 |
package Foo::Bar1; |
| 327 |
sub ffi_record_size { 220 }; |
| 328 |
} |
| 329 |
|
| 330 |
{ |
| 331 |
package Foo::Bar2; |
| 332 |
sub _ffi_record_size { 220 }; |
| 333 |
} |
| 334 |
|
| 335 |
{ |
| 336 |
package Foo::Bar3; |
| 337 |
} |
| 338 |
|
| 339 |
subtest 'good with size' => sub { |
| 340 |
|
| 341 |
my @good = qw( |
| 342 |
record(Foo::Bar1)* |
| 343 |
record(Foo::Bar2)* |
| 344 |
); |
| 345 |
|
| 346 |
foreach my $good (@good) |
| 347 |
{ |
| 348 |
my $type = $tp->parse($good); |
| 349 |
isa_ok $type, 'FFI::Platypus::Type'; |
| 350 |
} |
| 351 |
|
| 352 |
}; |
| 353 |
|
| 354 |
subtest 'bad without size' => sub { |
| 355 |
|
| 356 |
my @bad = qw( |
| 357 |
record(Foo::Bar3)* |
| 358 |
record(Foo::Bar4)* |
| 359 |
); |
| 360 |
|
| 361 |
foreach my $bad (@bad) |
| 362 |
{ |
| 363 |
eval { $tp->parse($bad) }; |
| 364 |
like "$@", qr/^Foo::Bar[34] has no ffi_record_size or _ffi_record_size method/; |
| 365 |
} |
| 366 |
|
| 367 |
}; |
| 368 |
|
| 369 |
{ |
| 370 |
package Foo::Bar5; |
| 371 |
use FFI::Platypus::Record; |
| 372 |
record_layout(qw( |
| 373 |
string(67) foo |
| 374 |
)); |
| 375 |
} |
| 376 |
|
| 377 |
subtest 'pass-by-value' => sub { |
| 378 |
|
| 379 |
my @bad = qw( |
| 380 |
record(Foo::Bar5) |
| 381 |
); |
| 382 |
|
| 383 |
foreach my $bad (@bad) |
| 384 |
{ |
| 385 |
my $type = $tp->parse($bad); |
| 386 |
isa_ok $type, 'FFI::Platypus::Type'; |
| 387 |
} |
| 388 |
|
| 389 |
}; |
| 390 |
|
| 391 |
subtest 'alias' => sub { |
| 392 |
|
| 393 |
local $@ = ''; |
| 394 |
my $check = eval { $tp->check_alias('foo_bar5_t') }; |
| 395 |
is "$@", ""; |
| 396 |
is $check, 1; |
| 397 |
|
| 398 |
eval { $tp->set_alias('foo_bar5_t', $tp->parse('record(Foo::Bar5)') ) }; |
| 399 |
is "$@", ""; |
| 400 |
|
| 401 |
is $tp->parse('foo_bar5_t')->type_code, FFI_PL_TYPE_RECORD_VALUE; |
| 402 |
|
| 403 |
is $tp->parse('foo_bar5_t*')->type_code, FFI_PL_TYPE_RECORD; |
| 404 |
is $tp->parse('foo_bar5_t*')->sizeof, 67; |
| 405 |
|
| 406 |
}; |
| 407 |
|
| 408 |
|
| 409 |
}; |
| 410 |
|
| 411 |
subtest 'check alias' => sub { |
| 412 |
|
| 413 |
is( |
| 414 |
$tp->check_alias('foo_bar_baz_1239_XOR'), |
| 415 |
1, |
| 416 |
); |
| 417 |
|
| 418 |
eval { $tp->check_alias('foo bar') }; |
| 419 |
like "$@", qr/^spaces not allowed in alias/; |
| 420 |
|
| 421 |
eval { $tp->check_alias('!$#!$#') }; |
| 422 |
like "$@", qr/^allowed characters for alias: \[A-Za-z0-9_\]/; |
| 423 |
|
| 424 |
eval { $tp->check_alias('void') }; |
| 425 |
like "$@", qr/^alias "void" conflicts with existing type/; |
| 426 |
|
| 427 |
eval { $tp->check_alias('struct') }; |
| 428 |
like "$@", qr/^reserved world "struct" cannot be used as an alias/; |
| 429 |
|
| 430 |
eval { $tp->check_alias('enum') }; |
| 431 |
like "$@", qr/^reserved world "enum" cannot be used as an alias/; |
| 432 |
|
| 433 |
my $tp = FFI::Platypus::TypeParser::Version2->new; |
| 434 |
|
| 435 |
$tp->type_map({ |
| 436 |
'foo_t' => 'sint16', |
| 437 |
}); |
| 438 |
|
| 439 |
eval { $tp->check_alias('foo_t') }; |
| 440 |
like "$@", qr/^alias "foo_t" conflicts with existing type/; |
| 441 |
|
| 442 |
$tp->set_alias('bar_t' => $tp->parse('sint32')); |
| 443 |
eval { $tp->check_alias('bar_t') }; |
| 444 |
like "$@", qr/^alias "bar_t" conflicts with existing type/; |
| 445 |
|
| 446 |
}; |
| 447 |
|
| 448 |
subtest 'use alias' => sub { |
| 449 |
|
| 450 |
my $tp = FFI::Platypus::TypeParser::Version2->new; |
| 451 |
|
| 452 |
$tp->set_alias('foo_t' => $tp->parse('sint8')); |
| 453 |
$tp->set_alias('bar_t' => $tp->parse('sint8*')); |
| 454 |
|
| 455 |
is( |
| 456 |
$tp->parse('foo_t')->type_code, |
| 457 |
FFI_PL_TYPE_SINT8, |
| 458 |
); |
| 459 |
|
| 460 |
is( |
| 461 |
$tp->parse('foo_t*')->type_code, |
| 462 |
FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER, |
| 463 |
); |
| 464 |
|
| 465 |
is( |
| 466 |
$tp->parse('foo_t[]')->type_code, |
| 467 |
FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY, |
| 468 |
); |
| 469 |
|
| 470 |
is( |
| 471 |
$tp->parse('foo_t[]')->meta->{size}, |
| 472 |
0, |
| 473 |
); |
| 474 |
|
| 475 |
is( |
| 476 |
$tp->parse('foo_t[99]')->type_code, |
| 477 |
FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_ARRAY, |
| 478 |
); |
| 479 |
|
| 480 |
is( |
| 481 |
$tp->parse('foo_t[99]')->meta->{size}, |
| 482 |
99, |
| 483 |
); |
| 484 |
|
| 485 |
is( |
| 486 |
$tp->parse('bar_t')->type_code, |
| 487 |
FFI_PL_TYPE_SINT8 | FFI_PL_SHAPE_POINTER, |
| 488 |
); |
| 489 |
|
| 490 |
eval { $tp->parse('bar_t*') }; |
| 491 |
like "$@", qr/^cannot make a pointer to bar_t/; |
| 492 |
|
| 493 |
eval { $tp->parse('bar_t[]') }; |
| 494 |
like "$@", qr/^cannot make an array of bar_t/; |
| 495 |
|
| 496 |
eval { $tp->parse('bar_t[200]') }; |
| 497 |
like "$@", qr/^cannot make an array of bar_t/; |
| 498 |
|
| 499 |
}; |
| 500 |
|
| 501 |
subtest 'object' => sub { |
| 502 |
|
| 503 |
{ package Roger; } |
| 504 |
|
| 505 |
is( |
| 506 |
$tp->parse('object(Roger)')->type_code, |
| 507 |
FFI_PL_SHAPE_OBJECT | FFI_PL_TYPE_OPAQUE, |
| 508 |
); |
| 509 |
|
| 510 |
is( |
| 511 |
$tp->parse('object(Roger,sint32)')->type_code, |
| 512 |
FFI_PL_SHAPE_OBJECT | FFI_PL_TYPE_SINT32, |
| 513 |
); |
| 514 |
|
| 515 |
local $@ = ''; |
| 516 |
eval { $tp->parse('object(Roger,float)') }; |
| 517 |
like "$@", qr/^cannot make an object of float/; |
| 518 |
|
| 519 |
}; |
| 520 |
|
| 521 |
done_testing; |