tcstream.pp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 TestCopyFrom;
  93. end;
  94. implementation
  95. { TTestStringStream }
  96. procedure TTestStringStream.TearDown;
  97. begin
  98. FreeAndNil(FStream);
  99. inherited TearDown;
  100. end;
  101. procedure TTestStringStream.DoCreate(S: String);
  102. begin
  103. FreeAndNil(FStream);
  104. FStream:=TStringStream.Create(S);
  105. end;
  106. procedure TTestStringStream.TestDataStringEmpty;
  107. begin
  108. DoCreate('');
  109. AssertEquals('Empty','',Stream.DataString);
  110. end;
  111. procedure TTestStringStream.TestDataString;
  112. begin
  113. DoCreate('ABCD');
  114. AssertEquals('Non-empty','ABCD',Stream.DataString);
  115. end;
  116. procedure TTestStringStream.TestWrite;
  117. begin
  118. DoCreate('');
  119. Stream.WriteBufferData('A');
  120. Stream.WriteBufferData('B');
  121. Stream.WriteBufferData('C');
  122. Stream.WriteBufferData('D');
  123. AssertEquals('Write Contents','ABCD',Stream.DataString);
  124. end;
  125. procedure TTestStringStream.TestRead;
  126. Var
  127. S : String;
  128. C : Char;
  129. I : Integer;
  130. begin
  131. S:='ABCD';
  132. DoCreate(S);
  133. For I:=1 to Length(S) do
  134. begin
  135. Stream.ReadBufferData(C);
  136. AssertEquals(Format('Character at',[i]),S[i],C);
  137. end;
  138. end;
  139. procedure TTestStringStream.TestCopyFrom;
  140. Var
  141. S2 : TStringStream;
  142. begin
  143. DoCreate('ABCD');
  144. S2:=TStringStream.Create('');
  145. try
  146. S2.CopyFrom(Stream,0);
  147. AssertEquals('Copied correctly','ABCD',S2.DataString);
  148. finally
  149. S2.Free;
  150. end;
  151. end;
  152. { TTestBigendianStream }
  153. procedure TTestBigendianStream.Setup;
  154. begin
  155. inherited Setup;
  156. Stream.Endian:=Tendian.Big;
  157. end;
  158. procedure TTestStream.TestHookUp;
  159. begin
  160. AssertNotNull('Have Stream',Stream);
  161. end;
  162. procedure TTestStream.TestBytes;
  163. Var
  164. B : TBytes;
  165. begin
  166. B:=CreateBytes(4);
  167. Stream.Write(B,4);
  168. AssertBytes(B,'Bytes, ignoring endianness',False);
  169. end;
  170. procedure TTestStream.TestBytesLarge;
  171. Var
  172. B : TBytes;
  173. begin
  174. B:=CreateBytes(8000);
  175. Stream.Write(B,Length(B));
  176. AssertBytes(B,'Bytes, ignoring endianness',False);
  177. end;
  178. procedure TTestStream.TestBytesLargeCopy;
  179. Var
  180. B : TBytes;
  181. S : TStream;
  182. begin
  183. S:=TBytesStream.Create([]);
  184. B:=CreateBytes(8000);
  185. S.Write(B,Length(B));
  186. Stream.CopyFrom(S,0);
  187. S.Free;
  188. AssertBytes(B,'Bytes, ignoring endianness',False);
  189. end;
  190. procedure TTestStream.TestByte;
  191. Var
  192. S,D : Byte;
  193. begin
  194. D:=0;
  195. S:=13;
  196. AssertEquals('Bytes written',1,Stream.WriteData(S));
  197. AssertBytes([S]);
  198. Stream.Position:=0;
  199. AssertEquals('Bytes read',1,Stream.ReadData(D));
  200. AssertEquals('Written data read correctly',S,D);
  201. end;
  202. procedure TTestStream.TestByteBuffer;
  203. Var
  204. S,D : Byte;
  205. begin
  206. D:=0;
  207. S:=13;
  208. Stream.WriteBufferData(S);
  209. AssertBytes([S]);
  210. Stream.Position:=0;
  211. Stream.ReadBufferData(D);
  212. AssertEquals('Written data read correctly',S,D);
  213. end;
  214. procedure TTestStream.TestInt8;
  215. Var
  216. S,D : Int8;
  217. begin
  218. D:=0;
  219. S:=-13;
  220. AssertEquals('Bytes written',1,Stream.WriteData(S));
  221. AssertBytes([S]);
  222. Stream.Position:=0;
  223. AssertEquals('Bytes read',1,Stream.ReadData(D));
  224. AssertEquals('Written data read correctly',S,D);
  225. end;
  226. procedure TTestStream.TestInt8Buffer;
  227. Var
  228. S,D : Int8;
  229. begin
  230. D:=0;
  231. S:=-13;
  232. Stream.WriteBufferData(S);
  233. AssertBytes([S]);
  234. Stream.Position:=0;
  235. Stream.ReadBufferData(D);
  236. AssertEquals('Written data read correctly',S,D);
  237. end;
  238. procedure TTestStream.TestUInt8;
  239. Var
  240. S,D : UInt8;
  241. begin
  242. D:=0;
  243. S:=139;
  244. AssertEquals('Bytes written',1,Stream.WriteData(S));
  245. AssertBytes([S]);
  246. Stream.Position:=0;
  247. AssertEquals('Bytes read',1,Stream.ReadData(D));
  248. AssertEquals('Written data read correctly',S,D);
  249. end;
  250. procedure TTestStream.TestUInt8Buffer;
  251. Var
  252. S,D : UInt8;
  253. begin
  254. D:=0;
  255. S:=139;
  256. Stream.WriteBufferData(S);
  257. AssertBytes([S]);
  258. Stream.Position:=0;
  259. Stream.ReadBufferData(D);
  260. AssertEquals('Written data read correctly',S,D);
  261. end;
  262. procedure TTestStream.TestSmallint;
  263. Var
  264. S,D : SmallInt;
  265. begin
  266. D:=0;
  267. S:=127*256+13;
  268. AssertEquals('Bytes written',2,Stream.WriteData(S));
  269. AssertBytes([13,127]);
  270. Stream.Position:=0;
  271. AssertEquals('Bytes read',2,Stream.ReadData(D));
  272. AssertEquals('Written data read correctly',S,D);
  273. end;
  274. procedure TTestStream.TestSmallintBuffer;
  275. Var
  276. S,D : SmallInt;
  277. begin
  278. D:=0;
  279. S:=127*256+13;
  280. Stream.WriteBufferData(S);
  281. AssertBytes([13,127]);
  282. Stream.Position:=0;
  283. Stream.ReadBufferData(D);
  284. AssertEquals('Written data read correctly',S,D);
  285. end;
  286. procedure TTestStream.TestInt16;
  287. Var
  288. S,D : Int16;
  289. begin
  290. D:=0;
  291. S:=127*256+13;
  292. AssertEquals('Bytes written',2,Stream.WriteData(S));
  293. AssertBytes([13,127]);
  294. Stream.Position:=0;
  295. AssertEquals('Bytes read',2,Stream.ReadData(D));
  296. AssertEquals('Written data read correctly',S,D);
  297. end;
  298. procedure TTestStream.TestInt16Neg;
  299. Var
  300. S,D : Int16;
  301. begin
  302. D:=0;
  303. S:=-4086; // $F00A;
  304. AssertEquals('Bytes written',2,Stream.WriteData(S));
  305. AssertBytes([$0A,$F0]);
  306. Stream.Position:=0;
  307. AssertEquals('Bytes read',2,Stream.ReadData(D));
  308. AssertEquals('Written data read correctly',S,D);
  309. end;
  310. procedure TTestStream.TestInt16Buffer;
  311. Var
  312. S,D : Int16;
  313. begin
  314. D:=0;
  315. S:=127*256+13;
  316. Stream.WriteBufferData(S);
  317. AssertBytes([13,127]);
  318. Stream.Position:=0;
  319. Stream.ReadBufferData(D);
  320. AssertEquals('Written data read correctly',S,D);
  321. end;
  322. procedure TTestStream.TestUInt16;
  323. Var
  324. S,D : UInt16;
  325. begin
  326. D:=0;
  327. S:=$F00A; // 61450
  328. AssertEquals('Bytes written',2,Stream.WriteData(S));
  329. AssertBytes([$0A,$F0]);
  330. Stream.Position:=0;
  331. AssertEquals('Bytes read',2,Stream.ReadData(D));
  332. AssertEquals('Written data read correctly',S,D);
  333. end;
  334. procedure TTestStream.TestUInt16Buffer;
  335. Var
  336. S,D : UInt16;
  337. begin
  338. D:=0;
  339. S:=$F00A;
  340. Stream.WriteBufferData(S);
  341. AssertBytes([$0A,$F0]);
  342. Stream.Position:=0;
  343. Stream.ReadBufferData(D);
  344. AssertEquals('Written data read correctly',S,D);
  345. end;
  346. procedure TTestStream.TestInt32;
  347. Var
  348. S,D : Int32;
  349. begin
  350. D:=0;
  351. // 2131560201
  352. S:=(127 shl 24) + (13 shl 16) + (7 shl 8) + 9;
  353. AssertEquals('Bytes written',4,Stream.WriteData(S));
  354. AssertBytes([9,7,13,127]);
  355. Stream.Position:=0;
  356. AssertEquals('Bytes read',4,Stream.ReadData(D));
  357. AssertEquals('Written data read correctly',S,D);
  358. end;
  359. procedure TTestStream.TestInt32Neg;
  360. Var
  361. S,D : Int32;
  362. begin
  363. D:=0;
  364. // -2146629879
  365. S:=Int32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  366. AssertEquals('Bytes written',4,Stream.WriteData(S));
  367. AssertBytes([9,7,13,128]);
  368. Stream.Position:=0;
  369. AssertEquals('Bytes read',4,Stream.ReadData(D));
  370. AssertEquals('Written data read correctly',S,D);
  371. end;
  372. procedure TTestStream.TestInt32Buffer;
  373. Var
  374. S,D : Int32;
  375. begin
  376. D:=0;
  377. // 2131560201
  378. S:=(127 shl 24) + (13 shl 16) + (7 shl 8) + 9;
  379. Stream.WriteBufferData(S);
  380. AssertBytes([9,7,13,127]);
  381. Stream.Position:=0;
  382. Stream.ReadBufferData(D);
  383. AssertEquals('Written data read correctly',S,D);
  384. end;
  385. procedure TTestStream.TestUInt32;
  386. Var
  387. S,D : UInt32;
  388. begin
  389. D:=0;
  390. // 2148337417
  391. S:=UINT32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  392. AssertEquals('Bytes written',4,Stream.WriteData(S));
  393. AssertBytes([9,7,13,128]);
  394. Stream.Position:=0;
  395. AssertEquals('Bytes read',4,Stream.ReadData(D));
  396. AssertEquals('Written data read correctly',S,D);
  397. end;
  398. procedure TTestStream.TestUInt32Buffer;
  399. Var
  400. S,D : UInt32;
  401. begin
  402. D:=0;
  403. // 2148337417
  404. S:=UINT32((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  405. Stream.WriteBufferData(S);
  406. AssertBytes([9,7,13,128]);
  407. Stream.Position:=0;
  408. Stream.ReadBufferData(D);
  409. AssertEquals('Written data read correctly',S,D);
  410. end;
  411. procedure TTestStream.TestInt64;
  412. Var
  413. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  414. begin
  415. D:=0;
  416. // 9154981354848060679
  417. // Javascript only has 52 bits : 7737333974279
  418. S:={$IFNDEF ECMASCRIPT} (127 shl 24) + (13 shl 16) {$endif}+ (7 shl 8) + 9;
  419. S:=(S shl 32) + ((125 shl 24) + (11 shl 16) + (5 shl 8) + 7);
  420. AssertEquals('Bytes written',8,Stream.WriteData(S));
  421. {$ifndef ECMASCRIPT}
  422. AssertBytes([7,5,11,125,9,7,{$IFNDEF ECMASCRIPT} 13,127 {$ELSE} 0,0 {$ENDIF}]);
  423. {$ENDIF}
  424. Stream.Position:=0;
  425. AssertEquals('Bytes read',8,Stream.ReadData(D));
  426. AssertEquals('Written data read correctly',S,D);
  427. end;
  428. procedure TTestStream.TestInt64Neg;
  429. Var
  430. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  431. begin
  432. D:=0;
  433. {$IFNDEF ECMASCRIPT}
  434. // -9219705124773231353
  435. S:=Int64((128 shl 24) + (13 shl 16) + (7 shl 8) + 9);
  436. S:=Int64((S shl 32) + ((128 shl 24) + (11 shl 16) + (5 shl 8) + 7));
  437. AssertEquals('Bytes written',8,Stream.WriteData(S));
  438. AssertBytes([7,5,11,128,9,7,13,128]);
  439. Stream.Position:=0;
  440. AssertEquals('Bytes read',8,Stream.ReadData(D));
  441. AssertEquals('Written data read correctly',S,D);
  442. {$ELSE}
  443. S:=-9000199254740991;
  444. AssertEquals('Bytes written',8,Stream.WriteData(S));
  445. Stream.Position:=0;
  446. AssertEquals('Bytes read',8,Stream.ReadData(D));
  447. AssertEquals('Written data read correctly',S,D);
  448. {$ENDIF}
  449. end;
  450. procedure TTestStream.TestInt64Buffer;
  451. Var
  452. S,D : {$IFDEF ECMASCRIPT}NativeLargeInt{$else}Int64{$endif};
  453. begin
  454. D:=0;
  455. // 9154981354848060679
  456. // 7737333974279 for ECMAScript
  457. S:={$IFNDEF ECMASCRIPT} (127 shl 24) + (13 shl 16) {$endif}+ (7 shl 8) + 9;
  458. S:=(S shl 32) + ((125 shl 24) + (11 shl 16) + (5 shl 8) + 7);
  459. Stream.WriteBufferData(S);
  460. {$IFNDEF ECMASCRIPT}
  461. AssertBytes([7,5,11,125,9,7,13,127]);
  462. {$ENDIF}
  463. Stream.Position:=0;
  464. Stream.ReadBufferData(D);
  465. AssertEquals('Written data read correctly',S,D);
  466. end;
  467. procedure TTestStream.TestBoolean;
  468. Var
  469. S,D : Boolean;
  470. begin
  471. D:=False;
  472. // 9154981354848060679
  473. S:=True;
  474. AssertEquals('Bytes written',1,Stream.WriteData(S));
  475. AssertBytes([1]);
  476. Stream.Position:=0;
  477. AssertEquals('Bytes read',1,Stream.ReadData(D));
  478. AssertEquals('Written data read correctly',S,D);
  479. end;
  480. procedure TTestStream.TestBooleanBuffer;
  481. Var
  482. S,D : Boolean;
  483. begin
  484. D:=False;
  485. // 9154981354848060679
  486. S:=True;
  487. Stream.WriteBufferData(S);
  488. AssertBytes([1]);
  489. Stream.Position:=0;
  490. Stream.ReadBufferData(D);
  491. AssertEquals('Written data read correctly',S,D);
  492. end;
  493. {$IFNDEF ECMASCRIPT}
  494. procedure TTestStream.TestAnsiChar;
  495. Var
  496. S,D : AnsiChar;
  497. begin
  498. D:=#0;
  499. S:='A';
  500. AssertEquals('Bytes written',1,Stream.WriteData(S));
  501. AssertBytes([Ord(S)]);
  502. Stream.Position:=0;
  503. AssertEquals('Bytes read',1,Stream.ReadData(D));
  504. AssertEquals('Written data read correctly',S,D);
  505. end;
  506. procedure TTestStream.TestAnsicharBuffer;
  507. Var
  508. S,D : AnsiChar;
  509. begin
  510. D:=#0;
  511. S:='A';
  512. Stream.WriteBufferData(S);
  513. AssertBytes([Ord(S)]);
  514. Stream.Position:=0;
  515. Stream.ReadBufferData(D);
  516. AssertEquals('Written data read correctly',S,D);
  517. end;
  518. {$endif}
  519. procedure TTestStream.TestWideChar;
  520. Var
  521. S,D : WideChar;
  522. begin
  523. D:=#0;
  524. S:='A';
  525. AssertEquals('Bytes written',2,Stream.WriteData(S));
  526. AssertBytes([Ord(S),0]);
  527. Stream.Position:=0;
  528. AssertEquals('Bytes read',2,Stream.ReadData(D));
  529. AssertEquals('Written data read correctly',Ord(S),Ord(D));
  530. end;
  531. procedure TTestStream.TestWideCharBuffer;
  532. Var
  533. S,D : WideChar;
  534. begin
  535. D:=#0;
  536. S:='A';
  537. Stream.WriteBufferData(S);
  538. AssertBytes([Ord(S),0]);
  539. Stream.Position:=0;
  540. Stream.ReadBufferData(D);
  541. AssertEquals('Written data read correctly',Ord(S),Ord(D));
  542. end;
  543. {$ifndef ECMASCRIPT}
  544. procedure TTestStream.TestSingle;
  545. Var
  546. S,D : Single;
  547. B : TBytes = Nil;
  548. begin
  549. D:=0;
  550. S:=123.45;
  551. AssertEquals('Bytes written',4,Stream.WriteData(S));
  552. Setlength(B,4);
  553. With TSingleRec(S) do
  554. begin
  555. B[0]:=Bytes[0];
  556. B[1]:=Bytes[1];
  557. B[2]:=Bytes[2];
  558. B[3]:=Bytes[3];
  559. end;
  560. AssertBytes(B);
  561. Stream.Position:=0;
  562. AssertEquals('Bytes read',4,Stream.ReadData(D));
  563. AssertEquals('Written data read correctly',S,D,0.0001);
  564. end;
  565. procedure TTestStream.TestSingleBuffer;
  566. Var
  567. S,D : Single;
  568. B : TBytes = Nil;
  569. begin
  570. D:=0;
  571. S:=123.45;
  572. Stream.WriteBufferData(S);
  573. Setlength(B,4);
  574. With TSingleRec(S) do
  575. begin
  576. B[0]:=Bytes[0];
  577. B[1]:=Bytes[1];
  578. B[2]:=Bytes[2];
  579. B[3]:=Bytes[3];
  580. end;
  581. AssertBytes(B);
  582. Stream.Position:=0;
  583. Stream.ReadBufferData(D);
  584. AssertEquals('Written data read correctly',S,D,0.0001);
  585. end;
  586. {$endif}
  587. procedure TTestStream.TestDouble;
  588. Var
  589. S,D : Double;
  590. B : TBytes;
  591. {$ifndef ECMASCRIPT}
  592. i : integer;
  593. {$endif}
  594. begin
  595. B:=Default(TBytes);
  596. D:=0;
  597. S:=123.45;
  598. AssertEquals('Bytes written',8,Stream.WriteData(S));
  599. Setlength(B,8);
  600. {$ifndef ECMASCRIPT}
  601. With TDoubleRec(S) do
  602. For I:=0 to 7 do
  603. B[I]:=Bytes[I];
  604. AssertBytes(B);
  605. {$endif}
  606. Stream.Position:=0;
  607. AssertEquals('Bytes read',8,Stream.ReadData(D));
  608. AssertEquals('Written data read correctly',S,D,0.0001);
  609. end;
  610. procedure TTestStream.TestDoubleBuffer;
  611. Var
  612. S,D : Double;
  613. B : TBytes;
  614. {$ifndef ECMASCRIPT}
  615. i : integer;
  616. {$endif}
  617. begin
  618. B:=Default(TBytes);
  619. D:=0;
  620. S:=123.45;
  621. Stream.WriteBufferData(S);
  622. Setlength(B,8);
  623. {$ifndef ECMASCRIPT}
  624. With TDoubleRec(S) do
  625. For I:=0 to 7 do
  626. B[I]:=Bytes[I];
  627. AssertBytes(B);
  628. {$endif}
  629. Stream.Position:=0;
  630. Stream.ReadBufferData(D);
  631. AssertEquals('Written data read correctly',S,D,0.0001);
  632. end;
  633. {$ifndef ECMASCRIPT}
  634. procedure TTestStream.TestExtended;
  635. Var
  636. S,D : Extended;
  637. B : TBytes = Nil;
  638. i : integer;
  639. begin
  640. D:=0;
  641. S:=123.45;
  642. AssertEquals('Bytes written',10,Stream.WriteData(S));
  643. Setlength(B,10);
  644. With TExtended80Rec(S) do
  645. For I:=0 to 9 do
  646. B[I]:=Bytes[I];
  647. AssertBytes(B);
  648. Stream.Position:=0;
  649. AssertEquals('Bytes read',10,Stream.ReadData(D));
  650. AssertEquals('Written data read correctly',S,D,0.0001);
  651. end;
  652. procedure TTestStream.TestExtendedBuffer;
  653. Var
  654. S,D : Extended;
  655. B : TBytes = Nil;
  656. i : integer;
  657. begin
  658. D:=0;
  659. S:=123.45;
  660. Stream.WriteBufferData(S);
  661. Setlength(B,10);
  662. With TExtended80Rec(S) do
  663. For I:=0 to 9 do
  664. B[I]:=Bytes[I];
  665. AssertBytes(B);
  666. Stream.Position:=0;
  667. Stream.ReadBufferData(D);
  668. AssertEquals('Written data read correctly',S,D,0.0001);
  669. end;
  670. {$endif}
  671. procedure TTestStream.SetUp;
  672. Var
  673. B : TBytes;
  674. begin
  675. B:=Default(TBytes);
  676. SetLength(B,0);
  677. FStream:=TBytesStream.Create(B);
  678. end;
  679. procedure TTestStream.TearDown;
  680. begin
  681. FreeAndNil(FStream);
  682. end;
  683. procedure TTestStream.AssertBytes(B: array of Byte; aMessage: string = ''; ObserveEndian : Boolean = True);
  684. Var
  685. L,I,E: integer;
  686. A : Byte;
  687. SB : TBytes;
  688. begin
  689. if AMessage<>'' then
  690. aMessage:=aMessage+': ';
  691. AssertEquals(aMessage+'Length bytes equals',Length(B),FStream.Size);
  692. SB:=TBytesStream(Stream).Bytes;
  693. L:=Length(B);
  694. for I:=0 to L-1 do
  695. begin
  696. E:=Byte(B[i] and $FF);
  697. if ObserveEndian and (Stream.Endian=Tendian.Big) then
  698. A:=SB[L-1-i]
  699. else
  700. A:=SB[i];
  701. AssertEquals(aMessage+'Byte['+IntToStr(I)+'] equals',E,A);
  702. end;
  703. end;
  704. {
  705. procedure TTestStream.AssertBytes(B: TBytes; aMessage : string = '');
  706. Var
  707. I : integer;
  708. SB : TBytes;
  709. begin
  710. if AMessage<>'' then
  711. aMessage:=aMessage+': ';
  712. AssertEquals(aMessage+'Length bytes equals',Length(B),FStream.Size);
  713. SB:=TBytesStream(Stream).Bytes;
  714. for I:=0 to Length(B)-1 do
  715. AssertEquals(aMessage+'Byte['+IntToStr(I)+'] equals',B[i],SB[i]);
  716. end;
  717. }
  718. function TTestStream.CreateBytes(aCount: integer): TBytes;
  719. Var
  720. I : Integer;
  721. begin
  722. Result:=Nil;
  723. SetLength(Result,aCount);
  724. For I:=0 to aCount-1 do
  725. Result[I]:=I+1;
  726. end;
  727. initialization
  728. RegisterTests([TTestStream,TTestBigendianStream,TTestStringStream]);
  729. end.