LCS/Tiny.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 30 / 30 Conditioncond 100% 17 / 18 (1 uncoverable) MC/DCmcdc 100% 8 / 10 (2 uncoverable) Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 120 / 123 (3 uncoverable) CC 24 SCAR 27.7
CC
24
Cov
100%
LCS
21.0
new
4.0
BEGIN
1.0
CRAP 15.9
↑ Summary LCS.pm »
1 package LCS::Tiny;
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 our $VERSION = '0.11';
7 #use utf8;
8  
9 6 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
10 6 my $class = shift;
11 # uncoverable condition false
12 6 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 3 3 6
@_ > 1 ? : 1 2 3
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
420
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
13 }
14  
15 # Implemented according to
16  
17 # J. W. Hunt and T. G. Szymanski.
18 # A fast algorithm for computing longest common subsequences.
19 # Commun. ACM, 20(5):350-353, 1977.
20  
21 24 ▶sub LCS {
Subroutine LCS: called CC=21 SCAR=30.4
Pod documented
22 24 my ($self, $a, $b) = @_;
23  
24 24 my ($amin, $amax, $bmin, $bmax) = (0, $#$a, 0, $#$b);
25  
26 24 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]) 13 24 37
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
!ll&&!rl&&r
22213
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
1135
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amin] eq $b->[$bmin]
$amin <= $amax $bmin <= $bmax $a->[$amin] eq $b->[$bmin]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amin] eq $b->[$bmin]
ABCresult
0XX0
10X0
1100
1111
27 13 $amin++;
28 13 $bmin++;
29 }
30 24 ▶ while ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]) 7 24 31
Condition100%6/6
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
!ll&&!rl&&r
6187
$amin <= $amax and $bmin <= $bmax
!ll&&!rl&&r
2425
MC/DC100%3/3
$amin <= $amax and $bmin <= $bmax and $a->[$amax] eq $b->[$bmax]
$amin <= $amax $bmin <= $bmax $a->[$amax] eq $b->[$bmax]
Truth table100%4/4
A and B and C
A= $amin <= $amaxB= $bmin <= $bmaxC= $a->[$amax] eq $b->[$bmax]
ABCresult
0XX0
10X0
1100
1111
31 7 $amax--;
32 7 $bmax--;
33 }
34  
35 24 my $bMatches;
36 24 unshift @{ $bMatches->{$b->[$_]} },$_ for $bmin..$bmax;
37  
38 24 my $matchVector = [];
39 24 my $thresh = [];
40 24 my $links = [];
41  
42 24 my ( $i, $ai, $j, $k );
43 24 ▶ for ( $i = $amin ; $i <= $amax ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $amax) 139 24 163
44 139 $ai = $a->[$i];
45 139 ▶ if ( exists( $bMatches->{$ai} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bMatches->{$ai}) 95 44 139
46 95 for $j ( @{ $bMatches->{$ai} } ) {
47 112 ▶ if ( !@$thresh || $j > $thresh->[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$thresh or $j > $thresh->[-1]) { } 83 29 112
Condition100%3/3
not @$thresh or $j > $thresh->[-1]
l!l&&r!l&&!r
146929
MC/DC100%2/2
not @$thresh or $j > $thresh->[-1]
not @$thresh $j > $thresh->[-1]
Truth table100%3/3
not(A) or B
A= not @$threshB= $j > $thresh->[-1]
ABresult
1X1
011
000
48 83 $k = $#$thresh+1;
49 83 $thresh->[$k] = $j;
50 }
51 #elsif ( $k and $thresh->[$k] > $j and $thresh->[ $k - 1 ] < $j ) {
52 # $thresh->[$k] = $j;
53 #}
54 else {
55 # binary search for insertion point
56 29 $k = 0;
57 29 my $index;
58 my $found;
59 29 my $high = $#$thresh;
60 29 ▶ while ( $k <= $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $high) 48 23 71
61 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 48 $index = ( $high + $k ) / 2;
63 #$index = int(( $high + $k ) / 2); # without 'use integer'
64 48 $found = $thresh->[$index];
65  
66 48 ▶ if ( $j == $found ) { $k = undef; last; }
Branch: 2 branches
BranchTrueFalseTotal
if ($j == $found) { } 6 42 48
elsif ($j > $found) { } 16 26 42
67 16 elsif ( $j > $found ) { $k = $index + 1; }
68 26 else { $high = $index - 1; }
69 }
70 # now insertion point is in $k.
71 29 ▶ $thresh->[$k] = $j if (defined $k); # overwrite next larger
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 23 6 29
72 }
73 112 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) 106 6 112
74 106 ▶ $links->[$k] = [ ( $k ? $links->[ $k - 1 ] : undef ), $i, $j ];
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 82 24 106
75 }
76 }
77 }
78 }
79 24 ▶ if (@$thresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$thresh) 14 10 24
80 14 ▶ for ( my $link = $links->[$#$thresh] ; $link ; $link = $link->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link) 83 14 97
81 83 unshift @$matchVector,[$link->[1],$link->[2]];
82 }
83 }
84 24 return [ map([$_ => $_], 0 .. ($bmin-1)),
85 @$matchVector,
86 map([++$amax => $_], ($bmax+1) .. $#$b) ];
87 }
88  
89 1 1;
90  
91 __END__
↑ Summary LCS.pm »