Functional coverage tree: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: more idiomatic: matching, string formatting)
Line 1,741: Line 1,741:
sub walktree ($data) {
sub walktree ($data) {
my @parts;
my @parts;
my $pos = 0;
my $cnt = 1;
while ($data ~~ m:nth($cnt++)/$<head>=[(\s*) {} \N+\n ]

$<body>=[[$0 \s+ \N+\n]*]/ ) {
loop {
last if $pos >= $data.chars;
$data ~~ m:p($pos)/(\s*)/; my $s = $0;
$data ~~ m:p($pos)/$<head>=[\s* \N+\n ] # split off one level as 'head' (or terminal 'leaf')
$<body>=[[$s \s+\N+\n]*]/; # next sub-level is 'body' (defined by extra depth of indentation)
$pos = $/.pos; # start next match here


my ($head, $body) = ($<head>, $<body>);
my ($head, $body) = ($<head>, $<body>);
$head ~~ /^.*? \| (\S*) \s*\| (\S*) \s*\|/;
$head ~~ /^.*? \| (\S*) \s*\| (\S*) \s*\|/;
my $weight = sprintf '%-8s', $0 ne '' ?? $0 !! 1;
my $weight = ($0 ne '' ?? $0 !! 1).fmt('%-8s');
my $coverage = sprintf '%-10s', $1 ne '' ?? $1 !! 0;
my $coverage = ($1 ne '' ?? $1 !! 0).fmt('%-10s');


my ($w, $wsum) = (0, 0);
my ($w, $wsum) = (0, 0);
Line 1,761: Line 1,756:
for walktree( $body );
for walktree( $body );


$coverage = sprintf '%-10.2g', $wsum/$w unless $w == 0;
$coverage = ($wsum/$w).fmt('%-10.2g') unless $w == 0;
@parts.push: [$head.subst(/\|\N+/, "|$weight|$coverage|"), $weight, $coverage ];
@parts.push: [$head.subst(/\|\N+/, "|$weight|$coverage|"), $weight, $coverage ];
}
}