| 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
Condition100%3/3
exists $self->{"tolerance"} and exists $self->{"precision"}
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"}
| |||||||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||||||
| 25 | 10 | $self->{+TOLERANCE} = $DEFAULT_TOLERANCE | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 27 | ▶ | confess "input must be defined for 'Float' check" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | unless defined $input; | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # Check for '' | ||||||||||||||||||||||||||||||||
| 32 | 26 | ▶ | confess "input must be a number for 'Float' check" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $input and $input =~ /\S/
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/
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $self->{"precision"} and not $self->{"precision"} =~ /^\d+$/
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+$/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 55 | 14 | my ($got) = @_; | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 14 | ▶ | return '' unless defined($got); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | return '' unless length($got) && $got =~ m/\S/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $got and $got =~ /\S/
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/
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 11 | ▶ | if ( $self->{+PRECISION} ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | return 'ne' if $self->{+NEGATE}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 63 | 1 | return 'eq'; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 10 | ▶ | return '!=' if $self->{+NEGATE}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 76 | 76 | ▶ | return 0 unless defined $got; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 74 | ▶ | return 0 if ref $got; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 78 | 72 | ▶ | return 0 unless length($got) && $got =~ m/\S/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $got and $got =~ /\S/
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/
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 92 | 46 | ▶ | if (defined $tolerance) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | 32 | ▶ | $equal = 1 if | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$got > $input - $tolerance and $got < $input + $tolerance
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 70 | for my $warn (@warnings) { | |||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | if ($warn =~ m/numeric/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||