blib/lib/Graph/RandomPath.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 87.5% 7 / 8 Conditioncond 81.8% 18 / 22 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 89.9% 89 / 99 CC 14 SCAR 17.7
CC
14
Cov
92%
create_generator
9.1
__ANON__
6.0
BEGIN
1.0
CRAP 5.9
↑ Summary
1 package Graph::RandomPath;
2
3 3 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 3 our $VERSION = '0.01';
11
12 3 our %EXPORT_TAGS = ( 'all' => [ qw(
13
14 ) ] );
15
16 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
17
18 3 our @EXPORT = qw(
19 );
20
21 40 ▶sub create_generator {
Subroutine create_generator: called CC=9 SCAR=22.1
Pod documented
22 40 my ($class, $g, $src, $dst, %opt) = @_;
23
24 40 ▶ $opt{max_length} //= 64;
Condition50%1/2
$opt{"max_length"} //= 64
l!l
400
MC/DC0%0/1
$opt{"max_length"} //= 64
$opt{"max_length"}
Truth table50%1/2
A
A= $opt{"max_length"}
Aresult
11
00
25
26 40 my %to_src = map { $_ => 1 } $src, $g->all_successors($src);
27 40 my %to_dst = map { $_ => 1 } $dst, $g->all_predecessors($dst);
28
29 40 my $copy = $g->new;
30 40 $copy->set_edge_weight($_->[1], $_->[0], 1) for grep {
31 $to_src{$_->[0]} and $to_src{$_->[1]} and
32 7459 ▶ $to_dst{$_->[0]} and $to_dst{$_->[1]}
Condition77%7/9
$to_src{$_->[0]} && $to_src{$_->[1]} && $to_dst{$_->[0]} && $to_dst{$_->[1]}
!ll&&!rl&&r
8597392
$to_src{$_->[0]} and $to_src{$_->[1]} and $to_dst{$_->[0]}
!ll&&!rl&&r
807451
$to_src{$_->[0]} and $to_src{$_->[1]}
!ll&&!rl&&r
807451
MC/DC50%2/4
$to_src{$_->[0]} && $to_src{$_->[1]} && $to_dst{$_->[0]} && $to_dst{$_->[1]}
$to_src{$_->[0]} $to_src{$_->[1]} $to_dst{$_->[0]} $to_dst{$_->[1]}
Truth table60%3/5
A and B and C && D
A= $to_src{$_->[0]}B= $to_src{$_->[1]}C= $to_dst{$_->[0]}D= $to_dst{$_->[1]}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
33 } $g->edges;
34
35 40 my $sptg;
36
37 40 eval {
38 40 $sptg = $copy->SPT_Dijkstra($dst);
39 };
40
41 40 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 40 40
42 # This is here in case the module is updated to allow user-
43 # supplied weights for the edges, which might then be nega-
44 # tive and require a different shortest path algorithm.
45 0 $sptg = $copy->SPT_Bellman_Ford($dst);
46 }
47
48 Carp::confess "Unable to generate paths for these parameters" unless
49 (defined $sptg->get_vertex_attribute($src, 'weight') and
50 40 ▶ $sptg->get_vertex_attribute($src, 'weight') < $opt{max_length});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sptg->get_vertex_attribute($src, "weight") and $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"} 5 35 40
Condition66%2/3
defined $sptg->get_vertex_attribute($src, "weight") and $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
!ll&&!rl&&r
5035
MC/DC50%1/2
defined $sptg->get_vertex_attribute($src, "weight") and $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
defined $sptg->get_vertex_attribute($src, "weight") $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
Truth table66%2/3
A and B
A= defined $sptg->get_vertex_attribute($src, "weight")B= $sptg->get_vertex_attribute($src, "weight") < $opt{"max_length"}
ABresult
0X0
100
111
51
52 350 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
53 350 my @path = ($src);
54 350 my $target = rand($opt{max_length});
55 350 while (1) {
56 11478 my $v = $copy->random_predecessor($path[-1]);
57
58 11478 ▶ last if $path[-1] eq $dst and
Branch: 1 branch
BranchTrueFalseTotal
if $path[-1] eq $dst and !defined($v) || @path > $target 350 11128 11478
Condition100%6/6
$path[-1] eq $dst and !defined($v) || @path > $target
!ll&&!rl&&r
10709419350
not defined $v or @path > $target
l!l&&r!l&&!r
30320419
MC/DC100%3/3
$path[-1] eq $dst and !defined($v) || @path > $target
$path[-1] eq $dst not defined $v @path > $target
Truth table100%4/4
A and not(B) or C
A= $path[-1] eq $dstB= not defined $vC= @path > $target
ABCresult
0XX0
1000
11X1
1011
59 (not defined $v or @path > $target);
60
61 11128 ▶ my $w = $sptg->get_vertex_attribute($v, 'weight') // 0;
Condition100%2/2
$sptg->get_vertex_attribute($v, "weight") // 0
l!l
10359769
MC/DC100%1/1
$sptg->get_vertex_attribute($v, "weight") // 0
$sptg->get_vertex_attribute($v, "weight")
Truth table100%2/2
A
A= $sptg->get_vertex_attribute($v, "weight")
Aresult
11
00
62
63 11128 ▶ if (@path + $w > $opt{max_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path + $w > $opt{"max_length"}) 3427 7701 11128
64 3427 my $v = $sptg->get_vertex_attribute($v, 'p');
65 };
66
67 11128 push @path, $v;
68 }
69 350 @path;
70 }
71 35 }
72
73 3 1;
74
75 __END__
↑ Summary