Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test inlined CStructs in CUnions
  • Loading branch information
FROGGS committed May 19, 2015
1 parent bf3a3be commit 9186fdf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
25 changes: 24 additions & 1 deletion t/04-nativecall/13-union.c
Expand Up @@ -18,15 +18,31 @@ typedef union {
typedef struct {
long intval;
double numval;
char byteval;
signed char byteval;
onion vegval;
float floatval;
} MyStruct;

typedef struct {
long intval;
float numval1;
float numval2;
signed char byteval;
} YourStruct;

typedef union {
MyStruct a;
YourStruct b;
} UnionOfStructs;

DLLEXPORT int SizeofMyStruct() {
return sizeof(MyStruct);
}

DLLEXPORT int SizeofUnionOfStructs() {
return sizeof(UnionOfStructs);
}

DLLEXPORT void SetLongMyStruct(MyStruct *obj) {
obj->vegval.l = 1 << 30;
}
Expand Down Expand Up @@ -99,3 +115,10 @@ DLLEXPORT MyStruct2 *ReturnMyStruct2() {

return obj;
}

DLLEXPORT UnionOfStructs *ReturnUnionOfStructs() {
UnionOfStructs *obj = (UnionOfStructs *)malloc(sizeof(UnionOfStructs));
obj->b.byteval = 42;

return obj;
}
24 changes: 23 additions & 1 deletion t/04-nativecall/13-union.t
Expand Up @@ -4,7 +4,7 @@ use lib 'lib';
use NativeCall;
use Test;

plan 23;
plan 28;

compile_test_lib('13-union');

Expand Down Expand Up @@ -116,4 +116,26 @@ is $onion.s, 1 +< 13, 'short in union*';
SetCharMyUnion($onion);
is $onion.c, 1 +< 6, 'char in union*';

class YourStruct is repr('CStruct') {
has long $.long;
has num32 $.num1;
has num32 $.num2;
has int8 $.byte;
}

class UnionOfStructs is repr('CUnion') {
has MyStruct $.a is inlined;
has YourStruct $.b is inlined;
}

sub ReturnUnionOfStructs() returns UnionOfStructs is native('./13-union') { * }
sub SizeofUnionOfStructs() returns int32 is native('./13-union') { * }

is nativesizeof(UnionOfStructs), SizeofUnionOfStructs(), 'sizeof(UnionOfStructs)';
my $uos = ReturnUnionOfStructs;
isa-ok $uos.a, MyStruct, 'member a of union is-a MyStruct';
isa-ok $uos.b, YourStruct, 'member b of union is-a YourStruct';
is $uos.a.byte, 42, 'a.byte was set to 42 by C';
is $uos.b.byte, 42, 'b.byte must be the same';

# vim:ft=perl6

0 comments on commit 9186fdf

Please sign in to comment.