Maker/Random.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 87.5% 14 / 16 Conditioncond 55.5% 10 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 3 totaltotal 81.0% 94 / 116 CC 19 SCAR 18.6
CC
19
Cov
88%
init
12.3
erdos
6.0
random
5.0
CRAP 6.4
« Maker/Empty.pm ↑ Summary Maker/Complete.pm »
1 package Graph::Maker::Random;
2
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base qw/Graph::Maker/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Graph::Maker::Complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Graph::Maker::Utils qw/is_valid_degree_seq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Math::Random qw/random_uniform_integer random_uniform/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 2 our $VERSION = '0.01';
13
14 sub init
15 4 ▶{
Subroutine init: called CC=10 SCAR=25.1
Pod undocumented
16 4 my ($self, %params) = @_;
17
18 4 ▶ my $n = delete($params{N}) || 0;
Condition50%1/2
delete $params{'N'} || 0
l!l
40
MC/DC0%0/1
delete $params{'N'} || 0
delete $params{'N'}
Truth table50%1/2
A
A= delete $params{'N'}
Aresult
11
00
19 4 ▶ my $m = delete($params{M}) || 0;
Condition100%2/2
delete $params{'M'} || 0
l!l
22
MC/DC100%1/1
delete $params{'M'} || 0
delete $params{'M'}
Truth table100%2/2
A
A= delete $params{'M'}
Aresult
11
00
20 4 ▶ my $p = delete($params{PR})|| 0;
Condition100%2/2
delete $params{'PR'} || 0
l!l
22
MC/DC100%1/1
delete $params{'PR'} || 0
delete $params{'PR'}
Truth table100%2/2
A
A= delete $params{'PR'}
Aresult
11
00
21 4 my $gm = delete($params{graph_maker});
22 4 ▶ croak "graph_maker must be a reference to a function that creates a Graph.\n" if $gm && ref($gm) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $gm and ref $gm ne "CODE" 0 4 4
Condition33%1/3
$gm and ref $gm ne "CODE"
!ll&&!rl&&r
400
MC/DC0%0/2
$gm and ref $gm ne "CODE"
$gm ref $gm ne "CODE"
Truth table33%1/3
A and B
A= $gmB= ref $gm ne "CODE"
ABresult
0X0
100
111
23 4 ▶ $gm ||= sub { new Graph(@_); };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$gm ||= sub { "Graph"->new(@_); }
l!l&&r!l&&!r
040
MC/DC0%0/2
$gm ||= sub { "Graph"->new(@_); }
$gm sub { "Graph"->new(@_); }
Truth table33%1/3
A ||= B
A= $gmB= sub { "Graph"->new(@_); }
ABresult
1X1
011
000
24
25 4 ▶ if($p == 1 || $m >= $n*($n-1)/2)
Branch: 1 branch
BranchTrueFalseTotal
if ($p == 1 or $m >= $n * ($n - 1) / 2) 0 4 4
Condition33%1/3
$p == 1 or $m >= $n * ($n - 1) / 2
l!l&&r!l&&!r
004
MC/DC0%0/2
$p == 1 or $m >= $n * ($n - 1) / 2
$p == 1 $m >= $n * ($n - 1) / 2
Truth table33%1/3
A or B
A= $p == 1B= $m >= $n * ($n - 1) / 2
ABresult
1X1
011
000
26 {
27 0 return new Graph::Maker('complete', N => $n, graph_maker => $gm, %params);
28 }
29
30 4 my $g = $gm->(%params);
31 4 $g->add_vertices(1..$n);
32
33 4 ▶ return erdos($g, $n, $p) if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 2 2 4
34 2 return random($g, $n, $m);
35 }
36
37 sub erdos
38 2 ▶{
Subroutine erdos: called CC=6 SCAR=17.9
Pod undocumented
39 2 my ($g, $n, $p, $m) = @_;
40
41 2 for my $u(1..$n)
42 {
43 200 for my $v(1..$n)
44 {
45 20000 ▶ next if $u == $v;
Branch: 1 branch
BranchTrueFalseTotal
if $u == $v 200 19800 20000
46 19800 ▶ if(random_uniform() < $p)
Branch: 1 branch
BranchTrueFalseTotal
if (random_uniform() < $p) 2144 17656 19800
47 {
48 2144 $g->add_edge($u, $v);
49 2144 ▶ $g->add_edge($v, $u) if $g->is_directed();
Branch: 1 branch
BranchTrueFalseTotal
if $g->is_directed 1078 1066 2144
50 }
51 }
52 }
53 2 return $g;
54 }
55
56 sub random
57 2 ▶{
Subroutine random: called CC=5 SCAR=16.1
Pod undocumented
58 2 my ($g, $n, $m) = @_;
59
60 2 for (1..$m)
61 {
62 152 my @e = random_uniform_integer(2, 1, $n);
63 152 ▶ redo if $e[0] == $e[1] || $g->has_edge(@e);
Branch: 1 branch
BranchTrueFalseTotal
if $e[0] == $e[1] or $g->has_edge(@e) 2 150 152
Condition66%2/3
$e[0] == $e[1] or $g->has_edge(@e)
l!l&&r!l&&!r
20150
MC/DC50%1/2
$e[0] == $e[1] or $g->has_edge(@e)
$e[0] == $e[1] $g->has_edge(@e)
Truth table66%2/3
A or B
A= $e[0] == $e[1]B= $g->has_edge(@e)
ABresult
1X1
011
000
64 150 $g->add_edge(@e);
65 150 ▶ $g->add_edge(reverse @e) unless $g->is_undirected();
Branch: 1 branch
BranchTrueFalseTotal
unless $g->is_undirected 75 75 150
66 }
67 2 return $g;
68 }
69
70 2 Graph::Maker->add_factory_type( 'random' => __PACKAGE__ );
71
72 2 1;
73
74 __DATA__
« Maker/Empty.pm ↑ Summary Maker/Complete.pm »