BinaryReaderTest.cs 27 KB

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