| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# Copyright IBM Corp. 2017 |
| 4 |
# |
| 5 |
# Usage: mkinfo <config_file> [-o <output_dir>] [--seed <seed>] |
| 6 |
# [<key>=<value>...] |
| 7 |
# |
| 8 |
# Create a fake lcov code coverage data file and optionally the corresponding |
| 9 |
# source tree. DATA_FILE contains all specifications for creating the data |
| 10 |
# file. Directives can be overridden using KEY=VALUE specifications with KEY |
| 11 |
# being in the form SECTION.KEY. SEED specifies the number used to initialize |
| 12 |
# the pseudo random number generator. |
| 13 |
# |
| 14 |
# Example: |
| 15 |
# mkinfo profiles/small -o src files.numfiles=12 |
| 16 |
# |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use Getopt::Long; |
| 22 |
use Cwd qw(abs_path getcwd); |
| 23 |
use File::Path qw(make_path); |
| 24 |
use File::Basename; |
| 25 |
use Data::Dumper; |
| 26 |
|
| 27 |
my $MAX_TAKEN = 1000; |
| 28 |
my $use_colors = -t STDIN; |
| 29 |
my $BOLD = $use_colors ? "\033[1m" : ""; |
| 30 |
my $RESET = $use_colors ? "\033[0m" : ""; |
| 31 |
|
| 32 |
sub usage() |
| 33 |
{ |
| 34 |
print(<<EOF); |
| 35 |
Usage: $0 <config_file> [-o <output_dir>] [--seed <seed>] [<key>=<value>...] |
| 36 |
|
| 37 |
Create a fake lcov code coverage data file and optionally the corresponding |
| 38 |
source tree. DATA_FILE contains all specifications for creating the data |
| 39 |
file. Directives can be overridden using KEY=VALUE specifications with KEY |
| 40 |
being in the form SECTION.KEY. SEED specifies the number used to initialize |
| 41 |
the pseudo random number generator. |
| 42 |
|
| 43 |
Example: |
| 44 |
$0 profiles/small -o src files.numfiles=12 |
| 45 |
EOF |
| 46 |
} |
| 47 |
|
| 48 |
sub read_config($) |
| 49 |
{ |
| 50 |
my ($filename) = @_; |
| 51 |
my $fd; |
| 52 |
my %config; |
| 53 |
my $section; |
| 54 |
|
| 55 |
open($fd, "<", $filename) or die("Could not open $filename: $!\n"); |
| 56 |
while (my $line = <$fd>) { |
| 57 |
my ($key, $value); |
| 58 |
|
| 59 |
$line =~ s/(^\s*|\s*$)//g; |
| 60 |
next if ($line eq "" || $line =~ /^#/); |
| 61 |
if ($line =~ /^\[\s*(\S+)\s*]$/) { |
| 62 |
$section = $1; |
| 63 |
next; |
| 64 |
} |
| 65 |
if ($line !~ /^(\S+)\s*=\s*(.*)$/) { |
| 66 |
die("$filename:$.: Unknown line format: $line\n"); |
| 67 |
} |
| 68 |
($key, $value) = ($1, $2); |
| 69 |
if (!defined($section)) { |
| 70 |
die("$filename:$.: Directive outside of section\n"); |
| 71 |
} |
| 72 |
$config{$section}->{$1} = $2; |
| 73 |
} |
| 74 |
close($fd); |
| 75 |
|
| 76 |
return \%config; |
| 77 |
} |
| 78 |
|
| 79 |
sub apply_config($$) |
| 80 |
{ |
| 81 |
my ($config, $directive) = @_; |
| 82 |
|
| 83 |
for my $dir (@$directive) { |
| 84 |
if ($dir !~ /^([^\.]+)\.([^=]+)=(.*)$/) { |
| 85 |
die("Unknown directive format: $dir\n"); |
| 86 |
} |
| 87 |
$config->{$1}->{$2} = $3; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
sub get_value($$;$) |
| 92 |
{ |
| 93 |
my ($config, $dir, $default) = @_; |
| 94 |
my ($section, $key, $value); |
| 95 |
|
| 96 |
if ($dir !~ /^([^\.]+)\.([^=]+)$/) { |
| 97 |
die("$0: Internal error: Unknown key format: $key\n"); |
| 98 |
} |
| 99 |
($section, $key) = ($1, $2); |
| 100 |
|
| 101 |
$value = $config->{$section}->{$key}; |
| 102 |
|
| 103 |
if (!defined($value)) { |
| 104 |
if (!defined($default)) { |
| 105 |
die("$0: Missing config value for $dir\n"); |
| 106 |
} |
| 107 |
$value = $default; |
| 108 |
} |
| 109 |
|
| 110 |
return $value; |
| 111 |
} |
| 112 |
|
| 113 |
sub get_int($$;$$$) |
| 114 |
{ |
| 115 |
my ($config, $dir, $default, $min, $max) = @_; |
| 116 |
my $value = get_value($config, $dir, $default); |
| 117 |
|
| 118 |
if ($value !~ /^\d+$/) { |
| 119 |
die("$0: Config value $dir must be an integer: $value\n"); |
| 120 |
} |
| 121 |
$value = int($value); |
| 122 |
if (defined($min) && $value < $min) { |
| 123 |
die("$0: Config value $dir is too low (min $min): $value\n"); |
| 124 |
} |
| 125 |
if (defined($max) && $value > $max) { |
| 126 |
die("$0: Config value $dir is too high (max $max): $value\n"); |
| 127 |
} |
| 128 |
|
| 129 |
return int($value); |
| 130 |
} |
| 131 |
|
| 132 |
sub get_list($$;$) |
| 133 |
{ |
| 134 |
my ($config, $dir, $default) = @_; |
| 135 |
my $value = get_value($config, $dir, $default); |
| 136 |
my @list = split(/\s+/, $value); |
| 137 |
|
| 138 |
return \@list; |
| 139 |
} |
| 140 |
|
| 141 |
sub randlist($) |
| 142 |
{ |
| 143 |
my ($list) = @_; |
| 144 |
|
| 145 |
return "" if (!@$list); |
| 146 |
return $list->[int(rand(scalar(@$list)))]; |
| 147 |
} |
| 148 |
|
| 149 |
sub randbool() |
| 150 |
{ |
| 151 |
return int(rand(2)); |
| 152 |
} |
| 153 |
|
| 154 |
# Reduce LIST to PERCENTAGE of its former size. |
| 155 |
sub reduce_list_per($$) |
| 156 |
{ |
| 157 |
my ($list, $percentage) = @_; |
| 158 |
my $remove; |
| 159 |
|
| 160 |
$remove = int((100 - $percentage) * scalar(@$list) / 100); |
| 161 |
|
| 162 |
for (my $i = 0; $i < $remove; $i++) { |
| 163 |
splice(@$list, int(rand(scalar(@$list))), 1); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
# Reduce LIST to NUM items. |
| 168 |
sub reduce_list_num($$) |
| 169 |
{ |
| 170 |
my ($list, $num) = @_; |
| 171 |
my $remove; |
| 172 |
|
| 173 |
$remove = scalar(@$list) - $num; |
| 174 |
|
| 175 |
for (my $i = 0; $i < $remove; $i++) { |
| 176 |
splice(@$list, int(rand(scalar(@$list))), 1); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
sub _gen_filename($$) |
| 181 |
{ |
| 182 |
my ($c, $root) = @_; |
| 183 |
my $ltop = get_list($c, "files.top", ""); |
| 184 |
my $lsub = get_list($c, "files.sub", ""); |
| 185 |
my $lsubsub = get_list($c, "files.subsub", ""); |
| 186 |
my $lprefix = get_list($c, "files.prefix"); |
| 187 |
my $lsuffix = get_list($c, "files.suffix", ""); |
| 188 |
my $lext = get_list($c, "files.ext"); |
| 189 |
my ($top, $sub, $subsub, $prefix, $suffix, $ext) = ("", "", "", "", "", ""); |
| 190 |
my $filename = ""; |
| 191 |
|
| 192 |
$top = randlist($ltop) if (randbool()); |
| 193 |
$sub = randlist($lsub) if (randbool()); |
| 194 |
$subsub = randlist($lsubsub) if (randbool()); |
| 195 |
$prefix = randlist($lprefix); |
| 196 |
$suffix = randlist($lsuffix) if (randbool()); |
| 197 |
$ext = randlist($lext); |
| 198 |
|
| 199 |
$filename = $root; |
| 200 |
$filename .= "/" . $top if ($top ne ""); |
| 201 |
$filename .= "/" . $sub if ($sub ne ""); |
| 202 |
$filename .= "/" . $subsub if ($subsub ne ""); |
| 203 |
$filename .= "/" . $prefix; |
| 204 |
$filename .= "_" . $suffix if ($suffix ne ""); |
| 205 |
$filename .= $ext; |
| 206 |
$filename =~ s#^//#/#; |
| 207 |
|
| 208 |
return $filename; |
| 209 |
} |
| 210 |
|
| 211 |
sub gen_filename($$$) |
| 212 |
{ |
| 213 |
my ($c, $root, $filenames) = @_; |
| 214 |
my $filename; |
| 215 |
|
| 216 |
do { |
| 217 |
$filename = _gen_filename($c, $root); |
| 218 |
} while ($filenames->{$filename}); |
| 219 |
$filenames->{$filename} = 1; |
| 220 |
|
| 221 |
return $filename; |
| 222 |
} |
| 223 |
|
| 224 |
sub gen_lines($$) |
| 225 |
{ |
| 226 |
my ($c, $length) = @_; |
| 227 |
my @lines = 1 .. $length; |
| 228 |
my $percent = get_int($c, "lines.instrumented", undef, 0, 100); |
| 229 |
|
| 230 |
reduce_list_per(\@lines, $percent); |
| 231 |
|
| 232 |
return \@lines; |
| 233 |
} |
| 234 |
|
| 235 |
sub gen_fnname($$) |
| 236 |
{ |
| 237 |
my ($c, $hash) = @_; |
| 238 |
my $lverb = get_list($c, "functions.verb"); |
| 239 |
my $ladj = get_list($c, "functions.adj", ""); |
| 240 |
my $lnoun = get_list($c, "functions.noun", ""); |
| 241 |
my ($verb, $adj, $noun) = ("", "", ""); |
| 242 |
my $fnname; |
| 243 |
|
| 244 |
$verb = randlist($lverb); |
| 245 |
$adj = randlist($ladj) if (randbool()); |
| 246 |
$noun = randlist($lnoun) if (randbool()); |
| 247 |
|
| 248 |
$fnname = $verb; |
| 249 |
$fnname .= "_" . $adj if ($adj ne ""); |
| 250 |
$fnname .= "_" . $noun if ($noun ne ""); |
| 251 |
|
| 252 |
if (exists($hash->{$fnname})) { |
| 253 |
my $i = 2; |
| 254 |
|
| 255 |
while (exists($hash->{$fnname . $i})) { |
| 256 |
$i++; |
| 257 |
} |
| 258 |
$fnname .= $i; |
| 259 |
} |
| 260 |
$hash->{$fnname} = 1; |
| 261 |
|
| 262 |
return $fnname; |
| 263 |
} |
| 264 |
|
| 265 |
sub gen_functions($$) |
| 266 |
{ |
| 267 |
my ($c, $lines) = @_; |
| 268 |
my @fnlines; |
| 269 |
my @functions; |
| 270 |
my %names; |
| 271 |
my $percent = get_int($c, "functions.perinstrumented", undef, 0, 100); |
| 272 |
|
| 273 |
@fnlines = @$lines; |
| 274 |
reduce_list_per(\@fnlines, $percent); |
| 275 |
|
| 276 |
foreach my $fnline (@fnlines) { |
| 277 |
push(@functions, [$fnline, gen_fnname($c, \%names)]); |
| 278 |
} |
| 279 |
|
| 280 |
return \@functions; |
| 281 |
} |
| 282 |
|
| 283 |
# Returns a value distribution object. This object can be used to randomly |
| 284 |
# choose one element from a list of elements with a given relative distribution. |
| 285 |
# |
| 286 |
# dist: [ sumprob, probs] |
| 287 |
# sumprob: Sum of all probabilities |
| 288 |
# probs: [ prob1, prob2, ... ] |
| 289 |
# prob: [ num, x ] |
| 290 |
# num: Value |
| 291 |
sub get_dist($$;$) |
| 292 |
{ |
| 293 |
my ($c, $dir, $default) = @_; |
| 294 |
my $list = get_list($c, $dir, $default); |
| 295 |
my $sumprob = 0; |
| 296 |
my @probs; |
| 297 |
|
| 298 |
foreach my $spec (@$list) { |
| 299 |
my ($n, $p); |
| 300 |
|
| 301 |
if ($spec =~ /^(\d+):(\d+)$/) { |
| 302 |
($n, $p) = ($1, $2); |
| 303 |
} elsif ($spec =~ /^(\d+)$/) { |
| 304 |
$n = $1; |
| 305 |
$p = 1; |
| 306 |
} else { |
| 307 |
die("$0: Config value $dir must be a distribution " . |
| 308 |
"list (a:p1 b:p2 ...)\n"); |
| 309 |
} |
| 310 |
$sumprob += $p; |
| 311 |
push(@probs, [$n, $sumprob]); |
| 312 |
} |
| 313 |
|
| 314 |
return [$sumprob, \@probs]; |
| 315 |
} |
| 316 |
|
| 317 |
sub rand_dist($) |
| 318 |
{ |
| 319 |
my ($dist) = @_; |
| 320 |
my ($sumprob, $probs) = @$dist; |
| 321 |
my $r = int(rand($sumprob)); |
| 322 |
|
| 323 |
foreach my $prob (@$probs) { |
| 324 |
my ($num, $x) = @$prob; |
| 325 |
return $num if ($r < $x); |
| 326 |
} |
| 327 |
|
| 328 |
die("Internal error: Incomplete distribution list\n"); |
| 329 |
} |
| 330 |
|
| 331 |
sub gen_branches($$) |
| 332 |
{ |
| 333 |
my ($c, $lines) = @_; |
| 334 |
my $percent = get_int($c, "branches.perinstrumented", undef, 0, 100); |
| 335 |
my @allblocks = @{get_list($c, "branches.blocks", "0")}; |
| 336 |
my $branchdist = get_dist($c, "branches.branchdist", "2"); |
| 337 |
my @brlines; |
| 338 |
my @branches; |
| 339 |
|
| 340 |
@brlines = @$lines; |
| 341 |
reduce_list_per(\@brlines, $percent); |
| 342 |
|
| 343 |
foreach my $brline (@brlines) { |
| 344 |
my @blocks = @allblocks; |
| 345 |
my $numblocks = int(rand(scalar(@blocks))) + 1; |
| 346 |
|
| 347 |
reduce_list_num(\@blocks, $numblocks); |
| 348 |
|
| 349 |
foreach my $block (@blocks) { |
| 350 |
my $numbranch = rand_dist($branchdist); |
| 351 |
|
| 352 |
for (my $branch = 0; $branch < $numbranch; $branch++) { |
| 353 |
push(@branches, [$brline, $block, $branch]); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
return \@branches; |
| 359 |
} |
| 360 |
|
| 361 |
sub gen_filesrc($) |
| 362 |
{ |
| 363 |
my ($c) = @_; |
| 364 |
my ($length, $lines, $functions, $branches); |
| 365 |
my $do_ln = get_int($c, "lines.enabled"); |
| 366 |
my $do_fn = get_int($c, "functions.enabled"); |
| 367 |
my $do_br = get_int($c, "branches.enabled"); |
| 368 |
|
| 369 |
$length = 1 + int(rand(get_int($c, "lines.maxlines"))); |
| 370 |
$lines = gen_lines($c, $length); |
| 371 |
$functions = gen_functions($c, $lines) if ($do_fn); |
| 372 |
$branches = gen_branches($c, $lines) if ($do_br); |
| 373 |
|
| 374 |
return [$length, $lines, $functions, $branches]; |
| 375 |
} |
| 376 |
|
| 377 |
# Generate fake source tree. |
| 378 |
# |
| 379 |
# returns: [ files, numlns, numfns, numbrs ] |
| 380 |
# files: filename -> filesrc |
| 381 |
# filesrc: [ length, lines, functions, branches ] |
| 382 |
# length: Total number of lines in file |
| 383 |
# |
| 384 |
# lines: [ line1, line2, ... ] |
| 385 |
# |
| 386 |
# functions: [ fn1, fn2, ... ] |
| 387 |
# fn: [ fnline, fnname ] |
| 388 |
# fnline: Starting line of function |
| 389 |
# fnname: Function name |
| 390 |
# |
| 391 |
# branches: [ brdata1, brdata2, ...] |
| 392 |
# brdata: [ brline, block, branch ] |
| 393 |
# brline: Line number containing branches |
| 394 |
# block: Block ID |
| 395 |
# branch: Branch ID |
| 396 |
# |
| 397 |
sub gen_src($$) |
| 398 |
{ |
| 399 |
my ($c, $root) = @_; |
| 400 |
my %files; |
| 401 |
my $numfiles = get_int($c, "files.numfiles"); |
| 402 |
my %filenames; |
| 403 |
my ($numlns, $numfns, $numbrs) = (0, 0, 0); |
| 404 |
|
| 405 |
for (my $i = 0; $i < $numfiles; $i++) { |
| 406 |
my $filename = gen_filename($c, $root, \%filenames); |
| 407 |
my $filesrc = gen_filesrc($c); |
| 408 |
|
| 409 |
$files{$filename} = $filesrc; |
| 410 |
$numlns += scalar(@{$filesrc->[1]}) if (defined($filesrc->[1])); |
| 411 |
$numfns += scalar(@{$filesrc->[2]}) if (defined($filesrc->[2])); |
| 412 |
$numbrs += scalar(@{$filesrc->[3]}) if (defined($filesrc->[3])); |
| 413 |
} |
| 414 |
|
| 415 |
return [\%files, $numlns, $numfns, $numbrs]; |
| 416 |
} |
| 417 |
|
| 418 |
sub write_src($) |
| 419 |
{ |
| 420 |
my ($src) = @_; |
| 421 |
my ($files, $numlns, $numfns, $numbrs) = @$src; |
| 422 |
|
| 423 |
foreach my $filename (sort(keys(%{$files}))) { |
| 424 |
my $filesrc = $files->{$filename}; |
| 425 |
my $length = $filesrc->[0]; |
| 426 |
my $dir = dirname($filename); |
| 427 |
my $fd; |
| 428 |
|
| 429 |
if (!-d $dir) { |
| 430 |
make_path($dir) or |
| 431 |
die("Could not create directory $dir\n"); |
| 432 |
} |
| 433 |
|
| 434 |
open($fd, ">", $filename) or |
| 435 |
die("Could not create file $filename: $!\n"); |
| 436 |
for (my $i = 0; $i < $length; $i++) { |
| 437 |
print($fd "\n"); |
| 438 |
} |
| 439 |
close($fd); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
sub write_branches($$$$) |
| 444 |
{ |
| 445 |
my ($fd, $branches, $brhits, $iref) = @_; |
| 446 |
my ($found, $hit) = (0, 0); |
| 447 |
|
| 448 |
# Line coverage data |
| 449 |
foreach my $brdata (@$branches) { |
| 450 |
my $brhit = $brhits->[$$iref++]; |
| 451 |
my ($brline, $block, $branch) = @$brdata; |
| 452 |
|
| 453 |
$found++; |
| 454 |
$hit++ if ($brhit ne "-" && $brhit > 0); |
| 455 |
print($fd "BRDA:$brline,$block,$branch,$brhit\n"); |
| 456 |
} |
| 457 |
if ($found > 0) { |
| 458 |
print($fd "BRF:$found\n"); |
| 459 |
print($fd "BRH:$hit\n"); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
sub write_lines($$$$) |
| 464 |
{ |
| 465 |
my ($fd, $lines, $lnhist, $iref) = @_; |
| 466 |
my ($found, $hit) = (0, 0); |
| 467 |
|
| 468 |
# Line coverage data |
| 469 |
foreach my $line (@$lines) { |
| 470 |
my $lnhit = $lnhist->[$$iref++]; |
| 471 |
|
| 472 |
$found++; |
| 473 |
$hit++ if ($lnhit > 0); |
| 474 |
print($fd "DA:$line,$lnhit\n"); |
| 475 |
} |
| 476 |
print($fd "LF:$found\n"); |
| 477 |
print($fd "LH:$hit\n"); |
| 478 |
} |
| 479 |
|
| 480 |
sub write_functions($$$$) |
| 481 |
{ |
| 482 |
my ($fd, $functions, $fnhits, $iref) = @_; |
| 483 |
my ($found, $hit) = (0, 0); |
| 484 |
|
| 485 |
# Function coverage data |
| 486 |
foreach my $fn (@$functions) { |
| 487 |
my ($fnline, $fnname) = @$fn; |
| 488 |
|
| 489 |
print($fd "FN:$fnline,$fnname\n"); |
| 490 |
} |
| 491 |
foreach my $fn (@$functions) { |
| 492 |
my ($fnline, $fnname) = @$fn; |
| 493 |
my $fnhit = $fnhits->[$$iref++]; |
| 494 |
|
| 495 |
$found++; |
| 496 |
$hit++ if ($fnhit > 0); |
| 497 |
print($fd "FNDA:$fnhit,$fnname\n"); |
| 498 |
} |
| 499 |
print($fd "FNF:$found\n"); |
| 500 |
print($fd "FNH:$hit\n"); |
| 501 |
} |
| 502 |
|
| 503 |
sub write_filesrc($$$$$) |
| 504 |
{ |
| 505 |
my ($c, $fd, $filesrc, $hits, $iter) = @_; |
| 506 |
my ($length, $lines, $functions, $branches) = @$filesrc; |
| 507 |
my $do_ln = get_int($c, "lines.enabled"); |
| 508 |
my $do_fn = get_int($c, "functions.enabled"); |
| 509 |
my $do_br = get_int($c, "branches.enabled"); |
| 510 |
|
| 511 |
write_functions($fd, $functions, $hits->[1], \$iter->[1]) if ($do_fn); |
| 512 |
write_branches($fd, $branches, $hits->[2], \$iter->[2]) if ($do_br); |
| 513 |
write_lines($fd, $lines, $hits->[0], \$iter->[0]) if ($do_ln); |
| 514 |
} |
| 515 |
|
| 516 |
sub write_info($$$$) |
| 517 |
{ |
| 518 |
my ($c, $filename, $src, $hits) = @_; |
| 519 |
my $files = $src->[0]; |
| 520 |
my $fd; |
| 521 |
my %iters; |
| 522 |
|
| 523 |
foreach my $testname (keys(%{$hits})) { |
| 524 |
$iters{$testname} = [0, 0, 0]; |
| 525 |
} |
| 526 |
|
| 527 |
open($fd, ">", $filename) or die("Could not create $filename: $!\n"); |
| 528 |
|
| 529 |
foreach my $filename (sort(keys(%{$files}))) { |
| 530 |
my $filesrc = $files->{$filename}; |
| 531 |
|
| 532 |
foreach my $testname (sort(keys(%{$hits}))) { |
| 533 |
my $testhits = $hits->{$testname}; |
| 534 |
my $iter = $iters{$testname}; |
| 535 |
|
| 536 |
print($fd "TN:$testname\n"); |
| 537 |
print($fd "SF:$filename\n"); |
| 538 |
|
| 539 |
write_filesrc($c, $fd, $filesrc, $testhits, $iter); |
| 540 |
|
| 541 |
print($fd "end_of_record\n"); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
close($fd); |
| 546 |
} |
| 547 |
|
| 548 |
sub get_hit_found($) |
| 549 |
{ |
| 550 |
my ($list) = @_; |
| 551 |
my ($hit, $found) = (0, 0); |
| 552 |
|
| 553 |
foreach my $e (@$list) { |
| 554 |
$hit++ if ($e ne "-" && $e > 0); |
| 555 |
$found++; |
| 556 |
} |
| 557 |
return ($hit, $found); |
| 558 |
} |
| 559 |
|
| 560 |
sub write_counts($$) |
| 561 |
{ |
| 562 |
my ($filename, $hits) = @_; |
| 563 |
my $fd; |
| 564 |
my (@tlnhits, @tfnhits, @tbrhits); |
| 565 |
|
| 566 |
foreach my $testname (keys(%{$hits})) { |
| 567 |
my $testhits = $hits->{$testname}; |
| 568 |
my ($lnhits, $fnhits, $brhits) = @$testhits; |
| 569 |
|
| 570 |
for (my $i = 0; $i < scalar(@$lnhits); $i++) { |
| 571 |
$tlnhits[$i] += $lnhits->[$i]; |
| 572 |
} |
| 573 |
for (my $i = 0; $i < scalar(@$fnhits); $i++) { |
| 574 |
$tfnhits[$i] += $fnhits->[$i]; |
| 575 |
} |
| 576 |
for (my $i = 0; $i < scalar(@$brhits); $i++) { |
| 577 |
my $h = $brhits->[$i]; |
| 578 |
|
| 579 |
$h = 0 if ($h eq "-"); |
| 580 |
$tbrhits[$i] += $h; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
open($fd, ">", $filename) or die("Could not create $filename: $!\n"); |
| 585 |
print($fd join(" ", |
| 586 |
get_hit_found(\@tlnhits), get_hit_found(\@tfnhits), |
| 587 |
get_hit_found(\@tbrhits)) . |
| 588 |
"\n"); |
| 589 |
close($fd); |
| 590 |
} |
| 591 |
|
| 592 |
# A branch hit value for a block that was not hit must be "-". A branch hit |
| 593 |
# value for a block that was hit cannot be "-", but must be "0" if not hit. |
| 594 |
sub sanitize_brhits($) |
| 595 |
{ |
| 596 |
my ($brhits) = @_; |
| 597 |
my $block_hit = 0; |
| 598 |
|
| 599 |
foreach my $brhit_ref (@$brhits) { |
| 600 |
if ($$brhit_ref ne "-" && $$brhit_ref > 0) { |
| 601 |
$block_hit = 1; |
| 602 |
last; |
| 603 |
} |
| 604 |
} |
| 605 |
foreach my $brhit_ref (@$brhits) { |
| 606 |
if (!$block_hit) { |
| 607 |
$$brhit_ref = "-"; |
| 608 |
} elsif ($$brhit_ref eq "-") { |
| 609 |
$$brhit_ref = 0; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
# Ensure coverage rate interdependencies are met |
| 615 |
sub sanitize_hits($$) |
| 616 |
{ |
| 617 |
my ($src, $hits) = @_; |
| 618 |
my $files = $src->[0]; |
| 619 |
|
| 620 |
foreach my $hits (values(%{$hits})) { |
| 621 |
my $brhits = $hits->[2]; |
| 622 |
my $i = 0; |
| 623 |
|
| 624 |
foreach my $filename (sort(keys(%{$files}))) { |
| 625 |
my $filesrc = $files->{$filename}; |
| 626 |
my $branches = $filesrc->[3]; |
| 627 |
my $lastblock; |
| 628 |
my $lastline; |
| 629 |
my @blist; |
| 630 |
|
| 631 |
foreach my $brdata (@$branches) { |
| 632 |
my ($brline, $block, $branch) = @$brdata; |
| 633 |
|
| 634 |
if (!defined($lastblock) || |
| 635 |
$block != $lastblock || |
| 636 |
$brline != $lastline) { |
| 637 |
sanitize_brhits(\@blist); |
| 638 |
@blist = (); |
| 639 |
$lastblock = $block; |
| 640 |
$lastline = $brline; |
| 641 |
} |
| 642 |
push(@blist, \$brhits->[$i++]); |
| 643 |
} |
| 644 |
sanitize_brhits(\@blist); |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
# Generate random coverage data |
| 650 |
# |
| 651 |
# returns: testname -> testhits |
| 652 |
# testhits: [ lnhits, fnhits, brhits ] |
| 653 |
# lnhits: [ ln1hit, ln2hit, ... ] |
| 654 |
# lnhit: Number of times a line was hit by a specific test |
| 655 |
# fnhits: [ fn1hit, fn2hit, ... ] |
| 656 |
# fnhit: Number of times a function was hit by a specific test |
| 657 |
# brhits: [ br1hit, br2hit, ... ] |
| 658 |
# brhit: Number of times a branch was hit by a specific test |
| 659 |
sub gen_hits($$) |
| 660 |
{ |
| 661 |
my ($c, $src) = @_; |
| 662 |
my (@lnhits, @fnhits, @brhits); |
| 663 |
my ($files, $numlns, $numfns, $numbrs) = @$src; |
| 664 |
my $testnames = get_list($c, "tests.names", ""); |
| 665 |
my %hits; |
| 666 |
|
| 667 |
$testnames = [""] if (!@$testnames); |
| 668 |
|
| 669 |
foreach my $testname (@$testnames) { |
| 670 |
my (@lnhits, @fnhits, @brhits); |
| 671 |
|
| 672 |
for (my $i = 0; $i < $numlns; $i++) { |
| 673 |
push(@lnhits, 1 + int(rand($MAX_TAKEN))); |
| 674 |
} |
| 675 |
|
| 676 |
for (my $i = 0; $i < $numfns; $i++) { |
| 677 |
push(@fnhits, 1 + int(rand($MAX_TAKEN))); |
| 678 |
} |
| 679 |
|
| 680 |
for (my $i = 0; $i < $numbrs; $i++) { |
| 681 |
push(@brhits, 1 + int(rand($MAX_TAKEN))); |
| 682 |
} |
| 683 |
|
| 684 |
$hits{$testname} = [\@lnhits, \@fnhits, \@brhits]; |
| 685 |
} |
| 686 |
|
| 687 |
sanitize_hits($src, \%hits); |
| 688 |
|
| 689 |
return \%hits; |
| 690 |
} |
| 691 |
|
| 692 |
# Return a hash containing RATE percent of indices [0..NUM-1]. |
| 693 |
sub gen_filter($$) |
| 694 |
{ |
| 695 |
my ($num, $rate) = @_; |
| 696 |
my @list = (0 .. ($num - 1)); |
| 697 |
my %hash; |
| 698 |
|
| 699 |
reduce_list_per(\@list, $rate); |
| 700 |
foreach my $i (@list) { |
| 701 |
$hash{$i} = 1; |
| 702 |
} |
| 703 |
|
| 704 |
return \%hash; |
| 705 |
} |
| 706 |
|
| 707 |
# Zero all entries in LIST identified by the indices in FILTER. |
| 708 |
sub zero_by_filter($$) |
| 709 |
{ |
| 710 |
my ($list, $filter) = @_; |
| 711 |
|
| 712 |
foreach my $i (keys(%{$filter})) { |
| 713 |
$list->[$i] = 0; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
# Add a random number of indices between [0..NUM-1] to FILTER. |
| 718 |
sub widen_filter($$) |
| 719 |
{ |
| 720 |
my ($filter, $num) = @_; |
| 721 |
my @list; |
| 722 |
|
| 723 |
for (my $i = 0; $i < $num; $i++) { |
| 724 |
push(@list, $i) if (!exists($filter->{$i})); |
| 725 |
} |
| 726 |
reduce_list_per(\@list, int(rand(101))); |
| 727 |
|
| 728 |
foreach my $i (@list) { |
| 729 |
$filter->{$i} = 1; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
# Zero coverage data in HITS until the combined coverage rates reach the |
| 734 |
# specified RATEs. |
| 735 |
sub reduce_hits($$$$$) |
| 736 |
{ |
| 737 |
my ($src, $hits, $lnrate, $fnrate, $brrate) = @_; |
| 738 |
my ($files, $numlns, $numfns, $numbrs) = @$src; |
| 739 |
my ($lnfilter, $fnfilter, $brfilter); |
| 740 |
|
| 741 |
$lnfilter = gen_filter($numlns, 100 - $lnrate); |
| 742 |
$fnfilter = gen_filter($numfns, 100 - $fnrate); |
| 743 |
$brfilter = gen_filter($numbrs, 100 - $brrate); |
| 744 |
|
| 745 |
foreach my $testhits (values(%{$hits})) { |
| 746 |
my ($lnhits, $fnhits, $brhits) = @$testhits; |
| 747 |
|
| 748 |
zero_by_filter($lnhits, $lnfilter); |
| 749 |
zero_by_filter($fnhits, $fnfilter); |
| 750 |
zero_by_filter($brhits, $brfilter); |
| 751 |
|
| 752 |
# Provide some variation between tests |
| 753 |
widen_filter($lnfilter, $numlns); |
| 754 |
widen_filter($fnfilter, $numfns); |
| 755 |
widen_filter($brfilter, $numbrs); |
| 756 |
} |
| 757 |
|
| 758 |
sanitize_hits($src, $hits); |
| 759 |
} |
| 760 |
|
| 761 |
sub zero_list($) |
| 762 |
{ |
| 763 |
my ($list) = @_; |
| 764 |
|
| 765 |
foreach my $i (@$list) { |
| 766 |
$i = 0; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
# Zero all coverage in HITS. |
| 771 |
sub zero_hits($$) |
| 772 |
{ |
| 773 |
my ($src, $hits) = @_; |
| 774 |
|
| 775 |
foreach my $testhits (values(%{$hits})) { |
| 776 |
my ($lnhits, $fnhits, $brhits) = @$testhits; |
| 777 |
|
| 778 |
zero_list($lnhits); |
| 779 |
zero_list($fnhits); |
| 780 |
zero_list($brhits); |
| 781 |
} |
| 782 |
|
| 783 |
sanitize_hits($src, $hits); |
| 784 |
} |
| 785 |
|
| 786 |
# Distribute items from LIST to A and B depending on whether the index for |
| 787 |
# an item is found in FILTER. |
| 788 |
sub split_by_filter($$$$) |
| 789 |
{ |
| 790 |
my ($list, $filter, $a, $b) = @_; |
| 791 |
|
| 792 |
for (my $i = 0; $i < scalar(@$list); $i++) { |
| 793 |
if (exists($filter->{$i})) { |
| 794 |
push(@$a, $list->[$i]); |
| 795 |
push(@$b, 0); |
| 796 |
} else { |
| 797 |
push(@$a, 0); |
| 798 |
push(@$b, $list->[$i]); |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
sub split_hits($$$) |
| 804 |
{ |
| 805 |
my ($c, $src, $hits) = @_; |
| 806 |
my ($files, $numlns, $numfns, $numbrs) = @$src; |
| 807 |
my ($lnsplit, $fnsplit, $brsplit); |
| 808 |
my (%a, %b); |
| 809 |
|
| 810 |
$lnsplit = gen_filter($numlns, int(rand(101))); |
| 811 |
$fnsplit = gen_filter($numfns, int(rand(101))); |
| 812 |
$brsplit = gen_filter($numbrs, int(rand(101))); |
| 813 |
|
| 814 |
foreach my $testname (keys(%{$hits})) { |
| 815 |
my $testhits = $hits->{$testname}; |
| 816 |
my ($lnhits, $fnhits, $brhits) = @$testhits; |
| 817 |
my (@lnhitsa, @fnhitsa, @brhitsa); |
| 818 |
my (@lnhitsb, @fnhitsb, @brhitsb); |
| 819 |
|
| 820 |
split_by_filter($lnhits, $lnsplit, \@lnhitsa, \@lnhitsb); |
| 821 |
split_by_filter($fnhits, $fnsplit, \@fnhitsa, \@fnhitsb); |
| 822 |
split_by_filter($brhits, $brsplit, \@brhitsa, \@brhitsb); |
| 823 |
|
| 824 |
$a{$testname} = [\@lnhitsa, \@fnhitsa, \@brhitsa]; |
| 825 |
$b{$testname} = [\@lnhitsb, \@fnhitsb, \@brhitsb]; |
| 826 |
} |
| 827 |
|
| 828 |
sanitize_hits($src, \%a); |
| 829 |
sanitize_hits($src, \%b); |
| 830 |
|
| 831 |
return (\%a, \%b); |
| 832 |
} |
| 833 |
|
| 834 |
sub plural($$$) |
| 835 |
{ |
| 836 |
my ($num, $sing, $plur) = @_; |
| 837 |
|
| 838 |
return $num <= 1 ? $sing : $plur; |
| 839 |
} |
| 840 |
|
| 841 |
sub print_intro($) |
| 842 |
{ |
| 843 |
my ($c) = @_; |
| 844 |
my $numtests = scalar(@{get_list($c, "tests.names")}); |
| 845 |
my $numfiles = get_int($c, "files.numfiles"); |
| 846 |
|
| 847 |
$numtests = 1 if ($numtests < 1); |
| 848 |
|
| 849 |
print($BOLD. "Creating coverage files ($numtests " . |
| 850 |
plural($numtests, "test", "tests") . ", $numfiles " . |
| 851 |
plural($numfiles, "source file", "source files") . ")\n" . $RESET); |
| 852 |
} |
| 853 |
|
| 854 |
sub main() |
| 855 |
{ |
| 856 |
my $opt_help; |
| 857 |
my $opt_output; |
| 858 |
my $opt_configfile; |
| 859 |
my $opt_seed = 0; |
| 860 |
my $c; |
| 861 |
my $src; |
| 862 |
my $hits; |
| 863 |
my $root; |
| 864 |
my $enum; |
| 865 |
my ($a, $b); |
| 866 |
|
| 867 |
# Parse options |
| 868 |
if (!GetOptions("output|o=s" => \$opt_output, |
| 869 |
"seed=s" => \$opt_seed, |
| 870 |
"help|h" => \$opt_help, |
| 871 |
)) { |
| 872 |
print(STDERR "Use $0 --help to get usage information\n"); |
| 873 |
exit(2); |
| 874 |
} |
| 875 |
|
| 876 |
if ($opt_help) { |
| 877 |
usage(); |
| 878 |
exit(0); |
| 879 |
} |
| 880 |
|
| 881 |
$opt_configfile = shift(@ARGV); |
| 882 |
if (!defined($opt_configfile)) { |
| 883 |
print(STDERR "Please specify a config file\n"); |
| 884 |
exit(2); |
| 885 |
} |
| 886 |
|
| 887 |
if (defined($opt_output)) { |
| 888 |
if (!-d $opt_output) { |
| 889 |
mkdir($opt_output) or |
| 890 |
die("$0: Could not create directory " . "$opt_output: $!\n"); |
| 891 |
} |
| 892 |
$root = abs_path($opt_output); |
| 893 |
} else { |
| 894 |
$root = "/"; |
| 895 |
} |
| 896 |
|
| 897 |
srand($opt_seed); |
| 898 |
|
| 899 |
# Get config |
| 900 |
$c = read_config($opt_configfile); |
| 901 |
apply_config($c, \@ARGV) if (@ARGV); |
| 902 |
|
| 903 |
print_intro($c); |
| 904 |
# Show lines on STDOUT without newline |
| 905 |
$| = 1; |
| 906 |
|
| 907 |
# Create source tree |
| 908 |
print(" Source tree ......... "); |
| 909 |
$src = gen_src($c, $root); |
| 910 |
# Write out source code if requested |
| 911 |
write_src($src) if (defined($opt_output)); |
| 912 |
print("done ("); |
| 913 |
print($src->[1] . " lines, "); |
| 914 |
print($src->[2] . " functions, "); |
| 915 |
print($src->[3] . " branches)\n"); |
| 916 |
|
| 917 |
# Write out full-coverage data files |
| 918 |
print(" Full coverage ....... "); |
| 919 |
$hits = gen_hits($c, $src); |
| 920 |
write_info($c, "full.info", $src, $hits); |
| 921 |
write_counts("full.counts", $hits); |
| 922 |
print("done\n"); |
| 923 |
|
| 924 |
# Write out data files with target coverage rates |
| 925 |
print(" Target coverage ..... "); |
| 926 |
reduce_hits($src, $hits, |
| 927 |
get_int($c, "lines.covered"), |
| 928 |
get_int($c, "functions.covered"), |
| 929 |
get_int($c, "branches.covered")); |
| 930 |
write_info($c, "target.info", $src, $hits); |
| 931 |
write_counts("target.counts", $hits); |
| 932 |
print("done\n"); |
| 933 |
|
| 934 |
# Write out partial data files |
| 935 |
print(" Partial coverage .... "); |
| 936 |
($a, $b) = split_hits($c, $src, $hits); |
| 937 |
write_info($c, "part1.info", $src, $a); |
| 938 |
write_counts("part1.counts", $a); |
| 939 |
write_info($c, "part2.info", $src, $b); |
| 940 |
write_counts("part2.counts", $b); |
| 941 |
print("done\n"); |
| 942 |
|
| 943 |
# Write out zero-coverage data files |
| 944 |
print(" Zero coverage ....... "); |
| 945 |
zero_hits($src, $hits); |
| 946 |
write_info($c, "zero.info", $src, $hits); |
| 947 |
write_counts("zero.counts", $hits); |
| 948 |
print("done\n"); |
| 949 |
} |
| 950 |
|
| 951 |
main(); |
| 952 |
exit(0); |