blib/lib/Algorithm/C3.pm

filter: Statementstmt 100% 71 / 71 Branchbran 100% 32 / 32 Conditioncond 100% 16 / 16 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 134 / 134 CC 26 SCAR 31.5
CC
26
Cov
100%
merge
26.0
BEGIN
1.0
BEGIN
1.0
CRAP 23.4
↑ Summary
1 package Algorithm::C3;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our $VERSION = '0.11';
9  
10 66 ▶sub merge {
Subroutine merge: called CC=26 SCAR=32.6
Pod documented
11 66 my ($root, $parent_fetcher, $cache) = @_;
12  
13 66 ▶ $cache ||= {};
Condition100%2/2
$cache ||= {}
l!l
2244
MC/DC100%1/1
$cache ||= {}
$cache
Truth table100%2/2
A
A= $cache
Aresult
11
00
14  
15 66 my @STACK; # stack for simulating recursion
16  
17 66 my $pfetcher_is_coderef = ref($parent_fetcher) eq 'CODE';
18  
19 66 ▶ unless ($pfetcher_is_coderef or $root->can($parent_fetcher)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pfetcher_is_coderef or $root->can($parent_fetcher)) 1 65 66
Condition100%3/3
$pfetcher_is_coderef or $root->can($parent_fetcher)
l!l&&r!l&&!r
55101
MC/DC100%2/2
$pfetcher_is_coderef or $root->can($parent_fetcher)
$pfetcher_is_coderef $root->can($parent_fetcher)
Truth table100%3/3
A or B
A= $pfetcher_is_coderefB= $root->can($parent_fetcher)
ABresult
1X1
011
000
20 1 confess "Could not find method $parent_fetcher in $root";
21 }
22  
23 65 my $current_root = $root;
24 65 my $current_parents = [ $root->$parent_fetcher ];
25 65 my $recurse_mergeout = [];
26 65 my $i = 0;
27 65 my %seen = ( $root => 1 );
28  
29 65 my ($new_root, $mergeout, %tails);
30 65 while(1) {
31 794 ▶ if($i < @$current_parents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$current_parents) 387 407 794
32 387 $new_root = $current_parents->[$i++];
33  
34 387 ▶ if($seen{$new_root}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$new_root}) 8 379 387
35 8 my @isastack;
36 my $reached;
37 8 ▶ for(my $i = 0; $i < $#STACK; $i += 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#STACK) 27 8 35
38 27 ▶ if($reached || ($reached = ($STACK[$i] eq $new_root))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reached or $reached = $STACK[$i] eq $new_root) 14 13 27
Condition100%3/3
$reached or $reached = $STACK[$i] eq $new_root
l!l&&r!l&&!r
8613
MC/DC100%2/2
$reached or $reached = $STACK[$i] eq $new_root
$reached $reached = $STACK[$i] eq $new_root
Truth table100%3/3
A or B
A= $reachedB= $reached = $STACK[$i] eq $new_root
ABresult
1X1
011
000
39 14 push(@isastack, $STACK[$i]);
40 }
41 }
42 8 my $isastack = join(q{ -> }, @isastack, $current_root, $new_root);
43 8 die "Infinite loop detected in parents of '$root': $isastack";
44 }
45 379 $seen{$new_root} = 1;
46  
47 379 ▶ unless ($pfetcher_is_coderef or $new_root->can($parent_fetcher)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pfetcher_is_coderef or $new_root->can($parent_fetcher)) 1 378 379
Condition100%3/3
$pfetcher_is_coderef or $new_root->can($parent_fetcher)
l!l&&r!l&&!r
335431
MC/DC100%2/2
$pfetcher_is_coderef or $new_root->can($parent_fetcher)
$pfetcher_is_coderef $new_root->can($parent_fetcher)
Truth table100%3/3
A or B
A= $pfetcher_is_coderefB= $new_root->can($parent_fetcher)
ABresult
1X1
011
000
48 1 confess "Could not find method $parent_fetcher in $new_root";
49 }
50  
51 378 push(@STACK, $current_root, $current_parents, $recurse_mergeout, $i);
52  
53 378 $current_root = $new_root;
54 378 ▶ $current_parents = $cache->{pfetch}->{$current_root} ||= [ $current_root->$parent_fetcher ];
Condition100%2/2
$cache->{"pfetch"}{$current_root} ||= [$current_root->$parent_fetcher]
l!l
166212
MC/DC100%1/1
$cache->{"pfetch"}{$current_root} ||= [$current_root->$parent_fetcher]
$cache->{"pfetch"}{$current_root}
Truth table100%2/2
A
A= $cache->{"pfetch"}{$current_root}
Aresult
11
00
55 378 $recurse_mergeout = [];
56 378 $i = 0;
57 378 next;
58 }
59  
60 407 $seen{$current_root} = 0;
61  
62 407 ▶ $mergeout = $cache->{merge}->{$current_root} ||= do {
Condition100%3/3
$cache->{"merge"}{$current_root} ||= do { my(@seqs) = map({[@$_];} @$recurse_mergeout); push @seqs, [@$current_parents] if @$current_parents; foreach my $seq (@seqs) { ++$tails{$seq->[$_]} foreach (1 .. $seq->$#*); } ; my(@res) = $current_root; while (1) { my($cand, $winner); foreach $_ (@seqs) { next unless @$_; if (not $winner) { $cand = $_->[0]; next if $tails{$cand}; push @res, $winner = $cand; shift @$_; --$tails{$_->[0]} if @$_; } elsif ($_->[0] eq $winner) { shift @$_; --$tails{$_->[0]} if @$_; } ; } ; last unless $cand; die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner; } ; \@res }
l!l&&r!l&&!r
1862201
MC/DC100%2/2
$cache->{"merge"}{$current_root} ||= do { my(@seqs) = map({[@$_];} @$recurse_mergeout); push @seqs, [@$current_parents] if @$current_parents; foreach my $seq (@seqs) { ++$tails{$seq->[$_]} foreach (1 .. $seq->$#*); } ; my(@res) = $current_root; while (1) { my($cand, $winner); foreach $_ (@seqs) { next unless @$_; if (not $winner) { $cand = $_->[0]; next if $tails{$cand}; push @res, $winner = $cand; shift @$_; --$tails{$_->[0]} if @$_; } elsif ($_->[0] eq $winner) { shift @$_; --$tails{$_->[0]} if @$_; } ; } ; last unless $cand; die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner; } ; \@res }
$cache->{"merge"}{$current_root} do { my(@seqs) = map({[@$_];} @$recurse_mergeout); push @seqs, [@$current_parents] if @$current_parents; foreach my $seq (@seqs) { ++$tails{$seq->[$_]} foreach (1 .. $seq->$#*); } ; my(@res) = $current_root; while (1) { my($cand, $winner); foreach $_ (@seqs) { next unless @$_; if (not $winner) { $cand = $_->[0]; next if $tails{$cand}; push @res, $winner = $cand; shift @$_; --$tails{$_->[0]} if @$_; } elsif ($_->[0] eq $winner) { shift @$_; --$tails{$_->[0]} if @$_; } ; } ; last unless $cand; die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner; } ; \@res }
Truth table100%3/3
A ||= B
A= $cache->{"merge"}{$current_root}B= do { my(@seqs) = map({[@$_];} @$recurse_mergeout); push @seqs, [@$current_parents] if @$current_parents; foreach my $seq (@seqs) { ++$tails{$seq->[$_]} foreach (1 .. $seq->$#*); } ; my(@res) = $current_root; while (1) { my($cand, $winner); foreach $_ (@seqs) { next unless @$_; if (not $winner) { $cand = $_->[0]; next if $tails{$cand}; push @res, $winner = $cand; shift @$_; --$tails{$_->[0]} if @$_; } elsif ($_->[0] eq $winner) { shift @$_; --$tails{$_->[0]} if @$_; } ; } ; last unless $cand; die q[Inconsistent hierarchy found while merging '] . $current_root . "':\n\t" . "current merge results [\n\t\t" . join(",\n\t\t", @res) . "\n\t]\n\t" . "merging failed on '${cand}'\n" unless $winner; } ; \@res }
ABresult
1X1
011
000
63  
64 # This do-block is the code formerly known as the function
65 # that was a perl-port of the python code at
66 # http://www.python.org/2.3/mro.html :)
67  
68 # Initial set (make sure everything is copied - it will be modded)
69 221 my @seqs = map { [@$_] } @$recurse_mergeout;
70 221 ▶ push(@seqs, [@$current_parents]) if @$current_parents;
Branch: 1 branch
BranchTrueFalseTotal
if @$current_parents 129 92 221
71  
72 # Construct the tail-checking hash (actually, it's cheaper and still
73 # correct to re-use it throughout this function)
74 221 foreach my $seq (@seqs) {
75 339 $tails{$seq->[$_]}++ for (1..$#$seq);
76 }
77  
78 221 my @res = ( $current_root );
79 221 while (1) {
80 750 my $cand;
81 my $winner;
82 750 foreach (@seqs) {
83 1879 ▶ next if !@$_;
Branch: 1 branch
BranchTrueFalseTotal
unless @$_ 750 1129 1879
84 1129 ▶ if(!$winner) { # looking for a winner
Branch: 2 branches
BranchTrueFalseTotal
if (not $winner) { } 601 528 1129
elsif ($_->[0] eq $winner) { } 296 232 528
85 601 $cand = $_->[0]; # seq head is candidate
86 601 ▶ next if $tails{$cand}; # he loses if in %tails
Branch: 1 branch
BranchTrueFalseTotal
if $tails{$cand} 72 529 601
87  
88 # Handy warn to give a output like the ones on
89 # http://www.python.org/download/releases/2.3/mro/
90 #warn " = " . join(' + ', @res) . " + merge([" . join('] [', map { join(', ', @$_) } grep { @$_ } @seqs) . "])\n";
91 529 push @res => $winner = $cand;
92 529 shift @$_; # strip off our winner
93 529 ▶ $tails{$_->[0]}-- if @$_; # keep %tails sane
Branch: 1 branch
BranchTrueFalseTotal
if @$_ 350 179 529
94 }
95 elsif($_->[0] eq $winner) {
96 296 shift @$_; # strip off our winner
97 296 ▶ $tails{$_->[0]}-- if @$_; # keep %tails sane
Branch: 1 branch
BranchTrueFalseTotal
if @$_ 138 158 296
98 }
99 }
100  
101 # Handy warn to give a output like the ones on
102 # http://www.python.org/download/releases/2.3/mro/
103 #warn " = " . join(' + ', @res) . "\n" if !$cand;
104  
105 750 ▶ last if !$cand;
Branch: 1 branch
BranchTrueFalseTotal
unless $cand 220 530 750
106 530 ▶ die q{Inconsistent hierarchy found while merging '}
Branch: 1 branch
BranchTrueFalseTotal
unless $winner 1 529 530
107 . $current_root . qq{':\n\t}
108 . qq{current merge results [\n\t\t}
109 . (join ",\n\t\t" => @res)
110 . qq{\n\t]\n\t} . qq{merging failed on '$cand'\n}
111 if !$winner;
112 }
113 220 \@res;
114 };
115  
116 406 ▶ return @$mergeout if !@STACK;
Branch: 1 branch
BranchTrueFalseTotal
unless @STACK 55 351 406
117  
118 351 $i = pop(@STACK);
119 351 $recurse_mergeout = pop(@STACK);
120 351 $current_parents = pop(@STACK);
121 351 $current_root = pop(@STACK);
122  
123 351 push(@$recurse_mergeout, $mergeout);
124 }
125 }
126  
127 11 1;
128  
129 __END__
↑ Summary