lib/Langertha/Skeid/Rate.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 36 / 36 Conditioncond 80.0% 28 / 35 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 91.2% 135 / 148 CC 33 SCAR 27.2
CC
33
Cov
94%
normalize
22.4
within
10.0
print
3.0
CRAP 15.2
« lib/Langertha/Skeid/Registry.pm ↑ Summary blib/lib/Langertha/Skeid/UsageStore/DBI.pm »
1 package Langertha::Skeid::Rate;
2 128 our $VERSION = '0.005';
3 # ABSTRACT: The request rate of a policy: its shape, and whether one rate is within another
4 128 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 128 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 128 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 128 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 128 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 252 ▶sub normalize {
Subroutine normalize: called CC=22 SCAR=31.1
Pod documented
12 252 my ($class, $rate, $where) = @_;
13 252 ▶ return undef unless defined $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate 219 33 252
14 33 ▶ my $at = defined($where) && length($where) ? "rate of $where" : 'rate';
Branch: 1 branch
BranchTrueFalseTotal
defined $where && length $where ? : 28 5 33
Condition66%2/3
defined $where and length $where
!ll&&!rl&&r
5028
MC/DC50%1/2
defined $where and length $where
defined $where length $where
Truth table66%2/3
A and B
A= defined $whereB= length $where
ABresult
0X0
100
111
15 33 ▶ croak "$at must be a mapping of per_s, burst, per_min" unless ref($rate) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rate eq "HASH" 1 32 33
16 32 my @unknown = grep { !/\A(?:per_s|burst|per_min)\z/ } sort keys %$rate;
17 32 ▶ croak "$at: unknown field " . join(', ', @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 31 32
18 31 for my $field (qw( per_s per_min )) {
19 60 ▶ next unless defined $rate->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate->{$field} 29 31 60
20 croak "$at: $field must be a number above 0"
21 31 ▶ unless !ref($rate->{$field}) && looks_like_number($rate->{$field}) && $rate->{$field} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $rate->{$field} and &looks_like_number($rate->{$field}) and $rate->{$field} > 0 2 29 31
Condition83%5/6
not ref $rate->{$field} and &looks_like_number($rate->{$field}) and $rate->{$field} > 0
!ll&&!rl&&r
1129
not ref $rate->{$field} and &looks_like_number($rate->{$field})
!ll&&!rl&&r
0130
MC/DC66%2/3
not ref $rate->{$field} and &looks_like_number($rate->{$field}) and $rate->{$field} > 0
not ref $rate->{$field} &looks_like_number($rate->{$field}) $rate->{$field} > 0
Truth table75%3/4
not(A) and B and C
A= not ref $rate->{$field}B= &looks_like_number($rate->{$field})C= $rate->{$field} > 0
ABCresult
0XX0
10X0
1100
1111
22 }
23 29 ▶ croak "$at needs per_s or per_min" unless defined($rate->{per_s}) || defined($rate->{per_min});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rate->{"per_s"} or defined $rate->{"per_min"} 1 28 29
Condition100%3/3
defined $rate->{"per_s"} or defined $rate->{"per_min"}
l!l&&r!l&&!r
2171
MC/DC100%2/2
defined $rate->{"per_s"} or defined $rate->{"per_min"}
defined $rate->{"per_s"} defined $rate->{"per_min"}
Truth table100%3/3
A or B
A= defined $rate->{"per_s"}B= defined $rate->{"per_min"}
ABresult
1X1
011
000
24 28 my $burst;
25 28 ▶ if (defined $rate->{per_s}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rate->{'per_s'}) { } 21 7 28
elsif (defined $rate->{'burst'}) { } 1 6 7
26 21 ▶ $burst = defined($rate->{burst}) ? $rate->{burst} : POSIX::ceil($rate->{per_s});
Branch: 1 branch
BranchTrueFalseTotal
defined $rate->{'burst'} ? : 16 5 21
27 21 ▶ croak "$at: burst must be a whole number of at least 1"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $burst and &looks_like_number($burst) and $burst >= 1 and $burst == int $burst 1 20 21
Condition55%5/9
not ref $burst and &looks_like_number($burst) and $burst >= 1 and $burst == int $burst
!ll&&!rl&&r
1020
not ref $burst and &looks_like_number($burst) and $burst >= 1
!ll&&!rl&&r
0120
not ref $burst and &looks_like_number($burst)
!ll&&!rl&&r
0021
MC/DC25%1/4
not ref $burst and &looks_like_number($burst) and $burst >= 1 and $burst == int $burst
not ref $burst &looks_like_number($burst) $burst >= 1 $burst == int $burst
Truth table40%2/5
not(A) and B and C and D
A= not ref $burstB= &looks_like_number($burst)C= $burst >= 1D= $burst == int $burst
ABCDresult
0XXX0
10XX0
110X0
11100
11111
28 unless !ref($burst) && looks_like_number($burst) && $burst >= 1 && $burst == int($burst);
29 } elsif (defined $rate->{burst}) {
30 1 croak "$at: burst needs per_s";
31 }
32 return {
33 (defined($rate->{per_s}) ? (per_s => 0 + $rate->{per_s}, burst => 0 + $burst) : ()),
34 26 ▶ (defined($rate->{per_min}) ? (per_min => 0 + $rate->{per_min}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $rate->{'per_s'} ? : 20 6 26
defined $rate->{'per_min'} ? : 7 19 26
35 };
36 }
37  
38  
39 12 ▶sub within {
Subroutine within: called CC=10 SCAR=23.0
Pod documented
40 12 my ($class, $spec, $ceiling) = @_;
41 12 my $row = $class->normalize($spec, 'the row');
42 11 ▶ return $row unless $ceiling;
Branch: 1 branch
BranchTrueFalseTotal
unless $ceiling 2 9 11
43 9 ▶ if (defined($row->{per_s}) && !defined($spec->{burst}) && defined($ceiling->{burst})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"} and $ceiling->{"burst"} < $row->{"burst"}) 3 6 9
Condition88%8/9
defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"} and $ceiling->{"burst"} < $row->{"burst"}
!ll&&!rl&&r
423
defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"}
!ll&&!rl&&r
405
defined $row->{"per_s"} and not defined $spec->{"burst"}
!ll&&!rl&&r
225
MC/DC75%3/4
defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"} and $ceiling->{"burst"} < $row->{"burst"}
defined $row->{"per_s"} not defined $spec->{"burst"} defined $ceiling->{"burst"} $ceiling->{"burst"} < $row->{"burst"}
Truth table80%4/5
A and not(B) and C and D
A= defined $row->{"per_s"}B= not defined $spec->{"burst"}C= defined $ceiling->{"burst"}D= $ceiling->{"burst"} < $row->{"burst"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
44 && $ceiling->{burst} < $row->{burst}) {
45 3 $row->{burst} = $ceiling->{burst};
46 }
47 9 for my $part (qw( per_s burst per_min )) {
48 22 ▶ next unless defined($row->{$part}) && defined($ceiling->{$part});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{$part} and defined $ceiling->{$part} 9 13 22
Condition100%3/3
defined $row->{$part} and defined $ceiling->{$part}
!ll&&!rl&&r
8113
MC/DC100%2/2
defined $row->{$part} and defined $ceiling->{$part}
defined $row->{$part} defined $ceiling->{$part}
Truth table100%3/3
A and B
A= defined $row->{$part}B= defined $ceiling->{$part}
ABresult
0X0
100
111
49 13 ▶ return undef if $row->{$part} > $ceiling->{$part};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{$part} > $ceiling->{$part} 4 9 13
50 }
51 5 my %rate = (%$ceiling, %$row);
52 # per_s and its burst go together: a row that sets only per_min keeps the ceiling's pair.
53 5 return \%rate;
54 }
55  
56  
57 355 ▶sub print {
Subroutine print: called CC=3 SCAR=11.0
Pod documented
58 355 my ($class, $rate) = @_;
59 355 ▶ return '' unless $rate;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate 259 96 355
60 96 ▶ return join '/', map { $rate->{$_} // '' } qw( per_s burst per_min );
Condition100%2/2
$rate->{$_} // ""
l!l
187101
MC/DC100%1/1
$rate->{$_} // ""
$rate->{$_}
Truth table100%2/2
A
A= $rate->{$_}
Aresult
11
00
61 }
62  
63 128 1;
64  
65 __END__
« lib/Langertha/Skeid/Registry.pm ↑ Summary blib/lib/Langertha/Skeid/UsageStore/DBI.pm »