Symbolic/Custom/DefaultTests.pm

filter: Statementstmt 85.2% 173 / 203 Branchbran 76.3% 84 / 110 Conditioncond 65.0% 39 / 60 MC/DCmcdc 38.2% 13 / 34 Subroutinesub 100% 23 / 23 Podpod 100% 10 / 10 totaltotal 77.7% 342 / 440 CC 81 SCAR 30.8
CC
81
Cov
79%
test_num_equiv
47.5
__ANON__
28.7
is_identical
20.7
CRAP 21.7
« Symbolic/Operator.pm ↑ Summary Symbolic/Custom/DefaultMods.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Custom::DefaultTests - Default Math::Symbolic tree tests
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic;
11  
12 =head1 DESCRIPTION
13  
14 This is a class of default tests for Math::Symbolic trees. Likewise,
15 Math::Symbolic::Custom::DefaultMods defines default tree transformation
16 routines.
17 For details on how the custom method delegation model works, please have
18 a look at the Math::Symbolic::Custom and Math::Symbolic::Custom::Base
19 classes.
20  
21 =head2 EXPORT
22  
23 Please see the docs for Math::Symbolic::Custom::Base for details, but
24 you should not try to use the standard Exporter semantics with this
25 class.
26  
27 =head1 SUBROUTINES
28  
29 =cut
30  
31 package Math::Symbolic::Custom::DefaultTests;
32  
33 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 23 ▶use Data::Dumper; # for numerical equivalence test
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 23 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 23 our $VERSION = '0.613';
41  
42 23 ▶use Math::Symbolic::Custom::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 23 ▶BEGIN { *import = \&Math::Symbolic::Custom::Base::aggregate_import }
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # Class Data: Special variable required by Math::Symbolic::Custom
50 # importing/exporting functionality.
51 # All subroutines that are to be exported to the Math::Symbolic::Custom
52 # namespace should be listed here.
53  
54 23 our $Aggregate_Export = [
55 qw/
56 is_one
57 is_zero
58 is_zero_or_one
59 is_sum
60 is_constant
61 is_simple_constant
62 is_integer
63 is_identical
64 is_identical_base
65 test_num_equiv
66 /
67 ];
68  
69 =head2 is_zero()
70  
71 Returns true (1) of the tree is a constant and '0'. Returns
72 false (0) otherwise.
73  
74 =cut
75  
76 36 ▶sub is_zero {
Subroutine is_zero: called CC=3 SCAR=11.0
Pod documented
77 36 my $tree = shift;
78 36 ▶ return 0 unless $tree->term_type() == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 1 29 7 36
79 7 ▶ return 1 if $tree->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"value"} == 0 2 5 7
80 5 return 0;
81 }
82  
83 =head2 is_one()
84  
85 Returns true (1) of the tree is a constant and '1'. Returns
86 false (0) otherwise.
87  
88 =cut
89  
90 35 ▶sub is_one {
Subroutine is_one: called CC=3 SCAR=11.0
Pod documented
91 35 my $tree = shift;
92 35 ▶ return 0 unless $tree->term_type() == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 1 29 6 35
93 6 ▶ return 1 if $tree->{value} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"value"} == 1 5 1 6
94 1 return 0;
95 }
96  
97 =head2 is_zero_or_one()
98  
99 Returns true ('1' for 1, '0E0' for 0) of the tree is a constant and '1' or '0'.
100 Returns false (0) otherwise.
101  
102 =cut
103  
104 4 ▶sub is_zero_or_one {
Subroutine is_zero_or_one: called CC=4 SCAR=14.1
Pod documented
105 4 my $tree = shift;
106 4 ▶ return 0 unless $tree->term_type() == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 1 2 2 4
107 2 ▶ return 1 if $tree->{value} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"value"} == 1 1 1 2
108 1 ▶ return "0E0" if $tree->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"value"} == 0 1 0 1
109 0 return 0;
110 }
111  
112 =head2 is_integer()
113  
114 is_integer() returns a boolean.
115  
116 It returns true (1) if the tree is a constant object representing an
117 integer value. It does I<not> compute the value of the tree.
118 (eg. '5*10' is I<not> considered an integer, but '50' is.)
119  
120 It returns false (0) otherwise.
121  
122 =cut
123  
124 12 ▶sub is_integer {
Subroutine is_integer: called CC=2 SCAR=6.9
Pod documented
125 12 my $tree = shift;
126 12 ▶ return 0 unless $tree->term_type() == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
unless $tree->term_type == 1 3 9 12
127 9 my $value = $tree->value();
128 9 return ( int($value) == $value );
129 }
130  
131 =head2 is_simple_constant()
132  
133 is_simple_constant() returns a boolean.
134  
135 It returns true if the tree consists of only constants and operators.
136 As opposed to is_constant(), is_simple_constant() does not apply derivatives
137 if necessary.
138  
139 It returns false (0) otherwise.
140  
141 =cut
142  
143 1095 ▶sub is_simple_constant {
Subroutine is_simple_constant: called CC=1 SCAR=0.0
Pod documented
144 1095 my $tree = shift;
145  
146 1095 my $return = 1;
147 $tree->descend(
148 in_place => 1,
149 11372 ▶ before => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
150 11372 my $tree = shift;
151 11372 my $ttype = $tree->term_type();
152 11372 ▶ if ( $ttype == T_CONSTANT ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ttype == 1) { } 2293 9079 11372
elsif ($ttype == 2) { } 2513 6566 9079
elsif ($ttype == 0) { } 6566 0 6566
153 2293 return undef;
154 }
155 elsif ( $ttype == T_VARIABLE ) {
156 2513 $return = 0;
157 2513 return undef;
158 }
159 elsif ( $ttype == T_OPERATOR ) {
160 6566 return ();
161 }
162 else {
163 0 croak "is_simple_constant called on " . "invalid tree type.";
164 }
165 },
166 1095 );
167 1095 return $return;
168 }
169  
170 =head2 is_constant()
171  
172 is_constant() returns a boolean.
173  
174 It returns true (1) if the tree consists of only constants and operators or
175 if it becomes a tree of only constants and operators after application
176 of derivatives.
177  
178 It returns false (0) otherwise.
179  
180 If you need not pay the price of applying derivatives, you should use the
181 is_simple_constant() method instead.
182  
183 =cut
184  
185 7 ▶sub is_constant {
Subroutine is_constant: called CC=1 SCAR=0.0
Pod documented
186 7 my $tree = shift;
187  
188 7 my $return = 1;
189 $tree->descend(
190 in_place => 1,
191 68 ▶ before => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
192 68 my $tree = shift;
193 68 my $ttype = $tree->term_type();
194 68 ▶ if ( $ttype == T_CONSTANT ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ttype == 1) { } 33 35 68
elsif ($ttype == 2) { } 4 31 35
elsif ($ttype == 0) { } 31 0 31
195 33 return undef;
196 }
197 elsif ( $ttype == T_VARIABLE ) {
198 4 $return = 0;
199 4 return undef;
200 }
201 elsif ( $ttype == T_OPERATOR ) {
202 31 my $tree = $tree->apply_derivatives();
203 31 $ttype = $tree->term_type();
204 31 ▶ return undef if $ttype == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
if $ttype == 1 0 31 31
205 31 ▶ ( $return = 0 ), return undef
Branch: 1 branch
BranchTrueFalseTotal
if $ttype == 2 0 31 31
206 if $ttype == T_VARIABLE;
207  
208 31 return { descend_into => [ @{ $tree->{operands} } ], };
209 }
210 else {
211 0 croak "is_constant called on " . "invalid tree type.";
212 }
213 },
214 7 );
215 7 return $return;
216 }
217  
218 =head2 is_identical()
219  
220 is_identical() returns a boolean.
221  
222 It compares the tree it is called on to its first argument. If the first
223 argument is not a Math::Symbolic tree, it is sent through the parser.
224  
225 is_identical() returns true (1) if the trees are completely identical. That
226 includes operands of commutating operators having the same order, etc. This
227 does I<not> test of mathematical equivalence! (Which is B<much, much> harder
228 to test for. If you know how to, I<please> let me know!)
229  
230 It returns false (0) otherwise.
231  
232 =cut
233  
234 1976 ▶sub is_identical {
Subroutine is_identical: called CC=20 SCAR=30.3
Pod documented
235 1976 my $tree1 = shift;
236 1976 my $tree2 = shift;
237 1976 ▶ $tree2 = Math::Symbolic::parse_from_string($tree2)
Branch: 1 branch
BranchTrueFalseTotal
unless ref($tree2) =~ /^Math::Symbolic/ 42 1934 1976
238 if not ref($tree2) =~ /^Math::Symbolic/;
239  
240 1976 my $tt1 = $tree1->term_type();
241 1976 my $tt2 = $tree2->term_type();
242  
243 1976 ▶ if ( $tt1 != $tt2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt1 != $tt2) { } 571 1405 1976
244 571 return 0;
245 }
246 else {
247 1405 ▶ if ( $tt1 == T_VARIABLE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tt1 == 2) { } 331 1074 1405
elsif ($tt1 == 1) { } 109 965 1074
elsif ($tt1 == 0) { } 965 0 965
248 331 ▶ return 0 if $tree1->name() ne $tree2->name();
Branch: 1 branch
BranchTrueFalseTotal
if $tree1->name ne $tree2->name 12 319 331
249 319 my @sig1 = $tree1->signature();
250 319 my @sig2 = $tree2->signature();
251 319 ▶ return 0 if scalar(@sig1) != scalar(@sig2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @sig1 != scalar @sig2 0 319 319
252 319 ▶ for ( my $i = 0 ; $i < @sig1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sig1) 331 318 649
253 331 ▶ return 0 if $sig1[$i] ne $sig2[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $sig1[$i] ne $sig2[$i] 1 330 331
254 }
255 318 return 1;
256 }
257 elsif ( $tt1 == T_CONSTANT ) {
258 109 my $sp1 = $tree1->special();
259 109 my $sp2 = $tree2->special();
260 109 ▶ if ( defined $sp1
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sp1 and defined $sp2 and $sp1 eq $sp2 and $sp1 ne "" and $sp1 =~ /\S/) 1 108 109
Condition66%8/12
defined $sp1 and defined $sp2 and $sp1 eq $sp2 and $sp1 ne "" and $sp1 =~ /\S/
!ll&&!rl&&r
10801
defined $sp1 and defined $sp2 and $sp1 eq $sp2 and $sp1 ne ""
!ll&&!rl&&r
31051
defined $sp1 and defined $sp2 and $sp1 eq $sp2
!ll&&!rl&&r
03106
defined $sp1 and defined $sp2
!ll&&!rl&&r
00109
MC/DC40%2/5
defined $sp1 and defined $sp2 and $sp1 eq $sp2 and $sp1 ne "" and $sp1 =~ /\S/
defined $sp1 defined $sp2 $sp1 eq $sp2 $sp1 ne "" $sp1 =~ /\S/
Truth table50%3/6
A and B and C and D and E
A= defined $sp1B= defined $sp2C= $sp1 eq $sp2D= $sp1 ne ""E= $sp1 =~ /\S/
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
261 and defined $sp2
262 and $sp1 eq $sp2
263 and $sp1 ne ''
264 and $sp1 =~ /\S/ )
265 {
266 1 return 1;
267 }
268 108 ▶ return 1 if $tree1->value() == $tree2->value();
Branch: 1 branch
BranchTrueFalseTotal
if $tree1->value == $tree2->value 99 9 108
269 9 return 0;
270 }
271 elsif ( $tt1 == T_OPERATOR ) {
272 965 my $t1 = $tree1->type();
273 965 my $t2 = $tree2->type();
274 965 ▶ return 0 if $t1 != $t2;
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != $t2 435 530 965
275 return 0
276 530 ▶ if @{ $tree1->{operands} } != @{ $tree2->{operands} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$tree1->{"operands"};} != @{$tree2->{"operands"};} 0 530 530
277  
278 530 my $i = 0;
279 530 foreach ( @{ $tree1->{operands} } ) {
280 return 0
281 856 ▶ unless is_identical( $_, $tree2->{operands}[ $i++ ] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_identical($_, $tree2->{"operands"}[$i++]) 179 677 856
282 }
283 351 return 1;
284 }
285 else {
286 0 croak "is_identical() called on invalid term type.";
287 }
288 0 die "Sanity check in is_identical(). Should not be reached.";
289 }
290 }
291  
292 =head2 is_identical_base
293  
294 is_identical_base() returns a boolean.
295  
296 It compares the tree it is called on to its first argument. If the first
297 argument is not a Math::Symbolic tree, it is sent through the parser.
298  
299 is_identical_base() returns true (1) if the trees are identical or
300 if they are exponentiations with the same base. The same gotchas that
301 apply to is_identical apply here, too.
302  
303 For example, 'x*y' and '(x*y)^e' result in a true return value because
304 'x*y' is equal to '(x*y)^1' and this has the same base as '(x*y)^e'.
305  
306 It returns false (0) otherwise.
307  
308 =cut
309  
310 6 ▶sub is_identical_base {
Subroutine is_identical_base: called CC=6 SCAR=18.1
Pod documented
311 6 my $o1 = shift;
312 6 my $o2 = shift;
313 6 ▶ $o2 = Math::Symbolic::parse_from_string($o2)
Branch: 1 branch
BranchTrueFalseTotal
unless ref($o2) =~ /^Math::Symbolic/ 6 0 6
314 if ref($o2) !~ /^Math::Symbolic/;
315  
316 6 my $tt1 = $o1->term_type();
317 6 my $tt2 = $o2->term_type();
318  
319 6 ▶ my $so1 =
Branch: 1 branch
BranchTrueFalseTotal
$tt1 == 0 && $o1->type == 7 ? : 4 2 6
Condition66%2/3
$tt1 == 0 and $o1->type == 7
!ll&&!rl&&r
024
MC/DC50%1/2
$tt1 == 0 and $o1->type == 7
$tt1 == 0 $o1->type == 7
Truth table66%2/3
A and B
A= $tt1 == 0B= $o1->type == 7
ABresult
0X0
100
111
320 ( $tt1 == T_OPERATOR and $o1->type() == B_EXP ) ? $o1->op1() : $o1;
321 6 ▶ my $so2 =
Branch: 1 branch
BranchTrueFalseTotal
$tt2 == 0 && $o2->type == 7 ? : 3 3 6
Condition66%2/3
$tt2 == 0 and $o2->type == 7
!ll&&!rl&&r
033
MC/DC50%1/2
$tt2 == 0 and $o2->type == 7
$tt2 == 0 $o2->type == 7
Truth table66%2/3
A and B
A= $tt2 == 0B= $o2->type == 7
ABresult
0X0
100
111
322 ( $tt2 == T_OPERATOR and $o2->type() == B_EXP ) ? $o2->op1() : $o2;
323  
324 6 return Math::Symbolic::Custom::is_identical( $so1, $so2 );
325 }
326  
327 =head2 is_sum()
328  
329 (beta)
330  
331 is_constant() returns a boolean.
332  
333 It returns true (1) if the tree contains no variables (because it can then
334 be evaluated to a single constant which is a sum). It also returns true if
335 it is a sum or difference of constants and variables. Furthermore, it is
336 true for products of integers and constants because those products are really
337 sums of variables.
338 If none of the above cases match, it applies all derivatives and tries again.
339  
340 It returns false (0) otherwise.
341  
342 Please contact the author in case you encounter bugs in the specs or
343 implementation. The heuristics aren't all that great.
344  
345 =cut
346  
347 7 ▶sub is_sum {
Subroutine is_sum: called CC=1 SCAR=0.3
Pod documented
348 7 my $tree = shift;
349  
350 7 my $return = 1;
351 $tree->descend(
352 in_place => 1,
353 15 ▶ before => sub {
Subroutine __ANON__: called CC=17 SCAR=33.6
354 15 my $tree = shift;
355 15 my $ttype = $tree->term_type();
356  
357 15 ▶ if ( $ttype == T_CONSTANT or $ttype == T_VARIABLE ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 1 or $ttype == 2) { } 4 11 15
elsif ($ttype == 0) { } 11 0 11
Condition100%3/3
$ttype == 1 or $ttype == 2
l!l&&r!l&&!r
3111
MC/DC100%2/2
$ttype == 1 or $ttype == 2
$ttype == 1 $ttype == 2
Truth table100%3/3
A or B
A= $ttype == 1B= $ttype == 2
ABresult
1X1
011
000
358 4 return undef;
359 }
360 elsif ( $ttype == T_OPERATOR ) {
361 11 my $type = $tree->type();
362 11 ▶ if ( $type == B_SUM
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 0 or $type == 1 or $type == 4) { } 4 7 11
elsif ($type == 2) { } 6 1 7
elsif ($type == 5 or $type == 6) { } 1 0 1
elsif (is_constant($tree)) { } 0 0 0
Condition100%6/6
$type == 0 or $type == 1 or $type == 4
l!l&&r!l&&!r
317
$type == 0 or $type == 1
l!l&&r!l&&!r
218
MC/DC100%3/3
$type == 0 or $type == 1 or $type == 4
$type == 0 $type == 1 $type == 4
Truth table100%4/4
A or B or C
A= $type == 0B= $type == 1C= $type == 4
ABCresult
1XX1
01X1
0011
0000
363 or $type == B_DIFFERENCE
364 or $type == U_MINUS )
365 {
366 4 return ();
367 }
368 elsif ( $type == B_PRODUCT ) {
369 $return = $tree->{operands}[0]->is_integer()
370 6 ▶ || $tree->{operands}[1]->is_integer();
Condition66%2/3
$tree->{'operands'}[0]->is_integer || $tree->{'operands'}[1]->is_integer
l!l&&r!l&&!r
501
MC/DC50%1/2
$tree->{'operands'}[0]->is_integer || $tree->{'operands'}[1]->is_integer
$tree->{'operands'}[0]->is_integer $tree->{'operands'}[1]->is_integer
Truth table66%2/3
A || B
A= $tree->{'operands'}[0]->is_integerB= $tree->{'operands'}[1]->is_integer
ABresult
1X1
011
000
371 6 return undef;
372 }
373 ▶ elsif ($type == U_P_DERIVATIVE
Condition33%1/3
$type == 5 or $type == 6
l!l&&r!l&&!r
100
MC/DC0%0/2
$type == 5 or $type == 6
$type == 5 $type == 6
Truth table33%1/3
A or B
A= $type == 5B= $type == 6
ABresult
1X1
011
000
374 or $type == U_T_DERIVATIVE )
375 {
376 1 my $tree = $tree->apply_derivatives();
377 1 $tree = $tree->simplify();
378 1 my $ttype = $tree->term_type();
379 return undef
380 1 ▶ if ( $ttype == T_CONSTANT
Branch: 1 branch
BranchTrueFalseTotal
if $ttype == 1 or $ttype == 2 0 1 1
Condition33%1/3
$ttype == 1 or $ttype == 2
l!l&&r!l&&!r
001
MC/DC0%0/2
$ttype == 1 or $ttype == 2
$ttype == 1 $ttype == 2
Truth table33%1/3
A or B
A= $ttype == 1B= $ttype == 2
ABresult
1X1
011
000
381 or $ttype == T_VARIABLE );
382  
383 1 ▶ if ( $ttype == T_OPERATOR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ttype == 0) { } 1 0 1
384 1 my $type = $tree->type();
385 1 ▶ if ( $type == U_P_DERIVATIVE
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 5 or $type == 6) { } 0 1 1
Condition33%1/3
$type == 5 or $type == 6
l!l&&r!l&&!r
001
MC/DC0%0/2
$type == 5 or $type == 6
$type == 5 $type == 6
Truth table33%1/3
A or B
A= $type == 5B= $type == 6
ABresult
1X1
011
000
386 || $type == U_T_DERIVATIVE )
387 {
388 0 $return = 0;
389 0 return undef;
390 }
391 else {
392 1 return { descend_into => [$tree] };
393 }
394 }
395 else {
396 0 die "apply_derivatives "
397 . "screwed the pooch in "
398 . "is_sum().";
399 }
400 }
401 elsif ( is_constant($tree) ) {
402 0 return undef;
403 }
404 else {
405 0 $return = 0;
406 0 return undef;
407 }
408 }
409 else {
410 0 croak "is_sum called on invalid tree type.";
411 }
412 0 die;
413 },
414 7 );
415 7 return $return;
416 }
417  
418 =head2 test_num_equiv()
419  
420 Takes another Math::Symbolic tree or a code ref as first
421 argument. Tests the tree
422 it is called on and the one passed in as first argument for
423 equivalence by sampling random numbers for their parameters and
424 evaluating them.
425  
426 This is no guarantee that the functions are actually similar. The
427 computation required for this test may be very high for large
428 numbers of tests.
429  
430 In case of a subroutine reference passed in, the values of the
431 parameters of the Math::Symbolic tree are passed to the sub
432 ref sorted by the parameter names.
433  
434 Following the test-tree, there may be various options as key/value
435 pairs:
436  
437 limits: A hash reference with parameter names as keys and code refs
438 as arguments. A code ref for parameter 'x', will be executed
439 for every number of 'x' that is generated. If the code
440 returns false, the number is discarded and regenerated.
441 tests: The number of tests to carry out. Default: 20
442 epsilon: The accuracy of the numeric comparison. Default: 1e-7
443 retries: The number of attempts to make if a function evaluation
444 throws an error.
445 upper: Upper limit of the random numbers. Default: 10
446 lower: Lower limit of the random numbers. Default: -10
447  
448 =cut
449  
450 33 ▶sub test_num_equiv {
Subroutine test_num_equiv: called CC=25 SCAR=38.6
Pod documented
451 33 my ($t1, $t2) = (shift(), shift());
452 33 ▶ if (ref($t1) !~ /^Math::Symbolic/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($t1) =~ /^Math::Symbolic/) 0 33 33
453 0 croak("test_numeric_equivalence() must be called on Math::Symbolic tree");
454 }
455 33 ▶ if (ref($t2) !~ /^Math::Symbolic/ and ref($t2) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref($t2) =~ /^Math::Symbolic/ and ref $t2 ne "CODE") 0 33 33
Condition66%2/3
not ref($t2) =~ /^Math::Symbolic/ and ref $t2 ne "CODE"
!ll&&!rl&&r
10230
MC/DC0%0/2
not ref($t2) =~ /^Math::Symbolic/ and ref $t2 ne "CODE"
not ref($t2) =~ /^Math::Symbolic/ ref $t2 ne "CODE"
Truth table66%2/3
not(A) and B
A= not ref($t2) =~ /^Math::Symbolic/B= ref $t2 ne "CODE"
ABresult
0X0
100
111
456 0 croak("first argument to test_numeric_equivalence() must be a Math::Symbolic tree or a code reference");
457 }
458  
459 33 ▶ my $is_code = ref($t2) eq 'CODE' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $t2 eq 'CODE' ? : 23 10 33
460  
461 33 my %args = @_;
462 33 ▶ my $limits = $args{limits} || {};
Condition100%2/2
$args{'limits'} || {}
l!l
1617
MC/DC100%1/1
$args{'limits'} || {}
$args{'limits'}
Truth table100%2/2
A
A= $args{'limits'}
Aresult
11
00
463 33 ▶ my $tests = $args{tests} || 20;
Condition50%1/2
$args{'tests'} || 20
l!l
033
MC/DC0%0/1
$args{'tests'} || 20
$args{'tests'}
Truth table50%1/2
A
A= $args{'tests'}
Aresult
11
00
464 33 ▶ my $eps = $args{epsilon} || 1e-7;
Condition50%1/2
$args{'epsilon'} || 1e-07
l!l
033
MC/DC0%0/1
$args{'epsilon'} || 1e-07
$args{'epsilon'}
Truth table50%1/2
A
A= $args{'epsilon'}
Aresult
11
00
465 33 ▶ my $retries = $args{retries} || 5;
Condition50%1/2
$args{'retries'} || 5
l!l
033
MC/DC0%0/1
$args{'retries'} || 5
$args{'retries'}
Truth table50%1/2
A
A= $args{'retries'}
Aresult
11
00
466 33 ▶ my $upper = $args{upper} || 10;
Condition50%1/2
$args{'upper'} || 10
l!l
033
MC/DC0%0/1
$args{'upper'} || 10
$args{'upper'}
Truth table50%1/2
A
A= $args{'upper'}
Aresult
11
00
467 33 ▶ my $lower = $args{lower} || -10;
Condition50%1/2
$args{'lower'} || -10
l!l
033
MC/DC0%0/1
$args{'lower'} || -10
$args{'lower'}
Truth table50%1/2
A
A= $args{'lower'}
Aresult
11
00
468  
469 33 my @s1 = $t1->signature();
470 33 ▶ my @s2 = $is_code ? () : $t2->signature();
Branch: 1 branch
BranchTrueFalseTotal
$is_code ? : 23 10 33
471  
472 33 my %sig = map {($_=>undef)} @s1, @s2;
473  
474 33 my $mult = $upper-$lower;
475  
476 33 my $retry = 0;
477 33 foreach (1..$tests) {
478 660 ▶ croak("Could not evaluate test functions with numbers -10..10")
Branch: 1 branch
BranchTrueFalseTotal
if $retry > $retries - 1 0 660 660
479 if $retry > $retries-1;
480 660 for (keys %sig) {
481 2615 my $num = rand()*$mult - $mult/2;
482 2615 ▶ redo if $limits->{$_} and not $limits->{$_}->($num);
Branch: 1 branch
BranchTrueFalseTotal
if $limits->{$_} and not $limits->{$_}->($num) 1675 940 2615
Condition100%3/3
$limits->{$_} and not $limits->{$_}->($num)
!ll&&!rl&&r
6003401675
MC/DC100%2/2
$limits->{$_} and not $limits->{$_}->($num)
$limits->{$_} not $limits->{$_}->($num)
Truth table100%3/3
A and not(B)
A= $limits->{$_}B= not $limits->{$_}->($num)
ABresult
0X0
100
111
483 940 $sig{$_} = $num;
484 }
485  
486 23 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 660 my($y1, $y2);
488 660 eval {$y1 = $t1->value(%sig);};
489 660 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 660 660
490 0 warn "error during evaluation: $@";
491 0 $retry++;
492 0 $mult /= 2;
493 0 redo;
494 }
495 660 ▶ if ($is_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_code) { } 460 200 660
496 460 eval {$y2 = $t2->(map {$sig{$_}} sort keys %sig)};
497 }
498 else {
499 200 eval {$y2 = $t2->value(%sig);};
500 }
501 660 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 660 660
502 0 warn "error during evaluation: $@";
503 0 $retry++;
504 0 $mult /= 2;
505 0 redo;
506 }
507  
508 660 ▶ if (not defined $y1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $y1) { } 0 660 660
elsif (not defined $y2) { } 0 660 660
509 0 warn "Result of '$t1' not defined; ".Dumper(\%sig);
510 0 ▶ next if not defined $y2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y2 0 0 0
511 0 $retry++;
512 0 redo;
513 }
514 elsif (not defined $y2) {
515 0 warn "Result of '$t2' not defined; ".Dumper(\%sig);
516 0 $retry++;
517 0 redo;
518 }
519  
520  
521 660 ▶ warn("1: $y1, 2: $y2; ".Dumper(\%sig)), return 0 if $y1+$eps < $y2 or $y1-$eps > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 + $eps < $y2 or $y1 - $eps > $y2 0 660 660
Condition33%1/3
$y1 + $eps < $y2 or $y1 - $eps > $y2
l!l&&r!l&&!r
00660
MC/DC0%0/2
$y1 + $eps < $y2 or $y1 - $eps > $y2
$y1 + $eps < $y2 $y1 - $eps > $y2
Truth table33%1/3
A or B
A= $y1 + $eps < $y2B= $y1 - $eps > $y2
ABresult
1X1
011
000
522  
523 660 $mult = $upper-$lower;
524 660 $retry = 0;
525 }
526  
527 33 return 1;
528 }
529  
530 23 1;
531 __END__
« Symbolic/Operator.pm ↑ Summary Symbolic/Custom/DefaultMods.pm »