blib/lib/Test2/Compare/Float.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 92.8% 39 / 42 Conditioncond 90.4% 19 / 21 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 94.5% 157 / 166 CC 30 SCAR 21.6
CC
30
Cov
96%
verify
13.0
init
10.0
operator
8.3
CRAP 8.7
« blib/lib/Test2/Compare/Number.pm ↑ Summary blib/lib/Test2/Plugin/ExitSummary.pm »
1 package Test2::Compare::Float;
2 171 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 171 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 171 ▶use base 'Test2::Compare::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 171 our $VERSION = '1.302226';
10  
11 171 our $DEFAULT_TOLERANCE = 1e-08;
12  
13 171 ▶use Test2::Util::HashBase qw/input tolerance precision/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Overloads '!' for us.
16 171 ▶use Test2::Compare::Negatable;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 ▶sub init {
Subroutine init: called CC=10 SCAR=23.0
Pod undocumented
19 28 my $self = shift;
20 28 my $input = $self->{+INPUT};
21  
22 28 ▶ if ( exists $self->{+TOLERANCE} and exists $self->{+PRECISION} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'tolerance'} and exists $self->{'precision'}) { } 1 27 28
elsif (not exists $self->{'precision'} || exists $self->{'tolerance'}) { } 10 17 27
Condition100%3/3
exists $self->{"tolerance"} and exists $self->{"precision"}
!ll&&!rl&&r
2161
MC/DC100%2/2
exists $self->{"tolerance"} and exists $self->{"precision"}
exists $self->{"tolerance"} exists $self->{"precision"}
Truth table100%3/3
A and B
A= exists $self->{"tolerance"}B= exists $self->{"precision"}
ABresult
0X0
100
111
23 1 confess "can't set both tolerance and precision";
24 ▶ } elsif (!exists $self->{+PRECISION} and !exists $self->{+TOLERANCE}) {
Condition100%3/3
exists $self->{'precision'} || exists $self->{'tolerance'}
l!l&&r!l&&!r
11610
MC/DC100%2/2
exists $self->{'precision'} || exists $self->{'tolerance'}
exists $self->{'precision'} exists $self->{'tolerance'}
Truth table100%3/3
A || B
A= exists $self->{'precision'}B= exists $self->{'tolerance'}
ABresult
1X1
011
000
25 10 $self->{+TOLERANCE} = $DEFAULT_TOLERANCE
26 }
27  
28 27 ▶ confess "input must be defined for 'Float' check"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 26 27
29 unless defined $input;
30  
31 # Check for ''
32 26 ▶ confess "input must be a number for 'Float' check"
Branch: 1 branch
BranchTrueFalseTotal
unless length $input and $input =~ /\S/ 2 24 26
Condition100%3/3
length $input and $input =~ /\S/
!ll&&!rl&&r
1124
MC/DC100%2/2
length $input and $input =~ /\S/
length $input $input =~ /\S/
Truth table100%3/3
A and B
A= length $inputB= $input =~ /\S/
ABresult
0X0
100
111
33 unless length($input) && $input =~ m/\S/;
34  
35 confess "precision must be an integer for 'Float' check"
36 24 ▶ if exists $self->{+PRECISION} && $self->{+PRECISION} !~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"precision"} and not $self->{"precision"} =~ /^\d+$/ 2 22 24
Condition100%3/3
exists $self->{"precision"} and not $self->{"precision"} =~ /^\d+$/
!ll&&!rl&&r
1392
MC/DC100%2/2
exists $self->{"precision"} and not $self->{"precision"} =~ /^\d+$/
exists $self->{"precision"} not $self->{"precision"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= exists $self->{"precision"}B= not $self->{"precision"} =~ /^\d+$/
ABresult
0X0
100
111
37  
38 22 $self->SUPER::init(@_);
39 }
40  
41 36 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
42 36 my $self = shift;
43 36 my $in = $self->{+INPUT};
44 36 my $precision = $self->{+PRECISION};
45 36 ▶ if ( defined $precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $precision) 12 24 36
46 12 return sprintf "%.*f", $precision, $in;
47 }
48 24 my $tolerance = $self->{+TOLERANCE};
49 24 return "$in +/- $tolerance";
50 }
51  
52 17 ▶sub operator {
Subroutine operator: called CC=8 SCAR=21.1
Pod documented
53 17 my $self = shift;
54 17 ▶ return '' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 14 17
55 14 my ($got) = @_;
56  
57 14 ▶ return '' unless defined($got);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 3 11 14
58 11 ▶ return '' unless length($got) && $got =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless length $got and $got =~ /\S/ 0 11 11
Condition33%1/3
length $got and $got =~ /\S/
!ll&&!rl&&r
0011
MC/DC0%0/2
length $got and $got =~ /\S/
length $got $got =~ /\S/
Truth table33%1/3
A and B
A= length $gotB= $got =~ /\S/
ABresult
0X0
100
111
59  
60 11 ▶ if ( $self->{+PRECISION} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"precision"}) 1 10 11
61 {
62 1 ▶ return 'ne' if $self->{+NEGATE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"negate"} 0 1 1
63 1 return 'eq';
64 }
65  
66 10 ▶ return '!=' if $self->{+NEGATE};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"negate"} 6 4 10
67 4 return '==';
68 }
69  
70 78 ▶sub verify {
Subroutine verify: called CC=13 SCAR=25.7
Pod documented
71 78 my $self = shift;
72 78 my %params = @_;
73 78 my ($got, $exists) = @params{qw/got exists/};
74  
75 78 ▶ return 0 unless $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 2 76 78
76 76 ▶ return 0 unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 2 74 76
77 74 ▶ return 0 if ref $got;
Branch: 1 branch
BranchTrueFalseTotal
if ref $got 2 72 74
78 72 ▶ return 0 unless length($got) && $got =~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless length $got and $got =~ /\S/ 2 70 72
Condition100%3/3
length $got and $got =~ /\S/
!ll&&!rl&&r
1170
MC/DC100%2/2
length $got and $got =~ /\S/
length $got $got =~ /\S/
Truth table100%3/3
A and B
A= length $gotB= $got =~ /\S/
ABresult
0X0
100
111
79  
80 70 my $input = $self->{+INPUT};
81 70 my $negate = $self->{+NEGATE};
82 70 my $tolerance = $self->{+TOLERANCE};
83 70 my $precision = $self->{+PRECISION};
84  
85 70 my @warnings;
86 my $out;
87 {
88 70 ▶ local $SIG{__WARN__} = sub { push @warnings => @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
89  
90 70 my $equal = ($input == $got);
91 70 ▶ if (!$equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 46 24 70
92 46 ▶ if (defined $tolerance) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tolerance) { } 32 14 46
93 32 ▶ $equal = 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $got > $input - $tolerance and $got < $input + $tolerance 10 22 32
Condition100%3/3
$got > $input - $tolerance and $got < $input + $tolerance
!ll&&!rl&&r
14810
MC/DC100%2/2
$got > $input - $tolerance and $got < $input + $tolerance
$got > $input - $tolerance $got < $input + $tolerance
Truth table100%3/3
A and B
A= $got > $input - $toleranceB= $got < $input + $tolerance
ABresult
0X0
100
111
94 $got > $input - $tolerance &&
95 $got < $input + $tolerance;
96 } else {
97 14 $equal =
98 sprintf("%.*f", $precision, $got) eq
99 sprintf("%.*f", $precision, $input);
100 }
101 }
102  
103 70 ▶ $out = $negate ? !$equal : $equal;
Branch: 1 branch
BranchTrueFalseTotal
$negate ? : 8 62 70
104 }
105  
106 70 for my $warn (@warnings) {
107 5 ▶ if ($warn =~ m/numeric/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn =~ /numeric/) 5 0 5
108 5 $out = 0;
109 5 next; # This warning won't help anyone.
110 }
111 0 warn $warn;
112 }
113  
114 70 return $out;
115 }
116  
117 171 1;
118  
119 __END__
« blib/lib/Test2/Compare/Number.pm ↑ Summary blib/lib/Test2/Plugin/ExitSummary.pm »