Permalink
Browse files

Add tests for sized ints and nums in arrays.

  • Loading branch information...
1 parent ee261c5 commit bf396de07b1e8c19e8a5189073123f872c3bea1a @arnsholt arnsholt committed Feb 2, 2013
Showing with 59 additions and 1 deletion.
  1. +29 −0 t/05-arrays.c
  2. +30 −1 t/05-arrays.t
View
29 t/05-arrays.c
@@ -60,3 +60,32 @@ DLLEXPORT void TakeAStructArray(Struct **structs) {
if(structs[2]->value != 13) printf("not ");
printf("ok - struct in position 2, C-side\n");
}
+
+DLLEXPORT int8_t *ReturnsAByteArray() {
+ int8_t *arr = malloc(3*sizeof(int8_t));
+ arr[0] = 100;
+ arr[1] = 90;
+ arr[2] = 80;
+ return arr;
+}
+
+DLLEXPORT void TakeAByteArray(int8_t *bytes) {
+ if(bytes[0] != 31) printf("not ");
+ printf("ok - byte in position 0, C-side\n");
+ if(bytes[1] != 28) printf("not ");
+ printf("ok - byte in position 1, C-side\n");
+ if(bytes[2] != 30) printf("not ");
+ printf("ok - byte in position 2, C-side\n");
+}
+
+DLLEXPORT float *ReturnsAFloatArray() {
+ float *arr = malloc(3*sizeof(float));
+ arr[0] = 1.23;
+ arr[1] = 4.56;
+ arr[2] = 7.89;
+ return arr;
+}
+
+DLLEXPORT float SumAFloatArray(float *floats) {
+ return floats[0] + floats[1];
+}
View
31 t/05-arrays.t
@@ -3,7 +3,7 @@ use t::CompileTestLib;
use NativeCall;
use Test;
-plan 16;
+plan 26;
compile_test_lib('05-arrays');
@@ -77,4 +77,33 @@ compile_test_lib('05-arrays');
TakeAStructArray(@arr);
}
+{
+ sub ReturnsAByteArray() returns CArray[int8] is native("./05-arrays") { * }
+ my @rarr := ReturnsAByteArray();
+ is @rarr[0], 100, 'byte in element 0';
+ is @rarr[1], 90, 'byte in element 1';
+ is @rarr[2], 80, 'byte in element 2';
+
+ sub TakeAByteArray(CArray[int8]) is native("./05-arrays") { * }
+ my @parr := CArray[int8].new;
+ @parr[0] = 31;
+ @parr[1] = 28;
+ @parr[2] = 30;
+ TakeAByteArray(@parr);
+}
+
+{
+ sub ReturnsAFloatArray() returns CArray[num32] is native("./05-arrays") { * }
+ my @rarr := ReturnsAFloatArray();
+ is_approx @rarr[0], 1.23e0, 'float in element 0';
+ is_approx @rarr[1], 4.56e0, 'float in element 1';
+ is_approx @rarr[2], 7.89e0, 'float in element 2';
+
+ sub SumAFloatArray(CArray[num32]) returns num32 is native("./05-arrays") { * }
+ my @parr := CArray[num32].new;
+ @parr[0] = 12.3e0;
+ @parr[1] = 45.6e0;
+ is_approx SumAFloatArray(@parr), 57.9e0, 'sum of float array';
+}
+
# vim:ft=perl6

0 comments on commit bf396de

Please sign in to comment.