blib/lib/Array/Sample/SysRand.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 6 / 6 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 100% 48 / 48 CC 7 SCAR 15.7
CC
7
Cov
100%
sample_sysrand
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package Array::Sample::SysRand;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2023-06-20'; # DATE
11 2 our $DIST = 'Array-Sample-SysRand'; # DIST
12 2 our $VERSION = '0.004'; # VERSION
13  
14 2 our @EXPORT_OK = qw(sample_sysrand);
15  
16 44 ▶sub sample_sysrand {
Subroutine sample_sysrand: called CC=7 SCAR=19.5
Pod documented
17 44 my ($ary, $n, $opts) = @_;
18 44 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
2024
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
19  
20 44 ▶ return () if $n < 1 || @$ary < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 or @$ary < 1 3 41 44
Condition100%3/3
$n < 1 or @$ary < 1
l!l&&r!l&&!r
2141
MC/DC100%2/2
$n < 1 or @$ary < 1
$n < 1 @$ary < 1
Truth table100%3/3
A or B
A= $n < 1B= @$ary < 1
ABresult
1X1
011
000
21  
22 41 my $k = @$ary / $n;
23 41 my $idx = rand() * @$ary;
24  
25 41 my @res;
26 41 for my $i (1..$n) {
27 61 ▶ push @res, $opts->{pos} ? int($idx) : $ary->[int($idx)];
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 30 31 61
28 61 $idx += $k;
29 61 ▶ $idx -= @$ary if $idx >= @$ary;
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= @$ary 41 20 61
30 }
31  
32 41 @res;
33 }
34  
35 2 1;
36 # ABSTRACT: Systematic random sampling from an array
37  
38 __END__
↑ Summary