testu.pp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 Copy(s,1,3)=#$EF#$BB#$BF then
  142. delete(s,1,3);
  143. if s<>'' then
  144. begin
  145. TrimB(s);
  146. if s[1]='{' then
  147. begin
  148. delete(s,1,1);
  149. TrimB(s);
  150. if (s<>'') and (s[1]='%') then
  151. begin
  152. delete(s,1,1);
  153. if GetEntry('OPT') then
  154. r.NeedOptions:=res
  155. else
  156. if GetEntry('TARGET') then
  157. r.NeedTarget:=res
  158. else
  159. if GetEntry('SKIPTARGET') then
  160. r.SkipTarget:=res
  161. else
  162. if GetEntry('CPU') then
  163. r.NeedCPU:=res
  164. else
  165. if GetEntry('SKIPCPU') then
  166. r.SkipCPU:=res
  167. else
  168. if GetEntry('SKIPEMU') then
  169. r.SkipEmu:=res
  170. else
  171. if GetEntry('VERSION') then
  172. r.MinVersion:=res
  173. else
  174. if GetEntry('MAXVERSION') then
  175. r.MaxVersion:=res
  176. else
  177. if GetEntry('RESULT') then
  178. Val(res,r.ResultCode,code)
  179. else
  180. if GetEntry('GRAPH') then
  181. r.UsesGraph:=true
  182. else
  183. if GetEntry('FAIL') then
  184. r.ShouldFail:=true
  185. else
  186. if GetEntry('RECOMPILE') then
  187. r.NeedRecompile:=true
  188. else
  189. if GetEntry('NORUN') then
  190. r.NoRun:=true
  191. else
  192. if GetEntry('NEEDLIBRARY') then
  193. r.NeedLibrary:=true
  194. else
  195. if GetEntry('KNOWNRUNERROR') then
  196. begin
  197. r.IsKnownRunError:=true;
  198. if res<>'' then
  199. begin
  200. val(res,l,code);
  201. if code>1 then
  202. begin
  203. part:=code;
  204. val(copy(res,1,code-1),l,code);
  205. delete(res,1,part);
  206. end;
  207. if code=0 then
  208. r.KnownRunError:=l;
  209. if res<>'' then
  210. r.KnownRunNote:=res;
  211. end;
  212. end
  213. else
  214. if GetEntry('KNOWNCOMPILEERROR') then
  215. begin
  216. r.IsKnownCompileError:=true;
  217. if res<>'' then
  218. begin
  219. val(res,l,code);
  220. if code>1 then
  221. begin
  222. part:=code;
  223. val(copy(res,1,code-1),l,code);
  224. delete(res,1,part);
  225. end;
  226. if code=0 then
  227. r.KnownCompileError:=l;
  228. if res<>'' then
  229. r.KnownCompileNote:=res;
  230. end;
  231. end
  232. else
  233. if GetEntry('INTERACTIVE') then
  234. r.IsInteractive:=true
  235. else
  236. if GetEntry('NOTE') then
  237. begin
  238. R.Note:='Note: '+res;
  239. Verbose(V_Normal,r.Note);
  240. end
  241. else
  242. if GetEntry('TIMEOUT') then
  243. Val(res,r.Timeout,code)
  244. else
  245. Verbose(V_Error,'Unknown entry: '+s);
  246. end;
  247. end
  248. else
  249. break;
  250. end;
  251. end;
  252. close(t);
  253. GetConfig:=true;
  254. end;
  255. Function GetFileContents (FN : String) : String;
  256. Var
  257. F : Text;
  258. S : String;
  259. begin
  260. Result:='';
  261. Assign(F,FN);
  262. {$I-}
  263. Reset(F);
  264. If IOResult<>0 then
  265. Exit;
  266. {$I+}
  267. While Not(EOF(F)) do
  268. begin
  269. ReadLn(F,S);
  270. Result:=Result+S+LineEnding;
  271. end;
  272. Close(F);
  273. end;
  274. end.