testu.pp 7.3 KB

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