Permalink
Browse files

Add tests for strings in structs.

  • Loading branch information...
1 parent a78f01d commit 484fb91a79f4e86317791e26e32bcc88cce2251f @arnsholt arnsholt committed Jul 6, 2012
Showing with 44 additions and 1 deletion.
  1. +20 −0 t/06-struct.c
  2. +24 −1 t/06-struct.t
View
@@ -25,6 +25,11 @@ typedef struct {
long *p;
} PointerStruct;
+typedef struct {
+ char *first;
+ char *second;
+} StringStruct;
+
DLLEXPORT MyStruct *ReturnAStruct()
{
MyStruct *obj = (MyStruct *) malloc(sizeof(MyStruct));
@@ -81,6 +86,21 @@ DLLEXPORT PointerStruct *ReturnAPointerStruct() {
return obj;
}
+DLLEXPORT StringStruct *ReturnAStringStruct() {
+ StringStruct *obj = (StringStruct *) malloc(sizeof(StringStruct));
+ obj->first = "OMG!";
+ obj->second = "Strings!";
+
+ return obj;
+}
+
+DLLEXPORT void TakeAStringStruct(StringStruct *obj) {
+ if(strcmp(obj->first, "Lorem")) printf("not ");
+ printf("ok - C-value of first string in struct\n");
+ if(strcmp(obj->second, "ipsum")) printf("not ");
+ printf("ok - C-value of second string in struct\n");
+}
+
DLLEXPORT long _deref(long *ptr) {
return *ptr;
}
View
@@ -3,7 +3,7 @@ use t::CompileTestLib;
use NativeCall;
use Test;
-plan 21;
+plan 25;
compile_test_lib('06-struct');
@@ -65,6 +65,16 @@ class StructStruct is repr('CStruct') {
}
}
+class StringStruct is repr('CStruct') {
+ has Str $.first;
+ has Str $.second;
+
+ method init {
+ $!first := 'Lorem';
+ $!second := 'ipsum';
+ }
+}
+
class PointerThing is repr('CPointer') {
sub _deref(PointerThing $x) returns int is native('./06-struct') { * }
method deref() { return _deref(self); }
@@ -82,6 +92,9 @@ sub TakeAStructStruct(StructStruct $arg) is native('./06-struct') { * }
sub ReturnAPointerStruct() returns PointerStruct is native('./06-struct') { * }
+sub ReturnAStringStruct() returns StringStruct is native('./06-struct') { * }
+sub TakeAStringStruct(StringStruct $arg) is native('./06-struct') { * }
+
# Perl-side tests:
my MyStruct $obj .= new;
$obj.init;
@@ -109,11 +122,21 @@ is $ss.b.second, 0.1e0, 'field 2 from struct 1 in struct';
my PointerStruct $x = ReturnAPointerStruct();
is $x.p.deref, 19, 'CPointer object in struct';
+my StringStruct $strstr = ReturnAStringStruct();
+is $strstr.first, 'OMG!', 'first string in struct';
+is $strstr.second, 'Strings!', 'second string in struct';
+
TakeAStruct($obj);
my StructStruct $ss2 .= new();
$ss2.init;
TakeAStructStruct($ss2);
+my StringStruct $strstr2 .= new();
+$strstr2.init;
+#$strstr2.first := "Lorem";
+#$strstr2.second := "ipsum";
+TakeAStringStruct($strstr2);
+
# vim:ft=perl6

0 comments on commit 484fb91

Please sign in to comment.