06983f1996-09-22Fredrik Hübinette (Hubbe) #!/usr/local/bin/pike
433b1d1999-03-06Henrik Grubbström (Grubba) /* $Id: test_pike.pike,v 1.20 1999/03/06 04:13:13 grubba Exp $ */
7bd16b1997-06-01Henrik Grubbström (Grubba) 
06983f1996-09-22Fredrik Hübinette (Hubbe) #include <simulate.h>
e9361f1995-11-06Fredrik Hübinette (Hubbe) 
c7dbb61996-06-09Fredrik Hübinette (Hubbe) #if !efun(_verify_internals) #define _verify_internals() #endif
73b75f1998-02-24Fredrik Hübinette (Hubbe) int foo(string opt) { if(opt=="" || !opt) return 1; return (int)opt; }
dddccc1998-04-15Fredrik Hübinette (Hubbe)  mapping(string:int) cond_cache=([]);
bd227a1999-01-21Fredrik Hübinette (Hubbe) #if constant(thread_create) #define HAVE_DEBUG #endif
a9679f1995-11-13David Hedbor int main(int argc, string *argv) {
c7dbb61996-06-09Fredrik Hübinette (Hubbe)  int e, verbose, successes, errors, t, check;
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  string *tests,tmp;
a9679f1995-11-13David Hedbor  program testprogram;
c3c7031996-12-04Fredrik Hübinette (Hubbe)  int start, fail, mem; int loop=1;
0073821998-04-05Fredrik Hübinette (Hubbe)  int end=0x7fffffff;
bd227a1999-01-21Fredrik Hübinette (Hubbe)  string extra_info="";
1869a91999-02-28Fredrik Hübinette (Hubbe)  int shift;
bd227a1999-01-21Fredrik Hübinette (Hubbe)  #if constant(signal) && constant(signum) if(signum("SIGQUIT")>=0) { signal(signum("SIGQUIT"),lambda() { master()->handle_error( ({"\nSIGQUIT recived, printing backtrace and continuing.\n",backtrace() }) ); }); } #endif
a9679f1995-11-13David Hedbor 
086bfa1998-04-09Fredrik Hübinette (Hubbe)  string *args=backtrace()[0][3]; args=args[..sizeof(args)-1-argc];
f751821998-04-16Fredrik Hübinette (Hubbe)  add_constant("RUNPIKE",Array.map(args,Process.sh_quote)*" ");
086bfa1998-04-09Fredrik Hübinette (Hubbe) 
73b75f1998-02-24Fredrik Hübinette (Hubbe)  foreach(Getopt.find_all_options(argv,aggregate( ({"help",Getopt.NO_ARG,({"-h","--help"})}), ({"verbose",Getopt.NO_ARG,({"-v","--verbose"})}), ({"start",Getopt.HAS_ARG,({"-s","--start-test"})}),
0073821998-04-05Fredrik Hübinette (Hubbe)  ({"end",Getopt.HAS_ARG,({"--end-after"})}),
73b75f1998-02-24Fredrik Hübinette (Hubbe)  ({"fail",Getopt.MAY_HAVE_ARG,({"-f","--fail"})}), ({"loop",Getopt.MAY_HAVE_ARG,({"-l","--loop"})}), ({"trace",Getopt.MAY_HAVE_ARG,({"-t","--trace"})}), ({"check",Getopt.MAY_HAVE_ARG,({"-c","--check"})}), ({"mem",Getopt.MAY_HAVE_ARG,({"-m","--mem","--memory"})}),
bd227a1999-01-21Fredrik Hübinette (Hubbe) #ifdef HAVE_DEBUG ({"debug",Getopt.MAY_HAVE_ARG,({"-d","--debug"})}), #endif
73b75f1998-02-24Fredrik Hübinette (Hubbe)  )),array opt)
a9679f1995-11-13David Hedbor  {
73b75f1998-02-24Fredrik Hübinette (Hubbe)  switch(opt[0]) { case "help": werror("Usage: "+argv[e]+" [-v | --verbose] [-h | --help] [-t <testno>] <testfile>\n"); return 0; case "verbose": verbose+=foo(opt[1]); break; case "start": start=foo(opt[1]); start--; break;
0073821998-04-05Fredrik Hübinette (Hubbe)  case "end": end=foo(opt[1]); break;
73b75f1998-02-24Fredrik Hübinette (Hubbe)  case "fail": fail+=foo(opt[1]); break; case "loop": loop+=foo(opt[1]); break; case "trace": t+=foo(opt[1]); break; case "check": check+=foo(opt[1]); break; case "mem": mem+=foo(opt[1]); break;
bd227a1999-01-21Fredrik Hübinette (Hubbe)  #ifdef HAVE_DEBUG case "debug": { object p=Stdio.Port(); p->bind(0); werror("Debug port is: %s\n",p->query_address()); sscanf(p->query_address(),"%*s %d",int portno); extra_info+=sprintf(" dport:%d",portno); thread_create(lambda(object p){ while(p) { if(object o=p->accept()) { object q=Stdio.FILE(); q->assign(o); destruct(o); Tools.Hilfe.GenericHilfe(q,q); } } },p); } #endif
73b75f1998-02-24Fredrik Hübinette (Hubbe)  }
a9679f1995-11-13David Hedbor  }
73b75f1998-02-24Fredrik Hübinette (Hubbe)  argv=Getopt.get_args(argv,1); if(sizeof(argv)<1)
06983f1996-09-22Fredrik Hübinette (Hubbe)  {
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  if(!tmp) {
73b75f1998-02-24Fredrik Hübinette (Hubbe)  werror("No tests?\n");
e42eaf1998-01-02Fredrik Hübinette (Hubbe)  exit(1); }
06983f1996-09-22Fredrik Hübinette (Hubbe)  }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  while(loop--) { successes=errors=0;
73b75f1998-02-24Fredrik Hübinette (Hubbe)  for(int f=1;f<sizeof(argv);f++)
a9679f1995-11-13David Hedbor  {
73b75f1998-02-24Fredrik Hübinette (Hubbe)  tmp=read_bytes(argv[f]); if(!tmp)
a9679f1995-11-13David Hedbor  {
73b75f1998-02-24Fredrik Hübinette (Hubbe)  werror("Failed to read test file, errno="+errno()+".\n"); exit(1);
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
73b75f1998-02-24Fredrik Hübinette (Hubbe)  tests=tmp/"\n....\n"; tmp=0; tests=tests[0..sizeof(tests)-2];
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
73b75f1998-02-24Fredrik Hübinette (Hubbe)  werror("Doing tests in %s (%d tests)\n",argv[f],sizeof(tests));
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  for(e=start;e<sizeof(tests);e++) { string test,condition; int type; object o; mixed a,b;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(check) _verify_internals();
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  test=tests[e]; if(sscanf(test,"COND %s\n%s",condition,test)==2)
dddccc1998-04-15Fredrik Hübinette (Hubbe)  {
1869a91999-02-28Fredrik Hübinette (Hubbe)  int tmp; if(!(tmp=cond_cache[condition])) { tmp=!!(clone(compile_string("mixed c() { return "+condition+"; }","Cond "+(e+1)))->c()); if(!tmp) tmp=-1; cond_cache[condition]=tmp; }
dddccc1998-04-15Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(tmp==-1) { if(verbose) werror("Not doing test "+(e+1)+"\n"); successes++; continue; }
73b75f1998-02-24Fredrik Hübinette (Hubbe)  }
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  sscanf(test,"%s\n%s",type,test); sscanf(type,"%*s expected result: %s",type);
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(verbose) { werror("Doing test %d (%d total)%s\n",e+1,successes+errors+1,extra_info); if(verbose>1) werror(test+"\n"); }
475a841999-02-20Henrik Grubbström (Grubba) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(check > 1) _verify_internals();
c3c7031996-12-04Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  shift++; string fname = argv[f] + ": Test " + (e + 1) + " (shift " + shift + ")";
475a841999-02-20Henrik Grubbström (Grubba) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  string widener = ([ 0:"",
475a841999-02-20Henrik Grubbström (Grubba)  1:"\nint \x30c6\x30b9\x30c8=0;\n",
1869a91999-02-28Fredrik Hübinette (Hubbe)  2:"\nint \x10001=0;\n" ])[shift%3];
32a6ac1998-04-11Henrik Grubbström (Grubba) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  switch(type) { case "COMPILE": if(catch(compile_string(test + widener, fname))) { werror(fname + " failed.\n"); werror(test+"\n"); errors++; }else{ successes++; } break;
086bfa1998-04-09Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  case "COMPILE_ERROR": master()->set_inhibit_compile_errors(1); if(catch(compile_string(test + widener, fname))) { successes++; }else{ werror(fname + " failed.\n"); werror(test+"\n"); errors++; } master()->set_inhibit_compile_errors(0); break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  case "EVAL_ERROR": master()->set_inhibit_compile_errors(1); if(catch(clone(compile_string(test + widener, fname))->a())) { successes++; }else{ werror(fname + " failed.\n"); werror(test+"\n"); errors++; } master()->set_inhibit_compile_errors(0); break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  default:
433b1d1999-03-06Henrik Grubbström (Grubba)  mixed err; if (err = catch{ o=clone(compile_string(test + widener,fname));
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
433b1d1999-03-06Henrik Grubbström (Grubba)  if(check > 1) _verify_internals();
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
433b1d1999-03-06Henrik Grubbström (Grubba)  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);
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
433b1d1999-03-06Henrik Grubbström (Grubba)  if(check > 1) _verify_internals(); }) { werror(fname + " failed.\n"); werror(test + "\n"); if (arrayp(err) && sizeof(err) && stringp(err[0])) { werror("Error: " + err[0]); } errors++; break; }
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  switch(type) { case "FALSE": if(a) { werror(fname + " failed.\n"); werror(test+"\n"); werror(sprintf("o->a(): %O\n",a)); errors++; }else{ successes++; } break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  case "TRUE": if(!a) { werror(fname + " failed.\n"); werror(test+"\n"); werror(sprintf("o->a(): %O\n",a)); errors++; }else{ successes++; } break; case "RUN":
73b75f1998-02-24Fredrik Hübinette (Hubbe)  successes++;
1869a91999-02-28Fredrik Hübinette (Hubbe)  break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  case "EQ": if(a!=b) { werror(fname + " failed.\n"); werror(test+"\n"); werror(sprintf("o->a(): %O\n",a)); werror(sprintf("o->b(): %O\n",b)); errors++; }else{ successes++; } break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  case "EQUAL": if(!equal(a,b)) { werror(fname + " failed.\n"); werror(test+"\n"); werror(sprintf("o->a(): %O\n",a)); werror(sprintf("o->b(): %O\n",b)); errors++; }else{ successes++; } break;
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  default: werror(sprintf("%s: Unknown test type (%O).\n", fname, type));
73b75f1998-02-24Fredrik Hübinette (Hubbe)  errors++;
1869a91999-02-28Fredrik Hübinette (Hubbe)  } }
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(check > 2) _verify_internals();
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(fail && errors) exit(1);
475a841999-02-20Henrik Grubbström (Grubba) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  if(!--end) exit(0);
73b75f1998-02-24Fredrik Hübinette (Hubbe) 
1869a91999-02-28Fredrik Hübinette (Hubbe)  a=b=0;
a9679f1995-11-13David Hedbor  }
c3c7031996-12-04Fredrik Hübinette (Hubbe)  }
73b75f1998-02-24Fredrik Hübinette (Hubbe)  if(mem)
c3c7031996-12-04Fredrik Hübinette (Hubbe)  {
73b75f1998-02-24Fredrik 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));
c3c7031996-12-04Fredrik Hübinette (Hubbe)  }
a9679f1995-11-13David Hedbor  }
b455c81998-03-20Fredrik Hübinette (Hubbe)  if(errors || verbose) { werror("Failed tests: "+errors+".\n"); }
0073821998-04-05Fredrik Hübinette (Hubbe)  werror("Total tests: %d\n",successes+errors);
a9679f1995-11-13David Hedbor  return errors; }