Util/Range.pm

filter: Statementstmt 89.0% 98 / 110 Branchbran 71.8% 23 / 32 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 75.0% 12 / 16 Podpod 100% 10 / 10 totaltotal 80.3% 151 / 188 CC 21 SCAR 20.2
CC
21
Cov
82%
_spaceship
20.0
new
12.3
inner_bounds
4.0
CRAP 7.5
« Util.pm ↑ Summary Util/Collection.pm »
1 package Net::IPAddress::Util::Range;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 '""' => 'as_string',
9 '<=>' => '_spaceship',
10 'cmp' => '_spaceship',
11 5 );
12  
13 5 ▶use Net::IPAddress::Util qw( :constr :manip );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 require Net::IPAddress::Util::Collection;
15  
16 5 our $VERSION = '5.001';
17  
18 222 ▶sub new {
Subroutine new: called CC=12 SCAR=25.1
Pod documented
19 222 my $class = shift;
20 222 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
02220
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
21 222 my ($arg_ref) = @_;
22 222 my ($l, $u);
23 222 ▶ if ($arg_ref->{ lower } && $arg_ref->{ upper }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_ref->{'lower'} and $arg_ref->{'upper'}) { } 112 110 222
elsif ($arg_ref->{'ip'}) { } 110 0 110
Condition66%2/3
$arg_ref->{"lower"} and $arg_ref->{"upper"}
!ll&&!rl&&r
1101120
MC/DC0%0/2
$arg_ref->{"lower"} and $arg_ref->{"upper"}
$arg_ref->{"lower"} $arg_ref->{"upper"}
Truth table33%1/3
A and B
A= $arg_ref->{"lower"}B= $arg_ref->{"upper"}
ABresult
0X0
100
111
24 112 $arg_ref->{ lower } = IP($arg_ref->{ lower });
25 112 $arg_ref->{ upper } = IP($arg_ref->{ upper });
26 112 ▶ if ($arg_ref->{ lower } > $arg_ref->{ upper }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"lower"} > $arg_ref->{"upper"}) 0 112 112
27 0 ($arg_ref->{ lower }, $arg_ref->{ upper }) = ($arg_ref->{ upper }, $arg_ref->{ lower });
28 }
29 112 return bless $arg_ref => $class;
30 }
31 elsif ($arg_ref->{ ip }) {
32 110 my $ip;
33 110 my $nm = 2;
34 110 ▶ if ($arg_ref->{ netmask }) {
Branch: 3 branches
BranchTrueFalseTotal
if ($arg_ref->{'netmask'}) { } 2 108 110
elsif ($arg_ref->{'ip'} =~ m[(.*?)/(\d+)]u) { } 8 100 108
elsif ($arg_ref->{'cidr'}) { } 98 2 100
35 2 $ip = IP($arg_ref->{ ip });
36 2 my $was_ipv4 = $ip->is_ipv4;
37 2 $nm = IP($arg_ref->{ netmask });
38 2 $ip &= $nm;
39 2 $nm = ~$nm;
40 2 ▶ if ($was_ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_ipv4) 2 0 2
41 2 $nm &= ipv4_mask();
42 }
43 2 $l = $ip;
44 2 $u = $ip | $nm;
45 }
46 elsif ($arg_ref->{ ip } =~ m{(.*?)/(\d+)}) {
47 8 my ($t, $cidr) = ($1, $2);
48 8 $ip = IP($t);
49 8 my $was_ipv4 = $ip->is_ipv4;
50 8 ▶ my $span
Branch: 1 branch
BranchTrueFalseTotal
$was_ipv4 ? : 4 4 8
51 = ($was_ipv4
52 ? 32
53 : 128) - $cidr
54 ;
55 8 $nm = implode_ip(substr(('1' x 128) . ('0' x $span), -128));
56 8 $ip &= $nm;
57 8 $l = $ip;
58 8 $u = $ip | ~$nm;
59 }
60 elsif ($arg_ref->{ cidr }) {
61 98 $ip = IP($arg_ref->{ ip });
62 98 my $was_ipv4 = $ip->is_ipv4;
63 98 my $cidr = $arg_ref->{ cidr };
64 98 ▶ my $span
Branch: 1 branch
BranchTrueFalseTotal
$was_ipv4 ? : 98 0 98
65 = ($was_ipv4
66 ? 32
67 : 128) - $cidr
68 ;
69 98 $nm = implode_ip(substr(('1' x 128) . ('0' x $span), -128));
70 98 $ip &= $nm;
71 98 $l = $ip;
72 98 $u = $ip | ~$nm;
73 }
74 else {
75 2 $l = IP($arg_ref->{ ip });
76 2 $u = IP($arg_ref->{ ip });
77 }
78 }
79 110 return bless { lower => $l, upper => $u } => $class;
80 }
81  
82 42 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
83 42 my $self = shift;
84 42 return "($self->{ lower } .. $self->{ upper })";
85 }
86  
87 174 ▶sub outer_bounds {
Subroutine outer_bounds: called CC=3 SCAR=11.0
Pod documented
88 174 my $self = shift;
89 174 my @l = explode_ip($self->{ lower });
90 174 my @u = explode_ip($self->{ upper });
91 174 my @cidr = common_prefix(@l, @u);
92 174 my $cidr = scalar @cidr;
93 174 my $base = implode_ip(ip_pad_prefix(@cidr));
94 174 ▶ if ($base->is_ipv4()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->is_ipv4) 172 2 174
95 172 $cidr -= 96;
96 }
97 174 my @mask = prefix_mask(@l, @u);
98 174 my $nm = implode_ip(ip_pad_prefix(@mask));
99 174 my $x = ~$nm;
100 174 ▶ if ($base->is_ipv4()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->is_ipv4) 172 2 174
101 172 $nm &= ipv4_mask();
102 }
103 174 my $hi = IP($base);
104 174 $hi |= $x;
105 174 return bless {
106 lower => $base,
107 cidr => $cidr,
108 netmask => $nm,
109 upper => $hi,
110 } => ref($self);
111 }
112  
113 36 ▶sub inner_bounds {
Subroutine inner_bounds: called CC=4 SCAR=13.9
Pod documented
114 36 my $self = shift;
115 36 ▶ return $self if $self->{ upper } == $self->{ lower };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"upper"} == $self->{"lower"} 0 36 36
116 36 my $bounds = $self->outer_bounds();
117 36 my $new = ref($self)->new($self);
118 36 ▶ while ($bounds->{ upper } > $self->{ upper } or $bounds->{ lower } < $self->{ lower }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bounds->{"upper"} > $self->{"upper"} or $bounds->{"lower"} < $self->{"lower"}) 96 36 132
Condition100%3/3
$bounds->{"upper"} > $self->{"upper"} or $bounds->{"lower"} < $self->{"lower"}
l!l&&r!l&&!r
326436
MC/DC100%2/2
$bounds->{"upper"} > $self->{"upper"} or $bounds->{"lower"} < $self->{"lower"}
$bounds->{"upper"} > $self->{"upper"} $bounds->{"lower"} < $self->{"lower"}
Truth table100%3/3
A or B
A= $bounds->{"upper"} > $self->{"upper"}B= $bounds->{"lower"} < $self->{"lower"}
ABresult
1X1
011
000
119 96 $new = ref($self)->new({ ip => $self->{ lower }, cidr => $bounds->{ cidr } + 1 });
120 96 $bounds = $new->outer_bounds();
121 }
122 36 return $new;
123 }
124  
125 18 ▶sub as_cidr {
Subroutine as_cidr: called CC=1 SCAR=0.0
Pod documented
126 18 my $self = shift;
127 18 my $hr = $self->outer_bounds();
128 18 return "$hr->{ lower }" . '/' . "$hr->{ cidr }";
129 }
130  
131 18 ▶sub as_netmask {
Subroutine as_netmask: called CC=1 SCAR=0.0
Pod documented
132 18 my $self = shift;
133 18 my $hr = $self->outer_bounds();
134 18 return "$hr->{ lower }" . ' (' . "$hr->{ netmask }" . ')';
135 }
136  
137 0 ▶sub loose {
Subroutine loose: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self = shift;
139 0 my $hr = $self->outer_bounds();
140 0 return ref($self)->new({ lower => $hr->{ lower }, upper => $hr->{ upper } });
141 }
142  
143 0 ▶sub _spaceship {
Subroutine _spaceship: not called CC=4 SCAR=30.0
144 0 my ($self, $rhs, $swapped) = @_;
145 0 ▶ ($self, $rhs) = ($rhs, $self) if $swapped;
Branch: 1 branch
BranchTrueFalseTotal
if $swapped 0 0 0
146 0 ▶ $rhs = ref($self)->new({ ip => $rhs }) unless ref($self) eq ref($rhs);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq ref $rhs 0 0 0
147 return
148 $self->{ lower } <=> $rhs->{ lower }
149 || $self->{ upper } <=> $rhs->{ upper }
150 0 ▶ ;
Condition0%0/3
$self->{'lower'} <=> $rhs->{'lower'} || $self->{'upper'} <=> $rhs->{'upper'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'lower'} <=> $rhs->{'lower'} || $self->{'upper'} <=> $rhs->{'upper'}
$self->{'lower'} <=> $rhs->{'lower'} $self->{'upper'} <=> $rhs->{'upper'}
Truth table0%0/3
A || B
A= $self->{'lower'} <=> $rhs->{'lower'}B= $self->{'upper'} <=> $rhs->{'upper'}
ABresult
1X1
011
000
151 }
152  
153 36 ▶sub tight {
Subroutine tight: called CC=2 SCAR=6.9
Pod documented
154 36 my $self = shift;
155 36 my $inner = $self->inner_bounds();
156 36 my $rv = Net::IPAddress::Util::Collection->new();
157 36 push @$rv, $inner;
158 36 ▶ if ($inner->{ upper } < $self->{ upper }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner->{"upper"} < $self->{"upper"}) 32 4 36
159 32 my $remainder = ref($self)->new({ lower => $inner->{ upper } + 1, upper => $self->{ upper } });
160 32 push @$rv, @{$remainder->tight()};
161 }
162 36 return $rv;
163 }
164  
165 0 ▶sub lower {
Subroutine lower: not called CC=1 SCAR=6.9
Pod documented
166 0 my $self = shift;
167 0 return $self->{ lower };
168 }
169  
170 0 ▶sub upper {
Subroutine upper: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172 0 return $self->{ upper };
173 }
174  
175 5 1;
176  
177 __END__
« Util.pm ↑ Summary Util/Collection.pm »