blib/lib/Set/Formula.pm

filter: Statementstmt 82.7% 178 / 215 Branchbran 61.4% 75 / 122 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 74.7% 269 / 360 CC 79 SCAR 38.9
CC
79
Cov
75%
formula_calcul
85.6
formula_checker
40.9
equality_checker
16.6
CRAP 49.0
↑ Summary
1 package Set::Formula;
2  
3 3 ▶use 5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 require Exporter;
8 3 our @ISA = qw(Exporter);
9 3 our @EXPORT = qw(formula_checker formula_calcul equality_checker);
10  
11 3 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 our $VERSION = '0.05';
13 3 my $debug = 0;
14  
15 3 my %operators = (
16 '+' => \&_union
17 ,'-' => \&_complement
18 ,'^' => \&_intersection
19 );
20  
21 3 my $counter = 0;
22 #------------------------------------------#
23 sub _union
24 2 ▶{
Subroutine _union: called CC=3 SCAR=11.0
25 2 my ($hrefA, $hrefB, $href_result) = @_;
26 2 %{$href_result} = (); # reset this hash
27  
28 2 for (keys %{$hrefA}) { ${$href_result}{$_} = 0 }
29 2 for (keys %{$hrefB}) { ${$href_result}{$_} = 0 }
30 }
31 #------------------------------------------#
32 sub _complement
33 2 ▶{ # Result := A - B
Subroutine _complement: called CC=3 SCAR=11.0
34 2 my ($hrefA, $hrefB, $href_result) = @_;
35 2 %{$href_result} = (); # reset this hash
36  
37 2 for (keys %{$hrefA})
38 4 ▶ { unless (exists ${$hrefB}{$_}) { ${$href_result}{$_} = 0 } }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists do { $hrefB }->{$_}) 2 2 4
39 }
40 #------------------------------------------#
41 sub _intersection
42 4 ▶{
Subroutine _intersection: called CC=3 SCAR=11.0
43 4 my ($hrefA, $hrefB, $href_result) = @_;
44 4 %{$href_result} = (); # reset this hash
45  
46 4 for (keys %{$hrefB})
47 18 ▶ { if (exists ${$hrefA}{$_}) { ${$href_result}{$_} = 0; } }
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $hrefA }->{$_}) 8 10 18
48 }
49 #------------------------------------------#
50 sub equality_checker # returns 1 if equal, else 0
51 2 ▶{
Subroutine equality_checker: called CC=9 SCAR=28.1
Pod documented
52 2 my ($hrefA, $hrefB, $debug) = @_;
53  
54 2 ▶ $debug = (defined $debug)? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
defined $debug ? : 0 2 2
55 2 ▶ if ($debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 2 2
56 {
57 0 printf "*** %s: running in debug mode ***\n", (caller(0))[3];
58 0 print "DEBUG: 1st operand: "; for (sort keys %{$hrefA}) { print "$_ "; }; print "\n";
59 0 print "DEBUG: 2nd operand: "; for (sort keys %{$hrefB}) { print "$_ "; }; print "\n";
60 }
61  
62 2 for (keys %{$hrefA})
63 5 ▶ { unless (exists ${$hrefB}{$_}) { return 0 } }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists do { $hrefB }->{$_}) 1 4 5
64  
65 1 for (keys %{$hrefB})
66 0 ▶ { unless (exists ${$hrefA}{$_}) { return 0 } }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists do { $hrefA }->{$_}) 0 3 3
67 1 return 1;
68 }
69 #------------------------------------------#
70  
71 sub formula_checker # returns true on success and undefined value on error
72 14 ▶{
Subroutine formula_checker: called CC=34 SCAR=37.1
Pod documented
73 14 my $formula = shift;
74 14 my $remainder;
75 14 ▶ my $debug = (defined $ARGV[0]) && $ARGV[0] ?1:0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ARGV[0] && $ARGV[0] ? : 0 14 14
Condition33%1/3
defined $ARGV[0] and $ARGV[0]
!ll&&!rl&&r
1400
MC/DC0%0/2
defined $ARGV[0] and $ARGV[0]
defined $ARGV[0] $ARGV[0]
Truth table33%1/3
A and B
A= defined $ARGV[0]B= $ARGV[0]
ABresult
0X0
100
111
76 14 ▶ $debug && printf "*** %s: running in debug mode ***\n", (caller(0))[3];
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "*** %s: running in debug mode ***\n", (caller 0)[3] 0 14 14
77 14 ▶ unless (defined $remainder) { $remainder = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $remainder) 14 0 14
78 14 my $parentheses_cnt = 0;
79 14 my $parentheses_number_cnt = 0;
80 14 my $operator;
81 my $regexpr ;
82  
83 # ---- check "interlacing": start ----
84 # rule 1: 2 operands can't be adjacent
85 # rule 2: 2 operators can't be adjacent
86 # rule 3: operator can be neither first not last formula element
87  
88 14 ▶ $debug && print "DEBUG 10: formula: $formula\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "DEBUG 10: formula: $formula\n" 0 14 14
89 14 $remainder = $formula;
90 14 $remainder =~ s/[()]//g; # remove all parentheses
91 14 ▶ $debug && print "DEBUG 20: remainder: $remainder\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "DEBUG 20: remainder: $remainder\n" 0 14 14
92 14 my $operator_found = 0;
93  
94 INTERLACING:
95 14 while(1)
96 {
97 34 ▶ unless ( $remainder =~ /^\s*(\w+)\s*/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($remainder =~ /^\s*(\w+)\s*/) 2 32 34
98 {
99 2 ▶ $debug && cluck "ERROR: remainder of formula $remainder must begin with operand";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: remainder of formula $remainder must begin with operand") 0 2 2
100 2 return;
101 }
102  
103 32 $remainder = $';
104 32 ▶ $debug && print "DEBUG 30: operand: $1 remainder: $remainder\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "DEBUG 30: operand: $1 remainder: $remainder\n" 0 32 32
105
106 32 ▶ unless ($remainder) { last INTERLACING; } # interlacing is correct
Branch: 1 branch
BranchTrueFalseTotal
unless ($remainder) 9 23 32
107
108 23 $operator_found = 0;
109 23 for (keys %operators)
110 {
111 40 ▶ $debug && print "DEBUG 40: operator: $_\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "DEBUG 40: operator: $_\n" 0 40 40
112 40 $regexpr = qq (^\\s*(\\$_)\\s*);
113 40 ▶ $debug && printf "DEBUG 45: regexpr = %s\n", $regexpr;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 45: regexpr = %s\n", $regexpr 0 40 40
114 40 ▶ if ( $remainder =~ /$regexpr/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /$regexpr/) 22 18 40
115 {
116 22 $operator_found = 1;
117 22 $operator = $_;
118 22 $remainder = $';
119 22 last;
120 }
121 }
122  
123 23 ▶ unless ($operator_found)
Branch: 1 branch
BranchTrueFalseTotal
unless ($operator_found) 1 22 23
124 {
125 1 ▶ $debug && cluck "ERROR: remainder of formula $remainder must begin with operator";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: remainder of formula $remainder must begin with operator") 0 1 1
126 1 return;
127 }
128  
129 22 ▶ $debug && printf "DEBUG 50: operator: %s\n", $operator;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 50: operator: %s\n", $operator 0 22 22
130  
131 22 ▶ unless ($remainder)
Branch: 1 branch
BranchTrueFalseTotal
unless ($remainder) 2 20 22
132 {
133 2 ▶ $debug && cluck "ERROR: formula must begin with operator";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: formula must begin with operator") 0 2 2
134 2 return;
135 }
136 } # end of 'while (1)'
137  
138 # ---- check "interlacing": finish ----
139  
140 # ---- check parentheses: start ----
141 9 $remainder = $formula;
142 9 ▶ $debug && printf "DEBUG 60: *** remainder = %s\n", $remainder;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 60: *** remainder = %s\n", $remainder 0 9 9
143  
144 9 for $operator (keys %operators)
145 {
146 23 $regexpr = qq (\\(\\s*\\$operator|\\$operator\\s*\\));
147 23 ▶ $debug && printf "DEBUG 65: *** operator = %-3s regexpr = %s\n", $operator, $regexpr;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 65: *** operator = %-3s regexpr = %s\n", $operator, $regexpr 0 23 23
148 23 ▶ if ( $remainder =~ /$regexpr/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /$regexpr/) 2 21 23
149 {
150 2 ▶ $debug && cluck "ERROR: $& has no sense";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: $& has no sense") 0 2 2
151 2 return;
152 }
153 }
154  
155 7 ▶ while ($remainder =~ /[()]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder =~ /[()]/) 26 5 31
156 {
157 26 ▶ $debug && printf "DEBUG 70: *** remainder = %s\n", $remainder;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 70: *** remainder = %s\n", $remainder 0 26 26
158 26 $parentheses_number_cnt ++;
159 26 $remainder = $';
160 0 ▶ unless (defined $remainder) { $remainder = ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $remainder) 0 26 26
161 26 ▶ $debug && printf "DEBUG 75: *** remainder = %s\n", $remainder;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "DEBUG 75: *** remainder = %s\n", $remainder 0 26 26
162 26 ▶ if ( $& =~ /\(/ ) { $parentheses_cnt ++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($& =~ /\(/) { } 13 13 26
163 else
164 {
165 13 $parentheses_cnt --;
166 13 ▶ if ($parentheses_cnt < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($parentheses_cnt < 0) 2 11 13
167 {
168 2 ▶ $debug && cluck "ERROR: negative parentheses amount at parentheses nr.$parentheses_number_cnt from left";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: negative parentheses amount at parentheses nr.$parentheses_number_cnt from left") 0 2 2
169 2 return;
170 }
171 }
172 } # end of "while ($remainder)"
173  
174 5 ▶ if ($parentheses_cnt)
Branch: 1 branch
BranchTrueFalseTotal
if ($parentheses_cnt) 2 3 5
175 {
176 2 ▶ $debug && cluck "ERROR: more opening parentheses than closing parentheses";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR: more opening parentheses than closing parentheses") 0 2 2
177 2 return;
178 }
179 3 return 1;
180 } # end of "sub formula_checker"
181 #------------------------------------------#
182 #------------------------------------------#
183  
184 sub formula_calcul
185 12 ▶{
Subroutine formula_calcul: called CC=32 SCAR=44.5
Pod documented
186 12 my ($curr_formula, $href_result, $href_HoH_sets, $debug) = @_;
187 12 %{$href_result} = (); # reset this hash
188 0 ▶ unless (defined $curr_formula) { $curr_formula = '' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $curr_formula) 0 12 12
189  
190 12 ▶ $debug = (defined $debug) && $debug ?1:0;
Branch: 1 branch
BranchTrueFalseTotal
defined $debug && $debug ? : 0 12 12
Condition66%2/3
defined $debug and $debug
!ll&&!rl&&r
480
MC/DC0%0/2
defined $debug and $debug
defined $debug $debug
Truth table66%2/3
A and B
A= defined $debugB= $debug
ABresult
0X0
100
111
191 12 ▶ $debug && printf "*** %s: running in debug mode ***\n", (caller(0))[3];
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "*** %s: running in debug mode ***\n", (caller 0)[3] 0 12 12
192  
193 12 ▶ $debug && print "\n--------------------------\n#1 curr_formula = $curr_formula\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "\n--------------------------\n#1 curr_formula = $curr_formula\n" 0 12 12
194  
195 12 my $href_operand_left;
196 my $href_operand_right;
197 12 my $operator; # a key of %operators
198 12 my $href_remainder;
199 12 my $new_name;
200  
201 # parse formula: search internal (...)
202 12 ▶ while ( $curr_formula =~ /\(([^(]+?)\)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_formula =~ /\(([^(]+?)\)/) 6 10 16
203 {
204 # $curr_formula contains parentheses
205 # $1 is innenmost formula part, that does not contains parentheses.
206 # Opened and closed parentheses are located on boundaries of $1 and don't belong it.
207 6 ▶ $debug && print "#2 in parentheses: $1\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#2 in parentheses: $1\n" 0 6 6
208 #------------------------------------------#
209 6 ▶ unless (defined &formula_calcul ($1, $href_result, $href_HoH_sets, $debug))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &formula_calcul($1, $href_result, $href_HoH_sets, $debug)) 2 4 6
210 2 ▶ { $debug && cluck "Error in formula \"$1\""; return; }
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck(qq[Error in formula "$1"]) 0 2 2
211  
212 4 $new_name = "InTeRmEdIaTe$counter";
213 4 $counter++;
214 4 for (keys %{$href_result}) { $href_HoH_sets->{$new_name}{$_} = 0; }
215 0 ▶ unless (exists $href_HoH_sets->{$new_name}) { $href_HoH_sets->{$new_name} = () }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $href_HoH_sets->{$new_name}) 0 4 4
216 4 %{$href_result} = (); # reset this hash
217  
218 4 $curr_formula = "$` $new_name $'";
219 4 ▶ $debug && print "#3 curr_formula = $curr_formula\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#3 curr_formula = $curr_formula\n\n" 0 4 4
220 }
221  
222 # parenthesesless formula.
223 # Calculate all operators with equal priority from left to right
224 10 ▶ if ( $curr_formula =~ /(\w+)\s*(\S+)\s*(\w+)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_formula =~ /(\w+)\s*(\S+)\s*(\w+)/) 10 0 10
225 {
226 10 $href_operand_left = $1;
227 10 $operator = $2;
228 10 $href_operand_right = $3;
229 10 $href_remainder = $';
230 10 ▶ if ($operator !~ /^(\+|\-|\^)$/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($operator =~ /^(\+|\-|\^)$/) 0 10 10
231 0 ▶ { $debug && cluck "ERROR: Unknown operator \"$operator\" in formula \"$curr_formula\"\n"; return; }
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck(qq[ERROR: Unknown operator "$operator" in formula "$curr_formula"\n]) 0 0 0
232  
233 10 ▶ $debug && printf "*** left operand = $href_operand_left, operator = $operator, right operand = $href_operand_right, remainder = %s\n", $href_remainder;
Branch: 1 branch
BranchTrueFalseTotal
$debug and printf "*** left operand = $href_operand_left, operator = $operator, right operand = $href_operand_right, remainder = %s\n", $href_remainder 0 10 10
234  
235 10 ▶ unless (exists $href_HoH_sets->{$href_operand_left})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $href_HoH_sets->{$href_operand_left}) 2 8 10
236 {
237 2 ▶ $debug && cluck "ERROR: Unknown left operand \"$href_operand_left\" in formula \"$curr_formula\"\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck(qq[ERROR: Unknown left operand "$href_operand_left" in formula "$curr_formula"\n]) 0 2 2
238 2 return;
239 }
240  
241 8 ▶ unless (exists $href_HoH_sets->{$href_operand_right})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $href_HoH_sets->{$href_operand_right}) 0 8 8
242 {
243 0 ▶ $debug && cluck "ERROR: Unknown right operand \"$href_operand_right\" in formula \"$curr_formula\"\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck(qq[ERROR: Unknown right operand "$href_operand_right" in formula "$curr_formula"\n]) 0 0 0
244 0 return;
245 }
246  
247 8 ▶ if ($debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 8 8
248 {
249 0 printf "!!! %-13s : ", $href_operand_left;
250 0 for ( keys %{$href_HoH_sets->{$href_operand_left}} ) {print "$_ "}
251 0 print "\n";
252  
253 0 printf "!!! %-13s : ", $href_operand_right;
254 0 for ( keys %{$href_HoH_sets->{$href_operand_right}} ) {print "$_ "}
255 0 print "\n";
256 }
257  
258 $operators{$operator} ( $href_HoH_sets->{$href_operand_left}
259 8 , $href_HoH_sets->{$href_operand_right}
260 , $href_result );
261  
262 8 ▶ if ($debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 8 8
263 {
264 0 print "DEBUG: result of ($href_operand_left $operator $href_operand_right) : ";
265 0 for (keys %{$href_result}) { print "$_ " }
266 0 print "\n";
267 }
268  
269 8 ▶ if ($href_remainder =~ /\S/)
Branch: 1 branch
BranchTrueFalseTotal
if ($href_remainder =~ /\S/) 2 6 8
270 {
271 2 $new_name = "InTeRmEdIaTe$counter";
272 2 $counter++;
273 2 for (keys %{$href_result}) { $href_HoH_sets->{$new_name}{$_} = 0 }
274 0 ▶ unless (exists $href_HoH_sets->{$new_name}) { $href_HoH_sets->{$new_name} = () }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $href_HoH_sets->{$new_name}) 0 2 2
275 2 %{$href_result} = (); # reset this hash
276  
277 2 $curr_formula = "$new_name $href_remainder";
278 2 ▶ $debug && print "#4 curr_formula = $curr_formula\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$debug and print "#4 curr_formula = $curr_formula\n\n" 0 2 2
279 2 ▶ if (defined &formula_calcul ($curr_formula, $href_result, $href_HoH_sets, $debug))
Branch: 1 branch
BranchTrueFalseTotal
if (defined &formula_calcul($curr_formula, $href_result, $href_HoH_sets, $debug)) { } 2 0 2
280 2 { return 1; }
281 0 ▶ else { $debug && cluck "ERROR 33\n"; return; }
Branch: 1 branch
BranchTrueFalseTotal
$debug and cluck("ERROR 33\n") 0 0 0
282 }
283 } # end of "if ( $curr_formula =~ ...)"
284 6 return 1;
285 } # end of "sub formula_calcul"
286  
287 3 1;
288 __END__
↑ Summary