Debian Codegraph @

t/gh260.t

raw
1 use Test2::V0 -no_srand => 1;
2 use FFI::Platypus;
3 use FFI::CheckLib qw( find_lib );
4 use Config qw( %Config );
5
6 # https://github.com/PerlFFI/FFI-Platypus/issues/260
7 #
8 # Confirm that the minimum and maximum value for each of the standard
9 # integer types survives a round trip unmolested as a scalar argument
10 # and return value, as a pointer, as a fixed size array, and as a
11 # variable size (pointer based) array. See also gh#258, where a bug
12 # in a related project was masked for a long time because existing
13 # tests only ever used small values that happen to fit into any
14 # integer representation.
15
16 my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';
17
18 my %minmax = (
19 sint8 => [ -128, 127 ],
20 uint8 => [ 0, 255 ],
21 sint16 => [ -32768, 32767 ],
22 uint16 => [ 0, 65535 ],
23 sint32 => [ -2147483648, 2147483647 ],
24 uint32 => [ 0, 4294967295 ],
25 );
26
27 if($Config{uvsize} >= 8)
28 {
29 $minmax{sint64} = [ -9223372036854775808, 9223372036854775807 ];
30 $minmax{uint64} = [ 0, 18446744073709551615 ];
31 }
32 else
33 {
34 note 'skipping sint64/uint64 since this Perl does not have 64 bit integer support';
35 }
36
37 foreach my $type (sort keys %minmax)
38 {
39 subtest $type => sub {
40
41 local $SIG{__WARN__} = sub {
42 my $message = shift;
43 return if $message =~ /^Subroutine main::.* redefined/;
44 warn $message;
45 };
46
47 my $ffi = FFI::Platypus->new( api => 2, lib => [@lib] );
48
49 $ffi->attach( [ "${type}_add" => 'add' ] => [ "$type", "$type" ] => "$type" );
50 $ffi->attach( [ "${type}_inc" => 'inc' ] => [ "${type}*", "$type" ] => "${type}*" );
51 $ffi->attach( [ "${type}_sum" => 'sum' ] => [ "${type}[10]" ] => "$type" );
52 $ffi->attach( [ "${type}_sum2" => 'sum2' ] => [ "${type}[]", 'size_t' ] => "$type" );
53
54 foreach my $value (@{ $minmax{$type} })
55 {
56 subtest "value = $value" => sub {
57
58 is add($value, 0), $value, 'scalar argument and return value';
59
60 my $copy = $value;
61 is ${ inc(\$copy, 0) }, $value, 'pointer argument and return value';
62 is $copy, $value, 'value pointed to is unmodified by increment of 0';
63
64 my @array10 = ($value, (0) x 9);
65 is sum(\@array10), $value, 'fixed size array argument';
66
67 my @array_n = ($value);
68 is sum2(\@array_n, scalar @array_n), $value, 'variable size (pointer) array argument';
69
70 };
71 }
72
73 };
74 }
75
76 done_testing;