blib/lib/Array/Pick/Scan.pm

filter: Statementstmt 81.8% 45 / 55 Branchbran 57.1% 16 / 28 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 75.0% 72 / 96 CC 18 SCAR 32.4
CC
18
Cov
74%
random_item
32.4
BEGIN
1.0
BEGIN
1.0
CRAP 25.5
↑ Summary
1 package Array::Pick::Scan;
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 = '2022-05-21'; # DATE
11 2 our $DIST = 'Array-Pick-Scan'; # DIST
12 2 our $VERSION = '0.005'; # VERSION
13  
14 2 our @EXPORT_OK = qw(random_item pick);
15  
16 203 ▶sub random_item {
Subroutine random_item: called CC=18 SCAR=34.8
Pod documented
17 203 my ($src, $num_items, $opts) = @_;
18 203 my $ref = ref $src;
19  
20 203 ▶ $num_items //= 1;
Condition50%1/2
$num_items //= 1
l!l
2030
MC/DC0%0/1
$num_items //= 1
$num_items
Truth table50%1/2
A
A= $num_items
Aresult
11
00
21 203 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
1202
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
22  
23 203 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 103 100 203
elsif ($ref eq 'CODE') { } 100 0 100
24 103 my $ary = $src;
25 103 my $ary_size = @$ary;
26  
27 103 ▶ if (!$ary_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ary_size) { } 2 101 103
elsif ($num_items == 1) { } 101 0 101
28 2 return ();
29 } elsif ($num_items == 1) {
30 101 my $idx = int(rand() * $ary_size);
31 101 ▶ return $opts->{pos} ? $idx : $ary->[$idx];
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 1 100 101
32 } else {
33 0 my @items;
34 0 for my $i (0..$ary_size-1) {
35 0 ▶ if (@items < $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items < $num_items) { } 0 0 0
36 # we haven't reached $num_items, insert item to array in a
37 # random position
38 0 my $idx = int(rand(@items+1));
39 0 ▶ splice @items, $idx, 0, ($opts->{pos} ? $i : $ary->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 0 0 0
40 } else {
41 # we have reached $num_items, just replace an item randomly,
42 # using algorithm from Learning Perl, slightly modified
43 0 ▶ if (rand($i+1) < @items) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand $i + 1 < @items) 0 0 0
44 0 my $idx = int(rand(@items));
45 0 ▶ splice @items, $idx, 1, ($opts->{pos} ? $i : $ary->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 0 0 0
46 }
47 }
48 }
49 0 return @items;
50 }
51 } elsif ($ref eq 'CODE') {
52 100 my $iter = $src;
53 100 my @items;
54 100 my $i = -1;
55 100 ▶ while (defined(my $item = $iter->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = &$iter())) 1000 100 1100
56 1000 $i++;
57 1000 ▶ if (@items < $num_items) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items < $num_items) { } 100 900 1000
58 # we haven't reached $num_items, insert item to array in a
59 # random position
60 100 my $idx = int(rand(@items+1));
61 100 ▶ splice @items, $idx, 0, ($opts->{pos} ? $i : $item);
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 0 100 100
62 } else {
63 # we have reached $num_items, just replace an item randomly,
64 # using algorithm from Learning Perl, slightly modified
65 900 ▶ if (rand($i+1) < @items) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand $i + 1 < @items) 184 716 900
66 184 my $idx = int(rand(@items));
67 184 ▶ splice @items, $idx, 1, $opts->{pos} ? $i : $item;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pos'} ? : 0 184 184
68 }
69 }
70 }
71 100 return @items;
72 } else {
73 0 die "Please specify arrayref or coderef iterator as source of items";
74 }
75 }
76  
77 {
78 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 *pick = \&random_item;
80 }
81  
82 2 1;
83 # ABSTRACT: Pick random items from an array (or iterator), without duplicates
84  
85 __END__
↑ Summary