Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test, and Cookbook example, of array-ref arg
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use strict; use warnings; | ||
use lib -e 't' ? 't' : 'test'; | ||
use diagnostics; | ||
use TestInlineSetup; | ||
use Inline Config => DIRECTORY => $TestInlineSetup::DIR; | ||
use Test::More; | ||
|
||
my $c_text = <<'EOC'; | ||
SV *sum(SV *array) { | ||
int total = 0; | ||
int numelts, i; | ||
SvGETMAGIC(array); | ||
if ((!SvROK(array)) | ||
|| (SvTYPE(SvRV(array)) != SVt_PVAV) | ||
|| ((numelts = av_top_index((AV *)SvRV(array))) < 0) | ||
) { | ||
return &PL_sv_undef; | ||
} | ||
for (i = 0; i <= numelts; i++) { | ||
total += SvIV(*av_fetch((AV *)SvRV(array), i, 0)); | ||
} | ||
return newSViv(total); | ||
} | ||
EOC | ||
Inline->bind(C => $c_text); | ||
|
||
is sum([(1..4)]), 10, 'correct sum'; | ||
is sum(undef), undef, 'return undef when given undef'; | ||
is sum('hello'), undef, 'return undef when given non-ref'; | ||
is sum([]), undef, 'return undef when given empty list'; | ||
|
||
done_testing; |