testu.pp 7.5 KB

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