Jump to content

Functional coverage tree: Difference between revisions

m
→‎{{header|Perl 6}}: simplified regex, relocate input table to end
m (→‎{{header|Perl 6}}: oops, stray curlies)
m (→‎{{header|Perl 6}}: simplified regex, relocate input table to end)
Line 1,690:
=={{header|Perl 6}}==
{{trans|Perl}}
<lang perl6>forsub q:to/END/walktree ($data) {
my (@parts, $cnt);
 
while ($data ~~ m:nth(++$cnt)/$<head>=[(\s*) \N+\n ] # split off one level as 'head' (or terminal 'leaf')
$<body>=[[$0 \s+ \N+\n]*]/ ) { # next sub-level is 'body' (defined by extra depth of indentation)
 
my ($head, $body) = ($<head>, $<body>);
$head ~~ /^.*? \'|' $<weight>=[\S*] \s*\ '|' $<coverage>=[\S*] \s*\|/; # save values of weight and coverage (if any) for later
 
my ($w, $wsum) = (0, 0);
$head ~= .[0],
$w += .[1],
$wsum += .[1] * .[2]
for walktree( $body );
 
my $weight = (~$<weight> or 1).fmt('%-8s');
my $coverage = $w == 0
?? (~$<coverage> or 0).fmt('%-10s')
!! ($wsum/$w) .fmt('%-10.2g');
@parts.push: [$head.subst(/\'|' \N+/, "|$weight|$coverage|"), $weight, $coverage ];
}
return @parts;
 
(say .[0] for walktree( $_) given
 
q:to/END/
NAME_HIERARCHY |WEIGHT |COVERAGE |
cleaning | | |
Line 1,735 ⟶ 1,761:
cinema | |0.75 |
END
}</lang>
{
say .[0] for walktree($_)
 
sub walktree ($data) {
my (@parts, $cnt);
 
while ($data ~~ m:nth(++$cnt)/$<head>=[(\s*) \N+\n ] # split off one level as 'head' (or terminal 'leaf')
$<body>=[[$0 \s+ \N+\n]*]/ ) { # next sub-level is 'body' (defined by extra depth of indentation)
 
my ($head, $body) = ($<head>, $<body>);
$head ~~ /^.*? \| $<weight>=[\S*] \s*\| $<coverage>=[\S*] \s*\|/; # save values of weight and coverage (if any) for later
 
my ($w, $wsum) = (0, 0);
$head ~= .[0],
$w += .[1],
$wsum += .[1] * .[2]
for walktree( $body );
 
my $weight = (~$<weight> or 1).fmt('%-8s');
my $coverage = $w == 0
?? (~$<coverage> or 0).fmt('%-10s')
!! ($wsum/$w) .fmt('%-10.2g');
@parts.push: [$head.subst(/\|\N+/, "|$weight|$coverage|"), $weight, $coverage ];
}
return @parts;
}</lang>
{{out}}
<pre style="font-size:70%;">NAME_HIERARCHY |WEIGHT |COVERAGE |
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.