blib/lib/Math/BaseArith.pm

filter: Statementstmt 100% 65 / 65 Branchbran 71.8% 23 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 87.9% 102 / 116 SCAR 30.1
CC
20
Cov
90%
encode
14.6
decode
7.0
BEGIN
1.0
CRAP 20.4
↑ Summary
1 package Math::BaseArith;
2
3 1 ▶use 5.006;
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 integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 require Exporter;
10
11 1 our @ISA = qw(Exporter);
12
13 1 our($debug) = 0;
14
15 # Items to export into callers namespace by default. Note: do not export
16 # names by default without a very good reason. Use EXPORT_OK instead.
17 # Do not simply export all your public functions/methods/constants.
18
19 # This allows declaration use Math::BaseArith ':all';
20 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21 # will save memory.
22 1 our %EXPORT_TAGS = ( 'all' => [ qw(
23 encode
24 decode
25 $Math::BaseArith::debug
26 ) ] );
27
28 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
29
30 1 our @EXPORT = qw(
31 encode
32 decode
33 );
34 1 our $VERSION = '1.00';
35
36 #######################################################################
37
38 sub encode {
39 16 ▶ croak "Function called in void context" unless defined wantarray;
Subroutine encode: called CC=14 SCAR=26.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 16 16
40
41 16 my $value = shift; # value to be encoded
42 16 my $b_listRef = shift; # list of base values
43
44 16 my @b_list = @$b_listRef; # copy the base value list
45 16 my @r_list;
46 16 my @radix_list = (1);
47
48 16 ▶ print STDERR "encode($value ,[@$b_listRef])"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::debug >= 1 0 16 16
49 if $Math::BaseArith::debug >= 1;
50
51 16 my $r = 0;
52 16 my $b = 1;
53
54 # Compute the radix divisors from the base list, and put in reverse order
55 # [1760,3,12] miles/yards/feet/inches becomes [63360,5280,1760]
56 # [2,2,2,2] becomes [16,8,4,2]
57 16 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@b_list and do { do { $b *= pop @b_list; unshift @radix_list, $b } } 27 16 43
58 43 $b *= pop @b_list;
59 43 unshift @radix_list, $b;
60 } while @b_list;
61
62 16 my $i = 0;
63 16 foreach $b (@radix_list) {
64 59 $i++;
65 59 ▶ if ($b > $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b > $value) 16 43 59
66 16 ▶ printf STDERR "%10d%10d%10d%10d\n", $b,$value,$r,$value%$b
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::debug >= 2 0 16 16
67 if $Math::BaseArith::debug >= 2;
68 16 ▶ push @r_list, 0 if $i > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 1 11 5 16
69 16 next;
70 }
71 43 ▶ my $r = $b ? int($value/$b) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b ? : 36 7 43
72 43 ▶ 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 43 43
$b ? : 0 0 0
73 43 push @r_list, $r;
74 43 ▶ $value %= $b if $b;
Branch: 1 branch
BranchTrueFalseTotal
if $b 36 7 43
75 }
76
77 16 ▶ shift @r_list while (scalar(@r_list) > scalar(@$b_listRef));
Branch: 1 branch
BranchTrueFalseTotal
scalar @r_list > scalar @$b_listRef and do { shift @r_list } 11 16 27
78
79 16 ▶ return wantarray ? @r_list : \@r_list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 0 16
80 }
81
82 #######################################################################
83
84 sub decode {
85 10 ▶ my $r_listRef = shift; # list of representation values
Subroutine decode: called CC=7 SCAR=19.5
Pod undocumented
86 10 my $b_listRef = shift; # list of base values
87
88 10 ▶ print STDERR "decode([@$r_listRef],[@$b_listRef])"
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::debug >= 1 0 10 10
89 if $Math::BaseArith::debug >= 1;
90
91 10 ▶ if ( scalar(@$r_listRef) > scalar(@$b_listRef) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$r_listRef > scalar @$b_listRef and scalar @$b_listRef != 1) 1 9 10
Condition100%3/3
scalar @$r_listRef > scalar @$b_listRef and scalar @$b_listRef != 1
!ll&&!rl&&r
811
MC/DC100%2/2
scalar @$r_listRef > scalar @$b_listRef and scalar @$b_listRef != 1
scalar @$r_listRef > scalar @$b_listRef scalar @$b_listRef != 1
Truth table100%3/3
A and B
A= scalar @$r_listRef > scalar @$b_listRefB= scalar @$b_listRef != 1
ABresult
0X0
100
111
92 scalar(@$b_listRef) != 1 )
93 {
94 1 carp "length error";
95 1 return;
96 }
97
98 9 my $value = 0;
99 9 my $b = 1;
100 9 my $base = 1;
101 9 my $r;
102
103 9 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@$r_listRef and do { do { $r = pop @$r_listRef; $value += $r * $base; printf STDERR "%10d%10d%10d%10d\n", $r, $b, $base, $value if $Math::BaseArith::debug >= 2; $b = pop @$b_listRef || $b; $base *= $b } } 22 9 31
104 31 $r = pop @$r_listRef;
105 31 $value += $r * $base;
106 31 ▶ printf STDERR "%10d%10d%10d%10d\n", $r,$b,$base,$value
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BaseArith::debug >= 2 0 31 31
107 if $Math::BaseArith::debug >= 2;
108 31 ▶ $b = pop @$b_listRef || $b;
Condition66%2/3
pop @$b_listRef || $b
l!l&&r!l&&!r
2560
MC/DC0%0/2
pop @$b_listRef || $b
pop @$b_listRef $b
Truth table66%2/3
A || B
A= pop @$b_listRefB= $b
ABresult
1X1
011
000
109 31 $base *= $b;
110 } while @$r_listRef;
111 9 $value;
112 }
113
114 1 1;
115 __END__
↑ Summary