| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964 |
- // BinaryReaderTest.cs - NUnit Test Cases for the SystemIO.BinaryReader class
- //
- // Eduardo Garcia Cebollero ([email protected])
- //
- // (C) Eduardo Garcia Cebollero.
- // (C) Ximian, Inc. http://www.ximian.com
- //
- using NUnit.Framework;
- using System;
- using System.IO;
- using System.Text;
- namespace MonoTests.System.IO
- {
- public class BinaryReaderTest : TestCase
- {
- protected override void SetUp () { }
-
- private string _codeFileName = "resources" + Path.DirectorySeparatorChar + "AFile.txt";
-
- public void TestCtor1()
- {
- {
- bool errorThrown = false;
- try {
- BinaryReader r = new BinaryReader ((Stream) null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- }
- Assert ("#01 null string error not thrown", errorThrown);
- }
- {
- bool errorThrown = false;
- FileStream f = new FileStream (_codeFileName, FileMode.Open, FileAccess.Write);
- try {
- BinaryReader r = new BinaryReader (f);
- r.Close ();
- } catch (ArgumentException) {
- errorThrown = true;
- }
- f.Close ();
- Assert ("#02 no read error not thrown", errorThrown);
- }
- {
- FileStream f = new FileStream (_codeFileName,
- FileMode.Open,
- FileAccess.Read);
- BinaryReader r = new BinaryReader (f);
- AssertNotNull ("#03 no binary reader created", r);
- r.Close ();
- f.Close ();
- }
-
- }
- public void TestCtor2 ()
- {
- {
- bool errorThrown = false;
- try {
- BinaryReader r = new BinaryReader ((Stream) null, Encoding.ASCII);
- } catch (ArgumentNullException) {
- errorThrown = true;
- } catch (Exception e) {
- Fail ("#04 Incorrect exception thrown: " + e.ToString ());
- }
- Assert ("#05 null stream error not thrown", errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- BinaryReader r = new BinaryReader ((Stream) null, Encoding.Unicode);
- } catch (ArgumentNullException) {
- errorThrown = true;
- } catch (Exception e) {
- Fail ("#06 Incorrect exception thrown: " + e.ToString ());
- }
- Assert("#07 null stream error not thrown", errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF7);
- } catch (ArgumentNullException) {
- errorThrown = true;
- } catch (Exception e) {
- Fail ("#08 Incorrect exception thrown: " + e.ToString ());
- }
- Assert ("#09 null stream error not thrown", errorThrown);
- }
- {
- bool errorThrown = false;
- try {
- BinaryReader r = new BinaryReader ((Stream) null, Encoding.UTF8);
- } catch (ArgumentNullException) {
- errorThrown = true;
- } catch (Exception e) {
- Fail ("#0A Incorrect exception thrown: " + e.ToString ());
- }
- Assert ("#0B null stream error not thrown", errorThrown);
- }
- }
- public void TestCtor3 ()
- {
- bool errorThrown = false;
- byte [] b = new byte [30];
- MemoryStream m = new MemoryStream (b);
- try {
- BinaryReader r = new BinaryReader (m, (Encoding) null);
- } catch (ArgumentNullException) {
- errorThrown = true;
- } catch(Exception e) {
- Fail ("#0C Incorrect Exception thrown: " + e.ToString ());
- }
- Assert ("#0D No exception trown: ", errorThrown);
- }
- //TODO: (TestCtor*) Verify the Use of a wrong Stream
- //TODO: (TestClose*) Verify the Close Method
- public void TestClose1 ()
- {
- {
- byte [] b = new byte [30];
- MemoryStream m = new MemoryStream (b);
- try {
- BinaryReader r = new BinaryReader (m);
- r.Close ();
- } catch (Exception e) {
- Fail ("#0E Unhandled Exception: "+ e.ToString ());
- }
- }
- }
- //TODO: (TestRead*) Verify Read Method
- public void TestReadBoolean ()
- {
- bool [] a = {true, true, false};
- byte [] arr_a = new byte [3];
- int i = 0;
- foreach (bool a1 in a) {
- arr_a [i] = Convert.ToByte (a1);
- i++;
- }
-
- bool b;
- MemoryStream m = new MemoryStream (arr_a);
- try {
- BinaryReader r = new BinaryReader (m);
- b = r.ReadBoolean ();
- AssertEquals ("#11 No well readed boolean: ", a [0], b);
- } catch (Exception e) {
- Fail ("#12 Unexpected exception thrown: " + e.ToString ());
- }
- }
- public void TestReadByte ()
- {
- byte [] a = {0, 2, 3, 1, 5, 2};
- byte b;
- MemoryStream m = new MemoryStream (a);
- try {
- BinaryReader r = new BinaryReader (m);
- b = r.ReadByte ();
- AssertEquals ("#13 No well readed byte: ", a [0], b);
- } catch (Exception e) {
- Fail ("#14 Unexpected Exception thrown: " + e.ToString ());
- }
- }
- public void TestReadChar()
- {
- char [] a = {'a','b','c','d','e'};
- byte [] arr_a = new byte [5];
- int i = 0;
- char c;
- foreach (char a1 in a) {
- arr_a [i] = Convert.ToByte (a1);
- i++;
- }
- MemoryStream m = new MemoryStream (arr_a);
- BinaryReader r = new BinaryReader (m);
- try {
- c = r.ReadChar ();
- AssertEquals ("#15 No well readed Char", a [0], c);
- } catch (Exception e) {
- Fail ("#16 Unexpeted Exception: " + e.ToString ());
- }
- }
- public void TestReadInt32 () //Uses BinaryWriter!!
- {
- int [] arr_int = {1,10,200,3000,40000,500000,6000000};
- byte [] arr_byte = new byte [28]; //Sizeof arr_int * 4
- int [] arr_int2 = new int [7];
- int i;
-
- MemoryStream mem_stream = new MemoryStream (arr_byte);
- BinaryWriter bin_writer = new BinaryWriter (mem_stream);
-
- foreach (int elem in arr_int) {
- bin_writer.Write(elem);
- }
-
- mem_stream.Seek(0,SeekOrigin.Begin);
- BinaryReader bin_reader = new BinaryReader (mem_stream);
- bin_reader.BaseStream.Seek(0,SeekOrigin.Begin);
- for (i=0;i<7;i++) {
- try{
- arr_int2 [i] = bin_reader.ReadInt32();
- AssertEquals("#2E Wrong Readed Int32 in iteration "+ i,arr_int [i],arr_int2 [i]);
- } catch (IOException e) {
- Fail("#2F Unexpected IO Exception" + e.ToString());
- }
- }
- }
- //-TODO: (TestRead[Type]*) Verify the ReadBoolean, ReadByte ....
- // ReadBoolean, ReadByte, ReadChar, ReadInt32 Done
-
- //TODO: (TestFillBuffer*) Verify the FillBuffer Method
- public void TestPeekChar ()
- {
- char char1, char2;
- char [] b = {'A', 'B', 'C'};
- byte [] arr_b = new byte [3];
- int i = 0;
- foreach (char b1 in b) {
- arr_b [i] = Convert.ToByte (b1);
- i++;
- }
-
- MemoryStream m = new MemoryStream (arr_b);
- BinaryReader r = new BinaryReader (m);
- try {
- char1 = (char) r.PeekChar ();
- char2 = (char) r.PeekChar ();
- AssertEquals ("#20 the stream pointer have been altered in peek", char1, char2);
- } catch (Exception e) {
- Fail ("#21 Unexpected exception thrown: " + e.ToString ());
- }
- }
-
- public void TestBaseSeek1 ()
- {
- char char1, char2;
- char [] b = {'A','B','C','D','E','F'};
- byte [] arr_b = new byte[6];
- int i = 0;
- foreach (char b1 in b) {
- arr_b [i] = Convert.ToByte (b1);
- i++;
- }
- MemoryStream m = new MemoryStream (arr_b);
- BinaryReader r = new BinaryReader (m);
- try {
- char1 = (char) r.PeekChar ();
- r.BaseStream.Seek (0,SeekOrigin.Current);
- char2 = (char) r.PeekChar ();
- AssertEquals ("#22 the stream Has been altered in Seek", char1, char2);
- } catch (Exception e) {
- Fail ("#23 Unexpected exception thrown: " + e.ToString ());
- }
- }
- public void TestBaseSeek2 ()
- {
- char char1, char2;
- char [] b = {'A','B','C','D','E','F'};
- byte [] arr_b = new byte[6];
- int i = 0;
- foreach (char b1 in b) {
- arr_b [i] = Convert.ToByte (b1);
- i++;
- }
-
- MemoryStream m = new MemoryStream (arr_b);
- BinaryReader r = new BinaryReader (m);
- try {
- char1 = (char) r.PeekChar ();
- r.BaseStream.Seek (3,SeekOrigin.Current);
- r.BaseStream.Seek (-3,SeekOrigin.Current);
- char2 = (char) r.PeekChar ();
- AssertEquals ("#24 the stream Has been altered in Seek", char1, char2);
- } catch (Exception e) {
- Fail ("#25 Unexpected exception thrown: " + e.ToString ());
- }
- }
- public void TestInterleavedSeek1 ()
- {
- byte int1;
- byte [] arr_byte = {0,1,2,3,4,5,6,7,8,9};
-
- MemoryStream m = new MemoryStream (arr_byte);
- BinaryReader r = new BinaryReader (m);
- {
- try {
- int1 = r.ReadByte();
- AssertEquals("#26 Not well readed Byte", int1, arr_byte[0]);
- } catch (Exception e) {
- Fail ("#27 Unexpected exception thrown: " + e.ToString ());
- }
- }
- {
- try {
- r.BaseStream.Seek(-1,SeekOrigin.End);
- int1 = r.ReadByte();
- AssertEquals("#28 Not well readed Byte",int1,arr_byte[9]);
- } catch (Exception e) {
- Fail ("#29 Unexpected exception thrown: " + e.ToString ());
- }
- }
- {
- try {
- r.BaseStream.Seek(3,SeekOrigin.Begin);
- int1 = r.ReadByte();
- AssertEquals("#2A Not well readed Byte",int1,arr_byte[3]);
- } catch (Exception e) {
- Fail ("#2B Unexpected exception thrown: " + e.ToString ());
- }
- }
- {
- try {
- r.BaseStream.Seek(2,SeekOrigin.Current);
- int1 = r.ReadByte();
- AssertEquals("#2C Not well readed Int32",int1,arr_byte [6]);
- } catch (Exception e) {
- Fail ("#2D Unexpected exception thrown: " + e.ToString ());
- }
- }
- }
- /// <summary>
- /// Throws an exception if stream is null
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void CtorNullExceptionStream ()
- {
- BinaryReader reader = new BinaryReader (null);
- Assertion.Fail();
- }
- /// <summary>
- /// Throws an exception if encoding is null
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentNullException))]
- public void CtorNullExceptionEncoding ()
- {
- MemoryStream stream = new MemoryStream (64);
- BinaryReader reader = new BinaryReader (stream, null);
- Assertion.Fail();
- }
-
- /// <summary>
- /// Throws an exception if stream does not support writing
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentExceptionCannotWrite ()
- {
- if (File.Exists (".BinaryReaderTestFile.1"))
- File.Delete (".BinaryReaderTestFile.1");
-
- FileStream file = new FileStream (".BinaryReaderTestFile.1", FileMode.CreateNew, FileAccess.Read);
- BinaryReader breader = new BinaryReader (file);
- if (File.Exists (".BinaryReaderTestFile.1"))
- File.Delete (".BinaryReaderTestFile.1");
-
- Assertion.Fail ();
- }
- /// <summary>
- /// Throws an exception if stream is already closed
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentExceptionClosedStream ()
- {
- if (File.Exists (".BinaryReaderTestFile.2"))
- File.Delete (".BinaryReaderTestFile.2");
-
- FileStream file = new FileStream (".BinaryReaderTestFile.1", FileMode.CreateNew, FileAccess.Write);
- file.Close ();
- BinaryReader breader = new BinaryReader (file);
- if (File.Exists (".BinaryReaderTestFile.2"))
- File.Delete (".BinaryReaderTestFile.2");
- }
- /// <summary>
- /// Throws an exception if stream is closed
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentException))]
- public void CtorArgumentExceptionEncoding ()
- {
- MemoryStream stream = new MemoryStream (64);
- stream.Close ();
-
- BinaryReader reader = new BinaryReader (stream, new ASCIIEncoding ());
- Assertion.Fail();
- }
-
- /// <summary>
- /// Tests read () method
- /// </summary>
- [Test]
- public void Read ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 0, reader.Read ());
- Assertion.AssertEquals ("test#02", 1, reader.Read ());
- Assertion.AssertEquals ("test#03", 2, reader.Read ());
- Assertion.AssertEquals ("test#04", 3, reader.Read ());
- Assertion.AssertEquals ("test#05", -1, reader.Read ());
- }
-
- [Test]
- public void PeakChar ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 0, reader.PeekChar ());
- Assertion.AssertEquals ("test#02", 0, reader.PeekChar ());
- Assertion.AssertEquals ("test#03", 0, reader.Read ());
- Assertion.AssertEquals ("test#03", 1, reader.Read ());
- Assertion.AssertEquals ("test#03", 2, reader.PeekChar ());
- }
-
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void CloseRead ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
- reader.Close ();
- reader.Read ();
- }
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void ClosePeakChar ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
- reader.Close ();
- reader.PeekChar ();
- }
- [Test]
- [ExpectedException(typeof(ObjectDisposedException))]
- public void CloseReadBytes ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
- reader.Close ();
- reader.ReadBytes (1);
- }
- [Test]
- public void BaseStream ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 4, reader.BaseStream.Length);
- Assertion.AssertEquals ("test#02", true, reader.BaseStream.CanRead);
- reader.Close ();
- Assertion.AssertEquals ("test#03", null, reader.BaseStream);
- }
-
- /// <summary>
- /// Tests read (byte [], int, int) method
- /// </summary>
- [Test]
- public void ReadByteArray ()
- {
- byte [] bytes = new byte [] {0, 1, 2, 3, 4, 5};
- MemoryStream stream = new MemoryStream (bytes);
- BinaryReader reader = new BinaryReader (stream);
-
- bytes = new byte [3];
- reader.Read (bytes, 0, 3);
- Assertion.AssertEquals ("test#01", 0, bytes [0]);
- Assertion.AssertEquals ("test#02", 1, bytes [1]);
- Assertion.AssertEquals ("test#03", 2, bytes [2]);
- bytes = new byte [6];
- reader.Read (bytes, 3, 3);
- Assertion.AssertEquals ("test#04", 0, bytes [0]);
- Assertion.AssertEquals ("test#05", 0, bytes [1]);
- Assertion.AssertEquals ("test#06", 0, bytes [2]);
- Assertion.AssertEquals ("test#07", 3, bytes [3]);
- Assertion.AssertEquals ("test#08", 4, bytes [4]);
- Assertion.AssertEquals ("test#09", 5, bytes [5]);
-
- bytes = new byte [2];
- reader.Read (bytes, 0, 2);
- Assertion.AssertEquals ("test#10", 0, bytes [0]);
- Assertion.AssertEquals ("test#11", 0, bytes [1]);
- }
-
- /// <summary>
- /// Test Read (char [], int, int)
- /// </summary>
- [Test]
- public void ReadCharArray ()
- {
-
- MemoryStream stream = new MemoryStream (new byte [] {109, 111, 110, 111, 58, 58});
- BinaryReader reader = new BinaryReader (stream);
-
- char [] chars = new char [3];
- reader.Read (chars, 0, 3);
- Assertion.AssertEquals ("test#01", 'm', chars [0]);
- Assertion.AssertEquals ("test#02", 'o', chars [1]);
- Assertion.AssertEquals ("test#03", 'n', chars [2]);
- chars = new char [6];
- reader.Read (chars, 3, 3);
- Assertion.AssertEquals ("test#04", 0, chars [0]);
- Assertion.AssertEquals ("test#05", 0, chars [1]);
- Assertion.AssertEquals ("test#06", 0, chars [2]);
- Assertion.AssertEquals ("test#07", 'o', chars [3]);
- Assertion.AssertEquals ("test#08", ':', chars [4]);
- Assertion.AssertEquals ("test#09", ':', chars [5]);
-
- chars = new char [2];
- reader.Read (chars, 0, 2);
- Assertion.AssertEquals ("test#08", 0, chars [0]);
- Assertion.AssertEquals ("test#09", 0, chars [1]);
- }
-
- /// <summary>
- /// Test ReadBoolean () method.
- /// </summary>
- [Test]
- public void ReadBoolean ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", false, reader.ReadBoolean ());
- Assertion.AssertEquals ("test#02", true, reader.ReadBoolean ());
- Assertion.AssertEquals ("test#03", true, reader.ReadBoolean ());
- Assertion.AssertEquals ("test#04", false, reader.ReadBoolean ());
- Assertion.AssertEquals ("test#05", true, reader.ReadBoolean ());
- }
-
- /// <summary>
- /// Test ReadBoolean () method exceptions.
- /// </summary>
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadBooleanException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadBoolean ();
- reader.ReadBoolean ();
- reader.ReadBoolean ();
- Assertion.Fail ();
- }
-
- /// <summary>
- /// Test ReadByte () method.
- /// </summary>
- [Test]
- public void ReadByte ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 0, reader.ReadByte ());
- Assertion.AssertEquals ("test#02", 1, reader.ReadByte ());
- Assertion.AssertEquals ("test#03", 99, reader.ReadByte ());
- Assertion.AssertEquals ("test#04", 0, reader.ReadByte ());
- Assertion.AssertEquals ("test#05", 13, reader.ReadByte ());
- }
-
- /// <summary>
- /// Test ReadByte () method exceptions.
- /// </summary>
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadByteException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadByte ();
- reader.ReadByte ();
- reader.ReadByte ();
- Assertion.Fail ();
- }
-
- /// <summary>
- /// Test ReadBytes (int) method.
- /// </summary>
- [Test]
- public void ReadBytes ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
-
- byte [] bytes = reader.ReadBytes (2);
- Assertion.AssertEquals ("test#01", 0, bytes [0]);
- Assertion.AssertEquals ("test#02", 1, bytes [1]);
-
- bytes = reader.ReadBytes (2);
- Assertion.AssertEquals ("test#03", 99, bytes [0]);
- Assertion.AssertEquals ("test#04", 0, bytes [1]);
-
- bytes = reader.ReadBytes (2);
- Assertion.AssertEquals ("test#05", 13, bytes [0]);
- Assertion.AssertEquals ("test#06", 1, bytes.Length);
- }
-
- /// <summary>
- /// Test ReadBytes (int) method exception.
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void ReadBytesException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadBytes (-1);
- }
-
- /// <summary>
- /// Test ReadChar () method.
- /// </summary>
- [Test]
- public void ReadChar ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 0, reader.ReadChar ());
- Assertion.AssertEquals ("test#02", 1, reader.ReadChar ());
- Assertion.AssertEquals ("test#03", 99, reader.ReadChar ());
- Assertion.AssertEquals ("test#04", 0, reader.ReadChar ());
- Assertion.AssertEquals ("test#05", 13, reader.ReadChar ());
- }
-
- /// <summary>
- /// Test ReadChar () method exception.
- /// </summary>
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadCharException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadChar ();
- reader.ReadChar ();
- reader.ReadChar ();
- Assertion.Fail ();
- }
- /// <summary>
- /// Test ReadChars (int) method.
- /// </summary>
- [Test]
- public void ReadChars ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
-
- char [] chars = reader.ReadChars (2);
- Assertion.AssertEquals ("test#01", 0, chars [0]);
- Assertion.AssertEquals ("test#02", 1, chars [1]);
-
- chars = reader.ReadChars (2);
- Assertion.AssertEquals ("test#03", 99, chars [0]);
- Assertion.AssertEquals ("test#04", 0, chars [1]);
-
- chars = reader.ReadChars (2);
- Assertion.AssertEquals ("test#05", 13, chars [0]);
- Assertion.AssertEquals ("test#06", 1, chars.Length);
- }
-
- /// <summary>
- /// Test ReadChars (int value) exceptions. If value is negative exception is thrown
- /// </summary>
- [Test]
- [ExpectedException(typeof(ArgumentOutOfRangeException))]
- public void ReadCharsException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {0, 1, 99, 0, 13});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadChars (-1);
- }
-
-
- /// <summary>
- /// Test ReadDecimal () method.
- /// </summary>
- [Test]
- public void ReadDecimal ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
- Assertion.AssertEquals ("test#01", -18295873486192640, reader.ReadDecimal ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadDecimalException ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadDecimal ();
- reader.ReadDecimal ();
- }
-
- [Test]
- public void ReadDouble ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
- Assertion.AssertEquals ("test#01", 1.89131277973112E-307, reader.ReadDouble ());
- Assertion.AssertEquals ("test#02", 1.2024538023802E+111, reader.ReadDouble ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadDoubleException ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadDouble ();
- reader.ReadDouble ();
- reader.ReadDouble ();
- }
-
- [Test]
- public void ReadInt16 ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 321, reader.ReadInt16 ());
- Assertion.AssertEquals ("test#02", 11040, reader.ReadInt16 ());
- Assertion.AssertEquals ("test#03", 773, reader.ReadInt16 ());
- Assertion.AssertEquals ("test#04", 54, reader.ReadInt16 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadInt16Exception ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadInt16 ();
- reader.ReadInt16 ();
- }
- [Test]
- public void ReadInt32 ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 723517761, reader.ReadInt32 ());
- Assertion.AssertEquals ("test#02", 3539717, reader.ReadInt32 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadInt32Exception ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadInt32 ();
- reader.ReadInt32 ();
- }
- [Test]
- public void ReadInt64 ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadInt64 ());
- Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadInt64 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadInt64Exception ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadInt64 ();
- reader.ReadInt64 ();
- reader.ReadInt64 ();
- }
-
- [Test]
- public void ReadSByte ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 200, 32});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 65, reader.ReadSByte ());
- Assertion.AssertEquals ("test#02", -56, reader.ReadSByte ());
- Assertion.AssertEquals ("test#03", 32, reader.ReadSByte ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadSByteException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 200});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadSByte ();
- reader.ReadSByte ();
- reader.ReadSByte ();
- }
-
- [Test]
- public void ReadSingle ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 7.183757E-41, reader.ReadSingle ());
- Assertion.AssertEquals ("test#01", 3.820471E-37, reader.ReadSingle ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadSingleException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 200, 0, 0, 0, 1, 2, 3, 4});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadSingle ();
- reader.ReadSingle ();
- reader.ReadSingle ();
- }
-
- [Test]
- public void ReadString ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {6,109, 111, 110, 111, 58, 58});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", "mono::", reader.ReadString ());
-
- stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
- reader = new BinaryReader (stream);
- Assertion.AssertEquals ("test#02", "mo", reader.ReadString ());
- Assertion.AssertEquals ("test#03", "o::", reader.ReadString ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadStringException ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {2,109, 111, 3, 111, 58, 58});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadString ();
- reader.ReadString ();
- reader.ReadString ();
- }
-
- [Test]
- public void ReadUInt16 ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {200, 200, 32, 43, 5, 3, 54, 0});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 51400, reader.ReadUInt16 ());
- Assertion.AssertEquals ("test#02", 11040, reader.ReadUInt16 ());
- Assertion.AssertEquals ("test#03", 773, reader.ReadUInt16 ());
- Assertion.AssertEquals ("test#04", 54, reader.ReadUInt16 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadUInt16Exception ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1});
- BinaryReader reader = new BinaryReader (stream);
- reader.ReadUInt16 ();
- reader.ReadUInt16 ();
- }
- [Test]
- public void ReadUInt32 ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43, 5, 3, 54, 0});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 723517761, reader.ReadUInt32 ());
- Assertion.AssertEquals ("test#02", 3539717, reader.ReadUInt32 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadUInt32Exception ()
- {
- MemoryStream stream = new MemoryStream (new byte [] {65, 1, 32, 43});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadUInt32 ();
- reader.ReadUInt32 ();
- }
- [Test]
- public void ReadUInt64 ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
-
- Assertion.AssertEquals ("test#01", 15202969475612993, reader.ReadUInt64 ());
- Assertion.AssertEquals ("test#02", 2471354792417887522, reader.ReadUInt64 ());
- }
-
- [Test]
- [ExpectedException(typeof(EndOfStreamException))]
- public void ReadUInt64Exception ()
- {
- 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});
- BinaryReader reader = new BinaryReader (stream);
-
- reader.ReadUInt64 ();
- reader.ReadUInt64 ();
- reader.ReadUInt64 ();
- }
- }
- }
|