#!/usr/bin/awk -f

function clearvars () {
  test       = "";
  cpu        = "";
  real       = "";
  codesize   = "";
  failed     = 0;
  crashed    = 0;
  runtime    = 0;
}

function output () {
  if (test != "") {

    failure = "";
     
    if ((crashed != 0) && (failed == 0)) {
       failure = "crashed";
    }
    else if  ((crashed == 0) && (failed != 0)) {
      failure = "failed";
    }
    else if  ((crashed != 0) && (failed != 0)) {
      failure = "crashed-and-failed";
    }
    
    if (failure == "") {
      printf "(%-10s   %11s   %11s   %9s)\n", test, cpu, real, codesize;
    }
    else {
      printf "(%-10s   %10s)\n", test, failure;
    }
  }
  clearvars();
}

BEGIN {
  clearvars();
}

/^Testing/ {
  output();
  test=$2;
}

/^Running.../ {
  runtime = 1;
}

runtime == 1 && $0 == "*** wrong result ***" {
  failed = 1;
}

runtime == 1 && /^Command .* with non-zero/ {
  crashed = 1;
}

runtime == 1 && /^Command terminated by signal/ {
  crashed = 1;
}

runtime == 1 && /Abort trap/ {
  crashed = 1;
}

runtime == 1 && /Error/ {
  crashed = 1;
}

runtime == 1 && / === context ===/ {
  crashed = 1;
}


runtime == 1 && /ERROR/ && $0 !~ /^FATAL-ERROR$/ && $0 !~ /^SCHEME-ERROR$/ && $0 !~ /^SLATEX-ERROR$/ {
  crashed = 1;
}

runtime == 1 && /^[ ]*[0-9]+ ms cpu time \([0-9]+ user, [0-9]+ system\)$/ {
  cpu = $1;
}

runtime == 1 && /^[ ]*[0-9]+ ms real time$/ {
  real = $1;
}

runtime == 1 && /^cpu time: ([0-9]+) real time: ([0-9]+)/ {
  cpu = $3;
  real = $6;
}

runtime == 1 && /^[ ]*([0-9.]+) real [ ]*([0-9.]+) user [ ]*([0-9.]+) sys$/ {
  cpu = ($3 + $5) * 1000;
  real = $1 * 1000;
}

runtime == 1 && /[ ]*([0-9.]+)s real [ ]*([0-9.]+)s user [ ]*([0-9.]+)s sys/ {
  real = $1 * 1000;
  cpu = ($3 + $5) * 1000;
}


runtime == 1 && /^Elapsed time\.\.\.: ([0-9]+) ms/{
  real = $3;
}

runtime == 1 && /\(User: ([0-9]+) ms; System: ([0-9]+) ms\)/{
  user= $6;
  sys=$9;
  cpu = user+sys;
}

# runtime == 1 && /([0-9.]+)s real/{
#   real= $1*1000;
# }
# runtime == 1 && /([0-9.]+)s user/{
#   user = $1*1000;
# }
# runtime == 1 && /([0-9.]+)s system/{
#   sys = $1*1000;
# }
# runtime == 1 && user && sys {
#   cpu = user + sys;
# }




runtime == 1 && /^code size = [0-9]+$/ {
  codesize = $4;
}

END { output() }