tcstreaming.pp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. {$mode objfpc}
  2. {$h+}
  3. unit tcstreaming;
  4. interface
  5. Uses
  6. SysUtils,Classes, fpcunit, testutils, testregistry;
  7. Type
  8. TTestStreaming = Class(TTestCase)
  9. Private
  10. FStream : TMemoryStream;
  11. Function ReadByte : byte;
  12. Function ReadWord : Word;
  13. Function ReadInteger : LongInt;
  14. Function ReadInt64 : Int64;
  15. function ReadBareStr: string;
  16. function ReadString(V : TValueType): string;
  17. function ReadWideString(V : TValueType): WideString;
  18. Procedure Fail(FMt : String; Args : Array of const); overload;
  19. Public
  20. Procedure Setup; override;
  21. Procedure TearDown; override;
  22. Procedure SaveToStream(C : TComponent);
  23. Function ReadValue : TValueType;
  24. Procedure ExpectValue(AValue : TValueType);
  25. Procedure ExpectFlags(Flags : TFilerFlags; APosition : Integer);
  26. Procedure ExpectInteger(AValue : Integer);
  27. Procedure ExpectByte(AValue : Byte);
  28. Procedure ExpectInt64(AValue : Int64);
  29. Procedure ExpectBareString(AValue : String);
  30. Procedure ExpectString(AValue : String);
  31. Procedure ExpectSingle(AValue : Single);
  32. Procedure ExpectExtended(AValue : Extended);
  33. Procedure ExpectCurrency(AValue : Currency);
  34. Procedure ExpectIdent(AValue : String);
  35. Procedure ExpectDate(AValue : TDateTime);
  36. Procedure ExpectWideString(AValue : WideString);
  37. Procedure ExpectEndofList;
  38. Procedure ExpectSignature;
  39. end;
  40. implementation
  41. uses typinfo;
  42. Function ValName(V : TValueType) : String;
  43. begin
  44. Result:=GetEnumName(TypeInfo(TValueType),Ord(v));
  45. end;
  46. { TTestStreaming }
  47. procedure TTestStreaming.ExpectByte(AValue: Byte);
  48. Var
  49. B : Byte;
  50. begin
  51. B:=ReadByte;
  52. If (B<>AValue) then
  53. Fail('Expected byte %d, got %d',[AValue,B]);
  54. end;
  55. procedure TTestStreaming.ExpectCurrency(AValue: Currency);
  56. Var
  57. C : Currency;
  58. begin
  59. ExpectValue(vaCurrency);
  60. FStream.Read(C,Sizeof(C));
  61. If (C<>AValue) then
  62. Fail('Expected currency %f, got %f',[AValue,C]);
  63. end;
  64. procedure TTestStreaming.ExpectDate(AValue: TDateTime);
  65. Var
  66. C : TDateTime;
  67. begin
  68. ExpectValue(vaDate);
  69. FStream.Read(C,Sizeof(C));
  70. If (C<>AValue) then
  71. Fail('Expected datetime %f, got %f',[AValue,C]);
  72. end;
  73. procedure TTestStreaming.ExpectEndofList;
  74. begin
  75. ExpectValue(vaNull);
  76. end;
  77. procedure TTestStreaming.ExpectExtended(AValue: Extended);
  78. Var
  79. E : Extended;
  80. begin
  81. ExpectValue(vaExtended);
  82. FStream.Read(E,Sizeof(E));
  83. If Abs(E-AValue)>0.01 then
  84. Fail('Expected extended %f, got %f',[AValue,E]);
  85. end;
  86. procedure TTestStreaming.ExpectFlags(Flags: TFilerFlags;
  87. APosition: Integer);
  88. var
  89. Prefix: Byte;
  90. F : TFilerFlags;
  91. B : Byte;
  92. I : Integer;
  93. begin
  94. F := [];
  95. I:=0;
  96. B:=ReadByte;
  97. if B and $F0 = $F0 then
  98. begin
  99. Integer(F) := B and $0F;
  100. if ffChildPos in Flags then
  101. I:=ReadInteger;
  102. end
  103. else
  104. FStream.Position:=FStream.Position-1;
  105. If (FLags<>F) then
  106. Fail('Wrong Flags, expected %d, got %d',[Integer(Flags),B]);
  107. If I<>APosition then
  108. Fail('Wrong position, expected %d, got %d',[APosition,I]);
  109. end;
  110. procedure TTestStreaming.ExpectIdent(AValue: String);
  111. var
  112. L : Byte;
  113. V : TValueType;
  114. S : String;
  115. begin
  116. V:=ReadValue;
  117. case V of
  118. vaIdent:
  119. begin
  120. L:=ReadByte;
  121. SetLength(S,L);
  122. FStream.Read(S[1], L);
  123. end;
  124. vaFalse:
  125. S := 'False';
  126. vaTrue:
  127. S := 'True';
  128. vaNil:
  129. S := 'nil';
  130. vaNull:
  131. S := 'Null';
  132. else
  133. Fail('Expected identifier property type, got %s',[valName(V)]);
  134. end;
  135. If (S<>AValue) then
  136. Fail('Wrong identifier %s, expected %s',[S,AValue]);
  137. end;
  138. procedure TTestStreaming.ExpectInt64(AValue: Int64);
  139. Var
  140. V : TValueType;
  141. I : Int64;
  142. begin
  143. V:=ReadValue;
  144. Case V of
  145. vaInt8 : I:=ReadByte;
  146. vaInt16 : I:=ReadWord;
  147. vaInt32 : I:=ReadInteger;
  148. vaInt64 : I:=ReadInt64;
  149. else
  150. Fail('Expected integer property type, got %s',[valName(V)]);
  151. end;
  152. If (AValue<>I) then
  153. Fail('Expected integer %d, but got %d',[AValue,I]);
  154. end;
  155. procedure TTestStreaming.ExpectInteger(AValue: Integer);
  156. Var
  157. V : TValueType;
  158. I : Integer;
  159. begin
  160. V:=ReadValue;
  161. Case V of
  162. vaInt8 : I:=ReadByte;
  163. vaInt16 : I:=ReadWord;
  164. vaInt32 : I:=ReadInteger;
  165. else
  166. Fail('Expected integer property type, got %s',[valName(V)]);
  167. end;
  168. If (AValue<>I) then
  169. Fail('Expected integer %d, but got %d',[AValue,I]);
  170. end;
  171. procedure TTestStreaming.ExpectSignature;
  172. const
  173. Sig : array[1..4] of Char = 'TPF0';
  174. var
  175. E,L : Longint;
  176. begin
  177. L:=ReadInteger;
  178. E:=Longint(Sig);
  179. if L<>E then
  180. Fail('Invalid signature %d, expected %d',[L,E]);
  181. end;
  182. procedure TTestStreaming.ExpectSingle(AValue: Single);
  183. Var
  184. S : Single;
  185. begin
  186. ExpectValue(vaSingle);
  187. FStream.Read(S,SizeOf(Single));
  188. If Abs(AValue-S)>0.0001 then
  189. Fail('Expected single %f, but got %s',[AValue,S]);
  190. end;
  191. function TTestStreaming.ReadString(V : TValueType): string;
  192. var
  193. L: Integer;
  194. B : Byte;
  195. begin
  196. If V in [vaWString, vaUTF8String] then
  197. Result := ReadWideString(V)
  198. else
  199. begin
  200. L := 0;
  201. case V of
  202. vaString:
  203. begin
  204. FStream.Read(B, SizeOf(B));
  205. L:=B;
  206. end;
  207. vaLString:
  208. FStream.Read(L, SizeOf(Integer));
  209. else
  210. Fail('Wrong type %s, expected string type.',[ValName(V)]);
  211. end;
  212. SetLength(Result, L);
  213. If (L>0) then
  214. FStream.Read(PByte(Result)^, L);
  215. end;
  216. end;
  217. function TTestStreaming.ReadWideString(V : TValueType): WideString;
  218. var
  219. L: Integer;
  220. Temp: String;
  221. begin
  222. if V in [vaString, vaLString] then
  223. Result := ReadString(V)
  224. else
  225. begin
  226. L := 0;
  227. case V of
  228. vaWString:
  229. begin
  230. FStream.Read(L, SizeOf(Integer));
  231. SetLength(Result, L);
  232. FStream.Read(Pointer(Result)^, L * 2);
  233. end;
  234. vaUTF8String:
  235. begin
  236. FStream.Read(L, SizeOf(Integer));
  237. SetLength(Temp, L);
  238. FStream.Read(Pointer(Temp)^, L);
  239. Result:=Temp
  240. end;
  241. else
  242. Fail('Wrong type %s, expected widestring type.',[ValName(V)]);
  243. end;
  244. end;
  245. end;
  246. procedure TTestStreaming.ExpectString(AValue: String);
  247. Var
  248. V : TValueType;
  249. S : String;
  250. begin
  251. V:=ReadValue;
  252. If v in [vaString,vaLstring,vaWString,vaUTF8String] then
  253. S:=ReadString(V)
  254. else
  255. Fail('Expected string type, but got : %s',[ValName(V)]);
  256. If (S<>AValue) then
  257. Fail('Expected string "%s", but got "%s"',[AVAlue,S]);
  258. end;
  259. procedure TTestStreaming.ExpectValue(AValue: TValueType);
  260. Var
  261. V : TValueType;
  262. begin
  263. V:=ReadValue;
  264. If (V<>AValue) then
  265. Fail('Expecting value %s, but read %s',[ValName(AValue),ValName(V)]);
  266. end;
  267. procedure TTestStreaming.ExpectWideString(AValue: WideString);
  268. Var
  269. W : WideString;
  270. V : TValueType;
  271. begin
  272. V:=ReadValue;
  273. If v in [vaString,vaLstring,vaWString,vaUTF8String] then
  274. W:=ReadWideString(V)
  275. else
  276. Fail('Expected string type, but got : %s',[ValName(V)]);
  277. If (W<>AValue) then
  278. Fail('Expected string "%s", but got "%s"',[AVAlue,W]);
  279. end;
  280. procedure TTestStreaming.Fail(Fmt: String; Args: array of const);
  281. begin
  282. Fail(Format(Fmt,Args));
  283. end;
  284. function TTestStreaming.ReadValue: TValueType;
  285. begin
  286. FStream.Read(Result,SizeOf(Result));
  287. end;
  288. procedure TTestStreaming.Setup;
  289. begin
  290. FStream:=TMemoryStream.Create;
  291. end;
  292. procedure TTestStreaming.SaveToStream(C: TComponent);
  293. begin
  294. C.Name:='Test'+C.ClassName;
  295. FStream.Clear;
  296. FStream.WriteComponent(C);
  297. FStream.Position:=0;
  298. end;
  299. procedure TTestStreaming.TearDown;
  300. begin
  301. FreeAndNil(FStream);
  302. end;
  303. function TTestStreaming.ReadByte: byte;
  304. begin
  305. FStream.Read(Result,SizeOf(Result));
  306. end;
  307. function TTestStreaming.ReadInt64: Int64;
  308. begin
  309. FStream.Read(Result,SizeOf(Result));
  310. end;
  311. function TTestStreaming.ReadInteger: LongInt;
  312. begin
  313. FStream.Read(Result,SizeOf(Result));
  314. end;
  315. function TTestStreaming.ReadWord: Word;
  316. begin
  317. FStream.Read(Result,SizeOf(Result));
  318. end;
  319. function TTestStreaming.ReadBareStr: string;
  320. var
  321. L: Byte;
  322. begin
  323. L:=ReadByte;
  324. SetLength(Result,L);
  325. Fstream.Read(Result[1], L);
  326. end;
  327. procedure TTestStreaming.ExpectBareString(AValue: String);
  328. Var
  329. S : String;
  330. begin
  331. S:=ReadBareStr;
  332. If (S<>AValue) then
  333. Fail('Expected bare string %s, got :%s',[AValue,S]);
  334. end;
  335. end.