blib/lib/Ordeal/Model/Shuffle.pm

filter: Statementstmt 85.7% 66 / 77 Branchbran 57.6% 15 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 100% 8 / 8 totaltotal 78.0% 103 / 132 SCAR 29.2
CC
16
Cov
78%
clone
12.0
draw
11.4
BUILD
4.1
CRAP 18.6
« blib/lib/Ordeal/Model/ChaCha20.pm ↑ Summary blib/lib/Ordeal/Model/Backend/PlainFile.pm »
1 package Ordeal::Model::Shuffle;
2  
3 # vim: ts=3 sts=3 sw=3 et ai :
4  
5 5 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict; # redundant, but still useful to document
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 { our $VERSION = '0.002'; }
9 5 ▶use English qw< -no_match_vars >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Mo qw< build default >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use experimental qw< signatures postderef >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶no warnings qw< experimental::signatures experimental::postderef >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 has auto_reshuffle => (default => 0);
17 5 has deck => (default => undef);
18 5 has default_n_draw => (default => undef);
19 5 has random_source => (default => undef);
20 5 has _draw_sorted => (default => 0);
21 5 has _i => (default => undef);
22 5 has _indexes => (default => undef);
23  
24 ▶sub BUILD ($self) {
Subroutine BUILD: called CC=4 SCAR=14.0
Pod documented
25 5 ▶ ouch 400, 'no deck defined' unless $self->deck;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->deck 0 5 5
26 5 ▶ if (! $self->random_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->random_source) 1 4 5
27 1 require Ordeal::Model::ChaCha20;
28 1 $self->random_source(Ordeal::Model::ChaCha20->new);
29 }
30 5 ▶ $self->default_n_draw($self->deck->n_cards)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->default_n_draw 0 5 5
31 unless defined $self->default_n_draw;
32  
33 5 $self->shuffle;
34  
35 5 return $self;
36 }
37  
38 ▶sub clone ($self, %args) {
Subroutine clone: not called CC=3 SCAR=24.8
Pod documented
39 0 my $other = ref($self)->new(
40 auto_reshuffle => $self->auto_reshuffle, # overridable
41 default_n_draw => $self->default_n_draw, # overridable
42 %args,
43 deck => $self->deck, # this can't be overridden
44 );
45 $other->random_source($self->random_source->clone)
46 0 ▶ unless exists $args{random_source};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"random_source"} 0 0 0
47 0 $other->_i($self->_i);
48 0 ▶ if (my $indexes = $self->_indexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexes = $self->_indexes) { } 0 0 0
49 0 $other->_indexes([$indexes->@*]);
50 }
51 else {
52 0 $other->_indexes(undef);
53 }
54 0 return $other;
55 }
56  
57 5 ▶sub draw ($self, $n = undef) {
Subroutine draw: called CC=11 SCAR=24.4
Pod documented
58 5 ▶ $n //= $self->default_n_draw;
Condition66%2/3
$n //= $self->default_n_draw
l!l&&r!l&&!r
230
MC/DC0%0/2
$n //= $self->default_n_draw
$n $self->default_n_draw
Truth table66%2/3
A //= B
A= $nB= $self->default_n_draw
ABresult
1X1
011
000
59 5 ▶ ouch 400, 'invalid number of cards', $n
Branch: 1 branch
BranchTrueFalseTotal
unless $n =~ /\A(?: 0 | [1-9]\d*)\z/msux 0 5 5
60 unless $n =~ m{\A(?: 0 | [1-9]\d*)\z}mxs;
61 5 my $deck = $self->deck;
62  
63 5 my $i = $self->_i;
64 5 ▶ $n = $i + 1 if $n == 0; # take them all
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 5 5
65 5 ▶ ouch 400, 'not enough cards left', $n, $i + 1
Branch: 1 branch
BranchTrueFalseTotal
if $n > $i + 1 0 5 5
66 if $n > $i + 1;
67  
68 5 my @retval;
69 5 ▶ if (my $indexes = $self->_indexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexes = $self->_indexes) { } 3 2 5
70 3 my $rs = $self->random_source;
71 3 ▶ while ($n-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- > 0) 13 3 16
72 13 my $j = $rs->int_rand(0, $i); # extremes included
73 13 (my $retval, $indexes->[$j]) = $indexes->@[$j, $i--];
74 13 push @retval, $deck->card_at($retval);
75 }
76 }
77 else {
78 2 my $top_index = $deck->n_cards - 1;
79 2 ▶ while ($n-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n-- > 0) 10 2 12
80 10 push @retval, $deck->card_at($top_index - $i--);
81 }
82 }
83  
84 # prepare for next call
85 5 ▶ $self->auto_reshuffle ? $self->shuffle : $self->_i($i);
Branch: 1 branch
BranchTrueFalseTotal
$self->auto_reshuffle ? : 0 5 5
86  
87 5 ▶ return $retval[0] if @retval == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @retval == 1 0 5 5
88 5 return @retval;
89 }
90  
91 0 ▶sub is_sorted ($self) { return !($self->_indexes) }
Subroutine is_sorted: not called CC=1 SCAR=6.9
Pod documented
92  
93 0 ▶sub n_remaining ($self) { return $self->_i + 1 }
Subroutine n_remaining: not called CC=1 SCAR=6.9
Pod documented
94  
95 ▶sub reset ($self) {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
96 0 $self->random_source->reset;
97 0 return $self->shuffle;
98 }
99  
100 ▶sub shuffle ($self) {
Subroutine shuffle: called CC=1 SCAR=0.0
Pod documented
101 7 $self->_i(my $i = $self->deck->n_cards - 1);
102 7 $self->_indexes([0 .. $i]);
103 7 return $self;
104 }
105  
106 ▶sub sort ($self) {
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
107 4 $self->_i($self->deck->n_cards - 1);
108 4 $self->_indexes(undef);
109 4 return $self;
110 }
111  
112 5 1;
« blib/lib/Ordeal/Model/ChaCha20.pm ↑ Summary blib/lib/Ordeal/Model/Backend/PlainFile.pm »