blib/lib/Devel/Cover/Truth_Table.pm untested

filter: Statementstmt 0.0% 0 / 220 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 356 CC 21 SCAR 25.9
CC
21
Cov
0%
merge_lineops
56.0
truth_table
20.0
error
20.0
CRAP 13.4
1 # Things to add to existing module
2 package Devel::Cover::DB::Criterion;
3 use Devel::Cover::Truth_Table;
4 use strict;
5 use warnings;
6  
7 #-------------------------------------------------------------------------------
8 # Subroutine : error()
9 # Purpose : Determine if any of the entries for a given metric type
10 # of a line of code are missing full coverage.
11 # Notes :
12 #-------------------------------------------------------------------------------
13 sub error {
14 my $self = shift;
15 my $line = shift;
16 foreach my $c (@{ $self->get($line) }) {
17 return 1 if $c->error;
18 }
19 return;
20 }
21  
22 #-------------------------------------------------------------------------------
23 # Subroutine : branch_coverage()
24 # Purpose : Generate textual representation of branches with/without
25 # coverage.
26 # Notes :
27 #-------------------------------------------------------------------------------
28 sub branch_coverage {
29 my $self = shift;
30 my $line = shift;
31 my @txt;
32 foreach my $c (@{ $self->get($line) }) {
33 push @txt, ($c->[0][0] ? ' T ' : '---') . ($c->[0][1] ? ' F ' : '---');
34 }
35 return @txt;
36 }
37  
38 #-------------------------------------------------------------------------------
39 # Subroutine : truth_table()
40 # Purpose : Generate truth table(s) for conditional expressions on a line.
41 # Notes :
42 #-------------------------------------------------------------------------------
43 sub truth_table {
44 my $self = shift;
45 my $line = shift;
46 my $c = $self->get($line);
47  
48 return if @$c > 16; # Too big - can't get any useful info anyway.
49  
50 my @lops;
51 foreach my $c (@$c) {
52 my $op = $c->[1]{type};
53 my @hit = map { defined() && $_ > 0 ? 1 : 0 } @{ $c->[0] };
54 @hit = reverse @hit if $op =~ /^or_[23]$/;
55 my $t = {
56 tt => Devel::Cover::Truth_Table->new_primitive($op, @hit),
57 cvg => $c->[1],
58 expr => join(' ', @{ $c->[1] }{qw/left op right/}),
59 };
60 push(@lops, $t);
61 }
62 return map { [ $_->{tt}->sort, $_->{expr} ] } merge_lineops(@lops);
63 }
64  
65 #-------------------------------------------------------------------------------
66 # Subroutine : merge_lineops()
67 # Purpose : Merge multiple conditional expressions into composite
68 # truth table(s).
69 # Notes :
70 #-------------------------------------------------------------------------------
71 sub merge_lineops {
72 my @ops = @_;
73 my $rotations;
74 while ($#ops > 0) {
75 my $rm;
76 for (1 .. $#ops) {
77 if ($ops[0]{expr} eq $ops[$_]{cvg}{left}) {
78 $ops[$_]{tt}->left_merge($ops[0]{tt});
79 $ops[0] = $ops[$_];
80 $rm = $_;
81 last;
82 } elsif ($ops[0]{expr} eq $ops[$_]{cvg}{right}) {
83 $ops[$_]{tt}->right_merge($ops[0]{tt});
84 $ops[0] = $ops[$_];
85 $rm = $_;
86 last;
87 } elsif ($ops[$_]{expr} eq $ops[0]{cvg}{left}) {
88 $ops[0]{tt}->left_merge($ops[$_]{tt});
89 $rm = $_;
90 last;
91 } elsif ($ops[$_]{expr} eq $ops[0]{cvg}{right}) {
92 $ops[0]{tt}->right_merge($ops[$_]{tt});
93 $rm = $_;
94 last;
95 }
96 }
97 if ($rm) {
98 splice(@ops, $rm, 1);
99 $rotations = 0;
100 } else {
101 # First op didn't merge with anything. Rotate @ops in hopes
102 # of finding something that can be merged.
103 unshift(@ops, pop @ops);
104  
105 # Hmm... we've come full circle and *still* haven't found
106 # anything to merge. Did the source code have multiple
107 # statements on the same line?
108 last if ($rotations++ > $#ops);
109 }
110 }
111 return @ops;
112 }
113  
114 package Devel::Cover::Truth_Table::Row;
115 use warnings;
116 use strict;
117  
118 sub new {
119 my $proto = shift;
120 my $class = ref($proto) || $proto;
121 my @args = @_;
122 # use Devel::Cover::Dumper; print Dumper \@args;
123 return bless {
124 inputs => $args[0],
125 result => $args[1],
126 covered => $args[2],
127 criterion => $args[2],
128 }, $class;
129 }
130  
131 sub inputs {
132 my $self = shift;
133 return @{ $self->{inputs} };
134 }
135  
136 sub leftcol {
137 my $self = shift;
138 return $self->{inputs}[0];
139 }
140  
141 sub rightcol {
142 my $self = shift;
143 return $self->{inputs}[-1];
144 }
145  
146 sub leftelems {
147 my $self = shift;
148 my $n = $#{ $self->{inputs} };
149 return @{ $self->{inputs} }[ 0 .. $n - 1 ];
150 }
151  
152 sub rightelems {
153 my $self = shift;
154 my $n = $#{ $self->{inputs} };
155 return @{ $self->{inputs} }[ 1 .. $n ];
156 }
157  
158 sub string {
159 return "@{$_[0]{inputs}}";
160 }
161  
162 sub result {
163 return $_[0]{result};
164 }
165  
166 sub covered {
167 return $_[0]{covered};
168 }
169  
170 sub error {
171 return 1;
172 # TODO - sort out how this should really work, or remove this report
173 # return $_[0]{error}[$_[1]];
174 }
175  
176 package Devel::Cover::Truth_Table;
177 use warnings;
178 use strict;
179 our $VERSION = '1.52'; # VERSION
180  
181 #-------------------------------------------------------------------------------
182 # Subroutine : new()
183 # Purpose : Create a new Truth_Table object.
184 # Notes : Probably best to keep usage of this internal...
185 #-------------------------------------------------------------------------------
186 sub new {
187 my $proto = shift;
188 my $class = ref($proto) || $proto;
189 return bless [@_], $class;
190 }
191  
192 #-------------------------------------------------------------------------------
193 # Subroutine : new_primitive()
194 # Purpose : Create a new Truth_Table object based on one of the built-in
195 # primitives.
196 # Notes :
197 #-------------------------------------------------------------------------------
198 sub new_primitive {
199 my ($proto, $type, @coverage) = @_;
200  
201 my %table = (
202 and_2 => \&boolean_tt,
203 and_3 => \&and_tt,
204 or_2 => \&boolean_tt,
205 or_3 => \&or_tt,
206 xor_4 => \&xor_tt,
207 );
208  
209 return $proto->new($table{$type}->(@coverage));
210 }
211  
212 #-------------------------------------------------------------------------------
213 # Subroutine : error()
214 # Purpose : Determine if a table is missing full coverage.
215 # Notes :
216 #-------------------------------------------------------------------------------
217 sub error {
218 my $self = shift;
219 if (@_) { print "[[[", $self->[shift]->error, "]]]\n"; die }
220 return $self->[shift]->error if @_;
221 foreach (@$self) {
222 return 1 if $_->error;
223 }
224 return;
225 }
226  
227 #-------------------------------------------------------------------------------
228 # Subroutine : percentage()
229 # Purpose : Determine the coverage proportion for a truth table.
230 # Notes : Don't care states (X) count as one path, not two.
231 #-------------------------------------------------------------------------------
232 sub percentage {
233 my $self = shift;
234 my ($p, $c) = (scalar @$self, 0);
235 foreach (@$self) {
236 $c++ if $_->covered;
237 }
238 return ($c == $p) ? 100 : 100 * $c / $p;
239 }
240  
241 # Basic truth table constructors
242 # Construct a new truth table for 'A <op> B' coverage listing
243 # primitives. More complicated tables are constructed by merging
244 # primitives. Each array element represents a row from a truth table,
245 # divided into two parts;
246 # * the input states: 0/1/X (X = don't care)
247 # * the output state and a flag to show whether that path has been
248 # hit.
249 # e.g. for the source '$a && $b', and_tt(1,0,1) generates this table:
250 #
251 # $a | $b | $a && $b | covered
252 # ----|----|----------|--------
253 # 0 | X | 0 | 1
254 # 1 | 0 | 0 | 0
255 # 1 | 1 | 1 | 1
256 #
257 sub and_tt {
258 return (
259 Devel::Cover::Truth_Table::Row->new([ 0, 'X' ], 0, shift),
260 Devel::Cover::Truth_Table::Row->new([ 1, 0 ], 0, shift),
261 Devel::Cover::Truth_Table::Row->new([ 1, 1 ], 1, shift)
262 );
263 }
264  
265 sub or_tt {
266 return (
267 Devel::Cover::Truth_Table::Row->new([ 0, 0 ], 0, shift),
268 Devel::Cover::Truth_Table::Row->new([ 0, 1 ], 1, shift),
269 Devel::Cover::Truth_Table::Row->new([ 1, 'X' ], 1, shift)
270 );
271 }
272  
273 sub xor_tt {
274 return (
275 Devel::Cover::Truth_Table::Row->new([ 0, 0 ], 0, shift),
276 Devel::Cover::Truth_Table::Row->new([ 0, 1 ], 1, shift),
277 Devel::Cover::Truth_Table::Row->new([ 1, 0 ], 1, shift),
278 Devel::Cover::Truth_Table::Row->new([ 1, 1 ], 0, shift)
279 );
280 }
281  
282 sub boolean_tt {
283 return (
284 Devel::Cover::Truth_Table::Row->new([0], 0, shift),
285 Devel::Cover::Truth_Table::Row->new([1], 1, shift)
286 );
287 }
288  
289 #-------------------------------------------------------------------------------
290 # Subroutine : sort()
291 # Purpose : Sort a truth table
292 # Notes :
293 #-------------------------------------------------------------------------------
294 sub sort {
295 my $self = shift;
296 @$self = sort { $a->string cmp $b->string } @$self;
297 return $self;
298 }
299  
300 #sub rows {return @{$_[0]}}
301  
302 #-------------------------------------------------------------------------------
303 # Subroutine : text()
304 # Purpose : Formatted text representation of a truth table
305 # Notes :
306 #-------------------------------------------------------------------------------
307 sub text {
308  
309 my $self = shift;
310 my $h = 'A';
311 my @h = map { $h++ } ($self->[0]->inputs);
312 my $hdr = "@h |exp|hit";
313 my @text;
314  
315 push @text, $hdr, '-' x length($hdr);
316 foreach (@$self) {
317 push @text,
318 sprintf("%s | %s |%s",
319 $_->string(), $_->result(), $_->covered() ? '+++' : '---');
320 }
321 push @text, '-' x length($hdr);
322 return @text;
323 }
324  
325 #-------------------------------------------------------------------------------
326 # Subroutine : html()
327 # Purpose : HTML representation of a truth table
328 # Notes :
329 #-------------------------------------------------------------------------------
330 sub html {
331 my $self = shift;
332 my @class = (shift || 'uncovered', shift || 'covered');
333 my $html = "<table><tr>";
334 my $h = 'A';
335 for ($self->[0]->inputs) {
336 $html .= "<th>$h</th>";
337 $h++;
338 }
339 $html .= "<th>dec</th></tr>";
340  
341 my $c = 0;
342 foreach (@$self) {
343 my $class = $class[ !$_->error($c++) || $_->covered ];
344 $html .= qq'<tr align="center"><td class="$class">';
345 $html .= join(qq'</td><td class="$class">', $_->inputs, $_->result);
346 $html .= "</td></tr>";
347 }
348 $html .= "</table>";
349 return $html;
350 }
351  
352 # Truth table merge routines:
353 # Combine simple truth tables into more complicated ones.
354 #
355 # Given two truth tables, A and B, such that
356 # A is the truth table for the expression 'a1 <op> a2'
357 # B is the truth table for the expression 'b1 <op> b2'
358 # b1 = 'a1 <op> a2'
359 #
360 # We want to merge the contents of A into B creating a new, larger truth
361 # table for the composite expression '(a1 <op> a2) <op> b2'. We do this
362 # by replacing elements of B corresponding to b1 with (all) the inputs
363 # to A where the result of A matches the element removed from B. e.g.
364 #
365 # A => a1 || a2 B => b1 && b2
366 # a1 a2 | a1 || a2 b1 b2 | b1 && b2
367 # ---------------- ----------------
368 # 0 0 | 0 0 X | 0
369 # 0 1 | 1 1 0 | 0
370 # 1 X | 1 1 1 | 1
371 #
372 # For the first row of B, b1 = 0. We replace this with the all the
373 # (a1,a2) values where the expression 'a1 || a2' = 0. In this case, just
374 # (0,0). Thus, the first row of our new table becomes (0,0,X).
375 #
376 # In the second row of B, b1 = 1. Thus, from A we add rows for A values
377 # (0,1) and (1,X) along with the value for b2 (0). Repeat the process
378 # for the final row of B where b2 = 1. The resulting truth table is:
379 #
380 # a1 a2 b2 | (a1 || a2) && b2
381 # ---------------------------
382 # 0 0 X | 0
383 # 0 1 0 | 0
384 # 1 X 0 | 0
385 # 0 1 1 | 1
386 # 1 X 1 | 1
387 #
388 # Note that we don't have to calculate the result, it's taken directly
389 # from table B. We can do this because we've replaced b1 with an
390 # something that evaluates to the same thing.
391 #
392 # This is a "left merge" because we merged A into the leftmost column of
393 # B. We can also do a "right merge" where we place A into the rightmost
394 # column of B. (This is what we would have done if we had had
395 # b2 = 'a1 <op> a2' instead of b1.)
396 #
397 # Finally, merging the truth tables isn't much use if we don't work out
398 # which paths have been covered. We haven't shown it, but each row of
399 # the truth tables also contains a "covered" boolean. The value of this
400 # in the merged table is the AND'd values from the input tables A and B.
401 # In the case where all the inputs from B are 'X' it is simply the
402 # value from table A.
403  
404 #-------------------------------------------------------------------------------
405 # Subroutine : right_merge(\@,\@)
406 # Purpose : Merge truth table 2 into the rightmost column of truth table 1.
407 # Notes :
408 #-------------------------------------------------------------------------------
409 sub right_merge {
410 my ($tt1, $tt2) = @_;
411  
412 # find the rows of tt2 that have a result of false/true
413 my @merge = ([ grep { !$_->result } @$tt2 ], [ grep { $_->result } @$tt2 ]);
414 # if the rightmost column of tt1 is 'X', we don't care what the
415 # input from tt2 was
416 my @dontcare = map { 'X' } $tt2->[0]->inputs;
417  
418 my @tt;
419 foreach my $row1 (@$tt1) {
420 if ($row1->rightcol eq 'X') {
421 push(
422 @tt,
423 Devel::Cover::Truth_Table::Row->new(
424 [ $row1->leftelems, @dontcare ],
425 $row1->result, $row1->covered
426 )
427 );
428 } else {
429 # expand value from tt1 with rows from tt2 that result in
430 # that value
431 foreach my $row2 (@{ $merge[ $row1->rightcol ] }) {
432 push(
433 @tt,
434 Devel::Cover::Truth_Table::Row->new(
435 [ $row1->leftelems, $row2->inputs ],
436 $row1->result,
437 $row1->covered && $row2->covered
438 )
439 );
440 }
441 }
442 }
443 $_[0] = $tt2->new(@tt);
444 }
445  
446 #-------------------------------------------------------------------------------
447 # Subroutine : left_merge(\@,\@)
448 # Purpose : Merge truth table 2 into the leftmost column of truth table 1.
449 # Notes :
450 #-------------------------------------------------------------------------------
451 sub left_merge {
452 my ($tt1, $tt2) = @_;
453  
454 # find the rows of tt2 that have a result of false/true
455 my @merge = ([ grep { !$_->result } @$tt2 ], [ grep { $_->result } @$tt2 ]);
456  
457 my @tt;
458 foreach my $row1 (@$tt1) {
459 my $rightmatters = grep { $_ ne 'X' } $row1->rightelems;
460 foreach my $row2 (@{ $merge[ $row1->leftcol ] }) {
461 # expand value from tt1 with rows from tt2 that result in
462 # that value
463 push(
464 @tt,
465 Devel::Cover::Truth_Table::Row->new(
466 [ $row2->inputs, $row1->rightelems ],
467 $row1->result,
468 ($rightmatters) ? $row1->covered && $row2->covered : $row2->covered
469 )
470 );
471 }
472 }
473 $_[0] = $tt2->new(@tt);
474 }
475  
476 1;
477  
478 =pod
479  
480 =head1 NAME
481  
482 Devel::Cover::Truth_Table - Truth tables for coverage objects.
483  
484 =head1 VERSION
485  
486 version 1.52
487  
488 =head1 SYNOPSIS
489  
490 use Devel::Cover::Truth_Table;
491  
492 # $a || $b
493 my $or_tt = Devel::Cover::Truth_Table->new_primitive('or_3', 0, 1, 1);
494  
495 # $c && $d
496 my $and_tt = Devel::Cover::Truth_Table->new_primitive('and_3', 1, 0, 1);
497  
498 # merge contents of $and_tt into right column of $or_tt, to create
499 # $a || ($c && $d)
500 $or_tt->right_merge($and_tt);
501  
502 # get a (sorted) textual representation
503 my @text = $or_tt->sort->text;
504 print "$_\n" foreach @text;
505  
506 __END__
507 A B C |exp|hit
508 --------------
509 0 0 X | 0 |---
510 0 1 0 | 0 |---
511 0 1 1 | 1 |+++
512 1 X X | 1 |+++
513 --------------
514  
515 =head1 DESCRIPTION
516  
517 This module provides methods for creating and merging conditional
518 primitives (C<$a && $b>, C<$c || $d>, etc.) into more complex composite
519 expressions.
520  
521 =head1 METHODS
522  
523 =head2 new_primitive($op, @coverage)
524  
525 Create a new truth table based on one of the built-in primitives, which
526 are the subclasses of Devel::Cover::DB::Condition. C<$op> is one of the
527 following:
528  
529 =over 4
530  
531 =item and_3
532  
533 C<and> or C<&&> with three conditional paths.
534  
535 =item or_3
536  
537 C<or> or C<||> with three conditional paths.
538  
539 =item or_2
540  
541 C<or> or C<||> with two conditional paths. (i.e., when the right hand
542 side of the expression is a constant)
543  
544 =item xor_4
545  
546 C<xor> with four conditional paths.
547  
548 =back
549  
550 C<@coverage> is a list booleans identifying which of the possible paths
551 have been covered.
552  
553 =head2 sort()
554  
555 Sorts a truth table (in place) and returns the sorted object.
556  
557 =head2 text()
558  
559 Format a truth table to an array of strings for printing.
560  
561 =head2 html()
562  
563 Format a truth table in HTML.
564  
565 =head2 error()
566  
567 =head2 percentage()
568  
569 Determines the proportion of possible conditions that have coverage.
570  
571 =head2 right_merge($sub_table)
572  
573 Merge entries from C<$sub_table> into right column of table.
574  
575 =head2 left_merge($sub_table)
576  
577 Merge entries from C<$sub_table> into left column of table.
578  
579 =head1 SEE ALSO
580  
581 Devel::Cover
582  
583 =head1 LICENSE
584  
585 Copyright 2002 Michael Carman <mjcarman@mchsi.com>
586  
587 This software is free. It is licensed under the same terms as Perl
588 itself. The latest version should be available from: https://pjcj.net
589  
590 =cut