Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test inlined and referenced CUnions in CStructs
  • Loading branch information
FROGGS committed Feb 23, 2015
1 parent 875f50b commit 1d9aee5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 49 deletions.
69 changes: 48 additions & 21 deletions t/04-nativecall/13-union.c
Expand Up @@ -7,18 +7,20 @@
#define DLLEXPORT extern
#endif

typedef union {
unsigned long l;
unsigned int i;
unsigned short s;
unsigned char c;
} onion;

/* Test for inlined union. */
typedef struct {
long intval;
double numval;
char byteval;
union {
unsigned long *l;
unsigned int *i;
unsigned short *s;
unsigned char *c;
} onion;
onion vegval;
float floatval;
long *arr;
} MyStruct;

DLLEXPORT int SizeofMyStruct() {
Expand All @@ -27,21 +29,46 @@ DLLEXPORT int SizeofMyStruct() {

DLLEXPORT MyStruct *ReturnMyStruct() {
MyStruct *obj = (MyStruct *)malloc(sizeof(MyStruct));
obj->intval = 17;
obj->numval = 4.2;
obj->byteval = 13;

obj->onion.l = (unsigned long *)malloc(sizeof(unsigned long));
*obj->onion.l = 0;
*obj->onion.i = 1 << 30;
*obj->onion.s = 1 << 14;
*obj->onion.c = 1 << 6;

obj->intval = 17;
obj->numval = 4.2;
obj->byteval = 13;

obj->vegval.l = 0;
obj->vegval.i = 1 << 30;
obj->vegval.s = 1 << 14;
obj->vegval.c = 1 << 6;

obj->floatval = -6.28;
obj->arr = (long *)malloc(3*sizeof(long));
obj->arr[0] = 2;
obj->arr[1] = 3;
obj->arr[2] = 5;

return obj;
}

/* Test for referenced union. */
typedef struct {
long intval;
double numval;
char byteval;
onion* vegval;
float floatval;
} MyStruct2;

DLLEXPORT int SizeofMyStruct2() {
return sizeof(MyStruct2);
}

DLLEXPORT MyStruct2 *ReturnMyStruct2() {
MyStruct2 *obj = (MyStruct2 *)malloc(sizeof(MyStruct2));
obj->intval = 17;
obj->numval = 4.2;
obj->byteval = 13;

obj->vegval = (onion *)malloc(sizeof(onion));
obj->vegval->l = 0;
obj->vegval->i = 1 << 30;
obj->vegval->s = 1 << 14;
obj->vegval->c = 1 << 6;

obj->floatval = -6.28;

return obj;
}
70 changes: 42 additions & 28 deletions t/04-nativecall/13-union.t
Expand Up @@ -4,7 +4,7 @@ use lib 'lib';
use NativeCall;
use Test;

plan 17;
plan 22;

compile_test_lib('13-union');

Expand All @@ -16,38 +16,22 @@ class Onion is repr('CUnion') {
}

class MyStruct is repr('CStruct') {
has long $.long;
has num64 $.num;
has int8 $.byte;
has Onion $.onion;
has num32 $.float;
has CArray $.arr;
has long $.long;
has num64 $.num;
has int8 $.byte;
has Onion $.onion;
has num32 $.float;

method init() {
$!long = 42;
$!byte = 7;
$!num = -3.7e0;
$!float = 3.14e0;
my $arr := CArray[long].new();
$arr[0] = 1;
$arr[1] = 2;
$!arr := $arr;
}
}

# Workaround a Rakudo-bug where $!arr := CArray[long].new() won't work if $.arr
# is declared as type CArray[long].
class MyStruct2 is repr('CStruct') {
has long $.long;
has num $.num;
has int8 $.byte;
has Onion $.onion;
has num32 $.float;
has CArray[long] $.arr;
}

sub ReturnMyStruct() returns MyStruct2 is native('./13-union') { * }
sub SizeofMyStruct() returns int32 is native('./13-union') { * }
sub ReturnMyStruct() returns MyStruct is native('./13-union') { * }
sub SizeofMyStruct() returns int32 is native('./13-union') { * }

is nativesizeof(MyStruct), SizeofMyStruct(), 'sizeof(MyStruct)';

Expand All @@ -59,7 +43,6 @@ is $obj.long, 42, 'getting long';
is_approx $obj.num, -3.7e0, 'getting num';
is $obj.byte, 7, 'getting int8';
is_approx $obj.float, 3.14e0, 'getting num32';
is $obj.arr[1], 2, 'getting CArray and element';

# C-side tests:
my $cobj = ReturnMyStruct;
Expand All @@ -68,13 +51,44 @@ is $cobj.long, 17, 'getting long from C-created struct';
is_approx $cobj.num, 4.2e0, 'getting num from C-created struct';
is $cobj.byte, 13, 'getting int8 from C-created struct';
is_approx $cobj.float, -6.28e0, 'getting num32 from C-created struct';
is $cobj.arr[0], 2, 'C-created array member, elem 1';
is $cobj.arr[1], 3, 'C-created array member, elem 2';
is $cobj.arr[2], 5, 'C-created array member, elem 3';

is $cobj.onion.l, 1 +< 30 +| 1 +< 14 +| 1 +< 6, 'long in union';
is $cobj.onion.i, 1 +< 30 +| 1 +< 14 +| 1 +< 6, 'int in union';
is $cobj.onion.s, 1 +< 14 +| 1 +< 6, 'short in union';
is $cobj.onion.c, 1 +< 6, 'char in union';

class MyStruct2 is repr('CStruct') {
has long $.long;
has num64 $.num;
has int8 $.byte;
has Pointer[Onion] $.onion;
has num32 $.float;

method init() {
$!long = 42;
$!byte = 7;
$!num = -3.7e0;
$!float = 3.14e0;
}
}

sub ReturnMyStruct2() returns MyStruct2 is native('./13-union') { * }
sub SizeofMyStruct2() returns int32 is native('./13-union') { * }

is nativesizeof(MyStruct2), SizeofMyStruct2(), 'sizeof(MyStruct2)';

# C-side tests:
my $cobj2 = ReturnMyStruct2;

is $cobj2.long, 17, 'getting long from C-created struct';
is_approx $cobj2.num, 4.2e0, 'getting num from C-created struct';
is $cobj2.byte, 13, 'getting int8 from C-created struct';
is_approx $cobj2.float, -6.28e0, 'getting num32 from C-created struct';

my $onion = $cobj2.onion.deref;
is $onion.l, 1 +< 30 +| 1 +< 14 +| 1 +< 6, 'long in union*';
is $onion.i, 1 +< 30 +| 1 +< 14 +| 1 +< 6, 'int in union*';
is $onion.s, 1 +< 14 +| 1 +< 6, 'short in union*';
is $onion.c, 1 +< 6, 'char in union*';

# vim:ft=perl6

0 comments on commit 1d9aee5

Please sign in to comment.