Collection/PickItems/Iterator.pm

filter: Statementstmt 55.5% 15 / 27 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 66.6% 2 / 3 Podpod 0.0% 0 / 1 totaltotal 42.5% 17 / 40 CC 5 SCAR 30.8
CC
5
Cov
43%
pick_items
30.0
BEGIN
1.0
BEGIN
1.0
CRAP 21.7
« Collection/PickItems/RandomPos.pm ↑ Summary Collection/FindItem/Iterator.pm »
1 package Role::TinyCommons::Collection::PickItems::Iterator;
2  
3 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
7 1 our $DATE = '2024-01-16'; # DATE
8 1 our $DIST = 'Role-TinyCommons-Collection'; # DIST
9 1 our $VERSION = '0.010'; # VERSION
10  
11 1 requires 'get_next_item';
12 1 requires 'has_next_item';
13 1 requires 'reset_iterator';
14  
15 1 with 'Role::TinyCommons::Collection::PickItems';
16  
17 0 ▶sub pick_items {
Subroutine pick_items: not called CC=5 SCAR=34.0
Pod undocumented
18 0 my ($self, %args) = @_;
19  
20 0 ▶ my $n = $args{n} || 1;
Condition0%0/2
$args{'n'} || 1
l!l
00
MC/DC0%0/1
$args{'n'} || 1
$args{'n'}
Truth table0%0/2
A
A= $args{'n'}
Aresult
11
00
21  
22 0 my @items;
23 0 my $i = -1;
24 0 $self->reset_iterator;
25 0 ▶ while ($self->has_next_item) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next_item) 0 0 0
26 0 $i++;
27 0 my $item = $self->get_next_item;
28 0 ▶ if (@items < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (@items < $n) { } 0 0 0
29 # we haven't reached $num_items, insert item to array in a random
30 # position
31 0 splice @items, rand(@items+1), 0, $item;
32 } else {
33 # we have reached $num_items, just replace an item randomly, using
34 # algorithm from Learning Perl, slightly modified
35 0 ▶ rand($i+1) < @items and splice @items, rand(@items), 1, $item;
Branch: 1 branch
BranchTrueFalseTotal
rand $i + 1 < @items and splice @items, rand @items, 1, $item 0 0 0
36 }
37 }
38 0 @items;
39 }
40  
41 1 1;
42 # ABSTRACT: Provide pick_items() that picks by iterating all items once
43  
44 __END__
« Collection/PickItems/RandomPos.pm ↑ Summary Collection/FindItem/Iterator.pm »