#!/usr/local/bin/perl -w
use strict; 

my $DOC = <<"EOF"; 
Usage: $0 <table dir> 

Run first: 
- fault_cov_instances_with_init

Args: 
- <table dir>

Affects: 
- <table dir>/fault_cov_with_init.txt

Header: 
- FaultNum, >= 1
- InInit, max of fault_cov_instances_with_init.InInit
- ExecingEvents, # distinct steps.EventNum for all fault_cov_instances_with_init with FaultNum and TimesExec > TimesInInit, plus InInit
- MinEventPreds, min of events.Preds for all fault_cov_instances_with_init with FaultNum, where Preds=0 for init code
- MaxEventPreds, max of events.Preds for all fault_cov_instances_with_init with FaultNum, where Preds=0 for init code
- MinEventSuccs, min of events.Succs for all fault_cov_instances_with_init with FaultNum, where Succs= # events with Depth=1 for init code
- MaxEventSuccs, max of events.Succs for all fault_cov_instances_with_init with FaultNum, where Succs= # events with Depth=1 for init code
- MinEventDepth, min of events.Depth for first fault_cov_instances_with_init with FaultNum, where Depth=0 for init code
- MinTimesExec, min of fault_cov_instances_with_init.TimesExec - fault_cov_instances_with_init.TimesInInit for all fault_cov_instances_with_init with FaultNum and fault_cov_instances_with_init.TimesInInit for all fault_cov_instances_with_init with FaultNum and TimesInInit > 0
- MaxTimesExec, max of fault_cov_instances_with_init.TimesExec - fault_cov_instances_with_init.TimesInInit for all fault_cov_instances_with_init with FaultNum and fault_cov_instances_with_init.TimesInInit for all fault_cov_instances_with_init with FaultNum and TimesInInit > 0
- Repeatable, 1 if MaxTimesExec > 1
- MinLinesExecBefore, min of steps.LinesExecBefore for first fault_cov_instances_with_init with FaultNum, where LinesExecBefore is adjusted to be 0 or # lines exec in init code if StepNum = 1
- MinLinesCovBefore, min of steps.LinesCovBefore for first fault_cov_instances_with_init with FaultNum, where LinesCovBefore is adjusted to be 0 or # lines cov in init code if StepNum = 1
- MinLinesExecWith, min of steps.LinesExec for all fault_cov_instances_with_init with FaultNum, where LinesExec is adjusted to be # lines exec in init code or # lines exec after init code if StepNum = 1
- MaxLinesExecWith, max of steps.LinesExec for all fault_cov_instances_with_init with FaultNum, where LinesExec is adjusted to be # lines exec in init code or # lines exec after init code if StepNum = 1
- MinLinesCovWith, min of steps.LinesCov for all fault_cov_instances_with_init with FaultNum, where LinesCov is adjusted to be # lines cov in init code or # lines cov after init code if StepNum = 1-- # lines cov after init code does not count lines covered by both init code and after-init code
- MaxLinesCovWith, max of steps.LinesCov for all fault_cov_instances_with_init with FaultNum, where LinesCov is adjusted to be # lines cov in init code or # lines cov after init code if StepNum = 1-- # lines cov after init code does not count lines covered by both init code and after-init code

Like fault_cov, but treats the initialization code as a separate event
at depth 0 of the EFG. Its successor set is all events at depth 1. When
StepNum=1, the step is split into two events: the init code and the
regular first event of the test case. If fault_cov_instances_with_init
has TimesExec > TimesInInit > 0, then the line is executed in both the
init code and the first event. 

EOF
;

if (@ARGV != 1) {
    print $DOC; 
    exit 1;
}

my ($table_dir) = @ARGV; 
$table_dir =~ s|/$||; 
my $out_file = "$table_dir/fault_cov_with_init.txt"; 
if (-e $out_file) {
    die "$out_file already exists\n"; 
}

my %events;  # key = EventNum
my $num_depth1_events = 0; 
my $events_file = "$table_dir/events.txt"; 
open(EVENTS, $events_file) or die "Couldn't read $events_file\n"; 
my $head = <EVENTS>; 
while (<EVENTS>) {
    chomp; 
    m/^(\d+)\s+"(.+)"\s+"(.+)"\s+"(.+)"\s+(\S+)\s+(\S+)\s+(\S+)$/; 
    my ($event_num, $window, $component, $action, $preds, $succs, $depth) = ($1, $2, $3, $4, $5, $6, $7); 
    $events{$event_num}{'Preds'} = $preds; 
    $events{$event_num}{'Succs'} = $succs;
    $events{$event_num}{'Depth'} = $depth;
    if ($depth == 1) {
	$num_depth1_events++; 
    }
}
close(EVENTS); 

