testu.pp 5.5 KB

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