Gin/Meld.pm

filter: Statementstmt 100% 74 / 74 Branchbran 95.4% 21 / 22 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 99.1% 123 / 124 CC 26 SCAR 21.2
CC
26
Cov
99%
melds_in
14.0
is_set
6.0
is_run
6.0
CRAP 8.3
« Gin/Deal.pm ↑ Summary Gin/Search.pm »
1 package Game::Gin::Meld;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Game::Gin::Card qw(rank_of suit_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our $VERSION = '0.03';
11 13 our @EXPORT_OK = qw(melds_in is_meld is_set is_run MIN_MELD);
12  
13 13 ▶use constant MIN_MELD => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 31550 ▶sub is_set {
Subroutine is_set: called CC=6 SCAR=17.9
Pod documented
16 31550 my ($cards) = @_;
17 31550 ▶ return 0 unless @$cards >= MIN_MELD && @$cards <= 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cards >= 3 and @$cards <= 4 10378 21172 31550
Condition100%3/3
@$cards >= 3 and @$cards <= 4
!ll&&!rl&&r
11037721172
MC/DC100%2/2
@$cards >= 3 and @$cards <= 4
@$cards >= 3 @$cards <= 4
Truth table100%3/3
A and B
A= @$cards >= 3B= @$cards <= 4
ABresult
0X0
100
111
18 21172 my $rank = rank_of($cards->[0]);
19 21172 my %suit;
20 21172 for my $c (@$cards) {
21 68903 ▶ return 0 unless rank_of($c) == $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless rank_of($c) == $rank 20620 48283 68903
22 48283 ▶ return 0 if $suit{ suit_of($c) }++;
Branch: 1 branch
BranchTrueFalseTotal
if $suit{suit_of($c)}++ 0 48283 48283
23 }
24 552 return 1;
25 }
26  
27 31005 ▶sub is_run {
Subroutine is_run: called CC=6 SCAR=17.9
Pod documented
28 31005 my ($cards) = @_;
29 31005 ▶ return 0 unless @$cards >= MIN_MELD;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cards >= 3 1 31004 31005
30 31004 my $suit = suit_of($cards->[0]);
31 31004 my @rank;
32 31004 for my $c (@$cards) {
33 99434 ▶ return 0 unless suit_of($c) eq $suit;
Branch: 1 branch
BranchTrueFalseTotal
unless suit_of($c) eq $suit 28287 71147 99434
34 71147 push @rank, rank_of($c);
35 }
36 2717 @rank = sort { $a <=> $b } @rank;
37 2717 for my $i (1 .. $#rank) {
38 5683 ▶ return 0 unless $rank[$i] == $rank[ $i - 1 ] + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $rank[$i] == $rank[$i - 1] + 1 1884 3799 5683
39 }
40 833 return 1;
41 }
42  
43 31546 ▶sub is_meld { my ($cards) = @_; return is_set($cards) || is_run($cards) ? 1 : 0 }
Subroutine is_meld: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_set($cards) || is_run($cards) ? : 1379 30167 31546
Condition100%3/3
is_set($cards) or is_run($cards)
l!l&&r!l&&!r
55082930167
MC/DC100%2/2
is_set($cards) or is_run($cards)
is_set($cards) is_run($cards)
Truth table100%3/3
A or B
A= is_set($cards)B= is_run($cards)
ABresult
1X1
011
000
44  
45 433707 ▶sub melds_in {
Subroutine melds_in: called CC=14 SCAR=26.4
Pod documented
46 433707 my ($cards) = @_;
47 433707 my @out;
48  
49 my %by_rank;
50 433707 push @{ $by_rank{ rank_of($_) } }, $_ for @$cards;
51 433707 for my $rank (sort { $a <=> $b } keys %by_rank) {
52 3077294 my @same = sort { $a <=> $b } @{ $by_rank{$rank} };
53 3077294 ▶ next unless @same >= MIN_MELD;
Branch: 1 branch
BranchTrueFalseTotal
unless @same >= 3 2846849 230445 3077294
54 230445 push @out, [@same];
55 230445 ▶ if (@same == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@same == 4) 25976 204469 230445
56 25976 for my $skip (0 .. 3) {
57 103904 push @out, [ map { $same[$_] } grep { $_ != $skip } 0 .. 3 ];
58 }
59 }
60 }
61  
62 433707 my %by_suit;
63 433707 push @{ $by_suit{ suit_of($_) } }, $_ for @$cards;
64 433707 for my $suit (sort keys %by_suit) {
65 1666914 my @same = sort { rank_of($a) <=> rank_of($b) } @{ $by_suit{$suit} };
66 1666914 ▶ next unless @same >= MIN_MELD;
Branch: 1 branch
BranchTrueFalseTotal
unless @same >= 3 836728 830186 1666914
67  
68 830186 my $start = 0;
69 830186 for my $i (1 .. @same) {
70 2990601 ▶ my $breaks = $i == @same
Condition100%3/3
$i == @same || rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
l!l&&r!l&&!r
8301861344417815998
MC/DC100%2/2
$i == @same || rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
$i == @same rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
Truth table100%3/3
A || B
A= $i == @sameB= rank_of($same[$i]) != rank_of($same[$i - 1]) + 1
ABresult
1X1
011
000
71 || rank_of($same[$i]) != rank_of($same[ $i - 1 ]) + 1;
72 2990601 ▶ next unless $breaks;
Branch: 1 branch
BranchTrueFalseTotal
unless $breaks 815998 2174603 2990601
73 2174603 my @stretch = @same[ $start .. $i - 1 ];
74 2174603 for my $len (MIN_MELD .. scalar @stretch) {
75 222326 for my $at (0 .. @stretch - $len) {
76 279148 push @out, [ @stretch[ $at .. $at + $len - 1 ] ];
77 }
78 }
79 2174603 $start = $i;
80 }
81 }
82  
83 433707 return @out;
84 }
85  
86 13 1;
87  
88 __END__
« Gin/Deal.pm ↑ Summary Gin/Search.pm »