blib/lib/Math/Int2Base.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 8 / 8 Conditioncond 85.1% 23 / 27 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 3 totaltotal 89.6% 95 / 106 CC 16 SCAR 17.7
CC
16
Cov
95%
int2base
9.0
base2int
7.0
base_chars
2.0
CRAP 5.8
↑ Summary
1 package Math::Int2Base;
2  
3 1 ▶use 5.008006;
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 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '1.01';
9  
10 1 require Exporter;
11 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our @EXPORT_OK = qw( int2base base2int base_chars );
14  
15 1 my @Chars = ('0'..'9', 'A'..'Z', 'a'..'z');
16 1 my $MaxBase = scalar @Chars;
17 1 my $CharStr = join '', @Chars;
18  
19 #---------------------------------------------------------------------
20 # int2base( $num, $base, $minlen ); # base ||= 10 minlen ||= 1
21 134 ▶sub int2base {
Subroutine int2base: called CC=9 SCAR=22.0
Pod undocumented
22  
23 134 my( $ret, $num, $base, $minlen ) = ( '', @_ );
24 134 ▶ $num ||= 0;
Condition100%2/2
$num ||= 0
l!l
1304
MC/DC100%1/1
$num ||= 0
$num
Truth table100%2/2
A
A= $num
Aresult
11
00
25 134 ▶ $base ||= 10;
Condition50%1/2
$base ||= 10
l!l
1340
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
26 134 ▶ $minlen ||= 1;
Condition100%2/2
$minlen ||= 1
l!l
11123
MC/DC100%1/1
$minlen ||= 1
$minlen
Truth table100%2/2
A
A= $minlen
Aresult
11
00
27  
28 134 ▶ if( $num < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 0 or $base < 2 or $MaxBase < $base or $minlen < 1) 4 130 134
Condition100%9/9
$num < 0 or $base < 2 or $MaxBase < $base or $minlen < 1
l!l&&r!l&&!r
31130
$num < 0 or $base < 2 or $MaxBase < $base
l!l&&r!l&&!r
21131
$num < 0 or $base < 2
l!l&&r!l&&!r
11132
MC/DC100%4/4
$num < 0 or $base < 2 or $MaxBase < $base or $minlen < 1
$num < 0 $base < 2 $MaxBase < $base $minlen < 1
Truth table100%5/5
A or B or C or D
A= $num < 0B= $base < 2C= $MaxBase < $baseD= $minlen < 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
29 || $base < 2
30 || $MaxBase < $base
31 || $minlen < 1
32 # || $num != int( $num ) # XXX[1] do we care?
33 ) {
34 4 croak "not supported: int2base( '$num', $base, $minlen )" }
35  
36 130 ▶ for (; $num; $num = int($num/$base) ) { $ret .= $Chars[$num % $base] }
Branch: 1 branch
BranchTrueFalseTotal
if ($num) 243 130 373
37 1 ▶ no warnings 'numeric'; # negative repeat count
Subroutine BEGIN: called CC=1 SCAR=0.0
38 130 return scalar reverse $ret . '0'x($minlen - length($ret));
39 }
40  
41 #---------------------------------------------------------------------
42 # base2int( $num, $base ); # base ||= 10
43 68 ▶sub base2int {
Subroutine base2int: called CC=7 SCAR=19.5
Pod undocumented
44  
45 68 my( $ret, $num, $base ) = ( 0, @_ );
46 68 ▶ $num ||= 0;
Condition100%2/2
$num ||= 0
l!l
653
MC/DC100%1/1
$num ||= 0
$num
Truth table100%2/2
A
A= $num
Aresult
11
00
47 68 ▶ $base ||= 10;
Condition50%1/2
$base ||= 10
l!l
680
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
48 68 my $chars = substr $CharStr, 0, $base;
49  
50 68 ▶ if( $num !~ /^[$chars]+$/
Branch: 1 branch
BranchTrueFalseTotal
if (not $num =~ /^[$chars]+$/ or $base < 2 or $MaxBase < $base) 4 64 68
Condition83%5/6
not $num =~ /^[$chars]+$/ or $base < 2 or $MaxBase < $base
l!l&&r!l&&!r
3164
not $num =~ /^[$chars]+$/ or $base < 2
l!l&&r!l&&!r
3065
MC/DC66%2/3
not $num =~ /^[$chars]+$/ or $base < 2 or $MaxBase < $base
not $num =~ /^[$chars]+$/ $base < 2 $MaxBase < $base
Truth table75%3/4
not(A) or B or C
A= not $num =~ /^[$chars]+$/B= $base < 2C= $MaxBase < $base
ABCresult
1XX1
01X1
0011
0000
51 || $base < 2
52 || $MaxBase < $base ) {
53 4 croak "not supported: base2int( '$num', $base )" }
54  
55 64 $num =~ s/^0+//; # trim leading zeros
56 64 ▶ for( my $i = length($num)-1, my $c = 0; $i >= 0; --$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 170 64 234
57 170 $ret += index($CharStr, substr($num, $i, 1)) * $base**$c++ }
58 64 return $ret;
59 }
60  
61 #---------------------------------------------------------------------
62 # base_chars( $base ); # base ||= 10
63 2 ▶sub base_chars {
Subroutine base_chars: called CC=2 SCAR=7.1
Pod undocumented
64 2 my( $base ) = @_;
65 2 ▶ $base ||= 10;
Condition50%1/2
$base ||= 10
l!l
20
MC/DC0%0/1
$base ||= 10
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
66 2 return substr $CharStr, 0, $base;
67 }
68  
69 1 1;
70 __END__
↑ Summary