testu.pp 5.4 KB

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