Affix
view release on metacpan or search on metacpan
t/021_shakedown.t view on Meta::CPAN
int h = r.bottom_right.y - r.top_left.y;
return w * h;
}
DLLEXPORT void move_rect_ptr(Rect* r, int dx, int dy) {
if (r) {
r->top_left.x += dx;
r->top_left.y += dy;
r->bottom_right.x += dx;
r->bottom_right.y += dy;
snprintf(r->label, 16, "Moved");
}
}
DLLEXPORT Point return_struct_val(int x, int y) {
Point p = { x, y };
return p;
}
// Recursive Linked List
typedef struct Node {
int value;
struct Node* next;
} Node;
DLLEXPORT int sum_list(Node* head) {
int sum = 0;
while(head) {
sum += head->value;
head = head->next;
}
return sum;
}
END_C
#
my $lib = compile_ok($c_source);
ok( $lib, "Library compiled at $lib" );
#
typedef Point => Struct [ x => Int, y => Int ];
typedef Rect => Struct [ top_left => Point(), bottom_right => Point(), label => Array [ Char, 16 ] ];
typedef 'Node';
typedef Node => Struct [ value => Int, next => Pointer [ Node() ] ]; # Recursive
#
subtest 'Structs: Value, Pointers, and Write-back' => sub {
isa_ok my $area = wrap( $lib, 'rect_area_val', [ Rect() ] => Int ), ['Affix'];
my $r = { top_left => { x => 0, y => 0 }, bottom_right => { x => 10, y => 5 }, label => "Test" };
is $area->($r), 50, 'Struct passed by value (nested)';
#
isa_ok my $move = wrap( $lib, 'move_rect_ptr', [ Pointer [ Rect() ], Int, Int ] => Void ), ['Affix'];
$move->( $r, 5, 5 );
is $r->{top_left}{x}, 5, 'Nested struct write-back (x)';
is $r->{top_left}{y}, 5, 'Nested struct write-back (y)';
like $r->{label}, qr/Moved/, 'Char array in struct write-back';
#
isa_ok my $mk_pt = wrap( $lib, 'return_struct_val', [ Int, Int ] => Point() ), ['Affix'];
my $p = $mk_pt->( 100, 200 );
is $p, { x => 100, y => 200 }, 'Struct returned by value';
};
subtest 'Recursive Data Structures (Linked List)' => sub {
isa_ok my $sum_nodes = wrap( $lib, 'sum_list', [ Pointer [ Node() ] ] => Int ), ['Affix'];
my $head = { value => 10, next => { value => 20, next => { value => 30, next => undef } } };
is $sum_nodes->($head), 60, 'Recursive linked list marshalled correctly';
};
#
done_testing;
( run in 1.392 second using v1.01-cache-2.11-cpan-d80b1682f3f )