blib/lib/Ticketmaster.pm

filter: Statementstmt 12.5% 10 / 80 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 27.2% 3 / 11 Podpod 80.0% 4 / 5 totaltotal 11.9% 17 / 142 CC 21 SCAR 33.2
CC
21
Cov
8%
_max_coin
72.0
_calculate
20.0
add_coins
20.0
CRAP 27.8
↑ Summary
1 package Ticketmaster;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 1 our $VERSION = '1.02';
9  
10 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
11 0 my $class = shift;
12 0 ▶ my $type = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
13 0 my $self = bless {}, $type;
14  
15 0 $self->{'coins'} = [];
16 0 ▶ $self->reload({ @_ }) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
17 0 $self;
18 }
19  
20 0 ▶sub reload {
Subroutine reload: not called CC=1 SCAR=6.9
Pod documented
21 0 my $self = shift;
22 0 $self->{'store'} = { @_ };
23  
24 0 $self->{'min_coin'} = shift @{ [ sort {$a <=> $b} keys %{$self->{'store'}} ] };
25 }
26  
27 0 ▶sub change {
Subroutine change: not called CC=2 SCAR=17.9
Pod documented
28 0 my $self = shift;
29 0 my $change = shift;
30  
31 0 ▶ return [] if $change <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $change <= 0 0 0 0
32 0 $self->{'change'} = $change;
33 0 $self->_calculate;
34  
35 0 $self->{'coins'};
36 }
37  
38 0 ▶sub _calculate {
Subroutine _calculate: not called CC=4 SCAR=30.0
39 0 my $self = shift;
40 0 my $limit = shift;
41  
42 0 my $store = $self->{'store'};
43 0 my $coin = $self->_max_coin($limit);
44  
45 0 ▶ if ($coin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coin) { } 0 0 0
46 0 push (@{$self->{'coins'}}, $coin);
47 0 ▶ return if $self->{'change'} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"change"} == 0 0 0 0
48 0 $self->_calculate($coin);
49 }else{
50 # give up if no way to make change
51 0 my $pop_coin = $self->_withdrawal;
52 0 ▶ return unless $pop_coin;
Branch: 1 branch
BranchTrueFalseTotal
unless $pop_coin 0 0 0
53 0 $self->_calculate($pop_coin - 1);
54 }
55 }
56  
57 0 ▶sub _withdrawal {
Subroutine _withdrawal: not called CC=3 SCAR=24.8
58 0 my $self = shift;
59 0 my $store = $self->{'store'};
60 0 my $coins = $self->{'coins'};
61  
62 0 my $coin = pop @$coins;
63 0 $self->{'change'} += $coin;
64 0 $store->{$coin} += 1;
65 0 ▶ return if scalar @$coins == 0 && $coin == $self->{'min_coin'};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$coins == 0 and $coin == $self->{"min_coin"} 0 0 0
Condition0%0/3
scalar @$coins == 0 and $coin == $self->{"min_coin"}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$coins == 0 and $coin == $self->{"min_coin"}
scalar @$coins == 0 $coin == $self->{"min_coin"}
Truth table0%0/3
A and B
A= scalar @$coins == 0B= $coin == $self->{"min_coin"}
ABresult
0X0
100
111
66 0 return $coin;
67 }
68  
69 0 ▶sub balance {
Subroutine balance: not called CC=3 SCAR=24.8
Pod documented
70 0 my $self = shift;
71
72 0 my %balance;
73 0 foreach (sort keys %{$self->{'store'}}) {
74 0 ▶ if ($self->{'store'}->{$_} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"store"}{$_} != 0) 0 0 0
75 0 $balance{$_} = $self->{'store'}->{$_};
76 }
77 }
78 0 \%balance;
79 }
80  
81 0 ▶sub _max_coin {
Subroutine _max_coin: not called CC=8 SCAR=42.8
82 0 my $self = shift;
83 0 my $limit = shift;
84 0 my $store = $self->{'store'};
85  
86 0 foreach my $coin (sort {$b <=> $a} keys %$store) {
87 # child coins shouldn't be bigger than his parent
88 0 ▶ next if $limit && $coin > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $coin > $limit 0 0 0
Condition0%0/3
$limit and $coin > $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $coin > $limit
$limit $coin > $limit
Truth table0%0/3
A and B
A= $limitB= $coin > $limit
ABresult
0X0
100
111
89 0 ▶ next unless $coin <= $self->{'change'};
Branch: 1 branch
BranchTrueFalseTotal
unless $coin <= $self->{"change"} 0 0 0
90 0 ▶ next unless $store->{$coin} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $store->{$coin} > 0 0 0 0
91 0 $store->{$coin} -= 1;
92 0 $self->{'change'} -= $coin;
93 0 ▶ my @available_coins = grep { $_ if $_ < $coin && $store->{$_} > 0 } keys %$store;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $coin and $store->{$_} > 0 0 0 0
Condition0%0/3
$_ < $coin and $store->{$_} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_ < $coin and $store->{$_} > 0
$_ < $coin $store->{$_} > 0
Truth table0%0/3
A and B
A= $_ < $coinB= $store->{$_} > 0
ABresult
0X0
100
111
94 0 $self->{'available_coins'} = \@available_coins;
95 0 return $coin;
96 }
97 0 return;
98 }
99  
100 0 ▶sub add_coins {
Subroutine add_coins: not called CC=4 SCAR=30.0
Pod documented
101 0 my $self = shift;
102 0 my $coins = { @_ };
103 0 my $store = $self->{'store'};
104  
105 0 my %new_store;
106 0 foreach my $pair ($coins, $store) {
107 0 ▶ while (my ($key, $value) = each %$pair) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$pair) 0 0 0
108 0 ▶ if (exists $new_store{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $new_store{$key}) { } 0 0 0
109 0 $new_store{$key} += $value;
110 }else{
111 0 $new_store{$key} = $value;
112 }
113 }
114 }
115 0 $self->reload(%new_store);
116 }
117  
118  
119 1 1;
120  
↑ Summary