Number/Tolerant.pm

filter: Statementstmt 100% 118 / 118 Branchbran 96.5% 85 / 88 Conditioncond 94.4% 68 / 72 MC/DCmcdc 75.6% 28 / 37 Subroutinesub 100% 31 / 31 Podpod 100% 10 / 10 totaltotal 95.5% 340 / 356 CC 73 SCAR 24.3
CC
73
Cov
97%
intersection
29.0
new
11.0
stringify
7.0
CRAP 11.4
↑ Summary Test/Tolerant.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Number::Tolerant 1.711; # TRIAL
4 # ABSTRACT: tolerance ranges for inexact numbers
5  
6 25 ▶use Sub::Exporter::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Sub::Exporter 0.950 -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 exports => { tolerance => Sub::Exporter::Util::curry_class('new'), },
9 groups => { default => [ qw(tolerance) ] },
10 25 };
11  
12 25 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #pod =head1 SYNOPSIS
16 #pod
17 #pod use Number::Tolerant;
18 #pod
19 #pod my $range = tolerance(10 => to => 12);
20 #pod my $random = 10 + rand(2);
21 #pod
22 #pod die "I shouldn't die" unless $random == $range;
23 #pod
24 #pod print "This line will always print.\n";
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod Number::Tolerant creates a number-like object whose value refers to a range of
29 #pod possible values, each equally acceptable. It overloads comparison operations
30 #pod to reflect this.
31 #pod
32 #pod I use this module to simplify the comparison of measurement results to
33 #pod specified tolerances.
34 #pod
35 #pod reject $product unless $measurement == $specification;
36 #pod
37 #pod =head1 METHODS
38 #pod
39 #pod =head2 Instantiation
40 #pod
41 #pod =head3 new
42 #pod
43 #pod =head3 tolerance
44 #pod
45 #pod There is a C<new> method on the Number::Tolerant class, but it also exports a
46 #pod simple function, C<tolerance>, which will return an object of the
47 #pod Number::Tolerant class. Both use the same syntax:
48 #pod
49 #pod my $range = Number::Tolerant->new( $x => $method => $y);
50 #pod
51 #pod my $range = tolerance( $x => $method => $y);
52 #pod
53 #pod The meaning of C<$x> and C<$y> are dependent on the value of C<$method>, which
54 #pod describes the nature of the tolerance. Tolerances can be defined in five ways,
55 #pod at present:
56 #pod
57 #pod method range
58 #pod -------------------+------------------
59 #pod plus_or_minus | x +/- y
60 #pod plus_or_minus_pct | x +/- (y% of x)
61 #pod or_more | x to Inf
62 #pod or_less | x to -Inf
63 #pod more_than | x to Inf, not x
64 #pod less_than | x to -Inf, not x
65 #pod to | x to y
66 #pod infinite | -Inf to Inf
67 #pod offset | (x + y1) to (x + y2)
68 #pod
69 #pod For C<or_less> and C<or_more>, C<$y> is ignored if passed. For C<infinite>,
70 #pod neither C<$x> nor C<$y> is used; "infinite" should be the sole argument. The
71 #pod first two arguments can be reversed for C<more_than> and C<less_than>, to be
72 #pod more English-like.
73 #pod
74 #pod Offset tolerances are slightly unusual. Here is an example:
75 #pod
76 #pod my $offset_tolerance = tolerance(10 => offset => (-3, 5));
77 #pod # stringifies to: 10 (-3 +5)
78 #pod
79 #pod An offset is very much like a C<plus_or_minus> tolerance, but its center value
80 #pod is not necessarily the midpoint between its extremes. This is significant for
81 #pod comparisons and numifications of the tolerance. Given the following two
82 #pod tolerances:
83 #pod
84 #pod my $pm_dice = tolerance(10.5 => plus_or_minus => 7.5);
85 #pod my $os_dice = tolerance(11 => offset => (-8, 7));
86 #pod
87 #pod The first will sort as numerically less than the second.
88 #pod
89 #pod If the given arguments can't be formed into a tolerance, an exception will be
90 #pod raised.
91 #pod
92 #pod =cut
93  
94 # these are the default plugins
95 25 my %_plugins;
96  
97 137 ▶sub _plugins {
Subroutine _plugins: called CC=1 SCAR=0.0
98 137 keys %_plugins
99 }
100  
101 2 ▶sub disable_plugin {
Subroutine disable_plugin: called CC=1 SCAR=0.0
Pod documented
102 2 my ($class, $plugin) = @_;
103 2 $class->_boot_up;
104 2 delete $_plugins{ $plugin };
105 2 return;
106 }
107  
108 255 ▶sub enable_plugin {
Subroutine enable_plugin: called CC=4 SCAR=13.9
Pod documented
109 255 my ($class, $plugin) = @_;
110 255 $class->_boot_up;
111  
112 # XXX: there has to be a better test to use here -- rjbs, 2006-01-27
113 255 ▶ unless (eval { $plugin->can('construct') }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $plugin->can("construct") } }) 252 3 255
114 252 ▶ eval "require $plugin" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $plugin" or die $@ 251 1 252
115 }
116  
117 254 ▶ unless (eval { $class->validate_plugin($plugin); }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $class->validate_plugin($plugin) } }) 1 253 254
118 1 Carp::croak "class $plugin is not a valid Number::Tolerant plugin: $@";
119 }
120  
121 253 $_plugins{ $plugin } = undef;
122 253 return;
123 }
124  
125 254 ▶sub validate_plugin {
Subroutine validate_plugin: called CC=3 SCAR=11.0
Pod documented
126 254 my ($class, $plugin) = @_;
127 254 for (qw(parse valid_args construct)) {
128 760 ▶ die "can't $_" unless $plugin->can($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin->can($_) 1 759 760
129 }
130 253 return 1;
131 }
132  
133 my $booted;
134 429 ▶sub _boot_up {
Subroutine _boot_up: called CC=3 SCAR=11.0
135 429 ▶ return if $booted;
Branch: 1 branch
BranchTrueFalseTotal
if $booted 404 25 429
136 25 $booted = 1;
137 my @_default_plugins =
138 25 map { "Number::Tolerant::Type::$_" }
139 qw(
140 constant infinite less_than
141 more_than offset or_less
142 or_more plus_or_minus plus_or_minus_pct
143 to
144 );
145  
146 25 __PACKAGE__->enable_plugin($_) for @_default_plugins;
147 }
148  
149 138 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
150 138 my $class = shift;
151 138 $class->_boot_up;
152 138 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 137 138
153 137 my $self;
154  
155 137 for my $type ($class->_plugins) {
156 957 ▶ next unless my @args = $type->valid_args(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless my(@args) = $type->valid_args(@_) 843 114 957
157 114 my $guts = $type->construct(@args);
158  
159 114 ▶ return $guts unless ref $guts and not Scalar::Util::blessed($guts);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $guts and not builtin::blessed($guts) 4 110 114
Condition66%2/3
ref $guts and not builtin::blessed($guts)
!ll&&!rl&&r
40110
MC/DC50%1/2
ref $guts and not builtin::blessed($guts)
ref $guts not builtin::blessed($guts)
Truth table66%2/3
A and not(B)
A= ref $gutsB= not builtin::blessed($guts)
ABresult
0X0
100
111
160  
161 110 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $guts->{"min"} and defined $guts->{"max"} and $guts->{"min"} == $guts->{"max"} and not $guts->{"constant"}) 3 107 110
Condition100%9/9
defined $guts->{"min"} and defined $guts->{"max"} and $guts->{"min"} == $guts->{"max"} and not $guts->{"constant"}
!ll&&!rl&&r
10253
defined $guts->{"min"} and defined $guts->{"max"} and $guts->{"min"} == $guts->{"max"}
!ll&&!rl&&r
59438
defined $guts->{"min"} and defined $guts->{"max"}
!ll&&!rl&&r
302951
MC/DC100%4/4
defined $guts->{"min"} and defined $guts->{"max"} and $guts->{"min"} == $guts->{"max"} and not $guts->{"constant"}
defined $guts->{"min"} defined $guts->{"max"} $guts->{"min"} == $guts->{"max"} not $guts->{"constant"}
Truth table100%5/5
A and B and C and not(D)
A= defined $guts->{"min"}B= defined $guts->{"max"}C= $guts->{"min"} == $guts->{"max"}D= not $guts->{"constant"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
162 defined $guts->{min} and defined $guts->{max} and
163 $guts->{min} == $guts->{max} and
164 not $guts->{constant}
165 ) {
166 3 @_ = ($class, $guts->{min});
167 3 goto &new;
168 }
169 107 $self = { method => $type, %$guts };
170 107 last;
171 }
172  
173 130 ▶ Carp::confess("couldn't form tolerance from given args") unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 23 107 130
174 107 bless $self => $self->{method};
175 }
176  
177 #pod =head3 from_string
178 #pod
179 #pod A new tolerance can be instantiated from the stringification of an old
180 #pod tolerance. For example:
181 #pod
182 #pod my $range = Number::Tolerant->from_string("10 to 12");
183 #pod
184 #pod die "Everything's OK!" if 11 == $range; # program dies of joy
185 #pod
186 #pod This will I<not> yet parse stringified unions, but that will be implemented in
187 #pod the future. (I just don't need it yet.)
188 #pod
189 #pod If a string can't be parsed, an exception is raised.
190 #pod
191 #pod =cut
192  
193 34 ▶sub from_string {
Subroutine from_string: called CC=4 SCAR=13.9
Pod documented
194 34 my ($class, $string) = @_;
195 34 $class->_boot_up;
196 34 ▶ Carp::croak "from_string is a class method" if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 33 34
197 33 for my $type (keys %_plugins) {
198 249 ▶ if (defined(my $tolerance = $type->parse($string, $class))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tolerance = $type->parse($string, $class))) 29 220 249
199 29 return $tolerance;
200 }
201 }
202  
203 4 Carp::confess("couldn't form tolerance from given string");
204 }
205  
206 53 ▶sub stringify {
Subroutine stringify: called CC=7 SCAR=19.5
Pod documented
207 53 my ($self) = @_;
208  
209 53 ▶ return 'any number' unless (defined $self->{min} || defined $self->{max});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"min"} or defined $self->{"max"} 4 49 53
Condition100%3/3
defined $self->{"min"} or defined $self->{"max"}
l!l&&r!l&&!r
37124
MC/DC100%2/2
defined $self->{"min"} or defined $self->{"max"}
defined $self->{"min"} defined $self->{"max"}
Truth table100%3/3
A or B
A= defined $self->{"min"}B= defined $self->{"max"}
ABresult
1X1
011
000
210  
211 49 my $string = '';
212  
213 49 ▶ if (defined $self->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"min"}) 37 12 49
214 37 ▶ $string .= "$self->{min} <" . ($self->{exclude_min} ? q{} : '=') . q{ };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'exclude_min'} ? : 10 27 37
215 }
216  
217 49 $string .= 'x';
218  
219 49 ▶ if (defined $self->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max"}) 33 16 49
220 33 ▶ $string .= ' <' . ($self->{exclude_max} ? q{} : '=') . " $self->{max}";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'exclude_max'} ? : 7 26 33
221 }
222  
223 49 return $string;
224 }
225  
226 #pod =head2 stringify_as
227 #pod
228 #pod my $string = $tolerance->stringify_as($type);
229 #pod
230 #pod This method does nothing! Someday, it will stringify the given tolerance as a
231 #pod different type, if possible. "10 +/- 1" will
232 #pod C<stringify_as('plus_or_minus_pct')> to "10 +/- 10%" for example.
233 #pod
234 #pod =cut
235  
236 2 ▶sub stringify_as { }
Subroutine stringify_as: called CC=1 SCAR=0.0
Pod documented
237  
238 #pod =head2 numify
239 #pod
240 #pod my $n = $tolerance->numify;
241 #pod
242 #pod This returns the numeric form of a tolerance. If a tolerance has both a
243 #pod minimum and a maximum, and they are the same, then that is the numification.
244 #pod Otherwise, numify returns undef.
245 #pod
246 #pod =cut
247  
248 3 ▶sub numify {
Subroutine numify: called CC=4 SCAR=14.2
Pod documented
249 # if a tolerance has equal min and max, it numifies to that number
250 return $_[0]{min}
251 3 ▶ if $_[0]{min} and $_[0]{max} and $_[0]{min} == $_[0]{max};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"min"} and $_[0]{"max"} and $_[0]{"min"} == $_[0]{"max"} 0 3 3
Condition83%5/6
$_[0]{"min"} and $_[0]{"max"} and $_[0]{"min"} == $_[0]{"max"}
!ll&&!rl&&r
210
$_[0]{"min"} and $_[0]{"max"}
!ll&&!rl&&r
111
MC/DC0%0/3
$_[0]{"min"} and $_[0]{"max"} and $_[0]{"min"} == $_[0]{"max"}
$_[0]{"min"} $_[0]{"max"} $_[0]{"min"} == $_[0]{"max"}
Truth table75%3/4
A and B and C
A= $_[0]{"min"}B= $_[0]{"max"}C= $_[0]{"min"} == $_[0]{"max"}
ABCresult
0XX0
10X0
1100
1111
252 ## no critic (ReturnUndef)
253 3 return undef;
254 }
255  
256 449 ▶sub _num_eq { not( _num_gt($_[0],$_[1]) or _num_lt($_[0],$_[1]) ) }
Subroutine _num_eq: called CC=2 SCAR=6.9
Condition100%3/3
_num_gt($_[0], $_[1]) || _num_lt($_[0], $_[1])
l!l&&r!l&&!r
124122203
MC/DC100%2/2
_num_gt($_[0], $_[1]) || _num_lt($_[0], $_[1])
_num_gt($_[0], $_[1]) _num_lt($_[0], $_[1])
Truth table100%3/3
A || B
A= _num_gt($_[0], $_[1])B= _num_lt($_[0], $_[1])
ABresult
1X1
011
000
257  
258 86 ▶sub _num_ne { not _num_eq(@_) }
Subroutine _num_ne: called CC=1 SCAR=0.0
259  
260 667 ▶sub _num_gt { $_[2] ? goto &_num_lt_canonical : goto &_num_gt_canonical }
Subroutine _num_gt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 108 559 667
261  
262 552 ▶sub _num_lt { $_[2] ? goto &_num_gt_canonical : goto &_num_lt_canonical }
Subroutine _num_lt: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 99 453 552
263  
264 50 ▶sub _num_gte { $_[1] == $_[0] ? 1 : goto &_num_gt; }
Subroutine _num_gte: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] == $_[0] ? : 25 25 50
265  
266 50 ▶sub _num_lte { $_[1] == $_[0] ? 1 : goto &_num_lt; }
Subroutine _num_lte: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] == $_[0] ? : 26 24 50
267  
268 658 ▶sub _num_gt_canonical {
Subroutine _num_gt_canonical: called CC=4 SCAR=13.9
269 658 ▶ return 1 if $_[0]{exclude_min} and $_[0]{min} == $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"exclude_min"} and $_[0]{"min"} == $_[1] 28 630 658
Condition100%3/3
$_[0]{"exclude_min"} and $_[0]{"min"} == $_[1]
!ll&&!rl&&r
5626828
MC/DC100%2/2
$_[0]{"exclude_min"} and $_[0]{"min"} == $_[1]
$_[0]{"exclude_min"} $_[0]{"min"} == $_[1]
Truth table100%3/3
A and B
A= $_[0]{"exclude_min"}B= $_[0]{"min"} == $_[1]
ABresult
0X0
100
111
270 defined $_[0]->{min} ? $_[1] < $_[0]->{min} : undef
271 630 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'min'} ? : 489 141 630
272  
273 561 ▶sub _num_lt_canonical {
Subroutine _num_lt_canonical: called CC=4 SCAR=13.9
274 561 ▶ return 1 if $_[0]{exclude_max} and $_[0]{max} == $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"exclude_max"} and $_[0]{"max"} == $_[1] 14 547 561
Condition100%3/3
$_[0]{"exclude_max"} and $_[0]{"max"} == $_[1]
!ll&&!rl&&r
4984914
MC/DC100%2/2
$_[0]{"exclude_max"} and $_[0]{"max"} == $_[1]
$_[0]{"exclude_max"} $_[0]{"max"} == $_[1]
Truth table100%3/3
A and B
A= $_[0]{"exclude_max"}B= $_[0]{"max"} == $_[1]
ABresult
0X0
100
111
275 defined $_[0]->{max} ? $_[1] > $_[0]->{max} : undef
276 547 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'max'} ? : 423 124 547
277  
278 8 ▶sub _union { $_[0]->union($_[1]); }
Subroutine _union: called CC=1 SCAR=0.0
279  
280 8 ▶sub union {
Subroutine union: called CC=1 SCAR=0.0
Pod documented
281 8 require Number::Tolerant::Union;
282 8 return Number::Tolerant::Union->new($_[0],$_[1]);
283 }
284  
285 24 ▶sub _intersection { $_[0]->intersection($_[1]); }
Subroutine _intersection: called CC=1 SCAR=0.0
286  
287 24 ▶sub intersection {
Subroutine intersection: called CC=29 SCAR=33.7
Pod documented
288 24 ▶ if (! ref $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[1]) 6 18 24
289 6 ▶ return $_[1] if $_[0] == $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == $_[1] 5 1 6
290 1 Carp::confess "no valid intersection of ($_[0]) and ($_[1])";
291 }
292  
293 18 my ($min, $max);
294 18 my ($exclude_min, $exclude_max);
295  
296 18 ▶ if (defined $_[0]->{min} and defined $_[1]->{min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{'min'} and defined $_[1]{'min'}) { } 8 10 18
Condition100%3/3
defined $_[0]{"min"} and defined $_[1]{"min"}
!ll&&!rl&&r
738
MC/DC100%2/2
defined $_[0]{"min"} and defined $_[1]{"min"}
defined $_[0]{"min"} defined $_[1]{"min"}
Truth table100%3/3
A and B
A= defined $_[0]{"min"}B= defined $_[1]{"min"}
ABresult
0X0
100
111
297 8 ($min) = sort {$b<=>$a} ($_[0]->{min}, $_[1]->{min});
298 } else {
299 10 ▶ $min = defined $_[0]->{min} ? $_[0]->{min} : $_[1]->{min};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'min'} ? : 3 7 10
300 }
301  
302 $exclude_min = 1
303 if ($_[0]{min} and $min == $_[0]{min} and $_[0]{exclude_min})
304 18 ▶ or ($_[1]{min} and $min == $_[1]{min} and $_[1]{exclude_min});
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"min"} and $min == $_[0]{"min"} and $_[0]{"exclude_min"} or $_[1]{"min"} and $min == $_[1]{"min"} and $_[1]{"exclude_min"} 3 15 18
Condition100%15/15
$_[0]{"min"} and $min == $_[0]{"min"} and $_[0]{"exclude_min"} or $_[1]{"min"} and $min == $_[1]{"min"} and $_[1]{"exclude_min"}
l!l&&r!l&&!r
1215
$_[0]{"min"} and $min == $_[0]{"min"} and $_[0]{"exclude_min"}
!ll&&!rl&&r
1161
$_[0]{"min"} and $min == $_[0]{"min"}
!ll&&!rl&&r
837
$_[1]{"min"} and $min == $_[1]{"min"} and $_[1]{"exclude_min"}
!ll&&!rl&&r
1142
$_[1]{"min"} and $min == $_[1]{"min"}
!ll&&!rl&&r
746
MC/DC83%5/6
$_[0]{"min"} and $min == $_[0]{"min"} and $_[0]{"exclude_min"} or $_[1]{"min"} and $min == $_[1]{"min"} and $_[1]{"exclude_min"}
$_[0]{"min"} $min == $_[0]{"min"} $_[0]{"exclude_min"} $_[1]{"min"} $min == $_[1]{"min"} $_[1]{"exclude_min"}
Truth table61%8/13
A and B and C or D and E and F
A= $_[0]{"min"}B= $min == $_[0]{"min"}C= $_[0]{"exclude_min"}D= $_[1]{"min"}E= $min == $_[1]{"min"}F= $_[1]{"exclude_min"}
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
305  
306 18 ▶ if (defined $_[0]->{max} and defined $_[1]->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{'max'} and defined $_[1]{'max'}) { } 8 10 18
Condition100%3/3
defined $_[0]{"max"} and defined $_[1]{"max"}
!ll&&!rl&&r
738
MC/DC100%2/2
defined $_[0]{"max"} and defined $_[1]{"max"}
defined $_[0]{"max"} defined $_[1]{"max"}
Truth table100%3/3
A and B
A= defined $_[0]{"max"}B= defined $_[1]{"max"}
ABresult
0X0
100
111
307 8 ($max) = sort {$a<=>$b} ($_[0]->{max}, $_[1]->{max});
308 } else {
309 10 ▶ $max = defined $_[0]->{max} ? $_[0]->{max} : $_[1]->{max};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'max'} ? : 3 7 10
310 }
311  
312 $exclude_max = 1
313 if ($_[0]{max} and $max == $_[0]{max} and $_[0]{exclude_max})
314 18 ▶ or ($_[1]{max} and $max == $_[1]{max} and $_[1]{exclude_max});
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"max"} and $max == $_[0]{"max"} and $_[0]{"exclude_max"} or $_[1]{"max"} and $max == $_[1]{"max"} and $_[1]{"exclude_max"} 3 15 18
Condition100%15/15
$_[0]{"max"} and $max == $_[0]{"max"} and $_[0]{"exclude_max"} or $_[1]{"max"} and $max == $_[1]{"max"} and $_[1]{"exclude_max"}
l!l&&r!l&&!r
2115
$_[0]{"max"} and $max == $_[0]{"max"} and $_[0]{"exclude_max"}
!ll&&!rl&&r
1332
$_[0]{"max"} and $max == $_[0]{"max"}
!ll&&!rl&&r
855
$_[1]{"max"} and $max == $_[1]{"max"} and $_[1]{"exclude_max"}
!ll&&!rl&&r
961
$_[1]{"max"} and $max == $_[1]{"max"}
!ll&&!rl&&r
727
MC/DC66%4/6
$_[0]{"max"} and $max == $_[0]{"max"} and $_[0]{"exclude_max"} or $_[1]{"max"} and $max == $_[1]{"max"} and $_[1]{"exclude_max"}
$_[0]{"max"} $max == $_[0]{"max"} $_[0]{"exclude_max"} $_[1]{"max"} $max == $_[1]{"max"} $_[1]{"exclude_max"}
Truth table53%7/13
A and B and C or D and E and F
A= $_[0]{"max"}B= $max == $_[0]{"max"}C= $_[0]{"exclude_max"}D= $_[1]{"max"}E= $max == $_[1]{"max"}F= $_[1]{"exclude_max"}
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
315  
316 18 ▶ return $_[0]->new('infinite') unless defined $min || defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min or defined $max 1 17 18
Condition100%3/3
defined $min or defined $max
l!l&&r!l&&!r
1431
MC/DC100%2/2
defined $min or defined $max
defined $min defined $max
Truth table100%3/3
A or B
A= defined $minB= defined $max
ABresult
1X1
011
000
317  
318 17 ▶ return $_[0]->new($min => ($exclude_min ? 'more_than' : 'or_more'))
Branch: 2 branches
BranchTrueFalseTotal
unless defined $max 4 13 17
$exclude_min ? : 2 2 4
319 unless defined $max;
320  
321 13 ▶ return $_[0]->new($max => ($exclude_max ? 'less_than' : 'or_less'))
Branch: 2 branches
BranchTrueFalseTotal
unless defined $min 3 10 13
$exclude_max ? : 2 1 3
322 unless defined $min;
323  
324 10 ▶ Carp::confess "no valid intersection of ($_[0]) and ($_[1])"
Branch: 1 branch
BranchTrueFalseTotal
if $max < $min or $min > $max 0 10 10
Condition33%1/3
$max < $min or $min > $max
l!l&&r!l&&!r
0010
MC/DC0%0/2
$max < $min or $min > $max
$max < $min $min > $max
Truth table33%1/3
A or B
A= $max < $minB= $min > $max
ABresult
1X1
011
000
325 if $max < $min or $min > $max;
326  
327 10 bless {
328 max => $max,
329 min => $min,
330 exclude_max => $exclude_max,
331 exclude_min => $exclude_min
332 } => 'Number::Tolerant::Type::to';
333 }
334  
335 #pod =head2 Overloading
336 #pod
337 #pod Tolerances overload a few operations, mostly comparisons.
338 #pod
339 #pod =over
340 #pod
341 #pod =item boolean
342 #pod
343 #pod Tolerances are always true.
344 #pod
345 #pod =item numify
346 #pod
347 #pod Most tolerances numify to undef; see C<L</numify>>.
348 #pod
349 #pod =item stringify
350 #pod
351 #pod A tolerance stringifies to a short description of itself, generally something
352 #pod like "m < x < n"
353 #pod
354 #pod infinite - "any number"
355 #pod to - "m <= x <= n"
356 #pod or_more - "m <= x"
357 #pod or_less - "x <= n"
358 #pod more_than - "m < x"
359 #pod less_than - "x < n"
360 #pod offset - "x (-y1 +y2)"
361 #pod constant - "x"
362 #pod plus_or_minus - "x +/- y"
363 #pod plus_or_minus_pct - "x +/- y%"
364 #pod
365 #pod =item equality
366 #pod
367 #pod A number is equal to a tolerance if it is neither less than nor greater than
368 #pod it. (See below).
369 #pod
370 #pod =item comparison
371 #pod
372 #pod A number is greater than a tolerance if it is greater than its maximum value.
373 #pod
374 #pod A number is less than a tolerance if it is less than its minimum value.
375 #pod
376 #pod No number is greater than an "or_more" tolerance or less than an "or_less"
377 #pod tolerance.
378 #pod
379 #pod "...or equal to" comparisons include the min/max values in the permissible
380 #pod range, as common sense suggests.
381 #pod
382 #pod =item tolerance intersection
383 #pod
384 #pod A tolerance C<&> a tolerance or number is the intersection of the two ranges.
385 #pod Intersections allow you to quickly narrow down a set of tolerances to the most
386 #pod stringent intersection of values.
387 #pod
388 #pod tolerance(5 => to => 6) & tolerance(5.5 => to => 6.5);
389 #pod # this yields: tolerance(5.5 => to => 6)
390 #pod
391 #pod If the given values have no intersection, C<()> is returned.
392 #pod
393 #pod An intersection with a normal number will yield that number, if it is within
394 #pod the tolerance.
395 #pod
396 #pod =item tolerance union
397 #pod
398 #pod A tolerance C<|> a tolerance or number is the union of the two. Unions allow
399 #pod multiple tolerances, whether they intersect or not, to be treated as one. See
400 #pod L<Number::Tolerant::Union> for more information.
401 #pod
402 #pod =cut
403  
404 use overload
405 25 ▶ fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
406 10 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
407 '0+' => 'numify',
408 90 ▶ '<=>' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
409 90 ▶ my $rv = $_[0] == $_[1] ? 0
Branch: 3 branches
BranchTrueFalseTotal
$_[0] == $_[1] ? : 41 49 90
$_[0] < $_[1] ? : 19 30 49
$_[0] > $_[1] ? : 30 0 30
410 : $_[0] < $_[1] ? -1
411 : $_[0] > $_[1] ? 1
412 : die "impossible";
413 90 ▶ $rv *= -1 if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 49 41 90
414 90 return $rv;
415 },
416 25 '""' => 'stringify',
417 '==' => '_num_eq',
418 '!=' => '_num_ne',
419 '>' => '_num_gt',
420 '<' => '_num_lt',
421 '>=' => '_num_gte',
422 '<=' => '_num_lte',
423 '|' => '_union',
424 '&' => '_intersection',
425 25 ;
426  
427 #pod =back
428 #pod
429 #pod =head1 EXTENDING
430 #pod
431 #pod This feature is slighly experimental, but it's here.
432 #pod
433 #pod New tolerance types may be written as subclasses of L<Number::Tolerant::Type>,
434 #pod providing the interface described in its documentation. They can then be
435 #pod enabled or disabled with the following methods:
436 #pod
437 #pod =head2 C< enable_plugin >
438 #pod
439 #pod Number::Tolerant->enable_plugin($class_name);
440 #pod
441 #pod This method enables the named class, so that attempts to create new tolerances
442 #pod will check against this class. Classes are checked against
443 #pod C<L</validate_plugin>> before being enabled. An exception is thrown if the
444 #pod class does not appear to provide the Number::Tolerant::Type interface.
445 #pod
446 #pod =head2 C< disable_plugin >
447 #pod
448 #pod Number::Tolerant->disable_plugin($class_name);
449 #pod
450 #pod This method will disable the named class, so that future attempts to create new
451 #pod tolerances will not check against this class.
452 #pod
453 #pod =head2 C< validate_plugin >
454 #pod
455 #pod Number::Tolerant->validate_plugin($class_name);
456 #pod
457 #pod This method checks (naively) that the given class provides the interface
458 #pod defined in Number::Tolerant::Type. If it does not, an exception is thrown.
459 #pod
460 #pod =head1 TODO
461 #pod
462 #pod =over 4
463 #pod
464 #pod =item * Extend C<from_string> to cover unions.
465 #pod
466 #pod =item * Extend C<from_string> to include Number::Range-type specifications.
467 #pod
468 #pod =item * Allow translation into forms not originally used:
469 #pod
470 #pod my $range = tolerance(9 => to => 17);
471 #pod my $range_pm = $range->convert_to('plus_minus');
472 #pod $range->stringify_as('plus_minus_pct');
473 #pod
474 #pod =item * Create a factory so that you can simultaneously work with two sets of plugins.
475 #pod
476 #pod This one is very near completion. There will now be two classes that should be
477 #pod used: Number::Tolerant::Factory, which produces tolerances, and
478 #pod Number::Tolerant::Tolerance, which is a tolerance. Both will inherit from
479 #pod N::T, for supporting old code, and N::T will dispatch construction methods to a
480 #pod default factory.
481 #pod
482 #pod =back
483 #pod
484 #pod =head1 SEE ALSO
485 #pod
486 #pod The module L<Number::Range> provides another way to deal with ranges of
487 #pod numbers. The major differences are: N::R is set-like, not range-like; N::R
488 #pod does not overload any operators. Number::Tolerant will not (like N::R) attempt
489 #pod to parse a textual range specification like "1..2,5,7..10" unless specifically
490 #pod instructed to. (The valid formats for strings passed to C<from_string> does
491 #pod not match Number::Range exactly. See TODO.)
492 #pod
493 #pod The C<Number::Range> code:
494 #pod
495 #pod $range = Number::Range->new("10..15","20..25");
496 #pod
497 #pod Is equivalent to the C<Number::Tolerant> code:
498 #pod
499 #pod $range = Number::Tolerant::Union->new(10..15,20..25);
500 #pod
501 #pod ...while the following code expresses an actual range:
502 #pod
503 #pod $range = tolerance(10 => to => 15) | tolerance(20 => to => 25);
504 #pod
505 #pod =head1 THANKS
506 #pod
507 #pod Thanks to Yuval Kogman and #perl-qa for helping find the bizarre bug that drove
508 #pod the minimum required perl up to 5.8
509 #pod
510 #pod Thanks to Tom Freedman, who reminded me that this code was fun to work on, and
511 #pod also provided the initial implementation for the offset type.
512 #pod
513 #pod =cut
514  
515 25 "1 +/- 0";
516  
517 __END__
↑ Summary Test/Tolerant.pm »