my @sorted_faults; 
my $faults_file = "$table_dir/faults.txt"; 
open(FAULTS, $faults_file) or die "Couldn't read $faults_file\n"; 
$head = <FAULTS>; 
while (<FAULTS>) {
    chomp; 
    my ($fault_num) = split; 
    push @sorted_faults, $fault_num;  # Assumes faults sorted by FaultNum!! 
}
close(FAULTS); 

my %steps;  # key = SuiteNum x TstNum x StepNum
my $steps_file = "$table_dir/steps.txt"; 
open(STEPS, $steps_file) or die "Couldn't read $steps_file\n"; 
$head = <STEPS>; 
while (<STEPS>) { 
    chomp; 
    my ($suite_num, $tst_num, $step_num, $event_num, $cov, $det, $lines_exec, $lines_exec_before, $lines_cov, $new_lines_cov, $lines_cov_before, $exec_diff, $cov_diff) = split; 
    $steps{$suite_num}{$tst_num}{$step_num}{'EventNum'} = $event_num; 
    $steps{$suite_num}{$tst_num}{$step_num}{'LinesExecBefore'} = $lines_exec_before; 
    $steps{$suite_num}{$tst_num}{$step_num}{'LinesCovBefore'} = $lines_cov_before; 
    $steps{$suite_num}{$tst_num}{$step_num}{'LinesExec'} = $lines_exec; 
    $steps{$suite_num}{$tst_num}{$step_num}{'LinesCov'} = $lines_cov; 
    $steps{$suite_num}{$tst_num}{$step_num}{'ExecDiff'} = $exec_diff; 
    $steps{$suite_num}{$tst_num}{$step_num}{'CovDiff'} = $cov_diff; 
}
close(STEPS); 

my $num_init_lines_exec = 0, my $num_init_lines_cov = 0; 
my $init_cov_file = "$table_dir/init_cov.txt"; 
open(INIT, $init_cov_file) or die "Couldn't read $init_cov_file\n"; 
while (<INIT>) {
    chomp; 
    my ($file, $line, $times) = split; 
    $num_init_lines_exec += $times; 
    $num_init_lines_cov++; 
}
close(INIT); 

my %fault_cov;  # key = FaultNum
foreach my $fault_num (@sorted_faults) {
    $fault_cov{$fault_num}{'InInit'} = 0; 
    $fault_cov{$fault_num}{'Repeatable'} = 0; 

    $fault_cov{$fault_num}{'MinEventPreds'} = -1; 
    $fault_cov{$fault_num}{'MinEventSuccs'} = -1; 
    $fault_cov{$fault_num}{'MinEventDepth'} = -1; 
    $fault_cov{$fault_num}{'MinTimesExec'} = -1; 
    $fault_cov{$fault_num}{'MinLinesExecBefore'} = -1; 
    $fault_cov{$fault_num}{'MinLinesCovBefore'} = -1; 
    $fault_cov{$fault_num}{'MinLinesExecWith'} = -1; 
    $fault_cov{$fault_num}{'MinLinesCovWith'} = -1; 

    $fault_cov{$fault_num}{'MaxEventPreds'} = -1; 
    $fault_cov{$fault_num}{'MaxEventSuccs'} = -1; 
    $fault_cov{$fault_num}{'MaxEventDepth'} = -1; 
    $fault_cov{$fault_num}{'MaxTimesExec'} = -1; 
    $fault_cov{$fault_num}{'MaxLinesExecBefore'} = -1; 
    $fault_cov{$fault_num}{'MaxLinesCovBefore'} = -1; 
    $fault_cov{$fault_num}{'MaxLinesExecWith'} = -1; 
    $fault_cov{$fault_num}{'MaxLinesCovWith'} = -1; 
}

