BinaryReaderTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. // BinaryReaderTest.cs - NUnit Test Cases for the SystemIO.BinaryReader class
  2. //
  3. // Eduardo Garcia Cebollero ([email protected])
  4. //
  5. // (C) Eduardo Garcia Cebollero.
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. //
  8. using NUnit.Framework;
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. namespace MonoTests.System.IO
  13. {
  14. public class BinaryReaderTest : TestCase
  15. {
  16. string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
  17. private string _codeFileName;
  18. public BinaryReaderTest()
  19. {
  20. if (Directory.Exists (TempFolder))
  21. Directory.Delete (TempFolder, true);
  22. Directory.CreateDirectory (TempFolder);
  23. _codeFileName = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
  24. }
  25. ~BinaryReaderTest ()
  26. {
  27. if (Directory.Exists (TempFolder))
  28. Directory.Delete (TempFolder, true);
  29. }
  30. override protected void SetUp() {
  31. if (!Directory.Exists (TempFolder))
  32. Directory.CreateDirectory (TempFolder);
  33. if (!File.Exists (_codeFileName))
  34. File.Create (_codeFileName).Close ();
  35. }
  36. public void TestCtor1()
  37. {
  38. {
  39. bool errorThrown = false;
  40. try {
  41. BinaryReader r = new BinaryReader ((Stream) null);
  42. } catch (ArgumentNullException) {
  43. errorThrown = true;
  44. }
  45. Assert ("#01 null string error not thrown", errorThrown);
  46. }
  47. {
  48. bool errorThrown = false;
  49. FileStream f = new FileStream (_codeFileName, FileMode.Open, FileAccess.Write);
  50. try {
  51. BinaryReader r = new BinaryReader (f);
  52. r.Close ();
  53. } catch (ArgumentException) {
  54. errorThrown = true;
  55. }
  56. f.Close ();
  57. Assert ("#02 no read error not thrown", errorThrown);
  58. }
  59. {
  60. FileStream f = new FileStream (_codeFileName,
  61. FileMode.Open,
  62. FileAccess.Read);
  63. BinaryReader r = new BinaryReader (f);
  64. AssertNotNull ("#03 no binary reader created", r);
  65. r.Close ();
  66. f.Close ();
  67. }
  68. }
  69. public void TestCtor2 ()
  70. {
  71. {
  72. bool errorThrown = false;
  73. try {
  74. BinaryReader r = new BinaryReader ((Stream) null, Encoding.ASCII);
  75. } catch (ArgumentNullException) {
  76. errorThrown = true;
  77. } catch (Exception e) {
  78. Fail ("#04 Incorrect exception thrown: " + e.ToString ());
  79. }
  80. Assert ("#05 null stream error not thrown", errorThrown);
  81. }
  82. {
  83. bool errorThrown = false;
  84. try {
  85. BinaryReader r = new BinaryReader ((Stream) null, Encoding.Unicode);
  86. } catch (ArgumentNullException) {
  87. errorThrown = true;
  88. } catch (Exception e) {
  89. Fail ("#06 Incorrect exception thrown: " + e.ToString ());
  90. }
  91. Assert("#07 null stream error not thrown", errorThrown);
  92. }
  93. {
  94. bool errorThrown = false;
  95. try {
  96. BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF7);
  97. } catch (ArgumentNullException) {
  98. errorThrown = true;
  99. } catch (Exception e) {
  100. Fail ("#08 Incorrect exception thrown: " + e.ToString ());
  101. }
  102. Assert ("#09 null stream error not thrown", errorThrown);
  103. }
  104. {
  105. bool errorThrown = false;
  106. try {
  107. BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF8);
  108. } catch (ArgumentNullException) {
  109. errorThrown = true;
  110. } catch (Exception e) {
  111. Fail ("#0A Incorrect exception thrown: " + e.ToString ());
  112. }
  113. Assert ("#0B null stream error not thrown", errorThrown);
  114. }
  115. }
  116. public void TestCtor3 ()
  117. {
  118. bool errorThrown = false;
  119. byte [] b = new byte [30];
  120. MemoryStream m = new MemoryStream (b);
  121. try {
  122. BinaryReader r = new BinaryReader (m, (Encoding) null);
  123. } catch (ArgumentNullException) {
  124. errorThrown = true;
  125. } catch(Exception e) {
  126. Fail ("#0C Incorrect Exception thrown: " + e.ToString ());
  127. }
  128. Assert ("#0D No exception trown: ", errorThrown);
  129. }
  130. //TODO: (TestCtor*) Verify the Use of a wrong Stream
  131. //TODO: (TestClose*) Verify the Close Method
  132. public void TestClose1 ()
  133. {
  134. {
  135. byte [] b = new byte [30];
  136. MemoryStream m = new MemoryStream (b);
  137. try {
  138. BinaryReader r = new BinaryReader (m);
  139. r.Close ();
  140. } catch (Exception e) {
  141. Fail ("#0E Unhandled Exception: "+ e.ToString ());
  142. }
  143. }
  144. }
  145. //TODO: (TestRead*) Verify Read Method
  146. public void TestReadBoolean ()
  147. {
  148. bool [] a = {true, true, false};
  149. byte [] arr_a = new byte [3];
  150. int i = 0;
  151. foreach (bool a1 in a) {
  152. arr_a [i] = Convert.ToByte (a1);
  153. i++;
  154. }
  155. bool b;
  156. MemoryStream m = new MemoryStream (arr_a);
  157. try {
  158. BinaryReader r = new BinaryReader (m);
  159. b = r.ReadBoolean ();
  160. AssertEquals ("#11 No well readed boolean: ", a [0], b);
  161. } catch (Exception e) {
  162. Fail ("#12 Unexpected exception thrown: " + e.ToString ());
  163. }
  164. }
  165. public void TestReadByte ()
  166. {
  167. byte [] a = {0, 2, 3, 1, 5, 2};
  168. byte b;
  169. MemoryStream m = new MemoryStream (a);
  170. try {
  171. BinaryReader r = new BinaryReader (m);
  172. b = r.ReadByte ();
  173. AssertEquals ("#13 No well readed byte: ", a [0], b);
  174. } catch (Exception e) {
  175. Fail ("#14 Unexpected Exception thrown: " + e.ToString ());
  176. }
  177. }
  178. public void TestReadChar()
  179. {
  180. char [] a = {'a','b','c','d','e'};
  181. byte [] arr_a = new byte [5];
  182. int i = 0;
  183. char c;
  184. foreach (char a1 in a) {
  185. arr_a [i] = Convert.ToByte (a1);
  186. i++;
  187. }
  188. MemoryStream m = new MemoryStream (arr_a);
  189. BinaryReader r = new BinaryReader (m);
  190. try {
  191. c = r.ReadChar ();
  192. AssertEquals ("#15 No well readed Char", a [0], c);
  193. } catch (Exception e) {
  194. Fail ("#16 Unexpeted Exception: " + e.ToString ());
  195. }
  196. }
  197. public void TestReadInt32 () //Uses BinaryWriter!!
  198. {
  199. int [] arr_int = {1,10,200,3000,40000,500000,6000000};
  200. byte [] arr_byte = new byte [28]; //Sizeof arr_int * 4
  201. int [] arr_int2 = new int [7];
  202. int i;
  203. MemoryStream mem_stream = new MemoryStream (arr_byte);
  204. BinaryWriter bin_writer = new BinaryWriter (mem_stream);
  205. foreach (int elem in arr_int) {
  206. bin_writer.Write(elem);
  207. }
  208. mem_stream.Seek(0,SeekOrigin.Begin);
  209. BinaryReader bin_reader = new BinaryReader (mem_stream);
  210. bin_reader.BaseStream.Seek(0,SeekOrigin.Begin);
  211. for (i=0;i<7;i++) {
  212. try{
  213. arr_int2 [i] = bin_reader.ReadInt32();
  214. AssertEquals("#2E Wrong Readed Int32 in iteration "+ i,arr_int [i],arr_int2 [i]);
  215. } catch (IOException e) {
  216. Fail("#2F Unexpected IO Exception" + e.ToString());
  217. }
  218. }
  219. }
  220. //-TODO: (TestRead[Type]*) Verify the ReadBoolean, ReadByte ....
  221. // ReadBoolean, ReadByte, ReadChar, ReadInt32 Done
  222. //TODO: (TestFillBuffer*) Verify the FillBuffer Method
  223. public void TestPeekChar ()
  224. {
  225. char char1, char2;
  226. char [] b = {'A', 'B', 'C'};
  227. byte [] arr_b = new byte [3];
  228. int i = 0;
  229. foreach (char b1 in b) {
  230. arr_b [i] = Convert.ToByte (b1);
  231. i++;
  232. }
  233. MemoryStream m = new MemoryStream (arr_b);
  234. BinaryReader r = new BinaryReader (m);
  235. try {
  236. char1 = (char) r.PeekChar ();
  237. char2 = (char) r.PeekChar ();
  238. AssertEquals ("#20 the stream pointer have been altered in peek", char1, char2);
  239. } catch (Exception e) {
  240. Fail ("#21 Unexpected exception thrown: " + e.ToString ());
  241. }
  242. }
  243. public void TestBaseSeek1 ()
  244. {
  245. char char1, char2;
  246. char [] b = {'A','B','C','D','E','F'};
  247. byte [] arr_b = new byte[6];
  248. int i = 0;
  249. foreach (char b1 in b) {
  250. arr_b [i] = Convert.ToByte (b1);
  251. i++;
  252. }
  253. MemoryStream m = new MemoryStream (arr_b);
  254. BinaryReader r = new BinaryReader (m);
  255. try {
  256. char1 = (char) r.PeekChar ();
  257. r.BaseStream.Seek (0,SeekOrigin.Current);
  258. char2 = (char) r.PeekChar ();
  259. AssertEquals ("#22 the stream Has been altered in Seek", char1, char2);
  260. } catch (Exception e) {
  261. Fail ("#23 Unexpected exception thrown: " + e.ToString ());
  262. }
  263. }
  264. public void TestBaseSeek2 ()
  265. {
  266. char char1, char2;
  267. char [] b = {'A','B','C','D','E','F'};
  268. byte [] arr_b = new byte[6];
  269. int i = 0;
  270. foreach (char b1 in b) {
  271. arr_b [i] = Convert.ToByte (b1);
  272. i++;
  273. }
  274. MemoryStream m = new MemoryStream (arr_b);
  275. BinaryReader r = new BinaryReader (m);
  276. try {
  277. char1 = (char) r.PeekChar ();
  278. r.BaseStream.Seek (3,SeekOrigin.Current);
  279. r.BaseStream.Seek (-3,SeekOrigin.Current);
  280. char2 = (char) r.PeekChar ();
  281. AssertEquals ("#24 the stream Has been altered in Seek", char1, char2);
  282. } catch (Exception e) {
  283. Fail ("#25 Unexpected exception thrown: " + e.ToString ());
  284. }
  285. }
  286. public void TestInterleavedSeek1 ()
  287. {
  288. byte int1;
  289. byte [] arr_byte = {0,1,2,3,4,5,6,7,8,9};
  290. MemoryStream m = new MemoryStream (arr_byte);
  291. BinaryReader r = new BinaryReader (m);
  292. {
  293. try {
  294. int1 = r.ReadByte();
  295. AssertEquals("#26 Not well readed Byte", int1, arr_byte[0]);
  296. } catch (Exception e) {
  297. Fail ("#27 Unexpected exception thrown: " + e.ToString ());
  298. }
  299. }
  300. {
  301. try {
  302. r.BaseStream.Seek(-1,SeekOrigin.End);
  303. int1 = r.ReadByte();
  304. AssertEquals("#28 Not well readed Byte",int1,arr_byte[9]);
  305. } catch (Exception e) {
  306. Fail ("#29 Unexpected exception thrown: " + e.ToString ());
  307. }
  308. }
  309. {
  310. try {
  311. r.BaseStream.Seek(3,SeekOrigin.Begin);
  312. int1 = r.ReadByte();
  313. AssertEquals("#2A Not well readed Byte",int1,arr_byte[3]);
  314. } catch (Exception e) {
  315. Fail ("#2B Unexpected exception thrown: " + e.ToString ());
  316. }
  317. }
  318. {
  319. try {
  320. r.BaseStream.Seek(2,SeekOrigin.Current);
  321. int1 = r.ReadByte();
  322. AssertEquals("#2C Not well readed Int32",int1,arr_byte [6]);
  323. } catch (Exception e) {
  324. Fail ("#2D Unexpected exception thrown: " + e.ToString ());
  325. }
  326. }
  327. }
  328. /// <summary>
  329. /// Throws an exception if stream is null
  330. /// </summary>
  331. [Test]
  332. [ExpectedException(typeof(ArgumentNullException))]
  333. public void CtorNullExceptionStream ()
  334. {
  335. BinaryReader reader = new BinaryReader (null);
  336. Assertion.Fail();
  337. }
  338. /// <summary>
  339. /// Throws an exception if encoding is null
  340. /// </summary>
  341. [Test]
  342. [ExpectedException(typeof(ArgumentNullException))]
  343. public void CtorNullExceptionEncoding ()
  344. {
  345. MemoryStream stream = new MemoryStream (64);
  346. BinaryReader reader = new BinaryReader (stream, null);
  347. Assertion.Fail();
  348. }
  349. /// <summary>
  350. /// Throws an exception if stream does not support writing
  351. /// </summary>
  352. [Test]
  353. [ExpectedException(typeof(ArgumentException))]
  354. public void CtorArgumentExceptionCannotWrite ()
  355. {
  356. string path = TempFolder + "/.BinaryReaderTestFile.1";
  357. DeleteFile (path);
  358. FileStream file = new FileStream (path, FileMode.CreateNew, FileAccess.Read);
  359. BinaryReader breader = new BinaryReader (file);
  360. if (File.Exists (path))
  361. File.Delete (path);
  362. }
  363. /// <summary>
  364. /// Throws an exception if stream is already closed
  365. /// </summary>
  366. [Test]
  367. [ExpectedException(typeof(ArgumentException))]
  368. public void CtorArgumentExceptionClosedStream ()
  369. {
  370. string path = TempFolder + "/.BinaryReaderTestFile.2";
  371. DeleteFile (path);
  372. FileStream file = new FileStream (path, FileMode.CreateNew, FileAccess.Write);
  373. file.Close ();
  374. BinaryReader breader = new BinaryReader (file);
  375. if (File.Exists (path))
  376. File.Delete (path);
  377. }
  378. /// <summary>
  379. /// Throws an exception if stream is closed
  380. /// </summary>
  381. [Test]
  382. [ExpectedException(typeof(ArgumentException))]
  383. public void CtorArgumentExceptionEncoding ()
  384. {
  385. MemoryStream stream = new MemoryStream (64);
  386. stream.Close ();
  387. BinaryReader reader = new BinaryReader (stream, new ASCIIEncoding ());
  388. Assertion.Fail();
  389. }
  390. /// <summary>
  391. /// Tests read () method
  392. /// </summary>
  393. [Test]
  394. public void Read ()
  395. {
  396. byte [] bytes = new byte [] {0, 1, 2, 3};
  397. MemoryStream stream = new MemoryStream (bytes);
  398. BinaryReader reader = new BinaryReader (stream);
  399. Assertion.AssertEquals ("test#01", 0, reader.Read ());
  400. Assertion.AssertEquals ("test#02", 1, reader.Read ());
  401. Assertion.AssertEquals ("test#03", 2, reader.Read ());
  402. Assertion.AssertEquals ("test#04", 3, reader.Read ());
  403. Assertion.AssertEquals ("test#05", -1, reader.Read ());
  404. }
  405. [Test]
  406. public void PeakChar ()
  407. {
  408. byte [] bytes = new byte [] {0, 1, 2, 3};
  409. MemoryStream stream = new MemoryStream (bytes);
  410. BinaryReader reader = new BinaryReader (stream);
  411. Assertion.AssertEquals ("test#01", 0, reader.PeekChar ());
  412. Assertion.AssertEquals ("test#02", 0, reader.PeekChar ());
  413. Assertion.AssertEquals ("test#03", 0, reader.Read ());
  414. Assertion.AssertEquals ("test#03", 1, reader.Read ());
  415. Assertion.AssertEquals ("test#03", 2, reader.PeekChar ());
  416. }
  417. [Test]
  418. [ExpectedException(typeof(ObjectDisposedException))]
  419. public void CloseRead ()
  420. {
  421. byte [] bytes = new byte [] {0, 1, 2, 3};
  422. MemoryStream stream = new MemoryStream (bytes);
  423. BinaryReader reader = new BinaryReader (stream);
  424. reader.Close ();
  425. reader.Read ();
  426. }
  427. [Test]
  428. [ExpectedException(typeof(ObjectDisposedException))]
  429. public void ClosePeakChar ()
  430. {
  431. byte [] bytes = new byte [] {0, 1, 2, 3};
  432. MemoryStream stream = new MemoryStream (bytes);
  433. BinaryReader reader = new BinaryReader (stream);
  434. reader.Close ();
  435. reader.PeekChar ();
  436. }
  437. [Test]
  438. [ExpectedException(typeof(ObjectDisposedException))]
  439. public void CloseReadBytes ()
  440. {
  441. byte [] bytes = new byte [] {0, 1, 2, 3};
  442. MemoryStream stream = new MemoryStream (bytes);
  443. BinaryReader reader = new BinaryReader (stream);
  444. reader.Close ();
  445. reader.ReadBytes (1);
  446. }
  447. [Test]
  448. public void BaseStream ()
  449. {
  450. byte [] bytes = new byte [] {0, 1, 2, 3};
  451. MemoryStream stream = new MemoryStream (bytes);
  452. BinaryReader reader = new BinaryReader (stream);
  453. Assertion.AssertEquals ("test#01", 4, reader.BaseStream.Length);
  454. Assertion.AssertEquals ("test#02", true, reader.BaseStream.CanRead);
  455. reader.Close ();
  456. Assertion.AssertEquals ("test#03", null, reader.BaseStream);
  457. }
  458. /// <summary>
  459. /// Tests read (byte [], int, int) method
  460. /// </summary>
  461. [Test]
  462. public void ReadByteArray ()
  463. {
  464. byte [] bytes = new byte [] {0, 1, 2, 3, 4, 5};
  465. MemoryStream stream = new MemoryStream (bytes);
  466. BinaryReader reader = new BinaryReader (stream);
  467. bytes = new byte [3];
  468. reader.Read (bytes, 0, 3);
  469. Assertion.AssertEquals ("test#01", 0, bytes [0]);
  470. Assertion.AssertEquals ("test#02", 1, bytes [1]);
  471. Assertion.AssertEquals ("test#03", 2, bytes [2]);
  472. bytes = new byte [6];
  473. reader.Read (bytes, 3, 3);
  474. Assertion.AssertEquals ("test#04", 0, bytes [0]);
  475. Assertion.AssertEquals ("test#05", 0, bytes [1]);
  476. Assertion.AssertEquals ("test#06", 0, bytes [2]);
  477. Assertion.AssertEquals ("test#07", 3, bytes [3]);
  478. Assertion.AssertEquals ("test#08", 4, bytes [4]);
  479. Assertion.AssertEquals ("test#09", 5, bytes [5]);
  480. bytes = new byte [2];
  481. reader.Read (bytes, 0, 2);
  482. Assertion.AssertEquals ("test#10", 0, bytes [0]);
  483. Assertion.AssertEquals ("test#11", 0, bytes [1]);
  484. }
  485. /// <summary>
  486. /// Test Read (char [], int, int)
  487. /// </summary>
  488. [Test]
  489. public void ReadCharArray ()
  490. {
  491. MemoryStream stream = new MemoryStream (new byte [] {109, 111, 110, 111, 58, 58});
  492. BinaryReader reader = new BinaryReader (stream);
  493. char [] chars = new char [3];
  494. reader.Read (chars, 0, 3);
  495. Assertion.AssertEquals ("test#01", 'm', chars [0]);
  496. Assertion.AssertEquals ("test#02", 'o', chars [1]);
  497. Assertion.AssertEquals ("test#03", 'n', chars [2]);
  498. chars = new char [6];
  499. reader.Read (chars, 3, 3);
  500. Assertion.AssertEquals ("test#04", 0, chars [0]);
  501. Assertion.AssertEquals ("test#05", 0, chars [1]);
  502. Assertion.AssertEquals ("test#06", 0, chars [2]);
  503. Assertion.AssertEquals ("test#07", 'o', chars [3]);
  504. Assertion.AssertEquals ("test#08", ':', chars [4]);
  505. Assertion.AssertEquals ("test#09", ':', chars [5]);
  506. chars = new char [2];
  507. reader.Read (chars, 0, 2);
  508. Assertion.AssertEquals ("test#08", 0, chars [0]);
  509. Assertion.AssertEquals ("test#09", 0, chars [1]);
  510. }
  511. /// <summary>
  512. /// Test ReadBoolean () method.
  513. /// </summary>
  514. [Test]
  515. public void ReadBoolean ()
  516. {
  517. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  518. BinaryReader reader = new BinaryReader (stream);
  519. Assertion.AssertEquals ("test#01", false, reader.ReadBoolean ());
  520. Assertion.AssertEquals ("test#02", true, reader.ReadBoolean ());
  521. Assertion.AssertEquals ("test#03", true, reader.ReadBoolean ());
  522. Assertion.AssertEquals ("test#04", false, reader.ReadBoolean ());
  523. Assertion.AssertEquals ("test#05", true, reader.ReadBoolean ());
  524. }
  525. /// <summary>
  526. /// Test ReadBoolean () method exceptions.
  527. /// </summary>
  528. [Test]
  529. [ExpectedException(typeof(EndOfStreamException))]
  530. public void ReadBooleanException ()
  531. {
  532. MemoryStream stream = new MemoryStream (new byte [] {0, 1});
  533. BinaryReader reader = new BinaryReader (stream);
  534. reader.ReadBoolean ();
  535. reader.ReadBoolean ();
  536. reader.ReadBoolean ();
  537. Assertion.Fail ();
  538. }
  539. /// <summary>
  540. /// Test ReadByte () method.
  541. /// </summary>
  542. [Test]
  543. public void ReadByte ()
  544. {
  545. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  546. BinaryReader reader = new BinaryReader (stream);
  547. Assertion.AssertEquals ("test#01", 0, reader.ReadByte ());
  548. Assertion.AssertEquals ("test#02", 1, reader.ReadByte ());
  549. Assertion.AssertEquals ("test#03", 99, reader.ReadByte ());
  550. Assertion.AssertEquals ("test#04", 0, reader.ReadByte ());
  551. Assertion.AssertEquals ("test#05", 13, reader.ReadByte ());
  552. }
  553. /// <summary>
  554. /// Test ReadByte () method exceptions.
  555. /// </summary>
  556. [Test]
  557. [ExpectedException(typeof(EndOfStreamException))]
  558. public void ReadByteException ()
  559. {
  560. MemoryStream stream = new MemoryStream (new byte [] {0, 1});
  561. BinaryReader reader = new BinaryReader (stream);
  562. reader.ReadByte ();
  563. reader.ReadByte ();
  564. reader.ReadByte ();
  565. Assertion.Fail ();
  566. }
  567. /// <summary>
  568. /// Test ReadBytes (int) method.
  569. /// </summary>
  570. [Test]
  571. public void ReadBytes ()
  572. {
  573. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  574. BinaryReader reader = new BinaryReader (stream);
  575. byte [] bytes = reader.ReadBytes (2);
  576. Assertion.AssertEquals ("test#01", 0, bytes [0]);
  577. Assertion.AssertEquals ("test#02", 1, bytes [1]);
  578. bytes = reader.ReadBytes (2);
  579. Assertion.AssertEquals ("test#03", 99, bytes [0]);
  580. Assertion.AssertEquals ("test#04", 0, bytes [1]);
  581. bytes = reader.ReadBytes (2);
  582. Assertion.AssertEquals ("test#05", 13, bytes [0]);
  583. Assertion.AssertEquals ("test#06", 1, bytes.Length);
  584. }
  585. /// <summary>
  586. /// Test ReadBytes (int) method exception.
  587. /// </summary>
  588. [Test]
  589. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  590. public void ReadBytesException ()
  591. {
  592. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  593. BinaryReader reader = new BinaryReader (stream);
  594. reader.ReadBytes (-1);
  595. }
  596. /// <summary>
  597. /// Test ReadChar () method.
  598. /// </summary>
  599. [Test]
  600. public void ReadChar ()
  601. {
  602. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  603. BinaryReader reader = new BinaryReader (stream);
  604. Assertion.AssertEquals ("test#01", 0, reader.ReadChar ());
  605. Assertion.AssertEquals ("test#02", 1, reader.ReadChar ());
  606. Assertion.AssertEquals ("test#03", 99, reader.ReadChar ());
  607. Assertion.AssertEquals ("test#04", 0, reader.ReadChar ());
  608. Assertion.AssertEquals ("test#05", 13, reader.ReadChar ());
  609. }
  610. /// <summary>
  611. /// Test ReadChar () method exception.
  612. /// </summary>
  613. [Test]
  614. [ExpectedException(typeof(EndOfStreamException))]
  615. public void ReadCharException ()
  616. {
  617. MemoryStream stream = new MemoryStream (new byte [] {0, 1});
  618. BinaryReader reader = new BinaryReader (stream);
  619. reader.ReadChar ();
  620. reader.ReadChar ();
  621. reader.ReadChar ();
  622. Assertion.Fail ();
  623. }
  624. /// <summary>
  625. /// Test ReadChars (int) method.
  626. /// </summary>
  627. [Test]
  628. public void ReadChars ()
  629. {
  630. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  631. BinaryReader reader = new BinaryReader (stream);
  632. char [] chars = reader.ReadChars (2);
  633. Assertion.AssertEquals ("test#01", 0, chars [0]);
  634. Assertion.AssertEquals ("test#02", 1, chars [1]);
  635. chars = reader.ReadChars (2);
  636. Assertion.AssertEquals ("test#03", 99, chars [0]);
  637. Assertion.AssertEquals ("test#04", 0, chars [1]);
  638. chars = reader.ReadChars (2);
  639. Assertion.AssertEquals ("test#05", 13, chars [0]);
  640. Assertion.AssertEquals ("test#06", 1, chars.Length);
  641. }
  642. /// <summary>
  643. /// Test ReadChars (int value) exceptions. If value is negative exception is thrown
  644. /// </summary>
  645. [Test]
  646. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  647. public void ReadCharsException ()
  648. {
  649. MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
  650. BinaryReader reader = new BinaryReader (stream);
  651. reader.ReadChars (-1);
  652. }
  653. /// <summary>
  654. /// Test ReadDecimal () method.
  655. /// </summary>
  656. [Test]
  657. public void ReadDecimal ()
  658. {
  659. MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
  660. BinaryReader reader = new BinaryReader (stream);
  661. Assertion.AssertEquals ("test#01", -18295873486192640, reader.ReadDecimal ());
  662. }
  663. [Test]
  664. [ExpectedException(typeof(EndOfStreamException))]
  665. public void ReadDecimalException ()
  666. {
  667. MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0, 0});
  668. BinaryReader reader = new BinaryReader (stream);
  669. reader.ReadDecimal ();
  670. reader.ReadDecimal ();
  671. }
  672. [Test]
  673. public void ReadDouble ()
  674. {
  675. MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
  676. BinaryReader reader = new BinaryReader (stream);
  677. Assertion.AssertEquals ("test#01", 1.89131277973112E-307, reader.ReadDouble ());
  678. Assertion.AssertEquals ("test#02", 1.2024538023802E+111, reader.ReadDouble ());
  679. }
  680. [Test]
  681. [ExpectedException(typeof(EndOfStreamException))]
  682. public void ReadDoubleException ()
  683. {
  684. MemoryStream stream = new MemoryStream (new byte [] {0, 0, 0, 0, 0, 0, 65, 0, 0, 0, 0, 0, 0, 0, 0 ,87, 98, 0, 0, 0, 0});
  685. BinaryReader reader = new BinaryReader (stream);
  686. reader.ReadDouble ();
  687. reader.ReadDouble ();
  688. reader.ReadDouble ();
  689. }
  690. [Test]
  691. public void ReadInt16 ()
  692. {
  693. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
  694. BinaryReader reader = new BinaryReader (stream);
  695. Assertion.AssertEquals ("test#01", 321, reader.ReadInt16 ());
  696. Assertion.AssertEquals ("test#02", 11040, reader.ReadInt16 ());
  697. Assertion.AssertEquals ("test#03", 773, reader.ReadInt16 ());
  698. Assertion.AssertEquals ("test#04", 54, reader.ReadInt16 ());
  699. }
  700. [Test]
  701. [ExpectedException(typeof(EndOfStreamException))]
  702. public void ReadInt16Exception ()
  703. {
  704. MemoryStream stream = new MemoryStream (new byte [] {65, 1});
  705. BinaryReader reader = new BinaryReader (stream);
  706. reader.ReadInt16 ();
  707. reader.ReadInt16 ();
  708. }
  709. [Test]
  710. public void ReadInt32 ()
  711. {
  712. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
  713. BinaryReader reader = new BinaryReader (stream);
  714. Assertion.AssertEquals ("test#01", 723517761, reader.ReadInt32 ());
  715. Assertion.AssertEquals ("test#02", 3539717, reader.ReadInt32 ());
  716. }
  717. [Test]
  718. [ExpectedException(typeof(EndOfStreamException))]
  719. public void ReadInt32Exception ()
  720. {
  721. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
  722. BinaryReader reader = new BinaryReader (stream);
  723. reader.ReadInt32 ();
  724. reader.ReadInt32 ();
  725. }
  726. [Test]
  727. public void ReadInt64 ()
  728. {
  729. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
  730. BinaryReader reader = new BinaryReader (stream);
  731. Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadInt64 ());
  732. Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadInt64 ());
  733. }
  734. [Test]
  735. [ExpectedException(typeof(EndOfStreamException))]
  736. public void ReadInt64Exception ()
  737. {
  738. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
  739. BinaryReader reader = new BinaryReader (stream);
  740. reader.ReadInt64 ();
  741. reader.ReadInt64 ();
  742. reader.ReadInt64 ();
  743. }
  744. [Test]
  745. public void ReadSByte ()
  746. {
  747. MemoryStream stream = new MemoryStream (new byte [] {65, 200, 32});
  748. BinaryReader reader = new BinaryReader (stream);
  749. Assertion.AssertEquals ("test#01", 65, reader.ReadSByte ());
  750. Assertion.AssertEquals ("test#02", -56, reader.ReadSByte ());
  751. Assertion.AssertEquals ("test#03", 32, reader.ReadSByte ());
  752. }
  753. [Test]
  754. [ExpectedException(typeof(EndOfStreamException))]
  755. public void ReadSByteException ()
  756. {
  757. MemoryStream stream = new MemoryStream (new byte [] {65, 200});
  758. BinaryReader reader = new BinaryReader (stream);
  759. reader.ReadSByte ();
  760. reader.ReadSByte ();
  761. reader.ReadSByte ();
  762. }
  763. [Test]
  764. public void ReadSingle ()
  765. {
  766. MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
  767. BinaryReader reader = new BinaryReader (stream);
  768. Assertion.AssertEquals ("test#01", 7.183757E-41, reader.ReadSingle ());
  769. Assertion.AssertEquals ("test#01", 3.820471E-37, reader.ReadSingle ());
  770. }
  771. [Test]
  772. [ExpectedException(typeof(EndOfStreamException))]
  773. public void ReadSingleException ()
  774. {
  775. MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
  776. BinaryReader reader = new BinaryReader (stream);
  777. reader.ReadSingle ();
  778. reader.ReadSingle ();
  779. reader.ReadSingle ();
  780. }
  781. [Test]
  782. public void ReadString ()
  783. {
  784. MemoryStream stream = new MemoryStream (new byte [] {6,109, 111, 110, 111, 58, 58});
  785. BinaryReader reader = new BinaryReader (stream);
  786. Assertion.AssertEquals ("test#01", "mono::", reader.ReadString ());
  787. stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
  788. reader = new BinaryReader (stream);
  789. Assertion.AssertEquals ("test#02", "mo", reader.ReadString ());
  790. Assertion.AssertEquals ("test#03", "o::", reader.ReadString ());
  791. }
  792. [Test]
  793. [ExpectedException(typeof(EndOfStreamException))]
  794. public void ReadStringException ()
  795. {
  796. MemoryStream stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
  797. BinaryReader reader = new BinaryReader (stream);
  798. reader.ReadString ();
  799. reader.ReadString ();
  800. reader.ReadString ();
  801. }
  802. [Test]
  803. public void ReadUInt16 ()
  804. {
  805. MemoryStream stream = new MemoryStream (new byte [] {200, 200, 32, 43, 5, 3, 54, 0});
  806. BinaryReader reader = new BinaryReader (stream);
  807. Assertion.AssertEquals ("test#01", 51400, reader.ReadUInt16 ());
  808. Assertion.AssertEquals ("test#02", 11040, reader.ReadUInt16 ());
  809. Assertion.AssertEquals ("test#03", 773, reader.ReadUInt16 ());
  810. Assertion.AssertEquals ("test#04", 54, reader.ReadUInt16 ());
  811. }
  812. [Test]
  813. [ExpectedException(typeof(EndOfStreamException))]
  814. public void ReadUInt16Exception ()
  815. {
  816. MemoryStream stream = new MemoryStream (new byte [] {65, 1});
  817. BinaryReader reader = new BinaryReader (stream);
  818. reader.ReadUInt16 ();
  819. reader.ReadUInt16 ();
  820. }
  821. [Test]
  822. public void ReadUInt32 ()
  823. {
  824. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
  825. BinaryReader reader = new BinaryReader (stream);
  826. Assertion.AssertEquals ("test#01", 723517761, reader.ReadUInt32 ());
  827. Assertion.AssertEquals ("test#02", 3539717, reader.ReadUInt32 ());
  828. }
  829. [Test]
  830. [ExpectedException(typeof(EndOfStreamException))]
  831. public void ReadUInt32Exception ()
  832. {
  833. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
  834. BinaryReader reader = new BinaryReader (stream);
  835. reader.ReadUInt32 ();
  836. reader.ReadUInt32 ();
  837. }
  838. [Test]
  839. public void ReadUInt64 ()
  840. {
  841. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
  842. BinaryReader reader = new BinaryReader (stream);
  843. Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadUInt64 ());
  844. Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadUInt64 ());
  845. }
  846. [Test]
  847. [ExpectedException(typeof(EndOfStreamException))]
  848. public void ReadUInt64Exception ()
  849. {
  850. MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0, 34, 5, 7, 4, 23, 4, 76, 34, 76, 2, 6,45});
  851. BinaryReader reader = new BinaryReader (stream);
  852. reader.ReadUInt64 ();
  853. reader.ReadUInt64 ();
  854. reader.ReadUInt64 ();
  855. }
  856. private void DeleteFile (string path)
  857. {
  858. if (File.Exists (path))
  859. File.Delete (path);
  860. }
  861. }
  862. }