testu.pp 7.4 KB

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