IdStreamTest.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 11267: IdStreamTest.pas
  11. {
  12. { Rev 1.0 11/12/2002 09:19:20 PM JPMugaas
  13. { Initial check in. Import from FTP VC.
  14. }
  15. unit IdStreamTest;
  16. interface
  17. uses IndyBox, classes;
  18. type
  19. TIdStreamTest = class(TIndyBox)
  20. procedure MakeFile(const AFileName, AEOL : String);
  21. procedure MakeFiles(APath : String);
  22. procedure TestStrings(const AStrings: String; ARes : TStrings);
  23. procedure TestStr(const AString : String);
  24. procedure FileReadTimeTest(const AFileName : String);
  25. procedure FileReadReliabilityTest(const AFileName : String);
  26. procedure FileTests;
  27. procedure Test; override;
  28. end;
  29. const
  30. FileNumLines = 10000; //10,000 lines for a speed test - INSANE!!!
  31. FileTextLine = 'test';
  32. implementation
  33. uses IdCoreGlobal, IdGlobal, IdStream, IdReadLineStream, SysUtils;
  34. const FNameEOL = 'CRLF.txt';
  35. FNameLFCR = 'LFCR.txt';
  36. FNameLF = 'LF.txt';
  37. FNameCR = 'CR.txt';
  38. const
  39. Line1 = 'Test';
  40. Line2 = 'Test Two';
  41. Line3 = 'Test Three';
  42. EmtrpyString : String = '';
  43. RegularString : String = Line1+EOL+Line2+EOL+Line3+EOL;
  44. LFCRString : String = Line1+LF+CR+Line2+LF+CR+Line3+LF+CR;
  45. CRString : String = Line1+CR+Line2+CR+Line3+CR;
  46. LFString : String = Line1+LF+Line2+LF+Line3+LF;
  47. CRLFString : String = Line1+LF+Line2+CR+Line3+LF;
  48. { TIdStreamTest }
  49. procedure TIdStreamTest.FileReadReliabilityTest(const AFileName: String);
  50. var m : TMemoryStream;
  51. sp : TIdReadLineStream;
  52. s : String;
  53. // StartTime : Cardinal;
  54. LineCount : Cardinal;
  55. const MaxLines = FileNumLines + 1;
  56. begin
  57. { m := TMemoryStream.Create;
  58. try
  59. m.LoadFromFile( AFileName );
  60. m.Position := 0;
  61. LineCount := 0;
  62. repeat
  63. s := TIdStream(m).ReadLn;
  64. Check(s=FileTextLine,Format('%s does not match %s - Line %d',[s,FileTextLine,LineCount]));
  65. inc(LineCount);
  66. until LineCount = MaxLines;
  67. Dec(LineCount);//correct for empty line returned at end
  68. Status(Format('TIdStream typecast- FileName: %s - %d millaseconds for %d line(s)',[AFileName,StartTime,LineCount]));
  69. finally
  70. FreeAndNil(m);
  71. end;
  72. }
  73. m := TMemoryStream.Create;
  74. try
  75. m.LoadFromFile( AFileName );
  76. m.Position := 0;
  77. LineCount := 0;
  78. sp := TIdReadLineStream.Create(m);
  79. repeat
  80. s := sp.ReadLn;
  81. inc(LineCount);
  82. until LineCount = MaxLines;
  83. // Dec(LineCount);//correct for empty line returned at end
  84. finally
  85. FreeAndNil(m);
  86. FreeAndNil(sp);
  87. end;
  88. end;
  89. procedure TIdStreamTest.FileReadTimeTest(const AFileName : String);
  90. var m : TMemoryStream;
  91. sp : TIdReadLineStream;
  92. s : String;
  93. StartTime : Cardinal;
  94. LineCount : Cardinal;
  95. const MaxLines = FileNumLines + 1;
  96. begin
  97. m := TMemoryStream.Create;
  98. try
  99. m.LoadFromFile( AFileName );
  100. m.Position := 0;
  101. LineCount := 0;
  102. StartTime := GetTickCount;
  103. repeat
  104. s := TIdStream(m).ReadLn;
  105. inc(LineCount);
  106. until LineCount = MaxLines;
  107. StartTime := GetTickCount - StartTime;
  108. Dec(LineCount);//correct for empty line returned at end
  109. Status(Format('TIdStream typecast- FileName: %s - %d millaseconds for %d line(s)',[AFileName,StartTime,LineCount]));
  110. finally
  111. FreeAndNil(m);
  112. end;
  113. m := TMemoryStream.Create;
  114. try
  115. m.LoadFromFile( AFileName );
  116. m.Position := 0;
  117. LineCount := 0;
  118. sp := TIdReadLineStream.Create(m);
  119. StartTime := GetTickCount;
  120. repeat
  121. s := sp.ReadLn;
  122. inc(LineCount);
  123. until LineCount = MaxLines;
  124. StartTime := GetTickCount - StartTime;
  125. Dec(LineCount);//correct for empty line returned at end
  126. Status(Format('TIdReadLineStream - FileName: %s - %d millaseconds for %d line(s)',[AFileName,StartTime,LineCount]));
  127. finally
  128. FreeAndNil(m);
  129. FreeAndNil(sp);
  130. end;
  131. end;
  132. procedure TIdStreamTest.FileTests;
  133. var
  134. LPath : String;
  135. begin
  136. LPath := IncludeTrailingSlash(GetTempDir);
  137. MakeFiles(LPath);
  138. end;
  139. procedure TIdStreamTest.MakeFile(const AFileName, AEOL: String);
  140. var i : Cardinal;
  141. f : Text;
  142. begin
  143. AssignFile(f,AFileName);
  144. Rewrite(f);
  145. try
  146. for i := 1 to FileNumLines do
  147. begin
  148. Write(f,FileTextLine+AEOL);
  149. end;
  150. finally
  151. CloseFile(f);
  152. end;
  153. end;
  154. procedure TIdStreamTest.MakeFiles(APath : String);
  155. begin
  156. MakeFile(APath+FNameEOL,EOL);
  157. MakeFile(APath+FNameLFCR,LF+CR);
  158. MakeFile(APath+FNameLF,LF);
  159. MakeFile(APath+FNameCR,CR);
  160. FileReadTimeTest(APath+FNameEOL);
  161. FileReadReliabilityTest(APath+FNameEOL);
  162. FileReadTimeTest(APath+FNameLFCR);
  163. FileReadReliabilityTest(APath+FNameLFCR);
  164. FileReadTimeTest(APath+FNameLF);
  165. FileReadReliabilityTest(APath+FNameLF);
  166. FileReadTimeTest(APath+FNameCR);
  167. FileReadReliabilityTest(APath+FNameCR);
  168. DeleteFile(APath+FNameEOL);
  169. DeleteFile(APath+FNameLFCR);
  170. DeleteFile(APath+FNameLF);
  171. DeleteFile(APath+FNameCR);
  172. end;
  173. procedure TIdStreamTest.Test;
  174. var st : TStrings;
  175. begin
  176. inherited;
  177. // emtpy string test
  178. Status('Emtpy string');
  179. TestStr(EmtrpyString);
  180. st := TStringList.Create;
  181. try
  182. st.Add(Line1);
  183. st.Add(Line2);
  184. st.Add(Line3);
  185. // Status('Lines with CRLF endings');
  186. // TestStrings(RegularString,st);
  187. Status('Lines with CR endings');
  188. TestStrings(CRString,st);
  189. Status('Lines with LF endings');
  190. TestStrings(LFString,st);
  191. Status('Lines with CR or LF endings');
  192. TestStrings(CRLFString,st);
  193. st.Text := StringReplace(st.Text,EOL,EOL+EOL,[rfReplaceAll]);
  194. Status('Lines with LFCR line-ending');
  195. TestStrings(LFCRString,st);
  196. finally
  197. FreeAndNil(st);
  198. end;
  199. FileTests;
  200. end;
  201. procedure TIdStreamTest.TestStr(const AString : String);
  202. var M : TStream;
  203. s : String;
  204. begin
  205. m := TMemoryStream.Create;
  206. try
  207. if AString<>'' then
  208. begin
  209. m.Write(AString[1],Length(AString));
  210. end;
  211. m.Position := 0;
  212. s := TIdStream(m).ReadLn;
  213. Check((s = AString),Format('%s does not match %s',[s, AString]));
  214. finally
  215. FreeAndNil(m);
  216. end;
  217. end;
  218. procedure TIdStreamTest.TestStrings(const AStrings: String; ARes : TStrings);
  219. var M : TStream;
  220. s : String;
  221. i : Integer;
  222. sp : TIdReadLineStream;
  223. begin
  224. sp := nil;
  225. m := TMemoryStream.Create;
  226. try
  227. if AStrings<>'' then
  228. begin
  229. m.Write(AStrings[1],Length(AStrings));
  230. end;
  231. m.Position := 0;
  232. for i := 0 to ARes.Count-1 do
  233. begin
  234. s := TIdStream(m).ReadLn;
  235. Check((s = ARes[i]),Format('%s does not match %s',[s, ARes[i] ]));
  236. end;
  237. finally
  238. FreeAndNil(m);
  239. end;
  240. m := TMemoryStream.Create;
  241. try
  242. if AStrings<>'' then
  243. begin
  244. m.Write(AStrings[1],Length(AStrings));
  245. end;
  246. m.Position := 0;
  247. sp := TIdReadLineStream.Create(m);
  248. for i := 0 to ARes.Count-1 do
  249. begin
  250. s := sp.ReadLn;
  251. Check((s = ARes[i]),Format('%s does not match %s',[s, ARes[i] ]));
  252. end;
  253. finally
  254. FreeAndNil(m);
  255. FreeAndNil(sp);
  256. end;
  257. end;
  258. initialization
  259. TIndyBox.RegisterBox(TIdStreamTest, 'IdStream Test', 'Misc');
  260. end.