tcstream.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. { TStream classes tests.
  2. Copyright (C) 2020 Michael Van Canneyt [email protected]
  3. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public
  4. License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version
  5. with the following modification:
  6. As a special exception, the copyright holders of this library give you permission to link this library with independent modules
  7. to produce an executable, regardless of the license terms of these independent modules,and to copy and distribute the resulting
  8. executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions
  9. of the license of that module. An independent module is a module which is not derived from or based on this library. If you
  10. modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do
  11. not wish to do so, delete this exception statement from your version.
  12. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
  16. }
  17. unit tcstream;
  18. {$mode objfpc}{$H+}
  19. interface
  20. uses
  21. Classes, SysUtils, fpcunit, testregistry;
  22. type
  23. { TTestStream }
  24. TTestStream= class(TTestCase)
  25. protected
  26. FStream : TStream;
  27. procedure SetUp; override;
  28. procedure TearDown; override;
  29. procedure AssertBytes(B : Array of Byte; aMessage : string = ''; ObserveEndian : Boolean = True); overload;
  30. // procedure AssertBytes(B : TBytes; aMessage : string = '');overload;
  31. Function CreateBytes(aCount : integer) : TBytes;
  32. Property Stream : TStream Read FStream;
  33. published
  34. procedure TestHookUp;
  35. Procedure TestBytes;
  36. Procedure TestBytesLarge;
  37. Procedure TestBytesLargeCopy;
  38. Procedure TestByte;
  39. Procedure TestByteBuffer;
  40. Procedure TestInt8;
  41. Procedure TestInt8Buffer;
  42. Procedure TestUInt8;
  43. Procedure TestUInt8Buffer;
  44. Procedure TestSmallint;
  45. Procedure TestSmallintBuffer;
  46. Procedure TestInt16;
  47. Procedure TestInt16Neg;
  48. Procedure TestInt16Buffer;
  49. Procedure TestUInt16;
  50. Procedure TestUInt16Buffer;
  51. Procedure TestInt32;
  52. Procedure TestInt32Neg;
  53. Procedure TestInt32Buffer;
  54. Procedure TestUInt32;
  55. Procedure TestUInt32Buffer;
  56. Procedure TestInt64;
  57. Procedure TestInt64Neg;
  58. Procedure TestInt64Buffer;
  59. Procedure TestBoolean;
  60. Procedure TestBooleanBuffer;
  61. Procedure TestWideChar;
  62. Procedure TestWideCharBuffer;
  63. Procedure TestDouble;
  64. Procedure TestDoubleBuffer;
  65. {$ifndef ECMASCRIPT}
  66. Procedure TestAnsiChar;
  67. Procedure TestAnsicharBuffer;
  68. Procedure TestSingle;
  69. Procedure TestSingleBuffer;
  70. Procedure TestExtended;
  71. Procedure TestExtendedBuffer;
  72. {$endif}
  73. end;
  74. { TTestBigendianStream }
  75. TTestBigendianStream= class(TTestStream)
  76. Public
  77. Procedure Setup; override;
  78. end;
  79. { TTestStringStream }
  80. TTestStringStream = class(TTestCase)
  81. private
  82. FStream: TStringStream;
  83. Public
  84. Procedure TearDown; override;
  85. Procedure DoCreate(S : String);
  86. Property Stream : TStringStream Read FStream;
  87. Published
  88. Procedure TestDataStringEmpty;
  89. Procedure TestDataString;
  90. Procedure TestWrite;
  91. Procedure TestRead;
  92. Procedure TestReadString;
  93. Procedure TestWriteString;
  94. Procedure TestCopyFrom;
  95. end;
  96. implementation
  97. { TTestStringStream }
  98. procedure TTestStringStream.TearDown;
  99. begin
  100. FreeAndNil(FStream);
  101. inherited TearDown;
  102. end;
  103. procedure TTestStringStream.DoCreate(S: String);
  104. begin
  105. FreeAndNil(FStream);
  106. FStream:=TStringStream.Create(S);
  107. end;
  108. procedure TTestStringStream.TestDataStringEmpty;
  109. begin
  110. DoCreate('');
  111. AssertEquals('Empty','',Stream.DataString);
  112. end;
  113. procedure TTestStringStream.TestDataString;
  114. begin
  115. DoCreate('ABCD');
  116. AssertEquals('Non-empty','ABCD',Stream.DataString);
  117. end;
  118. procedure TTestStringStream.TestWrite;
  119. begin
  120. DoCreate('');
  121. Stream.WriteBufferData('A');
  122. Stream.WriteBufferData('B');
  123. Stream.WriteBufferData('C');
  124. Stream.WriteBufferData('D');
  125. AssertEquals('Write Contents','ABCD',Stream.DataString);
  126. end;
  127. procedure TTestStringStream.TestRead;
  128. Var
  129. S : String;
  130. C : Char;
  131. I : Integer;
  132. begin
  133. S:='ABCD';
  134. DoCreate(S);
  135. For I:=1 to Length(S) do
  136. begin
  137. Stream.ReadBufferData(C);
  138. AssertEquals(Format('Character at',[i]),S[i],C);
  139. end;
  140. end;
  141. procedure TTestStringStream.TestReadString;
  142. Var
  143. S : String;
  144. begin
  145. S:='ABCDEFGH';
  146. DoCreate(S);
  147. AssertEquals('2 characters','AB',Stream.ReadString(2));
  148. AssertEquals('Top off characters','CDEFGH',Stream.ReadString(11));
  149. S:='Hello World';
  150. FreeAndNil(FStream);
  151. DoCreate(S);
  152. AssertEquals('Correct string',S,Stream.ReadString(Length(S)));
  153. end;
  154. procedure TTestStringStream.TestWriteString;
  155. begin
  156. DoCreate('');
  157. Stream.WriteString('AB');
  158. AssertEquals('Length 1',4,Stream.Size);
  159. AssertEquals('Datastring 1','AB',Stream.DataString);
  160. Stream.WriteString('CDEFGH');
  161. AssertEquals('Length 2',16,Stream.Size);
  162. AssertEquals('Datastring 2','ABCDEFGH',Stream.DataString);
  163. end;
  164. procedure TTestStringStream.TestCopyFrom;
  165. Var
  166. S2 : TStringStream;
  167. begin
  168. DoCreate('ABCD');
  169. S2:=TStringStream.Create('');
  170. try
  171. S2.CopyFrom(Stream,0);
  172. AssertEquals('Copied correctly','ABCD',S2.DataString);
  173. finally
  174. S2.Free;
  175. end;
  176. end;
  177. { TTestBigendianStream }
  178. procedure TTestBigendianStream.Setup;
  179. begin
  180. inherited Setup;
  181. Stream.Endian:=Tendian.Big;
  182. end;
  183. procedure TTestStream.TestHookUp;
  184. begin
  185. AssertNotNull('Have Stream',Stream);
  186. end;
  187. procedure TTestStream.TestBytes;
  188. Var
  189. B : TBytes;
  190. begin
  191. B:=CreateBytes(4);
  192. Stream.Write(B,4);
  193. AssertBytes(B,'Bytes, ignoring endianness',False);
  194. end;
  195. procedure TTestStream.TestBytesLarge;
  196. Var
  197. B : TBytes;
  198. begin
  199. B:=CreateBytes(8000);
  200. Stream.Write(B,Length(B));
  201. AssertBytes(B,'Bytes, ignoring endianness',False);
  202. end;
  203. procedure TTestStream.TestBytesLargeCopy;
  204. Var
  205. B : TBytes;
  206. S : TStream;
  207. begin
  208. S:=TBytesStream.Create([]);
  209. B:=CreateBytes(8000);
  210. S.Write(B,Length(B));
  211. Stream.CopyFrom(S,0);
  212. S.Free;
  213. AssertBytes(B,'Bytes, ignoring endianness',False);
  214. end;
  215. procedure TTestStream.TestByte;
  216. Var
  217. S,D : Byte;
  218. begin
  219. D:=0;
  220. S:=13;
  221. AssertEquals('Bytes written',1,Stream.WriteData(S));
  222. AssertBytes([S]);
  223. Stream.Position:=0;
  224. AssertEquals('Bytes read',1,Stream.ReadData(D));
  225. AssertEquals('Written data read correctly',S,D);
  226. end;
  227. procedure TTestStream.TestByteBuffer;
  228. Var
  229. S,D : Byte;
  230. begin
  231. D:=0;
  232. S:=13;
  233. Stream.WriteBufferData(S);
  234. AssertBytes([S]);
  235. Stream.Position:=0;
  236. Stream.ReadBufferData(D);
  237. AssertEquals('Written data read correctly',S,D);
  238. end;
  239. procedure TTestStream.TestInt8;
  240. Var
  241. S,D : Int8;
  242. begin
  243. D:=0;
  244. S:=-13;
  245. AssertEquals('Bytes written',1,Stream.WriteData(S));
  246. AssertBytes([S]);
  247. Stream.Position:=0;
  248. AssertEquals('Bytes read',1,Stream.ReadData(D));
  249. AssertEquals('Written data read correctly',S,D);
  250. end;
  251. procedure TTestStream.TestInt8Buffer;
  252. Var
  253. S,D : Int8;
  254. begin
  255. D:=0;
  256. S:=-13;
  257. Stream.WriteBufferData(S);
  258. AssertBytes([S]);
  259. Stream.Position:=0;
  260. Stream.ReadBufferData(D);
  261. AssertEquals('Written data read correctly',S,D);
  262. end;
  263. procedure TTestStream.TestUInt8;
  264. Var
  265. S,D : UInt8;
  266. begin
  267. D:=0;
  268. S:=139;
  269. AssertEquals('Bytes written',1,Stream.WriteData(S));
  270. AssertBytes([S]);
  271. Stream.Position:=0;
  272. AssertEquals('Bytes read',1,Stream.ReadData(D));
  273. AssertEquals('Written data read correctly',S,D);
  274. end;
  275. procedure TTestStream.TestUInt8Buffer;
  276. Var
  277. S,D : UInt8;
  278. begin
  279. D:=0;
  280. S:=139;
  281. Stream.WriteBufferData(S);
  282. AssertBytes([S]);
  283. Stream.Position:=0;
  284. Stream.ReadBufferData(D);
  285. AssertEquals('Written data read correctly',S,D);
  286. end;
  287. procedure TTestStream.TestSmallint;
  288. Var
  289. S,D : SmallInt;
  290. begin
  291. D:=0;
  292. S:=127*256+13;
  293. AssertEquals('Bytes written',2,Stream.WriteData(S));
  294. AssertBytes([13,127]);
  295. Stream.Position:=0;
  296. AssertEquals('Bytes read',2,Stream.ReadData(D));
  297. AssertEquals('Written data read correctly',S,D);
  298. end;
  299. procedure TTestStream.TestSmallintBuffer;
  300. Var
  301. S,D : SmallInt;
  302. begin
  303. D:=0;
  304. S:=127*256+13;
  305. Stream.WriteBufferData(S);
  306. AssertBytes([13,127]);
  307. Stream.Position:=0;
  308. Stream.ReadBufferData(D);
  309. AssertEquals('Written data read correctly',S,D);
  310. end;
  311. procedure TTestStream.TestInt16;
  312. Var
  313. S,D : Int16;
  314. begin
  315. D:=0;
  316. S:=127*256+13;
  317. AssertEquals('Bytes written',2,Stream.WriteData(S));
  318. AssertBytes([13,127]);
  319. Stream.Position:=0;
  320. AssertEquals('Bytes read',2,Stream.ReadData(D));
  321. AssertEquals('Written data read correctly',S,D);
  322. end;
  323. procedure TTestStream.TestInt16Neg;
  324. Var
  325. S,D : Int16;
  326. begin
  327. D:=0;
  328. S:=-4086; // $F00A;
  329. AssertEquals('Bytes written',2,Stream.WriteData(S));
  330. AssertBytes([$0A,$F0]);
  331. Stream.Position:=0;
  332. AssertEquals('Bytes read',2,Stream.ReadData(D));
  333. AssertEquals('Written data read correctly',S,D);
  334. end;
  335. procedure TTestStream.TestInt16Buffer;
  336. Var
  337. S,D : Int16;
  338. begin
  339. D:=0;
  340. S:=127*256+13;
  341. Stream.WriteBufferData(S);
  342. AssertBytes([13,127]);
  343. Stream.Position:=0;
  344. Stream.ReadBufferData(D);
  345. AssertEquals('Written data read correctly',S,D);
  346. end;
  347. procedure TTestStream.TestUInt16;
  348. Var
  349. S,D : UInt16;
  350. begin
  351. D:=0;
  352. S:=$F00A; // 61450
  353. AssertEquals('Bytes written',2,Stream.WriteData(S));
  354. AssertBytes([$0A,$F0]);
  355. Stream.Position:=0;
  356. AssertEquals('Bytes read',2,Stream.ReadData(D));
  357. AssertEquals('Written data read correctly',S,D);
  358. end;
  359. procedure TTestStream.TestUInt16Buffer;
  360. Var
  361. S,D : UInt16;
  362. begin
  363. D:=0;
  364. S:=$F00A;
  365. Stream.WriteBufferData(S);
  366. AssertBytes([$0A,$F0]);
  367. Stream.Position:=0;
  368. Stream.ReadBufferData(D);
  369. AssertEquals('Written data read correctly',S,D);
  370. end;
  371. procedure TTestStream.TestInt32;
  372. Var
  373. S,D : Int32;
  374. begin
  375. D:=0;
  376. // 2131560201
  377. S:=(127 shl 24) + (13 shl 16) + (7 shl 8) + 9;
  378. AssertEquals('Bytes written',4,Stream.WriteData(S));
  379. AssertBytes([9,7,13,127]);
  380. Stream.Position:=0;
  381. AssertEquals('Bytes read',4,Stream.ReadData(D));
  382. AssertEquals('Written data read correctly',S,D);
  383. end;
  384. procedure TTestStream.TestInt32Neg;
  385. Var
  386. S,D : Int32;
  387. begin
  388. D:=0;
  389. // -2146629879
  390. S:=Int32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  391. AssertEquals('Bytes written',4,Stream.WriteData(S));
  392. AssertBytes([9,7,13,128]);
  393. Stream.Position:=0;
  394. AssertEquals('Bytes read',4,Stream.ReadData(D));
  395. AssertEquals('Written data read correctly',S,D);
  396. end;
  397. procedure TTestStream.TestInt32Buffer;
  398. Var
  399. S,D : Int32;
  400. begin
  401. D:=0;
  402. // 2131560201
  403. S:=(127 shl 24) + (13 shl 16) + (7 shl 8) + 9;
  404. Stream.WriteBufferData(S);
  405. AssertBytes([9,7,13,127]);
  406. Stream.Position:=0;
  407. Stream.ReadBufferData(D);
  408. AssertEquals('Written data read correctly',S,D);
  409. end;
  410. procedure TTestStream.TestUInt32;
  411. Var
  412. S,D : UInt32;
  413. begin
  414. D:=0;
  415. // 2148337417
  416. S:=UINT32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  417. AssertEquals('Bytes written',4,Stream.WriteData(S));
  418. AssertBytes([9,7,13,128]);
  419. Stream.Position:=0;
  420. AssertEquals('Bytes read',4,Stream.ReadData(D));
  421. AssertEquals('Written data read correctly',S,D);
  422. end;
  423. procedure TTestStream.TestUInt32Buffer;
  424. Var
  425. S,D : UInt32;
  426. begin
  427. D:=0;
  428. // 2148337417
  429. S:=UINT32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  430. Stream.WriteBufferData(S);
  431. AssertBytes([9,7,13,128]);
  432. Stream.Position:=0;
  433. Stream.ReadBufferData(D);
  434. AssertEquals('Written data read correctly',S,D);
  435. end;
  436. procedure TTestStream.TestInt64;
  437. Var
  438. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  439. begin
  440. D:=0;
  441. // 9154981354848060679
  442. // Javascript only has 52 bits : 7737333974279
  443. S:={$IFNDEF ECMASCRIPT} (127 shl 24) + (13 shl 16) {$endif}+ (7 shl 8) + 9;
  444. S:=(S shl 32) + ((125 shl 24) + (11 shl 16) + (5 shl 8) + 7);
  445. AssertEquals('Bytes written',8,Stream.WriteData(S));
  446. {$ifndef ECMASCRIPT}
  447. AssertBytes([7,5,11,125,9,7,{$IFNDEF ECMASCRIPT} 13,127 {$ELSE} 0,0 {$ENDIF}]);
  448. {$ENDIF}
  449. Stream.Position:=0;
  450. AssertEquals('Bytes read',8,Stream.ReadData(D));
  451. AssertEquals('Written data read correctly',S,D);
  452. end;
  453. procedure TTestStream.TestInt64Neg;
  454. Var
  455. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  456. begin
  457. D:=0;
  458. {$IFNDEF ECMASCRIPT}
  459. // -9219705124773231353
  460. S:=Int64((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  461. S:=Int64((S shl 32) + ((128 shl 24) + (11 shl 16) + (5 shl 8) + 7));
  462. AssertEquals('Bytes written',8,Stream.WriteData(S));
  463. AssertBytes([7,5,11,128,9,7,13,128]);
  464. Stream.Position:=0;
  465. AssertEquals('Bytes read',8,Stream.ReadData(D));
  466. AssertEquals('Written data read correctly',S,D);
  467. {$ELSE}
  468. S:=-9000199254740991;
  469. AssertEquals('Bytes written',8,Stream.WriteData(S));
  470. Stream.Position:=0;
  471. AssertEquals('Bytes read',8,Stream.ReadData(D));
  472. AssertEquals('Written data read correctly',S,D);
  473. {$ENDIF}
  474. end;
  475. procedure TTestStream.TestInt64Buffer;
  476. Var
  477. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  478. begin
  479. D:=0;
  480. // 9154981354848060679
  481. // 7737333974279 for ECMAScript
  482. S:={$IFNDEF ECMASCRIPT} (127 shl 24) + (13 shl 16) {$endif}+ (7 shl 8) + 9;
  483. S:=(S shl 32) + ((125 shl 24) + (11 shl 16) + (5 shl 8) + 7);
  484. Stream.WriteBufferData(S);
  485. {$IFNDEF ECMASCRIPT}
  486. AssertBytes([7,5,11,125,9,7,13,127]);
  487. {$ENDIF}
  488. Stream.Position:=0;
  489. Stream.ReadBufferData(D);
  490. AssertEquals('Written data read correctly',S,D);
  491. end;
  492. procedure TTestStream.TestBoolean;
  493. Var
  494. S,D : Boolean;
  495. begin
  496. D:=False;
  497. // 9154981354848060679
  498. S:=True;
  499. AssertEquals('Bytes written',1,Stream.WriteData(S));
  500. AssertBytes([1]);
  501. Stream.Position:=0;
  502. AssertEquals('Bytes read',1,Stream.ReadData(D));
  503. AssertEquals('Written data read correctly',S,D);
  504. end;
  505. procedure TTestStream.TestBooleanBuffer;
  506. Var
  507. S,D : Boolean;
  508. begin
  509. D:=False;
  510. // 9154981354848060679
  511. S:=True;
  512. Stream.WriteBufferData(S);
  513. AssertBytes([1]);
  514. Stream.Position:=0;
  515. Stream.ReadBufferData(D);
  516. AssertEquals('Written data read correctly',S,D);
  517. end;
  518. {$IFNDEF ECMASCRIPT}
  519. procedure TTestStream.TestAnsiChar;
  520. Var
  521. S,D : AnsiChar;
  522. begin
  523. D:=#0;
  524. S:='A';
  525. AssertEquals('Bytes written',1,Stream.WriteData(S));
  526. AssertBytes([Ord(S)]);
  527. Stream.Position:=0;
  528. AssertEquals('Bytes read',1,Stream.ReadData(D));
  529. AssertEquals('Written data read correctly',S,D);
  530. end;
  531. procedure TTestStream.TestAnsicharBuffer;
  532. Var
  533. S,D : AnsiChar;
  534. begin
  535. D:=#0;
  536. S:='A';
  537. Stream.WriteBufferData(S);
  538. AssertBytes([Ord(S)]);
  539. Stream.Position:=0;
  540. Stream.ReadBufferData(D);
  541. AssertEquals('Written data read correctly',S,D);
  542. end;
  543. {$endif}
  544. procedure TTestStream.TestWideChar;
  545. Var
  546. S,D : WideChar;
  547. begin
  548. D:=#0;
  549. S:='A';
  550. AssertEquals('Bytes written',2,Stream.WriteData(S));
  551. AssertBytes([Ord(S),0]);
  552. Stream.Position:=0;
  553. AssertEquals('Bytes read',2,Stream.ReadData(D));
  554. AssertEquals('Written data read correctly',Ord(S),Ord(D));
  555. end;
  556. procedure TTestStream.TestWideCharBuffer;
  557. Var
  558. S,D : WideChar;
  559. begin
  560. D:=#0;
  561. S:='A';
  562. Stream.WriteBufferData(S);
  563. AssertBytes([Ord(S),0]);
  564. Stream.Position:=0;
  565. Stream.ReadBufferData(D);
  566. AssertEquals('Written data read correctly',Ord(S),Ord(D));
  567. end;
  568. {$ifndef ECMASCRIPT}
  569. procedure TTestStream.TestSingle;
  570. Var
  571. S,D : Single;
  572. B : TBytes = Nil;
  573. begin
  574. D:=0;
  575. S:=123.45;
  576. AssertEquals('Bytes written',4,Stream.WriteData(S));
  577. Setlength(B,4);
  578. With TSingleRec(S) do
  579. begin
  580. B[0]:=Bytes[0];
  581. B[1]:=Bytes[1];
  582. B[2]:=Bytes[2];
  583. B[3]:=Bytes[3];
  584. end;
  585. AssertBytes(B);
  586. Stream.Position:=0;
  587. AssertEquals('Bytes read',4,Stream.ReadData(D));
  588. AssertEquals('Written data read correctly',S,D,0.0001);
  589. end;
  590. procedure TTestStream.TestSingleBuffer;
  591. Var
  592. S,D : Single;
  593. B : TBytes = Nil;
  594. begin
  595. D:=0;
  596. S:=123.45;
  597. Stream.WriteBufferData(S);
  598. Setlength(B,4);
  599. With TSingleRec(S) do
  600. begin
  601. B[0]:=Bytes[0];
  602. B[1]:=Bytes[1];
  603. B[2]:=Bytes[2];
  604. B[3]:=Bytes[3];
  605. end;
  606. AssertBytes(B);
  607. Stream.Position:=0;
  608. Stream.ReadBufferData(D);
  609. AssertEquals('Written data read correctly',S,D,0.0001);
  610. end;
  611. {$endif}
  612. procedure TTestStream.TestDouble;
  613. Var
  614. S,D : Double;
  615. B : TBytes;
  616. {$ifndef ECMASCRIPT}
  617. i : integer;
  618. {$endif}
  619. begin
  620. B:=Default(TBytes);
  621. D:=0;
  622. S:=123.45;
  623. AssertEquals('Bytes written',8,Stream.WriteData(S));
  624. Setlength(B,8);
  625. {$ifndef ECMASCRIPT}
  626. With TDoubleRec(S) do
  627. For I:=0 to 7 do
  628. B[I]:=Bytes[I];
  629. AssertBytes(B);
  630. {$endif}
  631. Stream.Position:=0;
  632. AssertEquals('Bytes read',8,Stream.ReadData(D));
  633. AssertEquals('Written data read correctly',S,D,0.0001);
  634. end;
  635. procedure TTestStream.TestDoubleBuffer;
  636. Var
  637. S,D : Double;
  638. B : TBytes;
  639. {$ifndef ECMASCRIPT}
  640. i : integer;
  641. {$endif}
  642. begin
  643. B:=Default(TBytes);
  644. D:=0;
  645. S:=123.45;
  646. Stream.WriteBufferData(S);
  647. Setlength(B,8);
  648. {$ifndef ECMASCRIPT}
  649. With TDoubleRec(S) do
  650. For I:=0 to 7 do
  651. B[I]:=Bytes[I];
  652. AssertBytes(B);
  653. {$endif}
  654. Stream.Position:=0;
  655. Stream.ReadBufferData(D);
  656. AssertEquals('Written data read correctly',S,D,0.0001);
  657. end;
  658. {$ifndef ECMASCRIPT}
  659. procedure TTestStream.TestExtended;
  660. Var
  661. S,D : Extended;
  662. B : TBytes = Nil;
  663. i : integer;
  664. begin
  665. D:=0;
  666. S:=123.45;
  667. AssertEquals('Bytes written',10,Stream.WriteData(S));
  668. Setlength(B,10);
  669. With TExtended80Rec(S) do
  670. For I:=0 to 9 do
  671. B[I]:=Bytes[I];
  672. AssertBytes(B);
  673. Stream.Position:=0;
  674. AssertEquals('Bytes read',10,Stream.ReadData(D));
  675. AssertEquals('Written data read correctly',S,D,0.0001);
  676. end;
  677. procedure TTestStream.TestExtendedBuffer;
  678. Var
  679. S,D : Extended;
  680. B : TBytes = Nil;
  681. i : integer;
  682. begin
  683. D:=0;
  684. S:=123.45;
  685. Stream.WriteBufferData(S);
  686. Setlength(B,10);
  687. With TExtended80Rec(S) do
  688. For I:=0 to 9 do
  689. B[I]:=Bytes[I];
  690. AssertBytes(B);
  691. Stream.Position:=0;
  692. Stream.ReadBufferData(D);
  693. AssertEquals('Written data read correctly',S,D,0.0001);
  694. end;
  695. {$endif}
  696. procedure TTestStream.SetUp;
  697. Var
  698. B : TBytes;
  699. begin
  700. B:=Default(TBytes);
  701. SetLength(B,0);
  702. FStream:=TBytesStream.Create(B);
  703. end;
  704. procedure TTestStream.TearDown;
  705. begin
  706. FreeAndNil(FStream);
  707. end;
  708. procedure TTestStream.AssertBytes(B: array of Byte; aMessage: string = ''; ObserveEndian : Boolean = True);
  709. Var
  710. L,I,E: integer;
  711. A : Byte;
  712. SB : TBytes;
  713. begin
  714. if AMessage<>'' then
  715. aMessage:=aMessage+': ';
  716. AssertEquals(aMessage+'Length bytes equals',Length(B),FStream.Size);
  717. SB:=TBytesStream(Stream).Bytes;
  718. L:=Length(B);
  719. for I:=0 to L-1 do
  720. begin
  721. E:=Byte(B[i] and $FF);
  722. if ObserveEndian and (Stream.Endian=Tendian.Big) then
  723. A:=SB[L-1-i]
  724. else
  725. A:=SB[i];
  726. AssertEquals(aMessage+'Byte['+IntToStr(I)+'] equals',E,A);
  727. end;
  728. end;
  729. {
  730. procedure TTestStream.AssertBytes(B: TBytes; aMessage : string = '');
  731. Var
  732. I : integer;
  733. SB : TBytes;
  734. begin
  735. if AMessage<>'' then
  736. aMessage:=aMessage+': ';
  737. AssertEquals(aMessage+'Length bytes equals',Length(B),FStream.Size);
  738. SB:=TBytesStream(Stream).Bytes;
  739. for I:=0 to Length(B)-1 do
  740. AssertEquals(aMessage+'Byte['+IntToStr(I)+'] equals',B[i],SB[i]);
  741. end;
  742. }
  743. function TTestStream.CreateBytes(aCount: integer): TBytes;
  744. Var
  745. I : Integer;
  746. begin
  747. Result:=Nil;
  748. SetLength(Result,aCount);
  749. For I:=0 to aCount-1 do
  750. Result[I]:=I+1;
  751. end;
  752. initialization
  753. RegisterTests([{TTestStream,TTestBigendianStream,}TTestStringStream]);
  754. end.