testu.pp 6.8 KB

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