File: type_uint64.t

package info (click to toggle)
libffi-platypus-perl 2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,860 kB
  • sloc: perl: 7,388; ansic: 6,862; cpp: 53; sh: 19; makefile: 14
file content (148 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
# DO NOT MODIFY THIS FILE.
# This file generated from similar file t/type_uint8.t
# all instances of "int8" have been changed to "int64"
#
use Test2::V0 -no_srand => 1;
use FFI::Platypus;
use FFI::CheckLib;

my @lib = find_lib lib => 'test', symbol => 'f0', libpath => 't/ffi';

foreach my $api (0, 1, 2)
{

  subtest "api = $api" => sub {

    local $SIG{__WARN__} = sub {
      my $message = shift;
      return if $message =~ /^Subroutine main::.* redefined/;
      warn $message;
    };

    my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef ) );
    $ffi->type('uint64 *' => 'uint64_p');
    $ffi->type('uint64 [10]' => 'uint64_a');
    $ffi->type('uint64 []' => 'uint64_a2');
    $ffi->type('(uint64)->uint64' => 'uint64_c');

    $ffi->attach( [uint64_add => 'add'] => ['uint64', 'uint64'] => 'uint64');
    $ffi->attach( [uint64_inc => 'inc'] => ['uint64_p', 'uint64'] => 'uint64_p');
    $ffi->attach( [uint64_sum => 'sum'] => ['uint64_a'] => 'uint64');
    $ffi->attach( [uint64_sum2 => 'sum2'] => ['uint64_a2','size_t'] => 'uint64');
    $ffi->attach( [uint64_array_inc => 'array_inc'] => ['uint64_a'] => 'void');
    $ffi->attach( [pointer_null => 'null'] => [] => 'uint64_p');
    $ffi->attach( [pointer_is_null => 'is_null'] => ['uint64_p'] => 'int');
    $ffi->attach( [uint64_static_array => 'static_array'] => [] => 'uint64_a');
    $ffi->attach( [pointer_null => 'null2'] => [] => 'uint64_a');

    if($api >= 2)
    {
      $ffi->attach( [uint64_sum => 'sum3'] => ['uint64*'] => 'uint64' );
      $ffi->attach( [uint64_array_inc => 'array_inc2'] => ['uint64*'] => 'void');
    }

    is add(1,2), 3, 'add(1,2) = 3';
    is do { no warnings; add() }, 0, 'add() = 0';

    my $i = 3;
    is ${inc(\$i, 4)}, 7, 'inc(\$i,4) = \7';

    is $i, 3+4, "i=3+4";

    is ${inc(\3,4)}, 7, 'inc(\3,4) = \7';

    my @list = (1,2,3,4,5,6,7,8,9,10);

    is sum(\@list), 55, 'sum([1..10]) = 55';
    is sum2(\@list, scalar @list), 55, 'sum2([1..10],10) = 55';

    if($api >= 2)
    {
      is(sum3(\@list), 55, 'sum([1..10]) = 55 (passed as pointer)');
    }

    array_inc(\@list);
    do { local $SIG{__WARN__} = sub {}; array_inc() };

    is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment';

    if($api >= 2)
    {
      @list = (1,2,3,4,5,6,7,8,9,10);
      array_inc2(\@list);
      is \@list, [2,3,4,5,6,7,8,9,10,11], 'array increment';
   }

    is [null()], [$api >= 2 ? (undef) : ()], 'null() == undef';
    is is_null(undef), 1, 'is_null(undef) == 1';
    is is_null(), 1, 'is_null() == 1';
    is is_null(\22), 0, 'is_null(22) == 0';

    is static_array(), [1,4,6,8,10,12,14,16,18,20], 'static_array = [1,4,6,8,10,12,14,16,18,20]';

    is [null2()], [$api >= 2 ? (undef) : ()], 'null2() == undef';

    my $closure = $ffi->closure(sub { $_[0]+2 });
    $ffi->attach( [uint64_set_closure => 'set_closure'] => ['uint64_c'] => 'void');
    $ffi->attach( [uint64_call_closure => 'call_closure'] => ['uint64'] => 'uint64');

    set_closure($closure);
    is call_closure(2), 4, 'call_closure(2) = 4';

    $closure = $ffi->closure(sub { undef });
    set_closure($closure);
    is do { no warnings; call_closure(2) }, 0, 'call_closure(2) = 0';

    subtest 'custom type input' => sub {
      $ffi->custom_type(type1 => { native_type => 'uint64', perl_to_native => sub { is $_[0], 2; $_[0]*2 } });
      $ffi->attach( [uint64_add => 'custom_add'] => ['type1','uint64'] => 'uint64');
      is custom_add(2,1), 5, 'custom_add(2,1) = 5';
    };

    subtest 'custom type output' => sub {
      $ffi->custom_type(type2 => { native_type => 'uint64', native_to_perl => sub { is $_[0], 2; $_[0]*2 } });
      $ffi->attach( [uint64_add => 'custom_add2'] => ['uint64','uint64'] => 'type2');
      is custom_add2(1,1), 4, 'custom_add2(1,1) = 4';
    };

    subtest 'custom type post' => sub {
      $ffi->custom_type(type3 => { native_type => 'uint64', perl_to_native_post => sub { is $_[0], 1 } });
      $ffi->attach( [uint64_add => 'custom_add3'] => ['type3','uint64'] => 'uint64');
      is custom_add3(1,2), 3, 'custom_add3(1,2) = 3';
    };

    $ffi->attach( [pointer_is_null => 'closure_pointer_is_null'] => ['()->void'] => 'int');
    is closure_pointer_is_null(), 1, 'closure_pointer_is_null() = 1';

  };
}

foreach my $api (1,2)
{
  subtest 'object' => sub {

    { package Roger }

    my $ffi = FFI::Platypus->new( api => $api, lib => [@lib], experimental => ($api > 2 ? $api : undef ) );
    $ffi->type('object(Roger,uint64)', 'roger_t');

    my $int = 211;

    subtest 'argument' => sub {

      is $ffi->cast('roger_t' => 'uint64', bless(\$int, 'Roger')), $int;

    };

    subtest 'return value' => sub {

      my $obj1 = $ffi->cast('uint64' => 'roger_t', $int);
      isa_ok $obj1, 'Roger';
      is $$obj1, $int;

    };
  }
};

done_testing;