blib/lib/Linux/Event/Framer/LengthPrefix.pm

filter: Statementstmt 100% 41 / 41 Branchbran 61.5% 16 / 26 Conditioncond 62.5% 15 / 24 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 72.0% 80 / 111 CC 24 SCAR 26.8
CC
24
Cov
79%
_build_definition
21.4
_frame
9.8
BEGIN
1.0
CRAP 14.5
« blib/lib/Linux/Event/Stream/_Descriptor.pm ↑ Summary blib/lib/Linux/Event/Loop/Introspection.pm »
1 package Linux::Event::Framer::LengthPrefix;
2 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.105';
7  
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶sub _build_definition ($class, @args) {
Subroutine _build_definition: called CC=16 SCAR=30.6
12 9 ▶ croak 'LengthPrefix options must be key/value pairs' if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 9 9
13 9 my %opt = @args;
14 9 ▶ my $bytes = delete $opt{bytes} // 4;
Condition50%1/2
delete $opt{"bytes"} // 4
l!l
90
MC/DC0%0/1
delete $opt{"bytes"} // 4
delete $opt{"bytes"}
Truth table50%1/2
A
A= delete $opt{"bytes"}
Aresult
11
00
15 9 ▶ croak 'bytes must be 1, 2, or 4'
Branch: 1 branch
BranchTrueFalseTotal
if $bytes != 1 and $bytes != 2 and $bytes != 4 0 9 9
Condition83%5/6
$bytes != 1 and $bytes != 2 and $bytes != 4
!ll&&!rl&&r
540
$bytes != 1 and $bytes != 2
!ll&&!rl&&r
234
MC/DC0%0/3
$bytes != 1 and $bytes != 2 and $bytes != 4
$bytes != 1 $bytes != 2 $bytes != 4
Truth table75%3/4
A and B and C
A= $bytes != 1B= $bytes != 2C= $bytes != 4
ABCresult
0XX0
10X0
1100
1111
16 if $bytes != 1 && $bytes != 2 && $bytes != 4;
17 9 ▶ my $endian = delete $opt{endian} // 'big';
Condition100%2/2
delete $opt{"endian"} // "big"
l!l
63
MC/DC100%1/1
delete $opt{"endian"} // "big"
delete $opt{"endian"}
Truth table100%2/2
A
A= delete $opt{"endian"}
Aresult
11
00
18 9 ▶ croak 'endian must be big or little'
Branch: 1 branch
BranchTrueFalseTotal
if $endian ne "big" and $endian ne "little" 0 9 9
Condition66%2/3
$endian ne "big" and $endian ne "little"
!ll&&!rl&&r
810
MC/DC0%0/2
$endian ne "big" and $endian ne "little"
$endian ne "big" $endian ne "little"
Truth table66%2/3
A and B
A= $endian ne "big"B= $endian ne "little"
ABresult
0X0
100
111
19 if $endian ne 'big' && $endian ne 'little';
20 9 ▶ my $include_prefix = delete $opt{include_prefix} // 0;
Condition50%1/2
delete $opt{"include_prefix"} // 0
l!l
09
MC/DC0%0/1
delete $opt{"include_prefix"} // 0
delete $opt{"include_prefix"}
Truth table50%1/2
A
A= delete $opt{"include_prefix"}
Aresult
11
00
21 9 my $max_frame = delete $opt{max_frame};
22 9 ▶ croak 'max_frame must be a non-negative integer'
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_frame and !($max_frame =~ /\A\d+\z/u) || $max_frame < 0 0 9 9
Condition50%3/6
defined $max_frame and !($max_frame =~ /\A\d+\z/u) || $max_frame < 0
!ll&&!rl&&r
810
not $max_frame =~ /\A\d+\z/u or $max_frame < 0
l!l&&r!l&&!r
001
MC/DC0%0/3
defined $max_frame and !($max_frame =~ /\A\d+\z/u) || $max_frame < 0
defined $max_frame not $max_frame =~ /\A\d+\z/u $max_frame < 0
Truth table50%2/4
A and not(B) or C
A= defined $max_frameB= not $max_frame =~ /\A\d+\z/uC= $max_frame < 0
ABCresult
0XX0
1000
11X1
1011
23 if defined($max_frame) && ($max_frame !~ /\A\d+\z/ || $max_frame < 0);
24 9 ▶ croak 'unknown LengthPrefix options: ' . join(', ', sort keys %opt) if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 9 9
25  
26 9 ▶ my $native = {
Branch: 2 branches
BranchTrueFalseTotal
$endian eq 'little' ? : 1 8 9
$include_prefix ? : 0 9 9
27 read_mode => 4,
28 prefix_bytes => 0 + $bytes,
29 prefix_little => $endian eq 'little' ? 1 : 0,
30 include_prefix => $include_prefix ? 1 : 0,
31 max_frame => $max_frame,
32 };
33 9 return { native => $native, frame => \&_frame };
34 }
35  
36 3 ▶sub _frame ($config, $payload) {
Subroutine _frame: called CC=9 SCAR=22.8
37 3 ▶ $payload = '' if !defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 3 3
38 3 my $length = bytes::length($payload);
39 3 my $bytes = $config->{prefix_bytes};
40 3 ▶ my $max = $bytes == 1 ? 0xff : $bytes == 2 ? 0xffff : 0xffffffff;
Branch: 2 branches
BranchTrueFalseTotal
$bytes == 1 ? : 0 3 3
$bytes == 2 ? : 2 1 3
41 3 ▶ croak "send(): payload length $length exceeds prefix capacity $max"
Branch: 1 branch
BranchTrueFalseTotal
if $length > $max 0 3 3
42 if $length > $max;
43 croak "send(): payload length $length exceeds max_frame=$config->{max_frame}"
44 3 ▶ if defined($config->{max_frame}) && $length > $config->{max_frame};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"max_frame"} and $length > $config->{"max_frame"} 0 3 3
Condition33%1/3
defined $config->{"max_frame"} and $length > $config->{"max_frame"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $config->{"max_frame"} and $length > $config->{"max_frame"}
defined $config->{"max_frame"} $length > $config->{"max_frame"}
Truth table33%1/3
A and B
A= defined $config->{"max_frame"}B= $length > $config->{"max_frame"}
ABresult
0X0
100
111
45  
46 3 my @octets;
47 3 my $value = $length;
48 3 for (1 .. $bytes) {
49 8 unshift @octets, $value & 0xff;
50 8 $value >>= 8;
51 }
52 3 ▶ @octets = reverse @octets if $config->{prefix_little};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"prefix_little"} 1 2 3
53 3 return pack('C*', @octets) . $payload;
54 }
55  
56 7 1;
57  
58 __END__
« blib/lib/Linux/Event/Stream/_Descriptor.pm ↑ Summary blib/lib/Linux/Event/Loop/Introspection.pm »