testu.pp 7.0 KB

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