testu.pp 6.6 KB

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