| 1 |
use Test2::V0 -no_srand => 1; |
| 2 |
use FFI::Platypus; |
| 3 |
use FFI::Platypus::Memory; |
| 4 |
|
| 5 |
note "lib=@{[ $_ || 'undef' ]}" for FFI::Platypus::Memory->_ffi->lib; |
| 6 |
|
| 7 |
my $ffi = FFI::Platypus->new; |
| 8 |
$ffi->lib(undef); |
| 9 |
|
| 10 |
# TODO: break this subtest up into one for |
| 11 |
# malloc, calloc, memset and free |
| 12 |
subtest 'malloc calloc memset free' => sub { |
| 13 |
my $ptr1 = malloc 22; |
| 14 |
ok $ptr1, "malloc returns $ptr1"; |
| 15 |
memset $ptr1, 0, 22; |
| 16 |
memset $ptr1, ord 'x', 8; |
| 17 |
memset $ptr1, ord 'y', 4; |
| 18 |
my $ptr2 = calloc 9, $ffi->sizeof('char'); |
| 19 |
ok $ptr2, "calloc returns $ptr2"; |
| 20 |
my $string = $ffi->function(strcpy => ['opaque', 'opaque'] => 'string')->call($ptr2, $ptr1); |
| 21 |
is $string, 'yyyyxxxx', 'string = yyyyxxxx'; |
| 22 |
free $ptr1; |
| 23 |
ok 1, 'free $ptr1'; |
| 24 |
free $ptr2; |
| 25 |
ok 1, 'free $ptr2'; |
| 26 |
}; |
| 27 |
|
| 28 |
subtest 'memcpy' => sub { |
| 29 |
my $ptr1 = malloc 64; |
| 30 |
my $ptr2 = malloc 64; |
| 31 |
$ffi->function(strcpy => ['opaque', 'string'] => 'opaque')->call($ptr1, "starscream"); |
| 32 |
is( $ffi->cast('opaque','string', $ptr1), "starscream", "initial data copied" ); |
| 33 |
my $ret = memcpy $ptr2, $ptr1, 64; |
| 34 |
is( $ffi->cast('opaque','string', $ptr2), "starscream", "copy of copy" ); |
| 35 |
is $ret, $ptr2, "memcpy returns a pointer"; |
| 36 |
free $ptr1; |
| 37 |
ok 1, 'free $ptr1'; |
| 38 |
free $ptr2; |
| 39 |
ok 1, 'free $ptr2'; |
| 40 |
}; |
| 41 |
|
| 42 |
subtest 'realloc' => sub { |
| 43 |
my $ptr = realloc undef, 32; |
| 44 |
ok $ptr, "realloc call ptr = @{[ $ptr ]}"; |
| 45 |
$ffi->function(strcpy => ['opaque', 'string'] => 'opaque')->call($ptr, "hello"); |
| 46 |
is( $ffi->cast('opaque','string', $ptr), "hello", "initial data copied" ); |
| 47 |
$ptr = realloc $ptr, 1024*5; |
| 48 |
ok $ptr, "realloc call ptr = @{[ $ptr ]} (2)"; |
| 49 |
is( $ffi->cast('opaque','string', $ptr), "hello", "after realloc data there" ); |
| 50 |
free $ptr; |
| 51 |
ok 1, 'final free'; |
| 52 |
}; |
| 53 |
|
| 54 |
subtest 'strdup' => sub { |
| 55 |
note "strdup implementation = @{[ FFI::Platypus::Memory->_strdup_impl ]}"; |
| 56 |
my $ptr1 = malloc 32; |
| 57 |
my $tmp = strdup "this and\0"; |
| 58 |
memcpy $ptr1, $tmp, 9; |
| 59 |
free $tmp; |
| 60 |
my $string = $ffi->cast('opaque' => 'string', $ptr1); |
| 61 |
is $string, 'this and', 'string = this and'; |
| 62 |
free $ptr1; |
| 63 |
ok 1, 'free $ptr1'; |
| 64 |
}; |
| 65 |
|
| 66 |
subtest 'strndup' => sub { |
| 67 |
note "strndup implementation = @{[ FFI::Platypus::Memory->_strndup_impl ]}"; |
| 68 |
|
| 69 |
subtest 'full string' => sub { |
| 70 |
my $ptr = strndup "this and\0", 512; |
| 71 |
is($ffi->cast('opaque' => 'string', $ptr), 'this and'); |
| 72 |
free $ptr; |
| 73 |
}; |
| 74 |
|
| 75 |
subtest 'partial string' => sub { |
| 76 |
my $ptr = strndup "1234567890", 5; |
| 77 |
is($ffi->cast('opaque' => 'string', $ptr), '12345'); |
| 78 |
free $ptr; |
| 79 |
}; |
| 80 |
}; |
| 81 |
|
| 82 |
subtest 'strcpy' => sub { |
| 83 |
|
| 84 |
my $ptr = malloc(5); |
| 85 |
strcpy $ptr, "perl"; |
| 86 |
is($ffi->cast('opaque', 'string', $ptr), "perl", "pointer cast back to string matches"); |
| 87 |
free $ptr; |
| 88 |
|
| 89 |
}; |
| 90 |
|
| 91 |
done_testing; |