Skip to content

Commit

Permalink
Add tests for sized ints and nums in arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
arnsholt committed Feb 2, 2013
1 parent ee261c5 commit bf396de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
29 changes: 29 additions & 0 deletions t/05-arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
31 changes: 30 additions & 1 deletion t/05-arrays.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use t::CompileTestLib;
use NativeCall;
use Test;

plan 16;
plan 26;

compile_test_lib('05-arrays');

Expand Down Expand Up @@ -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.