testu.pp 7.8 KB

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