blib/lib/Test/Number/Delta.pm

filter: Statementstmt 100% 101 / 101 Branchbran 100% 42 / 42 Conditioncond 71.4% 15 / 21 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 92.4% 183 / 198 CC 31 SCAR 18.1
CC
31
Cov
96%
_check
13.0
import
7.0
_diag_default
3.0
CRAP 6.1
↑ Summary
1 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Test::Number::Delta;
5 # ABSTRACT: Compare the difference between numbers against a given tolerance
6  
7 11 our $VERSION = '1.06';
8  
9 11 ▶use vars qw (@EXPORT @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Required modules
12 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 @ISA = qw( Exporter );
17 11 @EXPORT = qw( delta_not_ok delta_ok delta_within delta_not_within );
18  
19 #pod =head1 SYNOPSIS
20 #pod
21 #pod # Import test functions
22 #pod use Test::Number::Delta;
23 #pod
24 #pod # Equality test with default tolerance
25 #pod delta_ok( 1e-5, 2e-5, 'values within 1e-6');
26 #pod
27 #pod # Inequality test with default tolerance
28 #pod delta_not_ok( 1e-5, 2e-5, 'values not within 1e-6');
29 #pod
30 #pod # Provide specific tolerance
31 #pod delta_within( 1e-3, 2e-3, 1e-4, 'values within 1e-4');
32 #pod delta_not_within( 1e-3, 2e-3, 1e-4, 'values not within 1e-4');
33 #pod
34 #pod # Compare arrays or matrices
35 #pod @a = ( 3.14, 1.41 );
36 #pod @b = ( 3.15, 1.41 );
37 #pod delta_ok( \@a, \@b, 'compare @a and @b' );
38 #pod
39 #pod # Set a different default tolerance
40 #pod use Test::Number::Delta within => 1e-5;
41 #pod delta_ok( 1.1e-5, 2e-5, 'values within 1e-5'); # ok
42 #pod
43 #pod # Set a relative tolerance
44 #pod use Test::Number::Delta relative => 1e-3;
45 #pod delta_ok( 1.01, 1.0099, 'values within 1.01e-3');
46 #pod
47 #pod
48 #pod =head1 DESCRIPTION
49 #pod
50 #pod At some point or another, most programmers find they need to compare
51 #pod floating-point numbers for equality. The typical idiom is to test
52 #pod if the absolute value of the difference of the numbers is within a desired
53 #pod tolerance, usually called epsilon. This module provides such a function for use
54 #pod with L<Test::More>. Usage is similar to other test functions described in
55 #pod L<Test::More>. Semantically, the C<delta_within> function replaces this kind
56 #pod of construct:
57 #pod
58 #pod ok ( abs($p - $q) < $epsilon, '$p is equal to $q' ) or
59 #pod diag "$p is not equal to $q to within $epsilon";
60 #pod
61 #pod While there's nothing wrong with that construct, it's painful to type it
62 #pod repeatedly in a test script. This module does the same thing with a single
63 #pod function call. The C<delta_ok> function is similar, but either uses a global
64 #pod default value for epsilon or else calculates a 'relative' epsilon on
65 #pod the fly so that epsilon is scaled automatically to the size of the arguments to
66 #pod C<delta_ok>. Both functions are exported automatically.
67 #pod
68 #pod Because checking floating-point equality is not always reliable, it is not
69 #pod possible to check the 'equal to' boundary of 'less than or equal to
70 #pod epsilon'. Therefore, Test::Number::Delta only compares if the absolute value
71 #pod of the difference is B<less than> epsilon (for equality tests) or
72 #pod B<greater than> epsilon (for inequality tests).
73 #pod
74 #pod =head1 USAGE
75 #pod
76 #pod =head2 use Test::Number::Delta;
77 #pod
78 #pod With no arguments, epsilon defaults to 1e-6. (An arbitrary choice on the
79 #pod author's part.)
80 #pod
81 #pod =head2 use Test::Number::Delta within => 1e-9;
82 #pod
83 #pod To specify a different default value for epsilon, provide a C<within> parameter
84 #pod when importing the module. The value must be non-zero.
85 #pod
86 #pod =head2 use Test::Number::Delta relative => 1e-3;
87 #pod
88 #pod As an alternative to using a fixed value for epsilon, provide a C<relative>
89 #pod parameter when importing the module. This signals that C<delta_ok> should
90 #pod test equality with an epsilon that is scaled to the size of the arguments.
91 #pod Epsilon is calculated as the relative value times the absolute value
92 #pod of the argument with the greatest magnitude. Mathematically, for arguments
93 #pod 'x' and 'y':
94 #pod
95 #pod epsilon = relative * max( abs(x), abs(y) )
96 #pod
97 #pod For example, a relative value of "0.01" would mean that the arguments are equal
98 #pod if they differ by less than 1% of the larger of the two values. A relative
99 #pod value of 1e-6 means that the arguments must differ by less than 1 millionth
100 #pod of the larger value. The relative value must be non-zero.
101 #pod
102 #pod =head2 Combining with a test plan
103 #pod
104 #pod use Test::Number::Delta 'no_plan';
105 #pod
106 #pod # or
107 #pod
108 #pod use Test::Number::Delta within => 1e-9, tests => 1;
109 #pod
110 #pod If a test plan has not already been specified, the optional
111 #pod parameter for Test::Number::Delta may be followed with a test plan (see
112 #pod L<Test::More> for details). If a parameter for Test::Number::Delta is
113 #pod given, it must come first.
114 #pod
115 #pod =cut
116  
117 11 my $Test = Test::Builder->new;
118 11 my $Epsilon = 1e-6;
119 11 my $Relative = undef;
120  
121 13 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
122 13 my $self = shift;
123 13 my $pack = caller;
124 13 my $found = grep /within|relative/, @_;
125 13 ▶ croak "Can't specify more than one of 'within' or 'relative'"
Branch: 1 branch
BranchTrueFalseTotal
if $found > 1 1 12 13
126 if $found > 1;
127 12 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 8 4 12
128 8 my ( $param, $value ) = splice @_, 0, 2;
129 8 ▶ croak "'$param' parameter must be non-zero"
Branch: 1 branch
BranchTrueFalseTotal
if $value == 0 2 6 8
130 if $value == 0;
131 6 ▶ if ( $param eq 'within' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param eq 'within') { } 3 3 6
elsif ($param eq 'relative') { } 2 1 3
132 3 $Epsilon = abs($value);
133 }
134 elsif ( $param eq 'relative' ) {
135 2 $Relative = abs($value);
136 }
137 else {
138 1 croak "Test::Number::Delta parameters must come first";
139 }
140 }
141 9 $Test->exported_to($pack);
142 9 $Test->plan(@_);
143 9 $self->export_to_level( 1, $self, $_ ) for @EXPORT;
144 }
145  
146 #--------------------------------------------------------------------------#
147 # _check -- recursive function to perform comparison
148 #--------------------------------------------------------------------------#
149  
150 113 ▶sub _check {
Subroutine _check: called CC=13 SCAR=25.7
151 113 my ( $p, $q, $e, $name, @indices ) = @_;
152 113 my $epsilon;
153  
154 113 ▶ if ( !defined $e ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $e) { } 82 31 113
155 82 ▶ $epsilon =
Branch: 2 branches
BranchTrueFalseTotal
$Relative ? : 31 51 82
abs $p > abs $q ? : 24 7 31
156 $Relative
157 ? $Relative * ( abs($p) > abs($q) ? abs($p) : abs($q) )
158 : $Epsilon;
159 }
160 else {
161 31 $epsilon = abs($e);
162 }
163  
164 113 my ( $ok, $diag ) = ( 1, q{} ); # assume true
165 113 ▶ if ( ref $p eq 'ARRAY' || ref $q eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'ARRAY' or ref $q eq 'ARRAY') { } 31 82 113
Condition66%2/3
ref $p eq "ARRAY" or ref $q eq "ARRAY"
l!l&&r!l&&!r
31082
MC/DC50%1/2
ref $p eq "ARRAY" or ref $q eq "ARRAY"
ref $p eq "ARRAY" ref $q eq "ARRAY"
Truth table66%2/3
A or B
A= ref $p eq "ARRAY"B= ref $q eq "ARRAY"
ABresult
1X1
011
000
166 31 ▶ if ( @$p == @$q ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$p == @$q) { } 30 1 31
167 30 for my $i ( 0 .. $#{$p} ) {
168 53 my @new_indices;
169 53 ▶ ( $ok, $diag, @new_indices ) =
Branch: 1 branch
BranchTrueFalseTotal
scalar @indices ? : 27 26 53
170 _check( $p->[$i], $q->[$i], $e, $name, scalar @indices ? @indices : (), $i, );
171 53 ▶ if ( not $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 11 42 53
172 11 @indices = @new_indices;
173 11 last;
174 }
175 }
176 }
177 else {
178 1 $ok = 0;
179 1 $diag =
180 "Got an array of length "
181 . scalar(@$p)
182 . ", but expected an array of length "
183 . scalar(@$q);
184 }
185 }
186 else {
187 82 ▶ $ok = $p == $q || abs( $p - $q ) < $epsilon;
Condition100%3/3
$p == $q || abs $p - $q < $epsilon
l!l&&r!l&&!r
203527
MC/DC100%2/2
$p == $q || abs $p - $q < $epsilon
$p == $q abs $p - $q < $epsilon
Truth table100%3/3
A || B
A= $p == $qB= abs $p - $q < $epsilon
ABresult
1X1
011
000
188 82 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 27 55 82
189 27 my ( $ep, $dp ) = _ep_dp($epsilon);
190 27 $diag = sprintf( "%.${dp}f and %.${dp}f are not equal" . " to within %.${ep}f",
191 $p, $q, $epsilon );
192 }
193 }
194 113 ▶ return ( $ok, $diag, scalar(@indices) ? @indices : () );
Branch: 1 branch
BranchTrueFalseTotal
scalar @indices ? : 60 53 113
195 }
196  
197 71 ▶sub _ep_dp {
Subroutine _ep_dp: called CC=3 SCAR=11.0
198 71 my $epsilon = shift;
199 71 ▶ return ( 0, 0 ) unless $epsilon;
Branch: 1 branch
BranchTrueFalseTotal
unless $epsilon 19 52 71
200 52 $epsilon = abs($epsilon);
201 52 my ($exp) = sprintf( "%e", $epsilon ) =~ m/e(.+)/;
202 52 ▶ my $ep = $exp < 0 ? -$exp : 1;
Branch: 1 branch
BranchTrueFalseTotal
$exp < 0 ? : 50 2 52
203 52 my $dp = $ep + 1;
204 52 return ( $ep, $dp );
205 }
206  
207 19 ▶sub _diag_default {
Subroutine _diag_default: called CC=3 SCAR=11.0
208 19 ▶ my ($ep) = _ep_dp( abs( $Relative || $Epsilon ) );
Condition66%2/3
$Relative || $Epsilon
l!l&&r!l&&!r
1360
MC/DC0%0/2
$Relative || $Epsilon
$Relative $Epsilon
Truth table66%2/3
A || B
A= $RelativeB= $Epsilon
ABresult
1X1
011
000
209 19 my $diag = "Arguments are equal to within ";
210 19 ▶ $diag .=
Branch: 1 branch
BranchTrueFalseTotal
$Relative ? : 13 6 19
211 $Relative
212 ? sprintf( "relative tolerance %.${ep}f", abs($Relative) )
213 : sprintf( "%.${ep}f", abs($Epsilon) );
214 19 return $diag;
215 }
216  
217 #pod =head1 FUNCTIONS
218 #pod
219 #pod =cut
220  
221 #--------------------------------------------------------------------------#
222 # delta_within()
223 #--------------------------------------------------------------------------#
224  
225 #pod =head2 delta_within
226 #pod
227 #pod delta_within( $p, $q, $epsilon, '$p and $q are equal within $epsilon' );
228 #pod delta_within( \@p, \@q, $epsilon, '@p and @q are equal within $epsilon' );
229 #pod
230 #pod This function tests for equality within a given value of epsilon. The test is
231 #pod true if the absolute value of the difference between $p and $q is B<less than>
232 #pod epsilon. If the test is true, it prints an "OK" statement for use in testing.
233 #pod If the test is not true, this function prints a failure report and diagnostic.
234 #pod Epsilon must be non-zero.
235 #pod
236 #pod The values to compare may be scalars or references to arrays. If the values
237 #pod are references to arrays, the comparison is done pairwise for each index value
238 #pod of the array. The pairwise comparison is recursive, so matrices may
239 #pod be compared as well.
240 #pod
241 #pod For example, this code sample compares two matrices:
242 #pod
243 #pod my @a = ( [ 3.14, 6.28 ],
244 #pod [ 1.41, 2.84 ] );
245 #pod
246 #pod my @b = ( [ 3.14, 6.28 ],
247 #pod [ 1.42, 2.84 ] );
248 #pod
249 #pod delta_within( \@a, \@b, 1e-6, 'compare @a and @b' );
250 #pod
251 #pod The sample prints the following:
252 #pod
253 #pod not ok 1 - compare @a and @b
254 #pod # At [1][0]: 1.4100000 and 1.4200000 are not equal to within 0.000001
255 #pod
256 #pod =cut
257  
258 7 ▶sub delta_within($$$;$) { ## no critic
Subroutine delta_within: called CC=3 SCAR=11.0
Pod documented
259 7 my ( $p, $q, $epsilon, $name ) = @_;
260 7 ▶ croak "Value of epsilon to delta_within must be non-zero"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $epsilon or $epsilon == 0 1 6 7
Condition66%2/3
not defined $epsilon or $epsilon == 0
l!l&&r!l&&!r
016
MC/DC50%1/2
not defined $epsilon or $epsilon == 0
not defined $epsilon $epsilon == 0
Truth table66%2/3
not(A) or B
A= not defined $epsilonB= $epsilon == 0
ABresult
1X1
011
000
261 if !defined($epsilon) || $epsilon == 0;
262 {
263 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
264 6 _delta_within( $p, $q, $epsilon, $name );
265 }
266 }
267  
268 35 ▶sub _delta_within {
Subroutine _delta_within: called CC=3 SCAR=11.0
269 35 my ( $p, $q, $epsilon, $name ) = @_;
270 35 my ( $ok, $diag, @indices ) = _check( $p, $q, $epsilon, $name );
271 35 ▶ if (@indices) {
Branch: 1 branch
BranchTrueFalseTotal
if (@indices) 4 31 35
272 4 $diag = "At [" . join( "][", @indices ) . "]: $diag";
273 }
274 35 ▶ return $Test->ok( $ok, $name ) || $Test->diag($diag);
Condition66%2/3
$Test->ok($ok, $name) || $Test->diag($diag)
l!l&&r!l&&!r
21140
MC/DC0%0/2
$Test->ok($ok, $name) || $Test->diag($diag)
$Test->ok($ok, $name) $Test->diag($diag)
Truth table66%2/3
A || B
A= $Test->ok($ok, $name)B= $Test->diag($diag)
ABresult
1X1
011
000
275 }
276  
277 #--------------------------------------------------------------------------#
278 # delta_ok()
279 #--------------------------------------------------------------------------#
280  
281 #pod =head2 delta_ok
282 #pod
283 #pod delta_ok( $p, $q, '$p and $q are close enough to equal' );
284 #pod delta_ok( \@p, \@q, '@p and @q are close enough to equal' );
285 #pod
286 #pod This function tests for equality within a default epsilon value. See L</USAGE>
287 #pod for details on changing the default. Otherwise, this function works the same
288 #pod as C<delta_within>.
289 #pod
290 #pod =cut
291  
292 29 ▶sub delta_ok($$;$) { ## no critic
Subroutine delta_ok: called CC=1 SCAR=0.0
Pod documented
293 29 my ( $p, $q, $name ) = @_;
294 {
295 29 local $Test::Builder::Level = $Test::Builder::Level + 1;
296 29 _delta_within( $p, $q, undef, $name );
297 }
298 }
299  
300 #--------------------------------------------------------------------------#
301 # delta_not_ok()
302 #--------------------------------------------------------------------------#
303  
304 #pod =head2 delta_not_within
305 #pod
306 #pod delta_not_within( $p, $q, '$p and $q are different' );
307 #pod delta_not_within( \@p, \@q, $epsilon, '@p and @q are different' );
308 #pod
309 #pod This test compares inequality in excess of a given value of epsilon. The test
310 #pod is true if the absolute value of the difference between $p and $q is B<greater
311 #pod than> epsilon. For array or matrix comparisons, the test is true if I<any>
312 #pod pair of values differs by more than epsilon. Otherwise, this function works
313 #pod the same as C<delta_within>.
314 #pod
315 #pod =cut
316  
317 7 ▶sub delta_not_within($$$;$) { ## no critic
Subroutine delta_not_within: called CC=3 SCAR=11.0
Pod documented
318 7 my ( $p, $q, $epsilon, $name ) = @_;
319 7 ▶ croak "Value of epsilon to delta_not_within must be non-zero"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $epsilon or $epsilon == 0 1 6 7
Condition66%2/3
not defined $epsilon or $epsilon == 0
l!l&&r!l&&!r
016
MC/DC50%1/2
not defined $epsilon or $epsilon == 0
not defined $epsilon $epsilon == 0
Truth table66%2/3
not(A) or B
A= not defined $epsilonB= $epsilon == 0
ABresult
1X1
011
000
320 if !defined($epsilon) || $epsilon == 0;
321 {
322 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
323 6 _delta_not_within( $p, $q, $epsilon, $name );
324 }
325 }
326  
327 25 ▶sub _delta_not_within($$$;$) { ## no critic
Subroutine _delta_not_within: called CC=3 SCAR=11.0
328 25 my ( $p, $q, $epsilon, $name ) = @_;
329 25 my ( $ok, undef, @indices ) = _check( $p, $q, $epsilon, $name );
330 25 $ok = !$ok;
331 25 my ( $ep, $dp ) = _ep_dp($epsilon);
332 25 ▶ my $diag =
Branch: 1 branch
BranchTrueFalseTotal
defined $epsilon ? : 6 19 25
333 defined($epsilon)
334 ? sprintf( "Arguments are equal to within %.${ep}f", abs($epsilon) )
335 : _diag_default();
336 25 ▶ return $Test->ok( $ok, $name ) || $Test->diag($diag);
Condition66%2/3
$Test->ok($ok, $name) || $Test->diag($diag)
l!l&&r!l&&!r
14110
MC/DC0%0/2
$Test->ok($ok, $name) || $Test->diag($diag)
$Test->ok($ok, $name) $Test->diag($diag)
Truth table66%2/3
A || B
A= $Test->ok($ok, $name)B= $Test->diag($diag)
ABresult
1X1
011
000
337 }
338  
339 #pod =head2 delta_not_ok
340 #pod
341 #pod delta_not_ok( $p, $q, '$p and $q are different' );
342 #pod delta_not_ok( \@p, \@q, '@p and @q are different' );
343 #pod
344 #pod This function tests for inequality in excess of a default epsilon value. See
345 #pod L</USAGE> for details on changing the default. Otherwise, this function works
346 #pod the same as C<delta_not_within>.
347 #pod
348 #pod =cut
349  
350 19 ▶sub delta_not_ok($$;$) { ## no critic
Subroutine delta_not_ok: called CC=1 SCAR=0.0
Pod documented
351 19 my ( $p, $q, $name ) = @_;
352 {
353 19 local $Test::Builder::Level = $Test::Builder::Level + 1;
354 19 _delta_not_within( $p, $q, undef, $name );
355 }
356 }
357  
358 #pod =head1 SEE ALSO
359 #pod
360 #pod =for :list
361 #pod * L<Number::Tolerant>
362 #pod * L<Test::Deep::NumberTolerant>
363 #pod
364 #pod =cut
365  
366 11 1;
367  
368 __END__
↑ Summary