| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 33 | ▶ | my $at = defined($where) && length($where) ? "rate of $where" : 'rate'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $where and length $where
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 33 | ▶ | croak "$at must be a mapping of per_s, burst, per_min" unless ref($rate) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 31 | for my $field (qw( per_s per_min )) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 60 | ▶ | next unless defined $rate->{$field}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not ref $rate->{$field} and &looks_like_number($rate->{$field}) and $rate->{$field} > 0
not ref $rate->{$field} and &looks_like_number($rate->{$field})
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 29 | ▶ | croak "$at needs per_s or per_min" unless defined($rate->{per_s}) || defined($rate->{per_min}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rate->{"per_s"} or defined $rate->{"per_min"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 28 | my $burst; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 28 | ▶ | if (defined $rate->{per_s}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 21 | ▶ | $burst = defined($rate->{burst}) ? $rate->{burst} : POSIX::ceil($rate->{per_s}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 21 | ▶ | croak "$at: burst must be a whole number of at least 1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition55%5/9
not ref $burst and &looks_like_number($burst) and $burst >= 1 and $burst == int $burst
not ref $burst and &looks_like_number($burst) and $burst >= 1
not ref $burst and &looks_like_number($burst)
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 9 | ▶ | if (defined($row->{per_s}) && !defined($spec->{burst}) && defined($ceiling->{burst}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"} and $ceiling->{"burst"} < $row->{"burst"}
defined $row->{"per_s"} and not defined $spec->{"burst"} and defined $ceiling->{"burst"}
defined $row->{"per_s"} and not defined $spec->{"burst"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $row->{$part} and defined $ceiling->{$part}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | return undef if $row->{$part} > $ceiling->{$part}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 96 | ▶ | return join '/', map { $rate->{$_} // '' } qw( per_s burst per_min ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$rate->{$_} // ""
MC/DC100%1/1
$rate->{$_} // ""
$rate->{$_}
Truth table100%2/2
A
A= $rate->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 128 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||