Skip to content
This repository
Browse code

Tests for objects in CArrays.

  • Loading branch information...
commit c9d7e10b49cd71048b5bae2e8761ccf9aaa30ebd 1 parent 87c4027
Arne Skjærholt authored April 22, 2012
26  t/05-arrays.c
@@ -34,3 +34,29 @@ DLLEXPORT int TakeAStringArrayAndReturnTotalLength(char **arr)
34 34
 {
35 35
     return (int)(strlen(arr[0]) + strlen(arr[1]));
36 36
 }
  37
+
  38
+typedef struct {
  39
+    long value;
  40
+} Struct;
  41
+
  42
+DLLEXPORT Struct **ReturnAStructArray() {
  43
+    Struct **arr = (Struct **) malloc(3*sizeof(Struct *));
  44
+    arr[0] = malloc(sizeof(Struct));
  45
+    arr[1] = malloc(sizeof(Struct));
  46
+    arr[2] = malloc(sizeof(Struct));
  47
+
  48
+    arr[0]->value = 2;
  49
+    arr[1]->value = 3;
  50
+    arr[2]->value = 5;
  51
+
  52
+    return arr;
  53
+}
  54
+
  55
+DLLEXPORT void TakeAStructArray(Struct **structs) {
  56
+    if(structs[0]->value != 7) printf("not ");
  57
+    printf("ok - struct in position 0, C-side\n");
  58
+    if(structs[1]->value != 11) printf("not ");
  59
+    printf("ok - struct in position 1, C-side\n");
  60
+    if(structs[2]->value != 13) printf("not ");
  61
+    printf("ok - struct in position 2, C-side\n");
  62
+}
30  t/05-arrays.t
@@ -2,7 +2,7 @@ use t::CompileTestLib;
2 2
 use NativeCall;
3 3
 use Test;
4 4
 
5  
-plan 8;
  5
+plan 14;
6 6
 
7 7
 compile_test_lib('05-arrays');
8 8
 
@@ -38,3 +38,31 @@ compile_test_lib('05-arrays');
38 38
     @arr[0] = 1;
39 39
     is @arr[0], 1, 'getting last element of managed array';
40 40
 }
  41
+
  42
+{
  43
+    class Struct is repr('CStruct') {
  44
+        has int $.val;
  45
+
  46
+        method set($x) {
  47
+            $!val = $x;
  48
+        }
  49
+    }
  50
+
  51
+    sub ReturnAStructArray() returns CArray[Struct] is native("05-arrays") { * }
  52
+    my @arr := ReturnAStructArray();
  53
+    is @arr[0].val, 2, 'int in struct in element 0';
  54
+    is @arr[1].val, 3, 'int in struct in element 1';
  55
+    is @arr[2].val, 5, 'int in struct in element 2';
  56
+
  57
+    sub TakeAStructArray(CArray[Struct] $obj) is native("05-arrays") { * }
  58
+    @arr := CArray[Struct].new;
  59
+    @arr[0] = Struct.new();
  60
+    @arr[1] = Struct.new();
  61
+    @arr[2] = Struct.new();
  62
+    @arr[0].set(7);
  63
+    @arr[1].set(11);
  64
+    @arr[2].set(13);
  65
+    TakeAStructArray(@arr);
  66
+}
  67
+
  68
+# vim:ft=perl6

0 notes on commit c9d7e10

Please sign in to comment.
Something went wrong with that request. Please try again.