blib/lib/Math/BaseArith.pm

filter: Statementstmt 90.1% 73 / 81 Branchbran 71.8% 23 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 85.2% 116 / 136 CC 20 SCAR 22.9
CC
20
Cov
85%
encode_base
15.7
decode_base
7.2
BEGIN
1.0
CRAP 9.9
↑ Summary
1 package Math::BaseArith;
2
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 5 require Exporter;
10
11 5 our $VERSION = '1.04';
12
13 5 our $DEBUG = 0; # set to 1 to enable debug printing
14
15 5 our @ISA = qw(Exporter);
16
17 # The primary functions of this module were originally named encode/decode.
18 # They were renamed encode_base and # decode_base as of version 1.02 so there
19 # would be less chance of them colliding with other encode/decode functions
20 # from other modules. However, since they were exported by default, it was
21 # necessary to keep them (and their default export) so as not to introduce
22 # an incompatible change. But, they can be turned off using !:old.
23 5 our @EXPORT = ( qw(encode decode) );
24
25 # use Math::BaseArith qw(:all !:old) to get encode_base/decode_base
26 # and to keep encode/decode out of the namespace.
27 5 our %EXPORT_TAGS = (
28 'all' => [
29 'encode_base',
30 'decode_base',
31 ],
32 'old' => [
33 'encode',
34 'decode',
35 ],
36 );
37
38 # use Math::BaseArith ( qw(!:old encode_base) ) to get just encode_base
39 5 our @EXPORT_OK = ( qw(
40 encode_base
41 decode_base
42 encode
43 decode
44 ));
45
46 #######################################################################
47
48 18 ▶sub encode_base {
Subroutine encode_base: called CC=14 SCAR=27.5
Pod documented
49 18 my ($value, $b_aref) = @_;
50
51 18 ▶ croak 'Function called in void context' unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 18 18
52
53 18 my @b_list = @{ $b_aref }; # copy the base value list
54 18 my @r_list;
55 18 my @radix_list = (1);
56
57 0 ▶ print {*STDERR} "encode_base($value ,[@{ $b_aref }])"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::DEBUG >= 1 0 18 18
58 if $Math::BaseArith::DEBUG >= 1;
59
60 18 my $r = 0;
61 18 my $b = 1;
62
63 # Compute the radix divisors from the base list, and put in reverse order
64 # [1760,3,12] miles/yards/feet/inches becomes [63360,5280,1760]
65 # [2,2,2,2] becomes [16,8,4,2]
66 18 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@b_list and do { do { $b *= pop @b_list; unshift @radix_list, $b } } 33 18 51
67 51 $b *= pop @b_list;
68 51 unshift @radix_list, $b;
69 } while @b_list;
70
71 18 my $i = 0;
72 18 foreach my $b (@radix_list) {
73 69 $i++;
74 69 ▶ if ($b > $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b > $value) 24 45 69
75 0 ▶ printf {*STDERR} "%10d%10d%10d%10d\n", $b,$value,$r,$value%$b
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::DEBUG >= 2 0 24 24
76 if $Math::BaseArith::DEBUG >= 2;
77 24 ▶ push @r_list, 0 if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 17 7 24
78 24 next;
79 }
80 45 ▶ $r = $b ? int($value/$b) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b ? : 38 7 45
81 0 ▶ printf {*STDERR} "%10d%10d%10d%10d\n", $b,$value,$r,$b?$value%$b:0 if $Math::BaseArith::DEBUG >= 2;
Branch: 2 branches
BranchTrueFalseTotal
if $Math::BaseArith::DEBUG >= 2 0 45 45
$b ? : 0 0 0
82 45 push @r_list, $r;
83 45 ▶ $value %= $b if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 38 7 45
84 }
85
86 18 ▶ shift @r_list while ( scalar(@r_list) > scalar( @{ $b_aref } ) );
Branch: 1 branch
BranchTrueFalseTotal
scalar @r_list > scalar @{$b_aref;} and do { shift @r_list } 11 18 29
87
88 18 ▶ return wantarray ? @r_list : \@r_list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
89 }
90
91 #######################################################################
92
93 12 ▶sub decode_base {
Subroutine decode_base: called CC=7 SCAR=19.8
Pod documented
94 12 my ($r_aref, $b_aref) = @_;
95
96 0 ▶ print {*STDERR} "decode_base( [ @{$r_aref} ],[ @{ $b_aref} ] )"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::DEBUG >= 1 0 12 12
97 if $Math::BaseArith::DEBUG >= 1;
98
99 12 ▶ if ( scalar( @{ $r_aref } ) > scalar( @{ $b_aref } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$r_aref;} > scalar @{$b_aref;} and scalar @{$b_aref;} != 1) 1 11 12
Condition100%3/3
scalar @{$r_aref;} > scalar @{$b_aref;} and scalar @{$b_aref;} != 1
!ll&&!rl&&r
1011
MC/DC100%2/2
scalar @{$r_aref;} > scalar @{$b_aref;} and scalar @{$b_aref;} != 1
scalar @{$r_aref;} > scalar @{$b_aref;} scalar @{$b_aref;} != 1
Truth table100%3/3
A and B
A= scalar @{$r_aref;} > scalar @{$b_aref;}B= scalar @{$b_aref;} != 1
ABresult
0X0
100
111
100 2 scalar( @{ $b_aref} ) != 1 )
101 {
102 1 carp 'length error';
103 1 return;
104 }
105
106 11 my $value = 0;
107 11 my $bb = 1;
108 11 my $base = 1;
109 11 my $r;
110 11 my @b_list = @{ $b_aref }; # copy the base value list
111 11 my @r_list = @{ $r_aref }; # copy the representation value list
112
113 11 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@r_list and do { do { $r = pop @r_list; $value += $r * $base; printf {*STDERR;} "%10d%10d%10d%10d\n", $r, $b, $base, $value if $Math::BaseArith::DEBUG >= 2; $bb = pop @b_list || $bb; $base *= $bb } } 28 11 39
114 39 $r = pop @r_list;
115 39 $value += $r * $base;
116 0 ▶ printf {*STDERR} "%10d%10d%10d%10d\n", $r,$b,$base,$value
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::DEBUG >= 2 0 39 39
117 if $Math::BaseArith::DEBUG >= 2;
118 39 ▶ $bb = pop @b_list || $bb;
Condition66%2/3
pop @b_list || $bb
l!l&&r!l&&!r
3360
MC/DC0%0/2
pop @b_list || $bb
pop @b_list $bb
Truth table66%2/3
A || B
A= pop @b_listB= $bb
ABresult
1X1
011
000
119 39 $base *= $bb;
120 } while @r_list;
121
122 11 return $value;
123 }
124
125 #######################################################################
126 # For signature compatibility with version < 1.02
127
128 1 ▶sub encode { encode_base(@_) }
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
129 1 ▶sub decode { decode_base(@_) }
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
130
131 #######################################################################
132
133 5 1;
134 __END__
↑ Summary