my %execing_events;  # key = FaultNum x EventNum, value = 1
my %seen_tst;  # key = SuiteNum x TstNum x FaultNum, value = 1 
my $fault_cov_instances_file = "$table_dir/fault_cov_instances_with_init.txt"; 
open(FCI, $fault_cov_instances_file) or die "Couldn't read $fault_cov_instances_file\n"; 
$head = <FCI>; 
while (<FCI>) {
    chomp; 
    my ($suite_num, $tst_num, $step_num, $fault_num, $times_exec, $times_in_init) = split; 
    my $first_instance = 0; 
    if (! $seen_tst{$suite_num}{$tst_num}{$fault_num}) {
	$seen_tst{$suite_num}{$tst_num}{$fault_num} = 1; 
	$first_instance = 1;   # Assumes fault_cov_instances sorted by StepNum!! 
    }
    my $event_num = $steps{$suite_num}{$tst_num}{$step_num}{'EventNum'}; 
    my $times_in_event = $times_exec - $times_in_init; 
    if ($times_in_init > 0) {
	if ($step_num != 1) {
	    die "suite_num=$suite_num, tst_num=$tst_num, step_num=$step_num has times_in_init=$times_in_init (>0)\n"; 
	}
	if (! $first_instance) {
	    die "suite_num=$suite_num, tst_num=$tst_num, step_num=$step_num has times_in_init=$times_in_init (>0) but first_instance=$first_instance\n"; 
	}
	$fault_cov{$fault_num}{'InInit'} = 1; 
	$execing_events{$fault_num}{'Init'} = 1; 
    }
    if ($times_in_event > 0) {
	$execing_events{$fault_num}{$event_num} = 1; 
    }

    if ($times_in_init > 0) {
	$fault_cov{$fault_num}{'MinEventPreds'} = 0; 
	$fault_cov{$fault_num}{'MinEventSuccs'} = my_min($num_depth1_events, $fault_cov{$fault_num}{'MinEventSuccs'}); 
	$fault_cov{$fault_num}{'MinEventDepth'} = 0; 
	$fault_cov{$fault_num}{'MinTimesExec'} = my_min($times_in_init, $fault_cov{$fault_num}{'MinTimesExec'}); 
	$fault_cov{$fault_num}{'MinLinesExecBefore'} = 0; 
	$fault_cov{$fault_num}{'MinLinesCovBefore'} = 0; 
	$fault_cov{$fault_num}{'MinLinesExecWith'} = my_min($num_init_lines_exec, $fault_cov{$fault_num}{'MinLinesExecWith'}); 
	$fault_cov{$fault_num}{'MinLinesCovWith'} = my_min($num_init_lines_cov, $fault_cov{$fault_num}{'MinLinesCovWith'}); 

	$fault_cov{$fault_num}{'MaxEventPreds'} = max(0, $fault_cov{$fault_num}{'MaxEventPreds'}); 
	$fault_cov{$fault_num}{'MaxEventSuccs'} = max($num_depth1_events, $fault_cov{$fault_num}{'MaxEventSuccs'}); 
	$fault_cov{$fault_num}{'MaxEventDepth'} = max(0, $fault_cov{$fault_num}{'MaxEventDepth'}); 
	$fault_cov{$fault_num}{'MaxTimesExec'} = max($times_in_init, $fault_cov{$fault_num}{'MaxTimesExec'}); 
	$fault_cov{$fault_num}{'MaxLinesExecBefore'} = max(0, $fault_cov{$fault_num}{'MaxLinesExecBefore'}); 
	$fault_cov{$fault_num}{'MaxLinesCovBefore'} = max(0, $fault_cov{$fault_num}{'MaxLinesCovBefore'}); 
	$fault_cov{$fault_num}{'MaxLinesExecWith'} = max($num_init_lines_exec, $fault_cov{$fault_num}{'MaxLinesExecWith'}); 
	$fault_cov{$fault_num}{'MaxLinesCovWith'} = max($num_init_lines_cov, $fault_cov{$fault_num}{'MaxLinesCovWith'}); 
    }

    if ($times_in_event > 0) {
	$fault_cov{$fault_num}{'MinEventPreds'} = my_min($events{$event_num}{'Preds'}, $fault_cov{$fault_num}{'MinEventPreds'}); 
	$fault_cov{$fault_num}{'MinEventSuccs'} = my_min($events{$event_num}{'Succs'}, $fault_cov{$fault_num}{'MinEventSuccs'}); 
	if ($first_instance) {
	    $fault_cov{$fault_num}{'MinEventDepth'} = my_min($events{$event_num}{'Depth'}, $fault_cov{$fault_num}{'MinEventDepth'}); 
	}
	$fault_cov{$fault_num}{'MinTimesExec'} = my_min($times_in_event, $fault_cov{$fault_num}{'MinTimesExec'}); 
	if ($step_num == 1) {
	    $fault_cov{$fault_num}{'MinLinesExecBefore'} = my_min($num_init_lines_exec, $fault_cov{$fault_num}{'MinLinesExecBefore'}); 
	    $fault_cov{$fault_num}{'MinLinesCovBefore'} = my_min($num_init_lines_cov, $fault_cov{$fault_num}{'MinLinesCovBefore'}); 
	    $fault_cov{$fault_num}{'MinLinesExecWith'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesExec'} - $num_init_lines_exec, $fault_cov{$fault_num}{'MinLinesExecWith'}); 
	    $fault_cov{$fault_num}{'MinLinesCovWith'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesCov'} - $num_init_lines_cov, $fault_cov{$fault_num}{'MinLinesCovWith'}); 
	} else {
	    if ($first_instance) {
		$fault_cov{$fault_num}{'MinLinesExecBefore'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesExecBefore'}, $fault_cov{$fault_num}{'MinLinesExecBefore'}); 
		$fault_cov{$fault_num}{'MinLinesCovBefore'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesCovBefore'}, $fault_cov{$fault_num}{'MinLinesCovBefore'}); 
	    }
	    $fault_cov{$fault_num}{'MinLinesExecWith'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesExec'}, $fault_cov{$fault_num}{'MinLinesExecWith'}); 
	    $fault_cov{$fault_num}{'MinLinesCovWith'} = my_min($steps{$suite_num}{$tst_num}{$step_num}{'LinesCov'}, $fault_cov{$fault_num}{'MinLinesCovWith'}); 
	}

	$fault_cov{$fault_num}{'MaxEventPreds'} = max($events{$event_num}{'Preds'}, $fault_cov{$fault_num}{'MaxEventPreds'}); 
	$fault_cov{$fault_num}{'MaxEventSuccs'} = max($events{$event_num}{'Succs'}, $fault_cov{$fault_num}{'MaxEventSuccs'}); 
	$fault_cov{$fault_num}{'MaxTimesExec'} = max($times_in_event, $fault_cov{$fault_num}{'MaxTimesExec'}); 
	if ($step_num == 1) {
	    $fault_cov{$fault_num}{'MaxLinesExecWith'} = max($steps{$suite_num}{$tst_num}{$step_num}{'LinesExec'} - $num_init_lines_exec, $fault_cov{$fault_num}{'MaxLinesExecWith'}); 
	    $fault_cov{$fault_num}{'MaxLinesCovWith'} = max($steps{$suite_num}{$tst_num}{$step_num}{'LinesCov'} - $num_init_lines_cov, $fault_cov{$fault_num}{'MaxLinesCovWith'}); 
	} else {
            $fault_cov{$fault_num}{'MaxLinesExecWith'} = max($steps{$suite_num}{$tst_num}{$step_num}{'LinesExec'}, $fault_cov{$fault_num}{'MaxLinesExecWith'}); 
            $fault_cov{$fault_num}{'MaxLinesCovWith'} = max($steps{$suite_num}{$tst_num}{$step_num}{'LinesCov'}, $fault_cov{$fault_num}{'MaxLinesCovWith'}); 
	}
    }

    if ($fault_cov{$fault_num}{'MaxTimesExec'} > 1) {
	$fault_cov{$fault_num}{'Repeatable'} = 1; 
    }

}
close(FCI); 

foreach my $fault_num (keys %execing_events) {
    $fault_cov{$fault_num}{'ExecingEvents'} = scalar(keys %{$execing_events{$fault_num}}); 
}

my @lines; 
push @lines, "FaultNum InInit ExecingEvents MinEventPreds MaxEventPreds MinEventSuccs MaxEventSuccs MinEventDepth MinTimesExec MaxTimesExec Repeatable MinLinesExecBefore MinLinesCovBefore MinLinesExecWith MaxLinesExecWith MinLinesCovWith MaxLinesCovWith\n"; 
for (my $fault_ind = 0; $fault_ind <= $#sorted_faults; $fault_ind++) {
    my $fault_num = $sorted_faults[$fault_ind]; 
    if (defined $fault_cov{$fault_num}{'ExecingEvents'}) {
	push @lines, "$fault_num $fault_cov{$fault_num}{'InInit'} $fault_cov{$fault_num}{'ExecingEvents'} $fault_cov{$fault_num}{'MinEventPreds'} $fault_cov{$fault_num}{'MaxEventPreds'} $fault_cov{$fault_num}{'MinEventSuccs'} $fault_cov{$fault_num}{'MaxEventSuccs'} $fault_cov{$fault_num}{'MinEventDepth'} $fault_cov{$fault_num}{'MinTimesExec'} $fault_cov{$fault_num}{'MaxTimesExec'} $fault_cov{$fault_num}{'Repeatable'} $fault_cov{$fault_num}{'MinLinesExecBefore'} $fault_cov{$fault_num}{'MinLinesCovBefore'} $fault_cov{$fault_num}{'MinLinesExecWith'} $fault_cov{$fault_num}{'MaxLinesExecWith'} $fault_cov{$fault_num}{'MinLinesCovWith'} $fault_cov{$fault_num}{'MaxLinesCovWith'}\n"; 
    }
}

open(OUT, ">$out_file") or die "Couldn't write to $out_file\n"; 
print OUT @lines; 
close(OUT); 


sub my_min {
    my ($a, $b) = @_; 
    if ($a < $b || $b == -1) {
	return $a; 
    } else {
	return $b; 
    }
}

sub max {
    my ($a, $b) = @_; 
    if ($a > $b) {
	return $a; 
    } else {
	return $b; 
    }
}
