Formula/Type.pm

filter: Statementstmt 86.0% 382 / 444 Branchbran 59.0% 176 / 298 Conditioncond 74.4% 70 / 94 MC/DCmcdc 60.7% 34 / 56 Subroutinesub 88.2% 113 / 128 Podpod 50.0% 3 / 6 totaltotal 75.8% 778 / 1026 CC 185 SCAR 33.0
CC
185
Cov
75%
_to_regexp
200.3
infix
182.0
infix
22.5
CRAP 27.2
« Formula.pm ↑ Summary Formula/Token.pm »
1 # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use v5.16; # fc
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package Math::Formula::Type;
10 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 $VERSION = '0.10';
12  
13 13 ▶use base 'Math::Formula::Token';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #!!! The declarations of all other packages in this file are indented to avoid
16 #!!! indexing by CPAN.
17  
18 13 ▶use Log::Report 'math-formula', import => [ qw/error __x/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Object is an ARRAY. The first element is the token, as read from the formula
21 # or constructed from a computed value. The second is a value, which can be
22 # used in computation. More elements are type specific.
23  
24 # new() is implemented in the base-class, but documented here
25  
26 #-----------------
27  
28 sub cast($)
29 6 ▶{ my ($self, $to, $context) = @_;
Subroutine cast: called CC=2 SCAR=6.9
Pod documented
30  
31 6 ▶ return MF::STRING->new(undef, $self->token)
Branch: 1 branch
BranchTrueFalseTotal
if $to eq "MF::STRING" 3 3 6
32 if $to eq 'MF::STRING';
33  
34 3 undef;
35 }
36  
37 # token() is implemented in de base-class ::Token, but documented here
38  
39  
40 # Returns a value as result of a calculation.
41 # nothing to compute for most types: simply itself
42 225 ▶sub _compute { $_[0] }
Subroutine _compute: called CC=1 SCAR=0.0
43  
44  
45 355 ▶sub value { my $self = shift; $self->[1] //= $self->_value($self->[0], @_) }
Subroutine value: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$self->[1] //= $self->_value($self->[0], @_)
l!l&&r!l&&!r
14818918
MC/DC100%2/2
$self->[1] //= $self->_value($self->[0], @_)
$self->[1] $self->_value($self->[0], @_)
Truth table100%3/3
A //= B
A= $self->[1]B= $self->_value($self->[0], @_)
ABresult
1X1
011
000
46 0 ▶sub _value { $_[1] }
Subroutine _value: not called CC=1 SCAR=6.9
47  
48  
49 1 ▶sub collapsed($) { $_[0]->token =~ s/\s+/ /gr =~ s/^ //r =~ s/ $//r }
Subroutine collapsed: called CC=1 SCAR=0.0
Pod documented
50  
51 sub prefix
52 0 ▶{ my ($self, $op, $child) = @_;
Subroutine prefix: not called CC=1 SCAR=6.9
Pod undocumented
53  
54 0 error __x"cannot find prefx operator '{op}' on a ({child})",
55 op => $op, child => $child;
56 }
57  
58 0 ▶sub attribute { warn "Cannot find attribute $_[1] for ".(ref $_[0]) }
Subroutine attribute: not called CC=1 SCAR=6.9
Pod undocumented
59  
60 sub infix($@)
61 23 ▶{ my $self = shift;
Subroutine infix: called CC=5 SCAR=16.8
Pod undocumented
62 23 my ($op, $right, $context) = @_;
63  
64 23 ▶ if($op eq '.' && $right->isa('MF::NAME'))
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "." and $right->isa("MF::NAME")) 22 1 23
Condition66%2/3
$op eq "." and $right->isa("MF::NAME")
!ll&&!rl&&r
1022
MC/DC50%1/2
$op eq "." and $right->isa("MF::NAME")
$op eq "." $right->isa("MF::NAME")
Truth table66%2/3
A and B
A= $op eq "."B= $right->isa("MF::NAME")
ABresult
0X0
100
111
65 22 ▶ { if(my $attr = $self->attribute($right->token))
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $self->attribute($right->token)) 22 0 22
66 22 { return $attr->($self, @_);
67 }
68 }
69  
70 # object used as string
71 1 ▶ return $self->cast('MF::STRING', $context)->infix(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "~" 1 0 1
72 if $op eq '~';
73  
74 0 error __x"cannot find infix operator '{op}' for ({left} -> {right})",
75 op => $op, left => ref $self, right => ref $right;
76 }
77  
78 #-----------------
79  
80 package MF::BOOLEAN;
81 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 13 $VERSION = '0.10';
83  
84  
85 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 sub prefix($)
88 2 ▶{ my ($self, $op) = @_;
Subroutine prefix: called CC=2 SCAR=7.6
89 2 ▶ if($op eq 'not')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "not") 2 0 2
90 2 { return MF::BOOLEAN->new(undef, ! $self->value);
91 }
92 0 $self->SUPER::prefix($op);
93 }
94  
95 sub infix($$$)
96 22 ▶{ my $self = shift;
Subroutine infix: called CC=10 SCAR=23.3
97 22 my ($op, $right, $context) = @_;
98  
99 22 ▶ if(my $r = $right->isa('MF::BOOLEAN') ? $right : $right->cast('MF::BOOLEAN', $context))
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $right->isa("MF::BOOLEAN") ? $right : $right->cast("MF::BOOLEAN", $context)) 22 0 22
$right->isa('MF::BOOLEAN') ? : 18 4 22
100 22 ▶ { my $v = $op eq 'and' ? ($self->value and $r->value)
Branch: 3 branches
BranchTrueFalseTotal
$op eq 'and' ? : 10 12 22
$op eq 'or' ? : 6 6 12
$op eq 'xor' ? : 6 0 6
Condition100%10/10
$self->value and $r->value
!ll&&!rl&&r
424
$self->value or $r->value
l!l&&r!l&&!r
312
$self->value xor $r->value
l&&rl&&!r!l&&r!l&&!r
2211
MC/DC100%2/2
$self->value and $r->value
$self->value $r->value
MC/DC100%2/2
$self->value or $r->value
$self->value $r->value
MC/DC100%2/2
$self->value xor $r->value
$self->value $r->value
Truth table100%3/3
A and B
A= $self->valueB= $r->value
ABresult
0X0
100
111
Truth table100%3/3
A or B
A= $self->valueB= $r->value
ABresult
1X1
011
000
Truth table100%4/4
A xor B
A= $self->valueB= $r->value
ABresult
110
101
011
000
101 : $op eq 'or' ? ($self->value or $r->value)
102 : $op eq 'xor' ? ($self->value xor $r->value)
103 : undef;
104  
105 22 ▶ return MF::BOOLEAN->new(undef, $v) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 22 0 22
106 }
107  
108 0 $self->SUPER::infix(@_);
109 }
110  
111 33 ▶sub _token($) { $_[1] ? 'true' : 'false' }
Subroutine _token: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 17 16 33
112 29 ▶sub _value($) { $_[1] eq 'true' }
Subroutine _value: called CC=1 SCAR=0.0
113  
114 #-----------------
115  
116 package MF::STRING;
117 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 13 $VERSION = '0.10';
119  
120  
121 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 13 ▶use Unicode::Collate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
124 13 my $collate = Unicode::Collate->new; #XXX which options do we need?
125  
126 9 ▶sub _token($) { '"' . ($_[1] =~ s/[\"]/\\$1/gr) . '"' }
Subroutine _token: called CC=1 SCAR=0.0
127  
128 sub _value($)
129 30 ▶{ my $token = $_[1];
Subroutine _value: called CC=3 SCAR=11.1
130  
131 30 ▶ substr($token, 0, 1) eq '"' ? $token =~ s/^"//r =~ s/"$//r =~ s/\\([\\"])/$1/gr
Branch: 2 branches
BranchTrueFalseTotal
substr($token, 0, 1) eq '"' ? : 28 2 30
substr($token, 0, 1) eq q['] ? : 2 0 2
132 : substr($token, 0, 1) eq "'" ? $token =~ s/^'//r =~ s/'$//r =~ s/\\([\\'])/$1/gr
133 : $token; # from code
134 }
135  
136 sub cast($)
137 10 ▶{ my ($self, $to) = @_;
Subroutine cast: called CC=3 SCAR=11.1
138  
139 10 ▶ $to eq 'MF::REGEXP' ? MF::REGEXP->_from_string($_[0])
Branch: 2 branches
BranchTrueFalseTotal
$to eq 'MF::REGEXP' ? : 5 5 10
$to eq 'MF::PATTERN' ? : 5 0 5
140 : $to eq 'MF::PATTERN' ? MF::PATTERN->_from_string($_[0])
141 : $self->SUPER::cast($to);
142 }
143  
144 sub infix($$$)
145 16 ▶{ my $self = shift;
Subroutine infix: called CC=20 SCAR=31.2
146 16 my ($op, $right, $context) = @_;
147  
148 16 ▶ if($op eq '~')
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '~') { } 3 13 16
elsif ($op eq '=~' or $op eq '!~') { } 4 9 13
elsif ($op eq 'like' or $op eq 'unlike') { } 4 5 9
elsif ($op eq 'cmp') { } 3 2 5
149 3 ▶ { my $r = $right->isa('MF::STRING') ? $right : $right->cast('MF::STRING', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::STRING') ? : 2 1 3
150 3 ▶ return MF::STRING->new(undef, $self->value . $r->value) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 3 0 3
151 }
152 ▶ elsif($op eq '=~' || $op eq '!~')
Condition100%3/3
$op eq "=~" or $op eq "!~"
l!l&&r!l&&!r
229
MC/DC100%2/2
$op eq "=~" or $op eq "!~"
$op eq "=~" $op eq "!~"
Truth table100%3/3
A or B
A= $op eq "=~"B= $op eq "!~"
ABresult
1X1
011
000
153 4 ▶ { my $r = $right->isa('MF::REGEXP') ? $right : $right->cast('MF::REGEXP', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::REGEXP') ? : 0 4 4
154 4 ▶ my $v = ! $r ? undef
Branch: 2 branches
BranchTrueFalseTotal
!$r ? : 0 4 4
$op eq '=~' ? : 2 2 4
155 : $op eq '=~' ? $self->value =~ $r->regexp : $self->value !~ $r->regexp;
156 4 ▶ return MF::BOOLEAN->new(undef, $v // 0) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 4 0 4
Condition50%1/2
$v // 0
l!l
40
MC/DC0%0/1
$v // 0
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
157 }
158 ▶ elsif($op eq 'like' || $op eq 'unlike')
Condition100%3/3
$op eq "like" or $op eq "unlike"
l!l&&r!l&&!r
225
MC/DC100%2/2
$op eq "like" or $op eq "unlike"
$op eq "like" $op eq "unlike"
Truth table100%3/3
A or B
A= $op eq "like"B= $op eq "unlike"
ABresult
1X1
011
000
159 4 ▶ { my $r = $right->isa('MF::PATTERN') ? $right : $right->cast('MF::PATTERN', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::PATTERN') ? : 0 4 4
160 4 ▶ my $v = ! $r ? undef
Branch: 2 branches
BranchTrueFalseTotal
!$r ? : 0 4 4
$op eq 'like' ? : 2 2 4
161 : $op eq 'like' ? $self->value =~ $r->regexp : $self->value !~ $r->regexp;
162 4 ▶ return MF::BOOLEAN->new(undef, $v // 0) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 4 0 4
Condition50%1/2
$v // 0
l!l
40
MC/DC0%0/1
$v // 0
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
163 }
164 elsif($op eq 'cmp')
165 3 ▶ { my $r = $right->isa('MF::STRING') ? $right : $right->cast('MF::STRING', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::STRING') ? : 3 0 3
166 3 return MF::INTEGER->new(undef, $collate->cmp($self->value, $right->value));
167 }
168  
169 2 $self->SUPER::infix(@_);
170 }
171  
172 my %string_attrs = (
173 1 ▶ length => sub { MF::INTEGER->new(undef, length($_[0]->value)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
174 1 ▶ lower => sub { MF::STRING->new(undef, fc($_[0]->value)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 13 );
176  
177 2 ▶sub attribute($) { $string_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
Subroutine attribute: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$string_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 2 2
178  
179 #-----------------
180  
181 package MF::INTEGER;
182 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 13 $VERSION = '0.10';
184  
185  
186 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
187 13 ▶use Log::Report 'math-formula', import => [ qw/error __x/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 sub cast($)
190 15 ▶{ my ($self, $to) = @_;
Subroutine cast: called CC=4 SCAR=13.9
191 15 ▶ $to eq 'MF::BOOLEAN' ? MF::BOOLEAN->new(undef, $_[0]->value == 0 ? 0 : 1)
Branch: 3 branches
BranchTrueFalseTotal
$to eq 'MF::BOOLEAN' ? : 12 3 15
$_[0]->value == 0 ? : 4 8 12
$to eq 'MF::FLOAT' ? : 0 3 3
192 : $to eq 'MF::FLOAT' ? MF::FLOAT->new(undef, $_[0]->value)
193 : $self->SUPER::cast($to);
194 }
195  
196 sub prefix($)
197 9 ▶{ my ($self, $op) = @_;
Subroutine prefix: called CC=3 SCAR=11.1
198 9 ▶ $op eq '+' ? $self
Branch: 2 branches
BranchTrueFalseTotal
$op eq '+' ? : 4 5 9
$op eq '-' ? : 5 0 5
199 : $op eq '-' ? MF::INTEGER->new(undef, - $self->value)
200 : $self->SUPER::prefix($op)
201 }
202  
203 sub infix($$$)
204 31 ▶{ my $self = shift;
Subroutine infix: called CC=15 SCAR=27.1
205 31 my ($op, $right, $context) = @_;
206  
207 31 ▶ return $self->cast('MF::BOOLEAN', $context)->infix(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "and" or $op eq "or" or $op eq "xor" 3 25 28
Condition100%6/6
$op eq "and" or $op eq "or" or $op eq "xor"
l!l&&r!l&&!r
4225
$op eq "and" or $op eq "or"
l!l&&r!l&&!r
3127
MC/DC100%3/3
$op eq "and" or $op eq "or" or $op eq "xor"
$op eq "and" $op eq "or" $op eq "xor"
Truth table100%4/4
A or B or C
A= $op eq "and"B= $op eq "or"C= $op eq "xor"
ABCresult
1XX1
01X1
0011
0000
208 if $op eq 'and' || $op eq 'or' || $op eq 'xor';
209  
210 25 ▶ if($right->isa('MF::INTEGER') || $right->isa('MF::FLOAT'))
Branch: 1 branch
BranchTrueFalseTotal
if ($right->isa("MF::INTEGER") or $right->isa("MF::FLOAT")) 21 4 25
Condition100%3/3
$right->isa("MF::INTEGER") or $right->isa("MF::FLOAT")
l!l&&r!l&&!r
1474
MC/DC100%2/2
$right->isa("MF::INTEGER") or $right->isa("MF::FLOAT")
$right->isa("MF::INTEGER") $right->isa("MF::FLOAT")
Truth table100%3/3
A or B
A= $right->isa("MF::INTEGER")B= $right->isa("MF::FLOAT")
ABresult
1X1
011
000
211 21 ▶ { my $v = $op eq '+' ? $self->value + $right->value
Branch: 4 branches
BranchTrueFalseTotal
$op eq '+' ? : 5 16 21
$op eq '-' ? : 4 12 16
$op eq '*' ? : 3 9 12
$op eq '%' ? : 2 7 9
212 : $op eq '-' ? $self->value - $right->value
213 : $op eq '*' ? $self->value * $right->value
214 : $op eq '%' ? $self->value % $right->value
215 : undef;
216 21 ▶ return ref($right)->new(undef, $v) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 14 7 21
217  
218 7 ▶ return MF::INTEGER->new(undef, $self->value <=> $right->value)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<=>" 6 1 7
219 if $op eq '<=>';
220  
221 1 ▶ return MF::FLOAT->new(undef, $self->value / $right->value)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "/" 1 0 1
222 if $op eq '/';
223 }
224  
225 4 ▶ return $right->infix($op, $self, @_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "*" and $right->isa("MF::DURATION") 1 3 4
Condition66%2/3
$op eq "*" and $right->isa("MF::DURATION")
!ll&&!rl&&r
301
MC/DC50%1/2
$op eq "*" and $right->isa("MF::DURATION")
$op eq "*" $right->isa("MF::DURATION")
Truth table66%2/3
A and B
A= $op eq "*"B= $right->isa("MF::DURATION")
ABresult
0X0
100
111
226 if $op eq '*' && $right->isa('MF::DURATION');
227  
228 3 $self->SUPER::infix(@_);
229 }
230  
231 13 my $gibi = 1024 * 1024 * 1024;
232  
233 13 my $multipliers = '[kMGTEZ](?:ibi)?\b';
234 13 ▶sub _multipliers { $multipliers }
Subroutine _multipliers: called CC=1 SCAR=0.0
235  
236 13 my %multipliers = (
237 k => 1000, M => 1000_000, G => 1000_000_000, T => 1000_000_000_000, E => 1e15, Z => 1e18,
238 kibi => 1024, Mibi => 1024*1024, Gibi => $gibi, Tibi => 1024*$gibi, Eibi => 1024*1024*$gibi,
239 Zibi => $gibi*$gibi,
240 );
241  
242 sub _value($)
243 58 ▶{ my ($v, $m) = $_[1] =~ m/^ ( [0-9]+ (?: _[0-9][0-9][0-9] )* ) ($multipliers)? $/x
Subroutine _value: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
() = $_[1] =~ /^ ( [0-9]+ (?: _[0-9][0-9][0-9] )* ) ($multipliers)? $/ux or &error(&__x("illegal number format for '{string}'", "string", $_[1])) 0 58 58
244 or error __x"illegal number format for '{string}'", string => $_[1];
245  
246 58 ▶ ($1 =~ s/_//gr) * ($2 ? $multipliers{$2} : 1);
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 3 55 58
247 }
248  
249 my %int_attrs = (
250 2 ▶ abs => sub { $_[0]->value < 0 ? MF::INTEGER->new(undef, - $_[0]->value) : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->value < 0 ? : 1 1 2
251 13 );
252 2 ▶sub attribute($) { $int_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
Subroutine attribute: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$int_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 2 2
253  
254 #-----------------
255  
256 package MF::FLOAT;
257 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
258 13 $VERSION = '0.10';
259  
260  
261 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
262 13 ▶use POSIX qw/floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 13 ▶sub _match { '[0-9]+ (?: \.[0-9]+ (?: e [+-][0-9]+ )? | e [+-][0-9]+ )' }
Subroutine _match: called CC=1 SCAR=0.0
265 7 ▶sub _value($) { $_[1] + 0.0 }
Subroutine _value: called CC=1 SCAR=0.0
266 7 ▶sub _token($) { my $t = sprintf '%g', $_[1]; $t =~ /[e.]/ ? $t : "$t.0" }
Subroutine _token: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /[e.]/u ? : 2 5 7
267  
268 sub cast($)
269 0 ▶{ my ($self, $to) = @_;
Subroutine cast: not called CC=2 SCAR=17.9
270 0 ▶ $to eq 'MF::INTEGER' ? MF::INTEGER->new(undef, floor($_[0]->value))
Branch: 1 branch
BranchTrueFalseTotal
$to eq 'MF::INTEGER' ? : 0 0 0
271 : $self->SUPER::cast($to);
272 }
273  
274 sub prefix($)
275 0 ▶{ my ($self, $op) = @_;
Subroutine prefix: not called CC=3 SCAR=24.8
276 0 ▶ $op eq '+' ? $self
Branch: 2 branches
BranchTrueFalseTotal
$op eq '+' ? : 0 0 0
$op eq '-' ? : 0 0 0
277 : $op eq '-' ? MF::FLOAT->new(undef, - $self->value)
278 : $self->SUPER::prefix($op)
279 }
280  
281 sub infix($$$)
282 0 ▶{ my $self = shift;
Subroutine infix: not called CC=13 SCAR=52.0
283 0 my ($op, $right, $context) = @_;
284  
285 0 ▶ return $self->cast('MF::BOOLEAN', $context)->infix(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "and" or $op eq "or" or $op eq "xor" 0 0 0
Condition0%0/6
$op eq "and" or $op eq "or" or $op eq "xor"
l!l&&r!l&&!r
000
$op eq "and" or $op eq "or"
l!l&&r!l&&!r
000
MC/DC0%0/3
$op eq "and" or $op eq "or" or $op eq "xor"
$op eq "and" $op eq "or" $op eq "xor"
Truth table0%0/4
A or B or C
A= $op eq "and"B= $op eq "or"C= $op eq "xor"
ABCresult
1XX1
01X1
0011
0000
286 if $op eq 'and' || $op eq 'or' || $op eq 'xor';
287  
288 0 ▶ if($right->isa('MF::FLOAT') || $right->isa('MF::INTEGER'))
Branch: 1 branch
BranchTrueFalseTotal
if ($right->isa("MF::FLOAT") or $right->isa("MF::INTEGER")) 0 0 0
Condition0%0/3
$right->isa("MF::FLOAT") or $right->isa("MF::INTEGER")
l!l&&r!l&&!r
000
MC/DC0%0/2
$right->isa("MF::FLOAT") or $right->isa("MF::INTEGER")
$right->isa("MF::FLOAT") $right->isa("MF::INTEGER")
Truth table0%0/3
A or B
A= $right->isa("MF::FLOAT")B= $right->isa("MF::INTEGER")
ABresult
1X1
011
000
289 { # Perl will upgrade the integers
290 0 ▶ my $v = $op eq '+' ? $self->value + $right->value
Branch: 5 branches
BranchTrueFalseTotal
$op eq '+' ? : 0 0 0
$op eq '-' ? : 0 0 0
$op eq '*' ? : 0 0 0
$op eq '%' ? : 0 0 0
$op eq '/' ? : 0 0 0
291 : $op eq '-' ? $self->value - $right->value
292 : $op eq '*' ? $self->value * $right->value
293 : $op eq '%' ? $self->value % $right->value
294 : $op eq '/' ? $self->value / $right->value
295 : undef;
296 0 ▶ return MF::FLOAT->new(undef, $v) if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
297  
298 0 ▶ return MF::INTEGER->new(undef, $self->value <=> $right->value)
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<=>" 0 0 0
299 if $op eq '<=>';
300 }
301 0 $self->SUPER::infix(@_);
302 }
303  
304 # I really do not want a math library in here! Use formulas with CODE expr
305 # my %float_attrs;
306 #sub attribute($) { $float_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
307  
308  
309 #-----------------
310  
311 package MF::DATETIME;
312 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
313 13 $VERSION = '0.10';
314  
315  
316 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
317 13 ▶use DateTime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
318
319 sub _match {
320 26 ▶ '[12][0-9]{3} \- (?:0[1-9]|1[012]) \- (?:0[1-9]|[12][0-9]|3[01]) T '
Subroutine _match: called CC=1 SCAR=0.0
321 . '(?:[01][0-9]|2[0-3]) \: [0-5][0-9] \: (?:[0-5][0-9]) (?:\.[0-9]+)?'
322 . '(?:[+-][0-9]{4})?';
323 }
324  
325 3 ▶sub _token($) { $_[1]->datetime . ($_[1]->time_zone->name =~ s/UTC$/+0000/r) }
Subroutine _token: called CC=1 SCAR=0.0
326  
327 sub _value($)
328 37 ▶{ my ($self, $token) = @_;
Subroutine _value: called CC=4 SCAR=13.9
329 37 ▶ $token =~ m/^
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /^ ([12][0-9]{3}) \- (0[1-9]|1[012]) \- (0[1-9]|[12][0-9]|3[01]) T ([01][0-9]|2[0-3]) \: ([0-5][0-9]) \: ([0-5][0-9]|6[01]) (?:(\.[0-9]+))? ([+-] [0-9]{4})? $ /ux or return 0 37 37
330 ([12][0-9]{3}) \- (0[1-9]|1[012]) \- (0[1-9]|[12][0-9]|3[01]) T
331 ([01][0-9]|2[0-3]) \: ([0-5][0-9]) \: ([0-5][0-9]|6[01]) (?:(\.[0-9]+))?
332 ([+-] [0-9]{4})?
333 $ /x or return;
334  
335 37 ▶ my $tz_offset = $8 // '+0000'; # careful with named matches :-(
Condition100%2/2
$8 // "+0000"
l!l
1225
MC/DC100%1/1
$8 // "+0000"
$8
Truth table100%2/2
A
A= $8
Aresult
11
00
336 37 ▶ my @args = ( year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6,
Condition100%2/2
$7 // 0
l!l
1522
MC/DC100%1/1
$7 // 0
$7
Truth table100%2/2
A
A= $7
Aresult
11
00
337 nanosecond => ($7 // 0) * 1_000_000_000 );
338 37 my $tz = DateTime::TimeZone::OffsetOnly->new(offset => $tz_offset);
339  
340 37 DateTime->new(@args, time_zone => $tz);
341 }
342  
343 sub _to_time($)
344 2 ▶{ +{ hour => $_[0]->hour, minute => $_[0]->minute, second => $_[0]->second, ns => $_[0]->nanosecond };
Subroutine _to_time: called CC=1 SCAR=0.0
345 }
346  
347 sub cast($)
348 2 ▶{ my ($self, $to) = @_;
Subroutine cast: called CC=3 SCAR=11.1
349 2 ▶ $to eq 'MF::TIME' ? MF::TIME->new(undef, _to_time($_[0]->value))
Branch: 2 branches
BranchTrueFalseTotal
$to eq 'MF::TIME' ? : 1 1 2
$to eq 'MF::DATE' ? : 1 0 1
350 : $to eq 'MF::DATE' ? MF::DATE->new(undef, $_[0]->value->clone)
351 : $self->SUPER::cast($to);
352 }
353  
354 sub infix($$$@)
355 21 ▶{ my $self = shift;
Subroutine infix: called CC=14 SCAR=26.6
356 21 my ($op, $right, $context) = @_;
357  
358 21 ▶ if($op eq '+' || $op eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" or $op eq "-") 3 18 21
Condition100%3/3
$op eq "+" or $op eq "-"
l!l&&r!l&&!r
1218
MC/DC100%2/2
$op eq "+" or $op eq "-"
$op eq "+" $op eq "-"
Truth table100%3/3
A or B
A= $op eq "+"B= $op eq "-"
ABresult
1X1
011
000
359 3 { my $dt = $self->value->clone;
360 3 ▶ if($right->isa('MF::DURATION'))
Branch: 1 branch
BranchTrueFalseTotal
if ($right->isa("MF::DURATION")) 2 1 3
361 2 ▶ { my $v = $op eq '+' ? $dt->add_duration($right->value) : $dt->subtract_duration($right->value);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 1 1 2
362 2 return MF::DATETIME->new(undef, $v);
363 }
364 1 ▶ if($op eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "-") 1 0 1
365 1 ▶ { my $r = $right->isa('MF::DATETIME') ? $right : $right->cast('MF::DATETIME', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::DATETIME') ? : 1 0 1
366 1 return MF::DURATION->new(undef, $dt->subtract_datetime($right->value));
367 }
368 }
369  
370 18 ▶ if($op eq '<=>')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<=>") 8 10 18
371 8 ▶ { return MF::INTEGER->new(undef, DateTime->compare($self->value, $right->value))
Branch: 1 branch
BranchTrueFalseTotal
if $right->isa("MF::DATETIME") 5 3 8
372 if $right->isa('MF::DATETIME');
373  
374 3 ▶ if($right->isa('MF::DATE'))
Branch: 1 branch
BranchTrueFalseTotal
if ($right->isa("MF::DATE")) 3 0 3
375 { # Many timezone problems solved by DateTime
376 3 my $date = $right->token;
377 3 ▶ my $begin = $self->_value($date =~ /\+/ ? $date =~ s/\+/T00:00:00+/r : $date.'T00:00:00');
Branch: 1 branch
BranchTrueFalseTotal
$date =~ /\+/u ? : 0 3 3
378 3 ▶ return MF::INTEGER->new(undef, -1) if DateTime->compare($begin, $self->value) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if "DateTime"->compare($begin, $self->value) > 0 1 2 3
379  
380 2 ▶ my $end = $self->_value($date =~ /\+/ ? $date =~ s/\+/T23:59:59+/r : $date.'T23:59:59');
Branch: 1 branch
BranchTrueFalseTotal
$date =~ /\+/u ? : 0 2 2
381 2 ▶ return MF::INTEGER->new(undef, DateTime->compare($self->value, $end) > 0 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
'DateTime'->compare($self->value, $end) > 0 ? : 1 1 2
382 }
383 }
384  
385 10 $self->SUPER::infix(@_);
386 }
387  
388 my %dt_attrs = (
389 1 ▶ 'time' => sub { MF::TIME->new(undef, _to_time($_[0]->value)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
390 1 ▶ date => sub { MF::DATE->new(undef, $_[0]->value) }, # dt's are immutable
Subroutine __ANON__: called CC=1 SCAR=0.0
391 1 ▶ hour => sub { MF::INTEGER->new(undef, $_[0]->value->hour) },
Subroutine __ANON__: called CC=1 SCAR=0.0
392 1 ▶ minute => sub { MF::INTEGER->new(undef, $_[0]->value->minute) },
Subroutine __ANON__: called CC=1 SCAR=0.0
393 1 ▶ second => sub { MF::INTEGER->new(undef, $_[0]->value->second) },
Subroutine __ANON__: called CC=1 SCAR=0.0
394 1 ▶ fracsec => sub { MF::FLOAT ->new(undef, $_[0]->value->fractional_second) },
Subroutine __ANON__: called CC=1 SCAR=0.0
395 13 );
396  
397 sub attribute($)
398 10 ▶{ $dt_attrs{$_[1]} || $MF::DATE::date_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]);
Subroutine attribute: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
$dt_attrs{$_[1]} or $MF::DATE::date_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 10 10
Condition66%2/3
$dt_attrs{$_[1]} or $MF::DATE::date_attrs{$_[1]}
l!l&&r!l&&!r
640
MC/DC0%0/2
$dt_attrs{$_[1]} or $MF::DATE::date_attrs{$_[1]}
$dt_attrs{$_[1]} $MF::DATE::date_attrs{$_[1]}
Truth table66%2/3
A or B
A= $dt_attrs{$_[1]}B= $MF::DATE::date_attrs{$_[1]}
ABresult
1X1
011
000
399 }
400  
401 #-----------------
402  
403 package MF::DATE;
404 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
405 13 $VERSION = '0.10';
406  
407  
408 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
409  
410 13 ▶use Log::Report 'math-formula', import => [ qw/error warning __x/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
411  
412 13 ▶use DateTime::TimeZone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
413 13 ▶use DateTime::TimeZone::OffsetOnly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
414  
415 26 ▶sub _match { '[12][0-9]{3} \- (?:0[1-9]|1[012]) \- (?:0[1-9]|[12][0-9]|3[01]) (?:[+-][0-9]{4})?' }
Subroutine _match: called CC=1 SCAR=0.0
416  
417 5 ▶sub _token($) { $_[1]->ymd . ($_[1]->time_zone->name =~ s/UTC$/+0000/r) }
Subroutine _token: called CC=1 SCAR=0.0
418  
419 sub _value($)
420 11 ▶{ my ($self, $token) = @_;
Subroutine _value: called CC=3 SCAR=11.2
421 11 ▶ $token =~ m/^
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /^ ([12][0-9]{3}) \- (0[1-9]|1[012]) \- (0[1-9]|[12][0-9]|3[01]) ([+-] [0-9]{4})? $ /ux or return 0 11 11
422 ([12][0-9]{3}) \- (0[1-9]|1[012]) \- (0[1-9]|[12][0-9]|3[01])
423 ([+-] [0-9]{4})?
424 $ /x or return;
425  
426 11 ▶ my $tz_offset = $4 // '+0000'; # careful with named matches :-(
Condition50%1/2
$4 // "+0000"
l!l
011
MC/DC0%0/1
$4 // "+0000"
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
427 11 my @args = ( year => $1, month => $2, day => $3);
428 11 my $tz = DateTime::TimeZone::OffsetOnly->new(offset => $tz_offset);
429  
430 11 DateTime->new(@args, time_zone => $tz);
431 }
432  
433 sub cast($)
434 3 ▶{ my ($self, $to) = @_;
Subroutine cast: called CC=4 SCAR=14.1
435 3 ▶ if($to eq 'MF::INTEGER')
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "MF::INTEGER") 2 1 3
436 { # In really exceptional cases, an integer expression can be mis-detected as DATE
437 2 bless $self, 'MF::INTEGER';
438 2 $self->[0] = $self->[1] = eval "$self->[0]";
439 2 return $self;
440 }
441  
442 1 ▶ if($to eq 'MF::DATETIME')
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "MF::DATETIME") 1 0 1
443 1 { my $t = $self->token;
444 1 ▶ my $dt = $t =~ /\+/ ? $t =~ s/\+/T00:00:00+/r : $t . 'T00:00:00';
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /\+/u ? : 1 0 1
445 1 return MF::DATETIME->new($dt);
446 }
447  
448 0 $self->SUPER::cast($to);
449 }
450  
451 sub infix($$@)
452 12 ▶{ my $self = shift;
Subroutine infix: called CC=17 SCAR=28.6
453 12 my ($op, $right, $context) = @_;
454  
455 12 ▶ if($op eq '+' && $right->isa('MF::TIME'))
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" and $right->isa("MF::TIME")) 1 11 12
Condition100%3/3
$op eq "+" and $right->isa("MF::TIME")
!ll&&!rl&&r
1011
MC/DC100%2/2
$op eq "+" and $right->isa("MF::TIME")
$op eq "+" $right->isa("MF::TIME")
Truth table100%3/3
A and B
A= $op eq "+"B= $right->isa("MF::TIME")
ABresult
0X0
100
111
456 1 { my $l = $self->value;
457 1 my $r = $right->value;
458 my $v = DateTime->new(year => $l->year, month => $l->month, day => $l->day,
459 hour => $r->{hour}, minute => $r->{minute}, second => $r->{second},
460 1 nanosecond => $r->{ns}, time_zone => $l->time_zone);
461  
462 1 return MF::DATETIME->new(undef, $v);
463 }
464  
465 11 ▶ if($op eq '-' && $right->isa('MF::DATE'))
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "-" and $right->isa("MF::DATE")) 1 10 11
Condition100%3/3
$op eq "-" and $right->isa("MF::DATE")
!ll&&!rl&&r
911
MC/DC100%2/2
$op eq "-" and $right->isa("MF::DATE")
$op eq "-" $right->isa("MF::DATE")
Truth table100%3/3
A and B
A= $op eq "-"B= $right->isa("MF::DATE")
ABresult
0X0
100
111
466 1 { return MF::DURATION->new(undef, $self->value->clone->subtract_datetime($right->value));
467 }
468  
469 10 ▶ if($op eq '+' || $op eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" or $op eq "-") 2 8 10
Condition100%3/3
$op eq "+" or $op eq "-"
l!l&&r!l&&!r
118
MC/DC100%2/2
$op eq "+" or $op eq "-"
$op eq "+" $op eq "-"
Truth table100%3/3
A or B
A= $op eq "+"B= $op eq "-"
ABresult
1X1
011
000
470 2 ▶ { my $r = $right->isa('MF::DURATION') ? $right : $right->cast('MF::DURATION', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::DURATION') ? : 2 0 2
471 2 ▶ ! $r || $r->token !~ m/T.*[1-9]/
Branch: 1 branch
BranchTrueFalseTotal
$r and $r->token =~ /T.*[1-9]/u and &error(&__x("only duration with full days with DATE, found '{value}'", "value", $r->token)) 0 2 2
Condition33%1/3
$r and $r->token =~ /T.*[1-9]/u
!ll&&!rl&&r
020
MC/DC0%0/2
$r and $r->token =~ /T.*[1-9]/u
$r $r->token =~ /T.*[1-9]/u
Truth table33%1/3
A and B
A= $rB= $r->token =~ /T.*[1-9]/u
ABresult
0X0
100
111
472 or error __x"only duration with full days with DATE, found '{value}'",
473 value => $r->token;
474  
475 2 my $dt = $self->value->clone;
476 2 ▶ my $v = $op eq '+' ? $dt->add_duration($right->value) : $dt->subtract_duration($right->value);
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 1 1 2
477 2 return MF::DATE->new(undef, $v);
478 }
479  
480 8 ▶ if($op eq '<=>')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<=>") 4 4 8
481 4 ▶ { my $r = $right->isa('MF::DATE') ? $right : $right->cast('MF::DATE', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::DATE') ? : 4 0 4
482 4 my ($ld, $ltz) = $self->token =~ m/(.{10})(.*)/;
483 4 my ($rd, $rtz) = $r->token =~ m/(.{10})(.*)/;
484  
485 # It is probably a configuration issue when you configure this.
486 4 ▶ $ld ne $rd || ($ltz //'') eq ($rtz //'')
Branch: 1 branch
BranchTrueFalseTotal
$ld ne $rd or ($ltz // "") eq ($rtz // "") or &warning(&__x("dates '{first}' and '{second}' do not match on timezone", "first", $self->token, "second", $r->token)) 1 3 4
Condition71%5/7
$ld ne $rd or ($ltz // "") eq ($rtz // "")
l!l&&r!l&&!r
211
$ltz // ""
l!l
20
$rtz // ""
l!l
20
MC/DC0%0/2
$ld ne $rd or ($ltz // "") eq ($rtz // "")
$ld ne $rd $ltz
MC/DC0%0/1
$rtz // ""
$rtz
Truth table66%2/3
A or B
A= $ld ne $rdB= $ltz
ABresult
1X1
011
000
Truth table50%1/2
A
A= $rtz
Aresult
11
00
487 or warning __x"dates '{first}' and '{second}' do not match on timezone",
488 first => $self->token, second => $r->token;
489  
490 4 return MF::INTEGER->new(undef, $ld cmp $rd);
491 }
492  
493 4 $self->SUPER::infix(@_);
494 }
495  
496 our %date_attrs = (
497 2 ▶ year => sub { MF::INTEGER->new(undef, $_[0]->value->year) },
Subroutine __ANON__: called CC=1 SCAR=0.0
498 2 ▶ month => sub { MF::INTEGER->new(undef, $_[0]->value->month) },
Subroutine __ANON__: called CC=1 SCAR=0.0
499 2 ▶ day => sub { MF::INTEGER->new(undef, $_[0]->value->day) },
Subroutine __ANON__: called CC=1 SCAR=0.0
500 2 ▶ tz => sub { MF::STRING ->new(undef, $_[0]->value->time_zone->name) },
Subroutine __ANON__: called CC=1 SCAR=0.0
501 13 );
502 4 ▶sub attribute($) { $date_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
Subroutine attribute: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$MF::DATE::date_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 4 4
503  
504 #-----------------
505  
506 package MF::TIME;
507 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
508 13 $VERSION = '0.10';
509  
510 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
511  
512 13 ▶use constant GIGA => 1_000_000_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
513  
514 26 ▶sub _match { '(?:[01][0-9]|2[0-3]) \: [0-5][0-9] \: (?:[0-5][0-9]) (?:\.[0-9]+)?' }
Subroutine _match: called CC=1 SCAR=0.0
515  
516 sub _token($)
517 7 ▶{ my $time = $_[1];
Subroutine _token: called CC=2 SCAR=6.9
518 7 my $ns = $time->{ns};
519 7 ▶ my $frac = $ns ? sprintf(".%09d", $ns) =~ s/0+$//r : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 2 5 7
520 7 sprintf "%02d:%02d:%02d%s", $time->{hour}, $time->{minute}, $time->{second}, $frac;
521 }
522  
523 sub _value($)
524 17 ▶{ my ($self, $token) = @_;
Subroutine _value: called CC=3 SCAR=11.1
525 17 ▶ $token =~ m/^ ([01][0-9]|2[0-3]) \: ([0-5][0-9]) \: ([0-5][0-9]) (?:(\.[0-9]+))? $/x
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /^ ([01][0-9]|2[0-3]) \: ([0-5][0-9]) \: ([0-5][0-9]) (?:(\.[0-9]+))? $/ux or return 0 17 17
526 or return;
527  
528 17 ▶ +{ hour => $1+0, minute => $2+0, second => $3+0, ns => ($4 //0) * GIGA };
Condition100%2/2
$4 // 0
l!l
98
MC/DC100%1/1
$4 // 0
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
529 }
530  
531 our %time_attrs = (
532 2 ▶ hour => sub { MF::INTEGER->new(undef, $_[0]->value->{hour}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
533 2 ▶ minute => sub { MF::INTEGER->new(undef, $_[0]->value->{minute}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
534 2 ▶ second => sub { MF::INTEGER->new(undef, $_[0]->value->{second}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
535 2 ▶ fracsec => sub { my $t = $_[0]->value; MF::FLOAT->new(undef, $t->{second} + $t->{ns}/GIGA) },
Subroutine __ANON__: called CC=1 SCAR=0.0
536 13 );
537  
538 8 ▶sub attribute($) { $time_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
Subroutine attribute: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$MF::TIME::time_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 8 8
539  
540 sub _sec_diff($$)
541 6 ▶{ my ($self, $diff, $ns) = @_;
Subroutine _sec_diff: called CC=3 SCAR=11.2
542 6 ▶ if($ns < 0) { $ns += GIGA; $diff -= 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($ns < 0) { } 1 5 6
elsif ($ns > 1000000000) { } 0 5 5
543 0 elsif($ns > GIGA) { $ns -= GIGA; $diff += 1 }
544  
545 6 my $sec = $diff % 60; $diff /= 60;
546 6 my $min = $diff % 60;
547 6 my $hrs = ($diff / 60) % 24;
548 6 +{ hour => $hrs, minute => $min, second => $sec, nanosecond => $ns};
549 }
550  
551 sub infix($$@)
552 10 ▶{ my $self = shift;
Subroutine infix: called CC=10 SCAR=23.0
553 10 my ($op, $right, $context) = @_;
554  
555 10 ▶ if($op eq '+' || $op eq '-')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "+" or $op eq "-") 6 4 10
Condition100%3/3
$op eq "+" or $op eq "-"
l!l&&r!l&&!r
244
MC/DC100%2/2
$op eq "+" or $op eq "-"
$op eq "+" $op eq "-"
Truth table100%3/3
A or B
A= $op eq "+"B= $op eq "-"
ABresult
1X1
011
000
556 { # normalization is a pain, so bluntly convert to seconds
557 6 my $time = $self->value;
558 6 my $was = $time->{hour} * 3600 + $time->{minute} * 60 + $time->{second};
559  
560 6 ▶ if(my $r = $right->isa('MF::TIME') ? $right : $right->cast('MF::TIME', $context))
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $right->isa("MF::TIME") ? $right : $right->cast("MF::TIME", $context)) 3 3 6
$right->isa('MF::TIME') ? : 3 3 6
561 3 { my $v = $r->value;
562 3 my $min = $v->{hour} * 3600 + $v->{minute} * 60 + $v->{second};
563 3 my $diff = $self->_sec_diff($was - $min, $time->{ns} - $v->{ns});
564 3 ▶ my $frac = $diff->{nanosecond} ? sprintf(".%09d", $diff->{nanosecond}) =~ s/0+$//r : '';
Branch: 1 branch
BranchTrueFalseTotal
$diff->{'nanosecond'} ? : 2 1 3
565 return MF::DURATION->new(sprintf "PT%dH%dM%d%sS", $diff->{hour}, $diff->{minute},
566 3 $diff->{second}, $frac);
567 }
568  
569 3 ▶ if(my $r = $right->isa('MF::DURATION') ? $right : $right->cast('MF::DURATION', $context))
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $right->isa("MF::DURATION") ? $right : $right->cast("MF::DURATION", $context)) 3 0 3
$right->isa('MF::DURATION') ? : 3 0 3
570 3 { my (undef, $hours, $mins, $secs, $ns) =
571 $r->value->in_units(qw/days hours minutes seconds nanoseconds/);
572  
573 3 my $dur = $hours * 3600 + $mins * 60 + $secs;
574 3 ▶ my $diff = $op eq '+' ? $was + $dur : $was - $dur;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 2 1 3
575 3 ▶ my $nns = $op eq '+' ? $time->{ns} + $ns : $time->{ns} - $ns;
Branch: 1 branch
BranchTrueFalseTotal
$op eq '+' ? : 2 1 3
576 3 return MF::TIME->new(undef, $self->_sec_diff($diff, $ns));
577 }
578 }
579  
580 4 $self->SUPER::infix(@_);
581 }
582  
583 #-----------------
584  
585 package MF::DURATION;
586 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
587 13 $VERSION = '0.10';
588  
589 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
590  
591 13 ▶use DateTime::Duration ();
Subroutine BEGIN: called CC=1 SCAR=0.0
592  
593 26 ▶sub _match { '[+-]? P (?:[0-9]+Y)? (?:[0-9]+M)? (?:[0-9]+D)? '
Subroutine _match: called CC=1 SCAR=0.0
594 . ' (?:T (?:[0-9]+H)? (?:[0-9]+M)? (?:[0-9]+(?:\.[0-9]+)?S)? )? \b';
595 }
596  
597 13 ▶use DateTime::Format::Duration::ISO8601 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
598 13 my $dur_format = DateTime::Format::Duration::ISO8601->new;
599 # Implementation dus not like negatives, but DateTime::Duration does.
600  
601 8 ▶sub _token($) { ($_[1]->is_negative ? '-' : '') . $dur_format->format_duration($_[1]) }
Subroutine _token: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->is_negative ? : 2 6 8
602  
603 sub _value($)
604 23 ▶{ my $value = $_[1];
Subroutine _value: called CC=2 SCAR=6.9
605 23 my $negative = $value =~ s/^-//;
606 23 my $duration = $dur_format->parse_duration($value);
607 23 ▶ $negative ? $duration->multiply(-1) : $duration;
Branch: 1 branch
BranchTrueFalseTotal
$negative ? : 2 21 23
608 }
609  
610 sub prefix($)
611 2 ▶{ my ($self, $op, $right) = @_;
Subroutine prefix: called CC=3 SCAR=11.1
612 2 ▶ $op eq '+' ? $self
Branch: 2 branches
BranchTrueFalseTotal
$op eq '+' ? : 1 1 2
$op eq '-' ? : 1 0 1
613 : $op eq '-' ? MF::DURATION->new('-' . $self->token)
614 : $self->SUPER::prefix($op)
615 }
616  
617 sub infix($$@)
618 7 ▶{ my $self = shift;
Subroutine infix: called CC=13 SCAR=27.4
619 7 my ($op, $right, $context) = @_;
620  
621 7 ▶ if($op eq '+' || $op eq '-')
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq '+' or $op eq '-') { } 2 5 7
elsif ($op eq '*') { } 2 3 5
elsif ($op eq '<=>') { } 3 0 3
Condition100%3/3
$op eq "+" or $op eq "-"
l!l&&r!l&&!r
115
MC/DC100%2/2
$op eq "+" or $op eq "-"
$op eq "+" $op eq "-"
Truth table100%3/3
A or B
A= $op eq "+"B= $op eq "-"
ABresult
1X1
011
000
622 2 ▶ { my $r = $right->isa('MF::DURATION') ? $right : $right->cast('MF::DURATION', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::DURATION') ? : 2 0 2
623 2 my $v = $self->value->clone;
624 2 ▶ my $dt = ! $r ? undef : $op eq '+' ? $v->add_duration($r->value) : $v->subtract_duration($r->value);
Branch: 2 branches
BranchTrueFalseTotal
!$r ? : 0 2 2
$op eq '+' ? : 1 1 2
625 2 ▶ return MF::DURATION->new(undef, $dt) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 2 0 2
626 }
627 elsif($op eq '*')
628 2 ▶ { my $r = $right->isa('MF::INTEGER') ? $right : $right->cast('MF::INTEGER', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::INTEGER') ? : 2 0 2
629 2 ▶ return MF::DURATION->new(undef, $self->value->clone->multiply($r->value)) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 2 0 2
630 }
631 elsif($op eq '<=>')
632 3 ▶ { my $r = $right->isa('MF::DURATION') ? $right : $right->cast('MF::DURATION', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::DURATION') ? : 3 0 3
633 3 ▶ return MF::INTEGER->new(undef, DateTime::Duration->compare($self->value, $r->value)) if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 3 0 3
634 }
635  
636 0 $self->SUPER::infix(@_);
637 }
638  
639 13 my %dur_attrs; # Sorry, the attributes of DateTime::Duration make no sense
640 0 ▶sub attribute($) { $dur_attrs{$_[1]} || $_[0]->SUPER::attribute($_[1]) }
Subroutine attribute: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$dur_attrs{$_[1]} or $_[0]->SUPER::attribute($_[1]) 0 0 0
641  
642 #-----------------
643  
644 package MF::NAME;
645 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
646 13 $VERSION = '0.10';
647  
648 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
649  
650 13 ▶use Log::Report 'math-formula', import => [ qw/error __x/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
651  
652 13 my $pattern = '[_\p{Alpha}][_\p{AlNum}]*';
653 13 ▶sub _match() { $pattern }
Subroutine _match: called CC=1 SCAR=0.0
654  
655  
656 sub validated($$)
657 0 ▶{ my ($class, $name, $where) = @_;
Subroutine validated: not called CC=2 SCAR=17.9
658  
659 0 ▶ $name =~ qr/^$pattern$/o
Branch: 1 branch
BranchTrueFalseTotal
$name =~ qr/^$pattern$/ou or &error(&__x("Illegal name '{name}' in '{where}'", "name", $name =~ s/[^_\p{AlNum}]/\317\264/gru, "where", $where)) 0 0 0
660 or error __x"Illegal name '{name}' in '{where}'",
661 name => $name =~ s/[^_\p{AlNum}]/ϴ/gr, where => $where;
662  
663 0 $class->new($name);
664 }
665  
666 sub cast(@)
667 0 ▶{ my ($self, $type, $context) = @_;
Subroutine cast: not called CC=3 SCAR=24.8
668  
669 0 ▶ if($type->isa('MF::FRAGMENT'))
Branch: 1 branch
BranchTrueFalseTotal
if ($type->isa("MF::FRAGMENT")) 0 0 0
670 0 { my $frag = $context->fragment($self->token);
671 0 ▶ return $frag if $frag;
Branch: 1 branch
BranchTrueFalseTotal
if $frag 0 0 0
672 }
673  
674 0 $context->evaluate($self->token, expect => $type);
675 }
676  
677 sub infix(@)
678 0 ▶{ my $self = shift;
Subroutine infix: not called CC=2 SCAR=17.9
679 0 my ($op, $right, $context) = @_;
680  
681 0 my $left = $context->evaluate($self->token);
682 0 ▶ $left ? $left->infix(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 0 0 0
683 }
684  
685 #-----------------
686  
687 package MF::PATTERN;
688 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
689 13 $VERSION = '0.10';
690
691 13 ▶use base 'MF::STRING';
Subroutine BEGIN: called CC=1 SCAR=0.0
692  
693 sub _from_string($)
694 5 ▶{ my ($class, $string) = @_;
Subroutine _from_string: called CC=1 SCAR=0.0
695 5 bless $string, $class;
696 }
697  
698 sub _to_regexp($)
699 5 ▶{ my @chars = $_[0] =~ m/( \\. | . )/gxu;
Subroutine _to_regexp: called CC=20 SCAR=53.0
700 5 my (@regexp, $in_alts, $in_range);
701  
702 5 foreach my $char (@chars)
703 12 ▶ { if(length $char==2) { push @regexp, $char; next }
Branch: 1 branch
BranchTrueFalseTotal
if (length $char == 2) 0 12 12
704 12 ▶ if($char !~ /^[\[\]*?{},!]$/) { push @regexp, $in_range ? $char : quotemeta $char }
Branch: 10 branches
BranchTrueFalseTotal
if (not $char =~ /^[\[\]*?{},!]$/u) { } 7 5 12
elsif ($char eq '*') { } 5 0 5
elsif ($char eq '?') { } 0 0 0
elsif ($char eq '[') { } 0 0 0
elsif ($char eq ']') { } 0 0 0
elsif ($char eq '!') { } 0 0 0
elsif ($char eq '{') { } 0 0 0
elsif ($char eq '}') { } 0 0 0
elsif ($char eq ',') { } 0 0 0
$in_range ? : 0 7 7
705 5 elsif($char eq '*') { push @regexp, '.*' }
706 0 elsif($char eq '?') { push @regexp, '.' }
707 0 elsif($char eq '[') { push @regexp, '['; $in_range++ }
708 0 elsif($char eq ']') { push @regexp, ']'; $in_range=0 }
709 0 ▶ elsif($char eq '!') { push @regexp, $in_range && $regexp[-1] eq '[' ? '^' : '\!' }
Branch: 1 branch
BranchTrueFalseTotal
$in_range && $regexp[-1] eq '[' ? : 0 0 0
Condition0%0/3
$in_range and $regexp[-1] eq "["
!ll&&!rl&&r
000
MC/DC0%0/2
$in_range and $regexp[-1] eq "["
$in_range $regexp[-1] eq "["
Truth table0%0/3
A and B
A= $in_rangeB= $regexp[-1] eq "["
ABresult
0X0
100
111
710 0 ▶ elsif($char eq '{') { push @regexp, $in_range ? '{' : '(?:'; $in_range or $in_alts++ }
Branch: 2 branches
BranchTrueFalseTotal
$in_range ? : 0 0 0
$in_range or ++$in_alts 0 0 0
711 0 ▶ elsif($char eq '}') { push @regexp, $in_range ? '}' : ')'; $in_range or $in_alts=0 }
Branch: 2 branches
BranchTrueFalseTotal
$in_range ? : 0 0 0
$in_range or $in_alts = 0 0 0 0
712 0 ▶ elsif($char eq ',') { push @regexp, $in_alts ? '|' : '\,' }
Branch: 1 branch
BranchTrueFalseTotal
$in_alts ? : 0 0 0
713 0 else {die}
714 }
715 5 my $regexp = join '', @regexp;
716 5 qr/^${regexp}$/u;
717 }
718  
719  
720 6 ▶sub regexp() { $_[0][2] //= _to_regexp($_[0]->value) }
Subroutine regexp: called CC=2 SCAR=7.2
Condition66%2/3
$_[0][2] //= &_to_regexp(scalar $_[0]->value)
l!l&&r!l&&!r
150
MC/DC0%0/2
$_[0][2] //= &_to_regexp(scalar $_[0]->value)
$_[0][2] &_to_regexp(scalar $_[0]->value)
Truth table66%2/3
A //= B
A= $_[0][2]B= &_to_regexp(scalar $_[0]->value)
ABresult
1X1
011
000
721  
722 #-----------------
723  
724 package MF::REGEXP;
725 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
726 13 $VERSION = '0.10';
727  
728 13 ▶use base 'MF::STRING';
Subroutine BEGIN: called CC=1 SCAR=0.0
729  
730 sub _from_string($)
731 5 ▶{ my ($class, $string) = @_;
Subroutine _from_string: called CC=1 SCAR=0.0
732 5 bless $string, $class;
733 }
734  
735  
736 sub regexp
737 6 ▶{ my $self = shift;
Subroutine regexp: called CC=2 SCAR=6.9
738 6 ▶ return $self->[2] if defined $self->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[2] 1 5 6
739 5 my $value = $self->value =~ s!/!\\/!gr;
740 5 $self->[2] = qr/$value/xu;
741 }
742  
743 #-----------------
744  
745 package MF::FRAGMENT;
746 13 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
747 13 $VERSION = '0.10';
748  
749 13 ▶use base 'Math::Formula::Type';
Subroutine BEGIN: called CC=1 SCAR=0.0
750  
751 13 ▶use Log::Report 'math-formula', import => [ qw/panic error __x/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
752  
753  
754 sub new(@)
755 1 ▶{ my ($class, $name, $context, %args) = @_;
Subroutine new: called CC=2 SCAR=7.1
756  
757 my $self = $class->SUPER::new($name, $context,
758 $args{attributes} || {},
759 1 ▶ );
Condition50%1/2
$args{'attributes'} || {}
l!l
10
MC/DC0%0/1
$args{'attributes'} || {}
$args{'attributes'}
Truth table50%1/2
A
A= $args{'attributes'}
Aresult
11
00
760  
761 1 $self;
762 }
763  
764 0 ▶sub name { $_[0]->token }
Subroutine name: not called CC=1 SCAR=6.9
765 0 ▶sub context { $_[0]->value }
Subroutine context: not called CC=1 SCAR=6.9
766  
767 sub infix($$@)
768 0 ▶{ my $self = shift;
Subroutine infix: not called CC=4 SCAR=30.0
769 0 my ($op, $right, $context) = @_;
770  
771 0 ▶ if($op eq '#')
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "#") 0 0 0
772 0 ▶ { my $r = $right->isa('MF::FRAGMENT') ? $right : $right->cast('MF::FRAGMENT', $context);
Branch: 1 branch
BranchTrueFalseTotal
$right->isa('MF::FRAGMENT') ? : 0 0 0
773 0 ▶ return $r if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
774 }
775  
776 0 $self->SUPER::infix(@_);
777 }
778  
779  
780 sub addAttribute($$)
781 0 ▶{ my ($self, $name, $code) = @_;
Subroutine addAttribute: not called CC=1 SCAR=6.9
782 0 $self->[2]{$name} = $code;
783 }
784  
785 sub attribute($)
786 0 ▶{ my ($self, $name) = @_;
Subroutine attribute: not called CC=2 SCAR=17.9
787 0 ▶ $self->[2]{$name} || $self->SUPER::attribute($name);
Branch: 1 branch
BranchTrueFalseTotal
$self->[2]{$name} or $self->SUPER::attribute($name) 0 0 0
788 }
789  
790 13 1;
« Formula.pm ↑ Summary Formula/Token.pm »