testu.pp 7.1 KB

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