blib/lib/Number/Denominal.pm

filter: Statementstmt 100% 90 / 90 Branchbran 93.4% 43 / 46 Conditioncond 90.0% 18 / 20 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 96.1% 176 / 183 CC 36 SCAR 25.9
CC
36
Cov
97%
_denominal
23.0
_prepare_extra_args
6.1
_process_den
6.0
CRAP 13.3
↑ Summary
1 package Number::Denominal;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use List::ToHumanString 1.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 require Exporter;
8 9 our @ISA = qw(Exporter);
9 9 our @EXPORT = qw(denominal denominal_hashref denominal_list);
10  
11 9 our $VERSION = '2.001002'; # VERSION
12  
13 600280 ▶sub denominal {
Subroutine denominal: called CC=1 SCAR=0.0
Pod documented
14 600280 my ( $num, @den ) = @_;
15 600280 return _denominal( $num, \@den, 'string' );
16 }
17  
18 13 ▶sub denominal_list {
Subroutine denominal_list: called CC=1 SCAR=0.0
Pod documented
19 13 my ( $num, @den ) = @_;
20 13 return _denominal( $num, \@den, 'list' );
21 }
22  
23 12 ▶sub denominal_hashref {
Subroutine denominal_hashref: called CC=1 SCAR=0.0
Pod documented
24 12 my ( $num, @den ) = @_;
25 12 return _denominal( $num, \@den, 'hashref' );
26 }
27  
28 600305 ▶sub _denominal {
Subroutine _denominal: called CC=23 SCAR=31.4
29 600305 my ( $num, $den, $mode ) = @_;
30  
31 600305 $num = abs $num;
32 600305 my $args = _prepare_extra_args( $den );
33 600304 $den = _process_den( $mode, $den );
34  
35 600304 my @bits;
36 600304 my $step = 1; # steps for precision, if enabled
37 600304 my $pre_bit; # a "pre" bit, again for precision to handle rounding.
38 my @ordered_bit_names; # need this for missing bits in denominal_list
39 600304 for my $bit ( _get_bits( @$den ) ) {
40 2107632 push @ordered_bit_names, $bit->{name}[0];
41  
42 2107632 $bit->{num} = sprintf '%d', $num / $bit->{divisor};
43 2107632 $num = $num - $bit->{num} * $bit->{divisor};
44  
45 2107632 ▶ if ( @bits
Branch: 1 branch
BranchTrueFalseTotal
if (@bits and $args->{"precision"} and ++$step > $args->{"precision"}) 300265 1807367 2107632
Condition100%6/6
@bits and $args->{"precision"} and ++$step > $args->{"precision"}
!ll&&!rl&&r
180735314300265
@bits and $args->{"precision"}
!ll&&!rl&&r
6142481193105300279
MC/DC100%3/3
@bits and $args->{"precision"} and ++$step > $args->{"precision"}
@bits $args->{"precision"} ++$step > $args->{"precision"}
Truth table100%4/4
A and B and C
A= @bitsB= $args->{"precision"}C= ++$step > $args->{"precision"}
ABCresult
0XX0
10X0
1100
1111
46 and $args->{precision} and ++$step > $args->{precision}
47 ) {
48 300265 ▶ last unless $bit->{num};
Branch: 1 branch
BranchTrueFalseTotal
unless $bit->{"num"} 42586 257679 300265
49  
50 # add current element and pre bit, for sake of calculations
51 # I should really really refactor this crap to something sane
52 257679 ▶ unshift @bits, $pre_bit if defined $pre_bit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pre_bit 5805 251874 257679
53 257679 push @bits, +{ %$bit };
54  
55 257679 for ( reverse 0 .. $#bits ) {
56 # increase the previous number if we have to round
57 $bits[$_-1]{num}++
58 if $bits[$_]{num} * $bits[$_]{divisor}
59 510460 ▶ / $bits[$_-1]{divisor} >= 0.5;
Branch: 1 branch
BranchTrueFalseTotal
if $bits[$_]{"num"} * $bits[$_]{"divisor"} / $bits[$_ - 1]{"divisor"} >= "0.5" 380413 130047 510460
60  
61 $bits[$_]{num} = 0
62 if $bits[$_]{num} * $bits[$_]{divisor}
63 510460 ▶ / $bits[$_-1]{divisor} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bits[$_]{"num"} * $bits[$_]{"divisor"} / $bits[$_ - 1]{"divisor"} == 1 456 510004 510460
64  
65 # stop checking previous numbers if we ran out of them
66 # or they haven't reached their limit (divisor) yet.
67 last if $_ == 0
68 or $bits[$_-1]{num} * $bits[$_-1]{divisor}
69 510460 ▶ < $bits[$_-2]{divisor};
Branch: 1 branch
BranchTrueFalseTotal
if $_ == 0 or $bits[$_ - 1]{"num"} * $bits[$_ - 1]{"divisor"} < $bits[$_ - 2]{"divisor"} 257679 252781 510460
Condition100%3/3
$_ == 0 or $bits[$_ - 1]{"num"} * $bits[$_ - 1]{"divisor"} < $bits[$_ - 2]{"divisor"}
l!l&&r!l&&!r
2523255354252781
MC/DC100%2/2
$_ == 0 or $bits[$_ - 1]{"num"} * $bits[$_ - 1]{"divisor"} < $bits[$_ - 2]{"divisor"}
$_ == 0 $bits[$_ - 1]{"num"} * $bits[$_ - 1]{"divisor"} < $bits[$_ - 2]{"divisor"}
Truth table100%3/3
A or B
A= $_ == 0B= $bits[$_ - 1]{"num"} * $bits[$_ - 1]{"divisor"} < $bits[$_ - 2]{"divisor"}
ABresult
1X1
011
000
70 }
71  
72 # pop last element off, since it was temporary
73 257679 pop @bits;
74  
75 # get rid of els with zero nums (including pre bit)
76 257679 @bits = grep $_->{num}, @bits;
77 257679 last;
78 }
79  
80 1807367 ▶ $pre_bit = $bit unless $bit->{num};
Branch: 1 branch
BranchTrueFalseTotal
unless $bit->{"num"} 177124 1630243 1807367
81 1807367 ▶ $bit->{num} or next; # don't insert the bit, if it's zero
Branch: 1 branch
BranchTrueFalseTotal
$bit->{"num"} or next 1630243 177124 1807367
82 1630243 push @bits, +{ %$bit };
83 }
84  
85 600304 my @result;
86 # process the bits into output format, depending on what type
87 # ... of output is wanted
88 600304 for ( @bits ) {
89 push @result, $mode eq 'hashref' ? ( $_->{name}[0] => $_->{num} )
90 : $mode eq 'list' ? { num => $_->{num}, name => $_->{name}[0] }
91 1630240 ▶ : $_->{num} . ' ' . $_->{name}[ $_->{num} == 1 ? 0 : 1 ];
Branch: 3 branches
BranchTrueFalseTotal
$mode eq 'hashref' ? : 22 1630218 1630240
$mode eq 'list' ? : 25 1630193 1630218
$_->{'num'} == 1 ? : 74732 1555461 1630193
92 }
93  
94 600304 ▶ if ( $mode eq 'list' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "list") 13 600291 600304
95 13 my @temp_result = @result;
96 13 @result = ();
97 13 my %bits_in_result;
98 @bits_in_result{ map $_->{name}, @temp_result }
99 13 = map $_->{num}, @temp_result;
100  
101 13 for ( @ordered_bit_names ) {
102 push @result, exists $bits_in_result{ $_ }
103 43 ▶ ? $bits_in_result{ $_ } : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $bits_in_result{$_} ? : 25 18 43
104 }
105 }
106  
107 600304 ▶ return $mode eq 'hashref'
Branch: 2 branches
BranchTrueFalseTotal
$mode eq 'hashref' ? : 12 600292 600304
$mode eq 'list' ? : 13 600279 600292
108 ? +{@result} :
109 $mode eq 'list'
110 ? @result : to_human_string '|list|', @result;
111 }
112  
113 600304 ▶sub _get_bits {
Subroutine _get_bits: called CC=4 SCAR=13.9
114 600304 my @den = @_;
115  
116 600304 my @bits;
117 600304 my $divisor = 1;
118 600304 for ( grep !($_%2), 0..$#den ) {
119 3001454 ▶ if ( not ref $den[ $_ ] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $den[$_]) 3001433 21 3001454
120 3001433 $den[ $_ ] = [
121 $den[ $_ ],
122 $den[ $_ ] . 's',
123 ];
124 }
125  
126 3001454 push @bits, {
127 name => $den[ $_ ],
128 divisor => $divisor,
129 };
130  
131 3001454 ▶ $divisor *= $den[ $_+1 ] || 1;
Condition100%2/2
$den[$_ + 1] || 1
l!l
2401151600303
MC/DC100%1/1
$den[$_ + 1] || 1
$den[$_ + 1]
Truth table100%2/2
A
A= $den[$_ + 1]
Aresult
11
00
132 }
133  
134 600304 return reverse @bits;
135 }
136  
137 600304 ▶sub _process_den {
Subroutine _process_den: called CC=6 SCAR=17.9
138 600304 my ( $mode, $den ) = @_;
139  
140 600304 ▶ if ( @$den == 1 and ref $den->[0] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$den == 1 and ref $den->[0] eq 'ARRAY') { } 1 600303 600304
elsif (@$den == 1 and ref $den->[0] eq 'SCALAR') { } 600271 64 600335
Condition100%3/3
@$den == 1 and ref $den->[0] eq "ARRAY"
!ll&&!rl&&r
326002711
MC/DC100%2/2
@$den == 1 and ref $den->[0] eq "ARRAY"
@$den == 1 ref $den->[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @$den == 1B= ref $den->[0] eq "ARRAY"
ABresult
0X0
100
111
141 1 my $idx = 0;
142 1 @$den = map +( 'el' . $idx++ => $_ ), @{ $den->[0] };
143 1 push @$den, 'el', $idx;
144 1 $mode = 'list';
145 }
146 ▶ elsif ( @$den == 1 and ref $den->[0] eq 'SCALAR' ) {
Condition66%2/3
@$den == 1 and ref $den->[0] eq "SCALAR"
!ll&&!rl&&r
320600271
MC/DC50%1/2
@$den == 1 and ref $den->[0] eq "SCALAR"
@$den == 1 ref $den->[0] eq "SCALAR"
Truth table66%2/3
A and B
A= @$den == 1B= ref $den->[0] eq "SCALAR"
ABresult
0X0
100
111
147 600271 my $unit_shortcut = ${ $den->[0] };
148 600271 my $values_for_unit = _get_units()->{ $unit_shortcut };
149  
150 600271 ▶ croak qq{Unknown unit shortcut ``$unit_shortcut''}
Branch: 1 branch
BranchTrueFalseTotal
unless $values_for_unit 0 600271 600271
151 unless $values_for_unit;
152 600271 $den = $values_for_unit;
153 }
154  
155 600304 return $den;
156 }
157  
158 600305 ▶sub _prepare_extra_args {
Subroutine _prepare_extra_args: called CC=6 SCAR=18.2
159 600305 my $den = shift;
160  
161 600305 ▶ return unless ref $den->[-1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $den->[-1] eq "HASH" 300034 300271 600305
162  
163 300271 my %extra_args = %{ delete $den->[-1] };
164  
165 300271 ▶ if ( exists $extra_args{precision} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra_args{"precision"}) 300271 0 300271
166 300271 my $p = $extra_args{precision};
167 300271 ▶ croak q{precision argument takes positive integers only,}
Branch: 2 branches
BranchTrueFalseTotal
unless $p and $p =~ /\A\d+\z/ 1 300270 300271
defined $p ? : 1 0 1
Condition66%2/3
$p and $p =~ /\A\d+\z/
!ll&&!rl&&r
10300270
MC/DC50%1/2
$p and $p =~ /\A\d+\z/
$p $p =~ /\A\d+\z/
Truth table66%2/3
A and B
A= $pB= $p =~ /\A\d+\z/
ABresult
0X0
100
111
168 . q{ but its value is } . (defined $p ? $p : '[undefined]')
169 unless $p and $p =~ /\A\d+\z/;
170 }
171  
172 300270 return \%extra_args;
173 }
174  
175 600272 ▶sub _get_units {
Subroutine _get_units: called CC=1 SCAR=0.0
176 return {
177 600272 time => [
178 second => 60 => minute => 60 => hour => 24 => day => 7 => 'week'
179 ],
180 weight => [
181 gram => 1000 => kilogram => 1000 => 'tonne',
182 ],
183 weight_imperial => [
184 ounce => 16 => pound => 14 => stone => 160 => 'ton',
185 ],
186 length => [
187 meter => 1000 => kilometer => 9_460_730_472.5808 => 'light year',
188 ],
189 length_mm => [
190 millimeter => 10 => centimeter => 100 => meter => 1000
191 => kilometer => 9_460_730_472.5808 => 'light year',
192 ],
193 length_imperial => [
194 [qw/inch inches/] => 12 =>
195 [qw/foot feet/] => 3 => yard => 1760
196 => [qw/mile miles/],
197 ],
198 volume => [
199 milliliter => 1000 => 'Liter',
200 ],
201 volume_imperial => [
202 'fluid ounce' => 20 => pint => 2 => quart => 4 => 'gallon',
203 ],
204 info => [
205 bit => 8 => byte => 1000 => kilobyte => 1000 => megabyte => 1000
206 => gigabyte => 1000 => terabyte => 1000 => petabyte => 1000
207 => exabyte => 1000 => zettabyte => 1000 => 'yottabyte',
208 ],
209 info_1024 => [
210 bit => 8 => byte => 1024 => kibibyte => 1024 => mebibyte => 1024
211 => gibibyte => 1024 => tebibyte => 1024 => pebibyte => 1024
212 => exbibyte => 1024 => zebibyte => 1024 => 'yobibyte',
213 ],
214 };
215 }
216  
217 9 q|
218 Q: how many programmers does it take to change a light bulb?
219 A: none, that's a hardware problem
220 |;
221  
222 __END__
↑ Summary