blib/lib/Text/Median.pm

filter: Statementstmt 48.1% 38 / 79 Branchbran 21.4% 6 / 28 Conditioncond 16.6% 2 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 71.4% 5 / 7 Podpod 100% 3 / 3 totaltotal 40.1% 55 / 137 CC 21 SCAR 47.2
CC
21
Cov
39%
find_median
210.0
add_data
12.0
new
6.0
CRAP 111.7
↑ Summary
1 package Text::Median;
2  
3 1 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Module::Runtime qw( require_module is_valid_module_name );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use Text::Median ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 1 our %EXPORT_TAGS = ( 'all' => [ qw(
21
22 ) ] );
23  
24 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25  
26 1 our @EXPORT = qw(
27
28 );
29  
30 1 our $VERSION = '0.02';
31  
32 4 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
33  
34 4 my $class = shift;
35 4 my $self = {};
36  
37 4 bless $self, $class;
38  
39 4 my %arguments = @_;
40  
41 4 foreach my $el (keys %arguments) {
42 6 $self->{$el} = $arguments{$el};
43 }
44  
45 4 ▶ if (!defined $self->{module} || !defined $self->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"module"} and defined $self->{"method"}) 1 3 4
Condition66%2/3
defined $self->{"module"} and defined $self->{"method"}
!ll&&!rl&&r
103
MC/DC50%1/2
defined $self->{"module"} and defined $self->{"method"}
defined $self->{"module"} defined $self->{"method"}
Truth table66%2/3
A and B
A= defined $self->{"module"}B= defined $self->{"method"}
ABresult
0X0
100
111
46 1 carp "Both a module and a method are required.\n";
47 1 return undef;
48 }
49  
50 3 ▶ if (!is_valid_module_name($self->{module})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_valid_module_name($self->{"module"})) 1 2 3
51 1 carp $self->{module}." is not a valid module name.\n";
52 1 return undef;
53 }
54  
55 2 eval {
56 2 require_module($self->{module});
57 };
58  
59 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
60 1 carp "Having a problem using that module, is it in the correct path?\n";
61 1 return undef;
62 }
63  
64 1 $self->{distancemethod} = $self->{module}."::".$self->{method};
65  
66 1 return $self;
67
68 }
69  
70 0 ▶sub add_data {
Subroutine add_data: not called CC=3 SCAR=24.8
Pod documented
71  
72 0 my $self = shift;
73 0 my @data = @_;
74  
75 0 my $count = 0;
76 0 ▶ if (defined $self->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"data"}) 0 0 0
77 0 $count = scalar(keys %{$self->{data}});
78 }
79 0 foreach my $el (@data) {
80 0 $self->{data}->{$count} = $el;
81 0 $count++;
82 }
83  
84 0 return 1;
85 }
86  
87 0 ▶sub find_median {
Subroutine find_median: not called CC=14 SCAR=53.5
Pod documented
88 0 my $self = shift;
89  
90 0 ▶ if (!defined $self->{data} || !scalar(keys %{$self->{data}}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data"} and scalar keys %{$$self{"data"};}) 0 0 0
Condition0%0/3
defined $self->{"data"} and scalar keys %{$$self{"data"};}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"data"} and scalar keys %{$$self{"data"};}
defined $self->{"data"} scalar keys %{$$self{"data"};}
Truth table0%0/3
A and B
A= defined $self->{"data"}B= scalar keys %{$$self{"data"};}
ABresult
0X0
100
111
91 0 carp "You must have data to determine the median.\n";
92 0 return 1;
93 }
94  
95 0 my $sum = 0;
96 0 my $minsum;
97 0 my $min = -1;
98  
99 0 my $method = \&{$self->{distancemethod}};
100 0 ▶ for (my $i = 0; $i < scalar(keys %{$self->{data}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar keys %{$$self{"data"};}) 0 0 0
101 0 ▶ for (my $j = 0; $j < scalar(keys %{$self->{data}}); $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar keys %{$$self{"data"};}) 0 0 0
102 0 ▶ next if ($i == $j);
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 0 0 0
103 0 ▶ if ($i < $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $j) { } 0 0 0
104 0 ▶ if (!defined $self->{matrix}->{$i}->{$j}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"matrix"}{$i}{$j}) 0 0 0
105 0 $self->{matrix}->{$i}->{$j} = $method->($self->{data}->{$i},$self->{data}->{$j});
106 }
107 0 $sum += $self->{matrix}->{$i}->{$j};
108 }
109 else {
110 0 ▶ if (!defined $self->{matrix}->{$j}->{$j}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"matrix"}{$j}{$j}) 0 0 0
111 0 $self->{matrix}->{$j}->{$i} = $method->($self->{data}->{$j},$self->{data}->{$i});
112 }
113 0 $sum += $self->{matrix}->{$j}->{$i};
114 }
115 }
116 0 ▶ if (defined $self->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'max'}) { } 0 0 0
117 0 ▶ if (!defined $minsum || $sum > $minsum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $minsum or $sum > $minsum) 0 0 0
Condition0%0/3
not defined $minsum or $sum > $minsum
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $minsum or $sum > $minsum
not defined $minsum $sum > $minsum
Truth table0%0/3
not(A) or B
A= not defined $minsumB= $sum > $minsum
ABresult
1X1
011
000
118 0 $minsum = $sum;
119 0 $min = $i;
120 }
121 }
122 else {
123 0 ▶ if (!defined $minsum || $sum < $minsum) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $minsum or $sum < $minsum) 0 0 0
Condition0%0/3
not defined $minsum or $sum < $minsum
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $minsum or $sum < $minsum
not defined $minsum $sum < $minsum
Truth table0%0/3
not(A) or B
A= not defined $minsumB= $sum < $minsum
ABresult
1X1
011
000
124 0 $minsum = $sum;
125 0 $min = $i;
126 }
127 }
128 0 $sum = 0;
129 }
130  
131 0 return $self->{data}->{$min};
132  
133 }
134  
135  
136 # Preloaded methods go here.
137  
138 1 1;
139 __END__
↑ Summary