dotest.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. {
  2. $Id$
  3. }
  4. program dotest;
  5. uses
  6. dos,
  7. redir;
  8. const
  9. {$ifdef UNIX}
  10. ExeExt='';
  11. {$else UNIX}
  12. ExeExt='exe';
  13. {$endif UNIX}
  14. type
  15. TVerboseLevel=(V_Abort,V_Error,V_Warning,V_Normal,V_Debug);
  16. TConfig = record
  17. NeedOptions,
  18. NeedCPU,
  19. NeedVersion : string;
  20. ResultCode : longint;
  21. NeedRecompile : boolean;
  22. IsInteractive : boolean;
  23. IsKnown : boolean;
  24. NoRun : boolean;
  25. UsesGraph : boolean;
  26. ShouldFail : boolean;
  27. Category : string;
  28. end;
  29. var
  30. Config : TConfig;
  31. CompilerBin : string;
  32. CompilerCPU : string;
  33. CompilerVersion : string;
  34. PPFile : string;
  35. TestName : string;
  36. const
  37. ResLogfile : string[32] = 'log';
  38. LongLogfile : string[32] = 'longlog';
  39. FailLogfile : string[32] = 'faillist';
  40. DoVerbose : boolean = false;
  41. DoGraph : boolean = false;
  42. DoInteractive : boolean = false;
  43. DoExecute : boolean = false;
  44. DoKnown : boolean = false;
  45. procedure Verbose(lvl:TVerboseLevel;const s:string);
  46. begin
  47. case lvl of
  48. V_Normal :
  49. writeln(s);
  50. V_Debug :
  51. if DoVerbose then
  52. writeln('Debug: ',s);
  53. V_Warning :
  54. writeln('Warning: ',s);
  55. V_Error :
  56. begin
  57. writeln('Error: ',s);
  58. halt(1);
  59. end;
  60. V_Abort :
  61. begin
  62. writeln('Abort: ',s);
  63. halt(0);
  64. end;
  65. end;
  66. end;
  67. Function FileExists (Const F : String) : Boolean;
  68. {
  69. Returns True if the file exists, False if not.
  70. }
  71. Var
  72. info : searchrec;
  73. begin
  74. FindFirst (F,anyfile,Info);
  75. FileExists:=DosError=0;
  76. FindClose (Info);
  77. end;
  78. function ToStr(l:longint):string;
  79. var
  80. s : string;
  81. begin
  82. Str(l,s);
  83. ToStr:=s;
  84. end;
  85. procedure TrimB(var s:string);
  86. begin
  87. while (s<>'') and (s[1] in [' ',#9]) do
  88. delete(s,1,1);
  89. end;
  90. procedure TrimE(var s:string);
  91. begin
  92. while (s<>'') and (s[length(s)] in [' ',#9]) do
  93. delete(s,length(s),1);
  94. end;
  95. function upper(const s : string) : string;
  96. var
  97. i : longint;
  98. begin
  99. for i:=1 to length(s) do
  100. if s[i] in ['a'..'z'] then
  101. upper[i]:=char(byte(s[i])-32)
  102. else
  103. upper[i]:=s[i];
  104. upper[0]:=s[0];
  105. end;
  106. function SplitPath(const s:string):string;
  107. var
  108. i : longint;
  109. begin
  110. i:=Length(s);
  111. while (i>0) and not(s[i] in ['/','\']) do
  112. dec(i);
  113. SplitPath:=Copy(s,1,i);
  114. end;
  115. function ForceExtension(Const HStr,ext:String):String;
  116. {
  117. Return a filename which certainly has the extension ext
  118. }
  119. var
  120. j : longint;
  121. begin
  122. j:=length(Hstr);
  123. while (j>0) and (Hstr[j]<>'.') do
  124. dec(j);
  125. if j=0 then
  126. j:=255;
  127. if Ext<>'' then
  128. ForceExtension:=Copy(Hstr,1,j-1)+'.'+Ext
  129. else
  130. ForceExtension:=Copy(Hstr,1,j-1);
  131. end;
  132. procedure Copyfile(const fn1,fn2:string;append:boolean);
  133. const
  134. bufsize = 16384;
  135. var
  136. f,g : file;
  137. i : longint;
  138. buf : pointer;
  139. begin
  140. if Append then
  141. Verbose(V_Debug,'Appending '+fn1+' to '+fn2)
  142. else
  143. Verbose(V_Debug,'Copying '+fn1+' to '+fn2);
  144. assign(f,fn1);
  145. assign(g,fn2);
  146. {$I-}
  147. reset(f,1);
  148. {$I+}
  149. if ioresult<>0 then
  150. Verbose(V_Error,'Can''t open '+fn1);
  151. if append then
  152. begin
  153. {$I-}
  154. reset(g,1);
  155. {$I+}
  156. if ioresult<>0 then
  157. append:=false
  158. else
  159. seek(g,filesize(g));
  160. end;
  161. if not append then
  162. begin
  163. {$I-}
  164. rewrite(g,1);
  165. {$I+}
  166. if ioresult<>0 then
  167. Verbose(V_Error,'Can''t open '+fn2+' for output');
  168. end;
  169. getmem(buf,bufsize);
  170. repeat
  171. blockread(f,buf^,bufsize,i);
  172. blockwrite(g,buf^,i);
  173. until i<bufsize;
  174. freemem(buf,bufsize);
  175. close(f);
  176. close(g);
  177. end;
  178. procedure AddLog(const logfile,s:string);
  179. var
  180. t : text;
  181. begin
  182. assign(t,logfile);
  183. {$I-}
  184. append(t);
  185. {$I+}
  186. if ioresult<>0 then
  187. begin
  188. {$I-}
  189. rewrite(t);
  190. {$I+}
  191. if ioresult<>0 then
  192. Verbose(V_Abort,'Can''t append to '+logfile);
  193. end;
  194. writeln(t,s);
  195. close(t);
  196. end;
  197. function GetConfig(const fn:string;var r:TConfig):boolean;
  198. var
  199. t : text;
  200. code : integer;
  201. s,res : string;
  202. function GetEntry(const entry:string):boolean;
  203. var
  204. i : longint;
  205. begin
  206. Getentry:=false;
  207. Res:='';
  208. if Upper(Copy(s,1,length(entry)))=Upper(entry) then
  209. begin
  210. Delete(s,1,length(entry));
  211. TrimB(s);
  212. if (s<>'') then
  213. begin
  214. if (s[1]='=') then
  215. begin
  216. delete(s,1,1);
  217. i:=pos('}',s);
  218. if i=0 then
  219. i:=255
  220. else
  221. dec(i);
  222. res:=Copy(s,1,i);
  223. TrimB(res);
  224. TrimE(res);
  225. end;
  226. Verbose(V_Debug,'Config: '+Entry+' = "'+Res+'"');
  227. GetEntry:=true;
  228. end;
  229. end;
  230. end;
  231. begin
  232. FillChar(r,sizeof(r),0);
  233. GetConfig:=false;
  234. Verbose(V_Debug,'Reading '+fn);
  235. assign(t,fn);
  236. {$I-}
  237. reset(t);
  238. {$I+}
  239. if ioresult<>0 then
  240. begin
  241. Verbose(V_Error,'Can''t open '+fn);
  242. exit;
  243. end;
  244. while not eof(t) do
  245. begin
  246. readln(t,s);
  247. if s<>'' then
  248. begin
  249. if s[1]='{' then
  250. begin
  251. delete(s,1,1);
  252. TrimB(s);
  253. if (s<>'') and (s[1]='%') then
  254. begin
  255. delete(s,1,1);
  256. if GetEntry('OPT') then
  257. r.NeedOptions:=res
  258. else
  259. if GetEntry('CPU') then
  260. r.NeedCPU:=res
  261. else
  262. if GetEntry('VERSION') then
  263. r.NeedVersion:=res
  264. else
  265. if GetEntry('RESULT') then
  266. Val(res,r.ResultCode,code)
  267. else
  268. if GetEntry('GRAPH') then
  269. r.UsesGraph:=true
  270. else
  271. if GetEntry('FAIL') then
  272. r.ShouldFail:=true
  273. else
  274. if GetEntry('RECOMPILE') then
  275. r.NeedRecompile:=true
  276. else
  277. if GetEntry('NORUN') then
  278. r.NoRun:=true
  279. else
  280. if GetEntry('KNOWN') then
  281. r.IsKnown:=true
  282. else
  283. if GetEntry('INTERACTIVE') then
  284. r.IsInteractive:=true
  285. else
  286. Verbose(V_Error,'Unknown entry: '+s);
  287. end;
  288. end
  289. else
  290. break;
  291. end;
  292. end;
  293. close(t);
  294. GetConfig:=true;
  295. end;
  296. function GetCompilerVersion:boolean;
  297. var
  298. t : text;
  299. begin
  300. GetCompilerVersion:=false;
  301. ExecuteRedir(CompilerBin,'-iV','','out','');
  302. assign(t,'out');
  303. {$I-}
  304. reset(t);
  305. readln(t,CompilerVersion);
  306. close(t);
  307. erase(t);
  308. {$I+}
  309. if ioresult<>0 then
  310. Verbose(V_Error,'Can''t get Compiler Version')
  311. else
  312. begin
  313. Verbose(V_Debug,'Current Compiler Version: '+CompilerVersion);
  314. GetCompilerVersion:=true;
  315. end;
  316. end;
  317. function GetCompilerCPU:boolean;
  318. var
  319. t : text;
  320. begin
  321. GetCompilerCPU:=false;
  322. ExecuteRedir(CompilerBin,'-iTP','','out','');
  323. assign(t,'out');
  324. {$I-}
  325. reset(t);
  326. readln(t,CompilerCPU);
  327. close(t);
  328. erase(t);
  329. {$I+}
  330. if ioresult<>0 then
  331. Verbose(V_Error,'Can''t get Compiler CPU Target')
  332. else
  333. begin
  334. Verbose(V_Debug,'Current Compiler CPU Target: '+CompilerCPU);
  335. GetCompilerCPU:=true;
  336. end;
  337. end;
  338. function RunCompiler:boolean;
  339. var
  340. outname,
  341. args : string;
  342. begin
  343. RunCompiler:=false;
  344. OutName:=ForceExtension(PPFile,'log');
  345. args:='-Fuunits';
  346. if Config.NeedOptions<>'' then
  347. args:=args+' '+Config.NeedOptions;
  348. args:=args+' '+ppfile;
  349. Verbose(V_Debug,'Executing '+compilerbin+' '+args);
  350. ExecuteRedir(CompilerBin,args,'',OutName,'');
  351. Verbose(V_Debug,'Exitcode '+ToStr(ExecuteResult));
  352. { Shoud the compile fail ? }
  353. if Config.ShouldFail then
  354. begin
  355. if ExecuteResult<>0 then
  356. begin
  357. AddLog(ResLogFile,'Success, compilation failed '+PPFile);
  358. RunCompiler:=true;
  359. end
  360. else
  361. begin
  362. AddLog(FailLogFile,TestName);
  363. AddLog(ResLogFile,'Failed, compilation successfull '+PPFile);
  364. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  365. AddLog(LongLogFile,'Failed, compilation successfull '+PPFile);
  366. CopyFile(OutName,LongLogFile,true);
  367. end;
  368. end
  369. else
  370. begin
  371. if ExecuteResult<>0 then
  372. begin
  373. AddLog(FailLogFile,TestName);
  374. AddLog(ResLogFile,'Failed to compile '+PPFile);
  375. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  376. AddLog(LongLogFile,'Failed to compile '+PPFile);
  377. CopyFile(OutName,LongLogFile,true);
  378. Verbose(V_Abort,'Exitcode: '+ToStr(ExecuteResult)+' (expected 0)');
  379. end
  380. else
  381. begin
  382. AddLog(ResLogFile,'Successfully compiled '+PPFile);
  383. RunCompiler:=true;
  384. end;
  385. end;
  386. end;
  387. function RunExecutable:boolean;
  388. var
  389. outname,
  390. TestExe : string;
  391. begin
  392. RunExecutable:=false;
  393. TestExe:=ForceExtension(PPFile,ExeExt);
  394. OutName:=ForceExtension(PPFile,'elg');
  395. Verbose(V_Debug,'Executing '+TestExe);
  396. ExecuteRedir(TestExe,'','',OutName,'');
  397. Verbose(V_Debug,'Exitcode '+ToStr(ExecuteResult));
  398. if ExecuteResult<>Config.ResultCode then
  399. begin
  400. AddLog(FailLogFile,TestName);
  401. AddLog(ResLogFile,'Failed to run '+PPFile);
  402. AddLog(LongLogFile,'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  403. AddLog(LongLogFile,'Failed to run '+PPFile+' ('+ToStr(ExecuteResult)+')');
  404. Copyfile(OutName,LongLogFile,true);
  405. Verbose(V_Abort,'Exitcode: '+ToStr(ExecuteResult)+' (expected '+ToStr(Config.ResultCode)+')');
  406. end
  407. else
  408. begin
  409. AddLog(ResLogFile,'Successfully run '+PPFile);
  410. RunExecutable:=true;
  411. end;
  412. end;
  413. procedure getargs;
  414. var
  415. ch : char;
  416. para : string;
  417. i : longint;
  418. procedure helpscreen;
  419. begin
  420. writeln('dotest [Options] <File>');
  421. writeln;
  422. writeln('Options can be:');
  423. writeln(' -C<compiler> set compiler to use');
  424. writeln(' -V verbose');
  425. writeln(' -E execute test also');
  426. writeln(' -A include ALL tests');
  427. writeln(' -G include graph tests');
  428. writeln(' -G include known bug tests');
  429. writeln(' -I include interactive tests');
  430. halt(1);
  431. end;
  432. begin
  433. PPFile:='';
  434. if exeext<>'' then
  435. CompilerBin:='ppc386.'+exeext
  436. else
  437. CompilerBin:='ppc386';
  438. for i:=1 to paramcount do
  439. begin
  440. para:=Paramstr(i);
  441. if (para[1]='-') then
  442. begin
  443. ch:=Upcase(para[2]);
  444. delete(para,1,2);
  445. case ch of
  446. 'A' :
  447. begin
  448. DoGraph:=true;
  449. DoInteractive:=true;
  450. DoKnown:=true;
  451. end;
  452. 'C' : CompilerBin:=Para;
  453. 'E' : DoExecute:=true;
  454. 'G' : DoGraph:=true;
  455. 'I' : DoInteractive:=true;
  456. 'V' : DoVerbose:=true;
  457. 'K' : DoKnown:=true;
  458. end;
  459. end
  460. else
  461. begin
  462. PPFile:=ForceExtension(Para,'pp');
  463. end;
  464. end;
  465. if (PPFile='') then
  466. HelpScreen;
  467. TestName:=Copy(PPFile,1,Pos('.pp',PPFile)-1);
  468. Verbose(V_Debug,'Running test '+TestName+', file '+PPFile);
  469. end;
  470. procedure RunTest;
  471. var
  472. Res : boolean;
  473. begin
  474. Res:=GetConfig(ppfile,Config);
  475. if Res then
  476. begin
  477. if Config.UsesGraph and (not DoGraph) then
  478. begin
  479. Verbose(V_Abort,'Skipping test because it uses graph');
  480. Res:=false;
  481. end;
  482. end;
  483. if Res then
  484. begin
  485. if Config.IsInteractive and (not DoInteractive) then
  486. begin
  487. Verbose(V_Abort,'Skipping test because it is interactive');
  488. Res:=false;
  489. end;
  490. end;
  491. if Res then
  492. begin
  493. if Config.IsKnown and (not DoKnown) then
  494. begin
  495. Verbose(V_Abort,'Skipping test because it is a known bug');
  496. Res:=false;
  497. end;
  498. end;
  499. if Res then
  500. begin
  501. if Config.NeedVersion<>'' then
  502. begin
  503. Verbose(V_Debug,'Required compiler version: '+Config.NeedVersion);
  504. Res:=GetCompilerVersion;
  505. if CompilerVersion<Config.NeedVersion then
  506. begin
  507. Verbose(V_Abort,'Compiler version too low '+CompilerVersion+' < '+Config.NeedVersion);
  508. Res:=false;
  509. end;
  510. end;
  511. end;
  512. if Res then
  513. begin
  514. if Config.NeedCPU<>'' then
  515. begin
  516. Verbose(V_Debug,'Required compiler cpu: '+Config.NeedCPU);
  517. Res:=GetCompilerCPU;
  518. if Upper(Config.NeedCPU)<>Upper(CompilerCPU) then
  519. begin
  520. Verbose(V_Abort,'Compiler cpu wrong '+CompilerCPU+' <> '+Config.NeedCPU);
  521. Res:=false;
  522. end;
  523. end;
  524. end;
  525. if Res then
  526. begin
  527. Res:=RunCompiler;
  528. if Res and Config.NeedRecompile then
  529. Res:=RunCompiler;
  530. end;
  531. if Res then
  532. begin
  533. if (Config.NoRun) then
  534. begin
  535. Verbose(V_Debug,'Skipping run test');
  536. end
  537. else
  538. begin
  539. if (not Config.ShouldFail) and DoExecute then
  540. begin
  541. if FileExists(ForceExtension(PPFile,'ppu')) or
  542. FileExists(ForceExtension(PPFile,'ppw')) then
  543. Verbose(V_Debug,'Unit found, skipping run test')
  544. else
  545. Res:=RunExecutable;
  546. end;
  547. end;
  548. end;
  549. end;
  550. begin
  551. GetArgs;
  552. RunTest;
  553. end.
  554. {
  555. $Log$
  556. Revision 1.9 2001-07-04 11:23:39 florian
  557. * spelling mistake fixed
  558. Revision 1.8 2001/06/02 00:41:36 peter
  559. * write exitcode for all executed programs in debug mode
  560. Revision 1.7 2000/12/09 16:01:10 peter
  561. + known bug flag
  562. + norun flag
  563. + recompile flag
  564. Revision 1.6 2000/12/04 22:06:25 peter
  565. * fixed stupid c&p bug for CPU check
  566. Revision 1.5 2000/12/03 22:59:10 florian
  567. * some problems for go32v2 fixed
  568. }