Identifier/Binode.pm

filter: Statementstmt 67.8% 57 / 84 Branchbran 36.3% 16 / 44 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 75.0% 15 / 20 Podpod 66.6% 4 / 6 totaltotal 52.5% 94 / 179 CC 28 SCAR 23.2
CC
28
Cov
52%
_traverse_depth_first
30.0
bin_to_ip
20.0
_traverse_width_first
20.0
CRAP 10.2
« Identifier_Role.pm ↑ Summary Identifier/Regex.pm »
1 #!/usr/bin/perl
2 #===============================================================================
3 # PODNAME: Net::IP::Identifier::Binode
4 # ABSTRACT: A node in the binary tree
5 #
6 # AUTHOR: Reid Augustin (REID)
7 # EMAIL: reid@hellosix.com
8 # CREATED: Mon Oct 6 10:20:33 PDT 2014
9 #===============================================================================
10  
11 4 ▶use 5.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 $DB::deep = 150; # IPv6 needs 128 plus some headroom
16  
17 package Net::IP::Identifier::Binode;
18 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our $VERSION = '0.106'; # VERSION
24  
25 4 has zero => (
26 is => 'rw',
27 isa => \&isnode,
28 );
29 4 has one => (
30 is => 'rw',
31 isa => \&isnode,
32 );
33 4 has payload => (
34 is => 'rw',
35 );
36  
37 sub isnode {
38 6 ▶ die if defined $_[0] and ref $_[0] ne __PACKAGE__;
Subroutine isnode: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and ref $_[0] ne __PACKAGE__ 0 6 6
Condition33%1/3
defined $_[0] and ref $_[0] ne __PACKAGE__
!ll&&!rl&&r
060
MC/DC0%0/2
defined $_[0] and ref $_[0] ne __PACKAGE__
defined $_[0] ref $_[0] ne __PACKAGE__
Truth table33%1/3
A and B
A= defined $_[0]B= ref $_[0] ne __PACKAGE__
ABresult
0X0
100
111
39 }
40  
41 sub bin_to_ip {
42 0 ▶ my ($bin) = @_;
Subroutine bin_to_ip: not called CC=4 SCAR=30.0
Pod undocumented
43  
44 0 my @results;
45 0 my $max = length $bin;
46 0 for (my $ii = 0; ; $ii++) {
47 0 ▶ if ($ii % 8 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ii % 8 == 0) 0 0 0
48 0 ▶ last if ($ii >= $max);
Branch: 1 branch
BranchTrueFalseTotal
if $ii >= $max 0 0 0
49 0 push @results, 0;
50 }
51 0 ▶ my $b = $ii < $max ? substr($bin, $ii, 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ii < $max ? : 0 0 0
52 0 $results[-1] <<= 1;
53 0 $results[-1] |= $b;
54 }
55 0 return join '.', @results;
56 }
57  
58 4 our $path = Math::BigInt->new(0);
59 sub construct {
60 3 ▶ my ($self, $path) = @_;
Subroutine construct: called CC=1 SCAR=0.0
Pod documented
61  
62 3 my $node;
63 $self->_follow(
64 $path,
65 0,
66 sub { # construction callback
67 12 ▶ my ($self, $path, $level) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
68  
69 12 ▶ if ($level < length $path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level < length $path) { } 9 3 12
70 9 ▶ if (substr($path, $level, 1)) { # next step
Branch: 1 branch
BranchTrueFalseTotal
if (substr $path, $level, 1) { } 3 6 9
71 3 ▶ $self->one($self->new) if (not $self->one);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->one 3 0 3
72 }
73 else {
74 6 ▶ $self->zero($self->new) if (not $self->zero);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->zero 3 3 6
75 }
76 }
77 else {
78 3 $node = $self; # when we reach the end
79 }
80 12 return 0; # always continue
81 }
82 3 );
83 3 return $node;
84 }
85  
86 sub follow {
87 3 ▶ my ($self, $path, $callback, @extra) = @_;
Subroutine follow: called CC=3 SCAR=12.5
Pod documented
88  
89 3 ▶ croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if not $callback or ref $callback ne "CODE" 0 3 3
Condition33%1/3
not $callback or ref $callback ne "CODE"
l!l&&r!l&&!r
003
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback ref $callback ne "CODE"
Truth table33%1/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
ABresult
1X1
011
000
90 3 return $self->_follow($path, 0, $callback, @extra);
91 }
92  
93 sub _follow {
94 4 ▶ no warnings 'recursion'; # IPv6 requires at least 128 levels
Subroutine BEGIN: called CC=1 SCAR=0.0
95 24 ▶ my ($self, $path, $level, $callback, @extra) = @_;
Subroutine _follow: called CC=6 SCAR=18.7
96  
97 24 ▶ return if $callback->($self, $path, $level, @extra);
Branch: 1 branch
BranchTrueFalseTotal
if &$callback($self, $path, $level, @extra) 0 24 24
98  
99 24 ▶ return $self if ($level >= length $path); # end of the line
Branch: 1 branch
BranchTrueFalseTotal
if $level >= length $path 6 18 24
100  
101 18 ▶ if (substr($path, $level, 1)) { # next step
Branch: 1 branch
BranchTrueFalseTotal
if (substr $path, $level, 1) { } 6 12 18
102 6 ▶ return $self->one->_follow($path, $level + 1, $callback, @extra) if ($self->one);
Branch: 1 branch
BranchTrueFalseTotal
if $self->one 6 0 6
103 }
104 else {
105 12 ▶ return $self->zero->_follow($path, $level + 1, $callback, @extra) if ($self->zero);
Branch: 1 branch
BranchTrueFalseTotal
if $self->zero 12 0 12
106 }
107 0 return; # no node at $path
108 }
109  
110 sub traverse_width_first {
111 0 ▶ my ($self, $callback, @extra) = @_;
Subroutine traverse_width_first: not called CC=3 SCAR=24.8
Pod documented
112  
113 0 ▶ croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if not $callback or ref $callback ne "CODE" 0 0 0
Condition0%0/3
not $callback or ref $callback ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback ref $callback ne "CODE"
Truth table0%0/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
ABresult
1X1
011
000
114 0 return $self->_traverse_width_first(0, $callback, @extra);
115 }
116  
117 sub _traverse_width_first {
118 4 ▶ no warnings 'recursion'; # IPv6 requires at least 128 levels
Subroutine BEGIN: called CC=1 SCAR=0.0
119 0 ▶ my ($self, $level, $callback, @extra) = @_;
Subroutine _traverse_width_first: not called CC=4 SCAR=30.0
120  
121 0 ▶ return if $callback->($self, $level, @extra);
Branch: 1 branch
BranchTrueFalseTotal
if &$callback($self, $level, @extra) 0 0 0
122  
123 #$path <<= 1;
124 0 ▶ $self->zero->_traverse_width_first($level + 1, $callback, @extra) if ($self->zero);
Branch: 1 branch
BranchTrueFalseTotal
if $self->zero 0 0 0
125 #$path |= 1;
126 0 ▶ $self->one ->_traverse_width_first($level + 1, $callback, @extra) if ($self->one);
Branch: 1 branch
BranchTrueFalseTotal
if $self->one 0 0 0
127 #$path >>= 1;
128 }
129  
130 sub traverse_depth_first {
131 0 ▶ my ($self, $callback, @extra) = @_;
Subroutine traverse_depth_first: not called CC=3 SCAR=24.8
Pod documented
132  
133 0 ▶ croak "Need a code ref\n" if (not $callback or ref $callback ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if not $callback or ref $callback ne "CODE" 0 0 0
Condition0%0/3
not $callback or ref $callback ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $callback or ref $callback ne "CODE"
not $callback ref $callback ne "CODE"
Truth table0%0/3
not(A) or B
A= not $callbackB= ref $callback ne "CODE"
ABresult
1X1
011
000
134 0 $self->_traverse_depth_first(0, $callback, @extra);
135 }
136  
137 sub _traverse_depth_first {
138 4 ▶ no warnings 'recursion'; # IPv6 requires at least 128 levels
Subroutine BEGIN: called CC=1 SCAR=0.0
139 0 ▶ my ($self, $level, $callback, @extra) = @_;
Subroutine _traverse_depth_first: not called CC=5 SCAR=34.0
140  
141 0 my $stop;
142 #print "path=", $path->as_hex, "\n";
143 #$path <<= 1;
144 0 ▶ $stop = $self->zero->_traverse_depth_first($level + 1, $callback, @extra) if ($self->zero);
Branch: 1 branch
BranchTrueFalseTotal
if $self->zero 0 0 0
145 #$path |= 1;
146 0 ▶ $stop = $self->one ->_traverse_depth_first($level + 1, $callback, @extra) if (not $stop and $self->one);
Branch: 1 branch
BranchTrueFalseTotal
if not $stop and $self->one 0 0 0
Condition0%0/3
not $stop and $self->one
!ll&&!rl&&r
000
MC/DC0%0/2
not $stop and $self->one
not $stop $self->one
Truth table0%0/3
not(A) and B
A= not $stopB= $self->one
ABresult
0X0
100
111
147 #$path >>= 1;
148  
149 0 ▶ $callback->($self, $level, @extra) if (not $stop);
Branch: 1 branch
BranchTrueFalseTotal
if not $stop 0 0 0
150 }
151  
152 4 1;
153  
154 __END__
« Identifier_Role.pm ↑ Summary Identifier/Regex.pm »