testu.pp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. {$mode objfpc}
  2. {$h+}
  3. unit testu;
  4. Interface
  5. { ---------------------------------------------------------------------
  6. utility functions, shared by several programs of the test suite
  7. ---------------------------------------------------------------------}
  8. type
  9. TVerboseLevel=(V_Abort,V_Error,V_Warning,V_Normal,V_Debug,V_SQL);
  10. TConfig = record
  11. NeedOptions,
  12. DelOptions,
  13. NeedCPU,
  14. SkipCPU,
  15. SkipEmu,
  16. NeedTarget,
  17. SkipTarget,
  18. MinVersion,
  19. MaxVersion,
  20. KnownRunNote,
  21. KnownCompileNote,
  22. RecompileOpt: string;
  23. ResultCode : longint;
  24. KnownRunError : longint;
  25. KnownCompileError : longint;
  26. NeedRecompile : boolean;
  27. NeedLibrary : boolean;
  28. NeededAfter : boolean;
  29. IsInteractive : boolean;
  30. IsKnownRunError,
  31. IsKnownCompileError : boolean;
  32. NoRun : boolean;
  33. UsesGraph : boolean;
  34. ShouldFail : boolean;
  35. Timeout : longint;
  36. Category : string;
  37. Note : string;
  38. Files : string;
  39. ConfigFileSrc : string;
  40. ConfigFileDst : string;
  41. WpoParas : string;
  42. WpoPasses : longint;
  43. DelFiles : string;
  44. end;
  45. Const
  46. DoVerbose : boolean = false;
  47. DoSQL : boolean = false;
  48. MaxLogSize : LongInt = 50000;
  49. procedure TrimB(var s:string);
  50. procedure TrimE(var s:string);
  51. function upper(const s : string) : string;
  52. procedure Verbose(lvl:TVerboseLevel;const s:string);
  53. function GetConfig(const fn:string;var r:TConfig):boolean;
  54. Function GetFileContents (FN : String) : String;
  55. Implementation
  56. procedure Verbose(lvl:TVerboseLevel;const s:string);
  57. begin
  58. case lvl of
  59. V_Normal :
  60. writeln(s);
  61. V_Debug :
  62. if DoVerbose then
  63. writeln('Debug: ',s);
  64. V_SQL :
  65. if DoSQL then
  66. writeln('SQL: ',s);
  67. V_Warning :
  68. writeln('Warning: ',s);
  69. V_Error :
  70. begin
  71. writeln('Error: ',s);
  72. halt(1);
  73. end;
  74. V_Abort :
  75. begin
  76. writeln('Abort: ',s);
  77. halt(0);
  78. end;
  79. end;
  80. end;
  81. procedure TrimB(var s:string);
  82. begin
  83. while (s<>'') and (s[1] in [' ',#9]) do
  84. delete(s,1,1);
  85. end;
  86. procedure TrimE(var s:string);
  87. begin
  88. while (s<>'') and (s[length(s)] in [' ',#9]) do
  89. delete(s,length(s),1);
  90. end;
  91. function upper(const s : string) : string;
  92. var
  93. i,l : longint;
  94. begin
  95. L:=Length(S);
  96. SetLength(upper,l);
  97. for i:=1 to l do
  98. if s[i] in ['a'..'z'] then
  99. upper[i]:=char(byte(s[i])-32)
  100. else
  101. upper[i]:=s[i];
  102. end;
  103. function GetConfig(const fn:string;var r:TConfig):boolean;
  104. var
  105. t : text;
  106. part,code : integer;
  107. l : longint;
  108. s,res : string;
  109. function GetEntry(const entry:string):boolean;
  110. var
  111. i : longint;
  112. begin
  113. Getentry:=false;
  114. Res:='';
  115. if Upper(Copy(s,1,length(entry)))=Upper(entry) then
  116. begin
  117. Delete(s,1,length(entry));
  118. TrimB(s);
  119. if (s<>'') then
  120. begin
  121. if (s[1]='=') then
  122. begin
  123. delete(s,1,1);
  124. i:=pos('}',s);
  125. if i=0 then
  126. i:=255
  127. else
  128. dec(i);
  129. res:=Copy(s,1,i);
  130. TrimB(res);
  131. TrimE(res);
  132. end;
  133. Verbose(V_Debug,'Config: '+Entry+' = "'+Res+'"');
  134. GetEntry:=true;
  135. end;
  136. end;
  137. end;
  138. begin
  139. FillChar(r,sizeof(r),0);
  140. GetConfig:=false;
  141. Verbose(V_Debug,'Reading '+fn);
  142. assign(t,fn);
  143. {$I-}
  144. reset(t);
  145. {$I+}
  146. if ioresult<>0 then
  147. begin
  148. Verbose(V_Error,'Can''t open '+fn);
  149. exit;
  150. end;
  151. r.Note:='';
  152. while not eof(t) do
  153. begin
  154. readln(t,s);
  155. if Copy(s,1,3)=#$EF#$BB#$BF then
  156. delete(s,1,3);
  157. TrimB(s);
  158. if s<>'' then
  159. begin
  160. if s[1]='{' then
  161. begin
  162. delete(s,1,1);
  163. TrimB(s);
  164. if (s<>'') and (s[1]='%') then
  165. begin
  166. delete(s,1,1);
  167. if GetEntry('OPT') then
  168. r.NeedOptions:=res
  169. else
  170. if GetEntry('DELOPT') then
  171. r.DelOptions:=res
  172. else
  173. if GetEntry('TARGET') then
  174. r.NeedTarget:=res
  175. else
  176. if GetEntry('SKIPTARGET') then
  177. r.SkipTarget:=res
  178. else
  179. if GetEntry('CPU') then
  180. r.NeedCPU:=res
  181. else
  182. if GetEntry('SKIPCPU') then
  183. r.SkipCPU:=res
  184. else
  185. if GetEntry('SKIPEMU') then
  186. r.SkipEmu:=res
  187. else
  188. if GetEntry('VERSION') then
  189. r.MinVersion:=res
  190. else
  191. if GetEntry('MAXVERSION') then
  192. r.MaxVersion:=res
  193. else
  194. if GetEntry('RESULT') then
  195. Val(res,r.ResultCode,code)
  196. else
  197. if GetEntry('GRAPH') then
  198. r.UsesGraph:=true
  199. else
  200. if GetEntry('FAIL') then
  201. r.ShouldFail:=true
  202. else
  203. if GetEntry('RECOMPILE') then
  204. begin
  205. r.NeedRecompile:=true;
  206. r.RecompileOpt:=res;
  207. end
  208. else
  209. if GetEntry('NORUN') then
  210. r.NoRun:=true
  211. else
  212. if GetEntry('NEEDLIBRARY') then
  213. r.NeedLibrary:=true
  214. else
  215. if GetEntry('NEEDEDAFTER') then
  216. r.NeededAfter:=true
  217. else
  218. if GetEntry('KNOWNRUNERROR') then
  219. begin
  220. r.IsKnownRunError:=true;
  221. if res<>'' then
  222. begin
  223. val(res,l,code);
  224. if code>1 then
  225. begin
  226. part:=code;
  227. val(copy(res,1,code-1),l,code);
  228. delete(res,1,part);
  229. end;
  230. if code=0 then
  231. r.KnownRunError:=l;
  232. if res<>'' then
  233. r.KnownRunNote:=res;
  234. end;
  235. end
  236. else
  237. if GetEntry('KNOWNCOMPILEERROR') then
  238. begin
  239. r.IsKnownCompileError:=true;
  240. if res<>'' then
  241. begin
  242. val(res,l,code);
  243. if code>1 then
  244. begin
  245. part:=code;
  246. val(copy(res,1,code-1),l,code);
  247. delete(res,1,part);
  248. end;
  249. if code=0 then
  250. r.KnownCompileError:=l;
  251. if res<>'' then
  252. r.KnownCompileNote:=res;
  253. end;
  254. end
  255. else
  256. if GetEntry('INTERACTIVE') then
  257. r.IsInteractive:=true
  258. else
  259. if GetEntry('NOTE') then
  260. begin
  261. R.Note:='Note: '+res;
  262. Verbose(V_Normal,r.Note);
  263. end
  264. else
  265. if GetEntry('TIMEOUT') then
  266. Val(res,r.Timeout,code)
  267. else
  268. if GetEntry('FILES') then
  269. r.Files:=res
  270. else
  271. if GetEntry('CONFIGFILE') then
  272. begin
  273. l:=Pos(' ',res);
  274. if l>0 then
  275. begin
  276. r.ConfigFileSrc:=Copy(res,1,l-1);
  277. r.ConfigFileDst:=Copy(res,l+1,Length(res)-l+1);
  278. if r.ConfigFileSrc='' then
  279. Verbose(V_Error,'Config file source is empty');
  280. if r.ConfigFileDst='' then
  281. Verbose(V_Error,'Config file destination is empty');
  282. end
  283. else
  284. begin
  285. r.ConfigFileSrc:=res;
  286. r.ConfigFileDst:=res;
  287. end;
  288. end
  289. else
  290. if GetEntry('WPOPARAS') then
  291. r.wpoparas:=res
  292. else
  293. if GetEntry('WPOPASSES') then
  294. val(res,r.wpopasses,code)
  295. else
  296. if GetEntry('DELFILES') then
  297. r.DelFiles:=res
  298. else
  299. Verbose(V_Error,'Unknown entry: '+s);
  300. end;
  301. end
  302. else
  303. break;
  304. end;
  305. end;
  306. close(t);
  307. GetConfig:=true;
  308. end;
  309. Function GetFileContents (FN : String) : String;
  310. Var
  311. F : Text;
  312. S : String;
  313. begin
  314. Result:='';
  315. Assign(F,FN);
  316. {$I-}
  317. Reset(F);
  318. If IOResult<>0 then
  319. Exit;
  320. {$I+}
  321. While Not(EOF(F)) do
  322. begin
  323. ReadLn(F,S);
  324. if length(Result)<MaxLogSize then
  325. Result:=Result+S+LineEnding;
  326. end;
  327. Close(F);
  328. end;
  329. end.