Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tests from CStruct in CStruct.

  • Loading branch information...
commit 5560e305ca28d2c5859ee002ec7653e3f0c4aead 1 parent 3c2dbdf
@arnsholt arnsholt authored
Showing with 80 additions and 1 deletion.
  1. +30 −0 t/06-struct.c
  2. +50 −1 t/06-struct.t
View
30 t/06-struct.c
@@ -14,6 +14,13 @@ typedef struct {
long *arr;
} MyStruct;
+typedef struct { long first, second; } IntStruct;
+typedef struct { double first, second; } NumStruct;
+typedef struct {
+ IntStruct *a;
+ NumStruct *b;
+} StructStruct;
+
DLLEXPORT MyStruct *ReturnAStruct()
{
MyStruct *obj = (MyStruct *) malloc(sizeof(MyStruct));
@@ -38,3 +45,26 @@ DLLEXPORT void TakeAStruct(MyStruct *obj)
if(!obj->arr || obj->arr[1] != 2) printf("not ");
printf("ok - C-side array value, element 2\n");
}
+
+DLLEXPORT StructStruct *ReturnAStructStruct() {
+ StructStruct *ss = (StructStruct *) malloc(sizeof(StructStruct));
+ ss->a = (IntStruct *) malloc(sizeof(IntStruct));
+ ss->b = (NumStruct *) malloc(sizeof(NumStruct));
+ ss->a->first = 7;
+ ss->a->second = 11;
+ ss->b->first = 3.7;
+ ss->b->second = 0.1;
+
+ return ss;
+}
+
+DLLEXPORT void TakeAStructStruct(StructStruct *obj) {
+ if(!obj->a || obj->a->first != 13) printf("not ");
+ printf("ok - int 1 in struct 1 in struct\n");
+ if(!obj->a || obj->a->second != 17) printf("not ");
+ printf("ok - int 2 in struct 1 in struct\n");
+ if(!obj->b || obj->b->first != 0.9) printf("not ");
+ printf("ok - int 1 in struct 2 in struct\n");
+ if(!obj->b || obj->b->second != 3.14) printf("not ");
+ printf("ok - int 2 in struct 2 in struct\n");
+}
View
51 t/06-struct.t
@@ -29,9 +29,47 @@ class MyStruct2 is repr('CStruct') {
has CArray[int] $.arr;
}
+class IntStruct is repr('CStruct') {
+ has int $.first;
+ has int $.second;
+
+ # Work around struct members not being containerized yet.
+ method init {
+ $!first = 13;
+ $!second = 17;
+ }
+}
+
+class NumStruct is repr('CStruct') {
+ has num $.first;
+ has num $.second;
+
+ # Work around struct members not being containerized yet.
+ method init {
+ $!first = 0.9e0;
+ $!second = 3.14e0;
+ }
+}
+
+class StructStruct is repr('CStruct') {
+ has IntStruct $.a;
+ has NumStruct $.b;
+
+ # Work around struct members not being containerized yet.
+ method init {
+ $!a := IntStruct.new;
+ $!b := NumStruct.new;
+ $!a.init;
+ $!b.init;
+ }
+}
+
sub ReturnAStruct() returns MyStruct2 is native('06-struct') { * }
sub TakeAStruct(MyStruct $arg) is native('06-struct') { * }
+sub ReturnAStructStruct() returns StructStruct is native('06-struct') { * }
+sub TakeAStructStruct(StructStruct $arg) is native('06-struct') { * }
+
# Perl-side tests:
my MyStruct $obj .= new;
$obj.init;
@@ -41,7 +79,6 @@ is $obj.num, -3.7e0, 'getting num';
is $obj.arr[1], 2, 'getting CArray and element';
# C-side tests:
-
my $cobj = ReturnAStruct;
is $cobj.int, 17, 'getting int from C-created struct';
@@ -50,6 +87,18 @@ 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';
+my StructStruct $ss = ReturnAStructStruct();
+is $ss.a.first, 7, 'field 1 from struct 1 in struct';
+is $ss.a.second, 11, 'field 2 from struct 1 in struct';
+
+is $ss.b.first, 3.7e0, 'field 1 from struct 1 in struct';
+is $ss.b.second, 0.1e0, 'field 2 from struct 1 in struct';
+
TakeAStruct($obj);
+my StructStruct $ss2 .= new();
+$ss2.init;
+
+TakeAStructStruct($ss2);
+
# vim:ft=perl6
Please sign in to comment.
Something went wrong with that request. Please try again.