06983f1996-09-22Fredrik Hübinette (Hubbe) #!/usr/local/bin/pike #include <simulate.h>
e9361f1995-11-06Fredrik Hübinette (Hubbe) 
c7dbb61996-06-09Fredrik Hübinette (Hubbe) #if !efun(_verify_internals) #define _verify_internals() #endif
a9679f1995-11-13David Hedbor  int main(int argc, string *argv) {
c7dbb61996-06-09Fredrik Hübinette (Hubbe)  int e, verbose, successes, errors, t, check;
a9679f1995-11-13David Hedbor  string *tests; program testprogram;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  int start, fail, mem; int loop=1;
a9679f1995-11-13David Hedbor  for(e=1;e<argc;e++) {
a67f2f1996-05-16Fredrik Hübinette (Hubbe)  string opt; int arg; arg=1; if(sscanf(argv[e],"--%s=%d",opt,arg)==2) opt="--"+opt; else if(sscanf(argv[e],"-%s%d",opt,arg)==2) opt="-"+opt; else opt=argv[e]; switch(opt)
a9679f1995-11-13David Hedbor  { case "-h": case "--help": perror("Usage: "+argv[e]+" [-v | --verbose] [-h | --help] [-t <testno>] <testfile>\n"); return 0; case "-v": case "--verbose":
a67f2f1996-05-16Fredrik Hübinette (Hubbe)  verbose+=arg;
a9679f1995-11-13David Hedbor  break; case "-s": case "--start-test":
a67f2f1996-05-16Fredrik Hübinette (Hubbe)  start=arg;
a9679f1995-11-13David Hedbor  start--; break; case "-f": case "--fail":
a67f2f1996-05-16Fredrik Hübinette (Hubbe)  fail+=arg;
a9679f1995-11-13David Hedbor  break;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  case "-l": case "--loop": loop+=arg; break;
a9679f1995-11-13David Hedbor  case "-t": case "--trace":
a67f2f1996-05-16Fredrik Hübinette (Hubbe)  t+=arg;
e9361f1995-11-06Fredrik Hübinette (Hubbe)  break;
c7dbb61996-06-09Fredrik Hübinette (Hubbe)  case "-c": case "--check": check++;
a9679f1995-11-13David Hedbor  break;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  case "-m": case "--mem": case "--memory": mem++; break;
a9679f1995-11-13David Hedbor  default: if(tests) {
03764e1996-11-12Fredrik Hübinette (Hubbe)  perror("Unknown argument: "+opt+".\n");
a9679f1995-11-13David Hedbor  exit(1); } tests=(read_bytes(argv[e])||"")/"\n....\n"; } }
06983f1996-09-22Fredrik Hübinette (Hubbe)  if(!tests) { tests=(clone((program)"/precompiled/file","stdin")->read(0x7fffffff)||"")/"\n....\n"; }
2625671996-09-23Fredrik Hübinette (Hubbe)  if(!tests)
06983f1996-09-22Fredrik Hübinette (Hubbe)  { perror("Failed to read test file!\n"); exit(1); } tests=tests[0..sizeof(tests)-2];
c7dbb61996-06-09Fredrik Hübinette (Hubbe) 
c3c7031996-12-04Fredrik Hübinette (Hubbe)  while(loop--) { successes=errors=0;
c7dbb61996-06-09Fredrik Hübinette (Hubbe) 
c3c7031996-12-04Fredrik Hübinette (Hubbe)  for(e=start;e<sizeof(tests);e++)
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  string test,condition; int type; object o; mixed a,b; if(check) _verify_internals(); test=tests[e]; if(sscanf(test,"COND %s\n%s",condition,test)==2)
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  if(!clone(compile_string("mixed c() { return "+condition+"; }","Cond "+(e+1)))->c()) { if(verbose) perror("Not doing test "+(e+1)+"\n"); successes++; continue; }
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  sscanf(test,"%s\n%s",type,test); sscanf(type,"%*s expected result: %s",type); if(verbose)
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  perror("Doing test "+(e+1)+"\n"); if(verbose>1) perror(test+"\n");
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
c7dbb61996-06-09Fredrik Hübinette (Hubbe)  if(check > 1) _verify_internals();
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
a9679f1995-11-13David Hedbor  switch(type) {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  case "COMPILE_ERROR": master()->set_inhibit_compile_errors(1); if(catch(compile_string(test,"Test "+(e+1))))
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  successes++; }else{
a9679f1995-11-13David Hedbor  perror("Test "+(e+1)+" failed.\n"); perror(test+"\n"); errors++; }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  master()->set_inhibit_compile_errors(0);
a9679f1995-11-13David Hedbor  break;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  case "EVAL_ERROR": master()->set_inhibit_compile_errors(1); if(catch(clone(compile_string(test,"Test "+(e+1)))->a()))
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  successes++; }else{
a9679f1995-11-13David Hedbor  perror("Test "+(e+1)+" failed.\n"); perror(test+"\n"); errors++; }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  master()->set_inhibit_compile_errors(0);
a9679f1995-11-13David Hedbor  break;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  default: o=clone(compile_string(test,"Test "+(e+1))); if(check > 1) _verify_internals(); a=b=0; if(t) trace(t); if(functionp(o->a)) a=o->a(); if(functionp(o->b)) b=o->b(); if(t) trace(0); if(check > 1) _verify_internals(); switch(type)
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  case "FALSE": a=!a; case "TRUE": if(!a) { perror("Test "+(e+1)+" failed.\n"); perror(test+"\n"); errors++; }else{ successes++; } break; case "RUN":
a9679f1995-11-13David Hedbor  successes++;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  break; case "EQ": if(a!=b) { perror("Test "+(e+1)+" failed.\n"); perror(test+"\n"); perror(sprintf("o->a(): %O\n",a)); perror(sprintf("o->b(): %O\n",b)); errors++; }else{ successes++; } break; case "EQUAL": if(!equal(a,b)) { perror("Test "+(e+1)+" failed.\n"); perror(test+"\n"); perror(sprintf("o->a(): %O\n",a)); perror(sprintf("o->b(): %O\n",b)); errors++; }else{ successes++; } break; default: perror(sprintf("Unknown test type (%O).\n",type)); errors++;
a9679f1995-11-13David Hedbor  } }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  if(check > 2) _verify_internals(); if(fail && errors) exit(1); a=b=0;
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  if(errors + successes != sizeof(tests)) { perror("Errors + Successes != number of tests!\n"); errors++; } if(errors || verbose) { perror("Failed tests: "+errors+".\n"); }
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  if(mem)
a9679f1995-11-13David Hedbor  {
c3c7031996-12-04Fredrik Hübinette (Hubbe)  int total; tests=0; gc(); mapping tmp=_memory_usage(); write(sprintf("%-10s: %6s %10s\n","Category","num","bytes")); foreach(sort(indices(tmp)),string foo) { if(sscanf(foo,"%s_bytes",foo)) { write(sprintf("%-10s: %6d %10d\n", foo+"s", tmp["num_"+foo+"s"], tmp[foo+"_bytes"])); total+=tmp[foo+"_bytes"]; } } write(sprintf("%-10s: %6s %10d\n", "Total", "", total));
a9679f1995-11-13David Hedbor  } return errors; }