[Mono-list] Re: A NUnit test with System.IO.BinaryReader

Eduardo Garcia kiwnix@yahoo.es
17 Aug 2002 17:20:12 +0200


--=-h0fD3FRx2KF1bMAZst8A
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

El s=C3=A1b, 17-08-2002 a las 17:17, Eduardo Garcia escribi=C3=B3:
> I sent it to the author of the class but no answer recived, here is the
> test case:
>=20
> I wish it is fine, and this is usefull.
>=20
> It pass all the tests fine.

Argggg, i sent the class not the Nunit test, sorry :(((=20


--=-h0fD3FRx2KF1bMAZst8A
Content-Description: 
Content-Disposition: attachment; filename=BinaryReaderTest.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8

// BinaryReaderTest.cs - NUnit Test Cases for the SystemIO.BinaryReader cla=
ss
//
// Eduardo Garcia Cebollero (kiwnix@yahoo.es)
//
// (C) Eduardo Garcia Cebollero.
// (C) Ximian, Inc.  http://www.ximian.com
//=20

using NUnit.Framework;
using System;
using System.IO;
using System.Text;

namespace MonoTests.System.IO
{
=09
	public class BinaryReaderTest : TestCase
	{
		public BinaryReaderTest() :=20
			base ("MonoTests.System.IO.BinaryReaderTest testsuite") {}
		public BinaryReaderTest(string name) : base(name) {}
		=09
		protected override void SetUp()=20
		{
		}
	=09
	=09
		public static ITest Suite {
		get {=20
			return new TestSuite(typeof(BinaryReaderTest));=20
		}
		}
	=09
		private string _codeFileName =3D "resources" + Path.DirectorySeparatorCha=
r + "AFile.txt";
		=09
		public void TestCtor1()=20
		{
			{
				bool errorThrown =3D false;
				try {
					BinaryReader r =3D new BinaryReader((Stream)null);
				} catch (ArgumentNullException) {
					errorThrown =3D true;
				}
				Assert("#01 null string error not thrown", errorThrown);
			}
			{
				bool errorThrown =3D false;
				FileStream f =3D new FileStream(_codeFileName, FileMode.Open, FileAcces=
s.Write);
				try {
					BinaryReader r =3D new BinaryReader(f);
					r.Close();
				} catch (ArgumentException) {
					errorThrown =3D true;
				}
				f.Close();
				Assert("#02 no read error not thrown", errorThrown);
			}
			{
				FileStream f =3D new FileStream(_codeFileName,=20
								FileMode.Open,=20
								FileAccess.Read);
				BinaryReader r =3D new BinaryReader(f);
				AssertNotNull("#03 no binary reader created", r);
				r.Close();
				f.Close();
			}
			=09
		}
		public void TestCtor2()=20
		{
			{
				bool errorThrown =3D false;
				try {
					BinaryReader r =3D new BinaryReader((Stream)null,Encoding.ASCII);
				} catch (ArgumentNullException) {
					errorThrown =3D true;
				} catch (Exception e) {
					Fail ("#04 Incorrect exception thrown: " + e.ToString());
				}
				Assert("#05 null stream error not thrown", errorThrown);
			}
			{
				bool errorThrown =3D false;
				try {
					BinaryReader r =3D new BinaryReader((Stream)null,Encoding.Unicode);
				} catch (ArgumentNullException) {
					errorThrown =3D true;
				} catch (Exception e) {
					Fail ("#06 Incorrect exception thrown: " + e.ToString());
				}
				Assert("#07 null stream error not thrown", errorThrown);
			}
			{
				bool errorThrown =3D false;
				try {
					BinaryReader r =3D new BinaryReader((Stream)null,Encoding.UTF7);
				} catch (ArgumentNullException) {
					errorThrown =3D true;
				} catch (Exception e) {
					Fail ("#08 Incorrect exception thrown: " + e.ToString());
				}
				Assert("#09 null stream error not thrown", errorThrown);
			}
			{
				bool errorThrown =3D false;
				try {
					BinaryReader r =3D new BinaryReader((Stream)null,Encoding.UTF8);
				} catch (ArgumentNullException) {
					errorThrown =3D true;
				} catch (Exception e) {
					Fail ("#0A Incorrect exception thrown: " + e.ToString());
				}
				Assert("#0B null stream error not thrown", errorThrown);
			}
		}
		public void TestCtor3()
		{
			{
				bool errorThrown =3D false;
				byte[] b =3D new byte[30];
				MemoryStream m =3D new MemoryStream(b);
				try {
					BinaryReader r =3D new BinaryReader(m,(Encoding)null);
				}
				catch(ArgumentNullException) {
					errorThrown =3D true;
				}=20
				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 =3D new byte[30];
				MemoryStream m =3D new MemoryStream(b);
				try
				{
						BinaryReader r =3D new BinaryReader(m);
						r.Close();
				}
				catch(Exception e)
				{
					Fail("#0E Unhandled Exception: "+ e.ToString());
				}
			}
			{ //TODO Know what is the exact result of closing 2 timeas a Stream.
				bool errorThrown =3D false;
				byte[] b =3D new byte[30];
				MemoryStream m =3D new MemoryStream(b);
				try
				{
						BinaryReader r =3D new BinaryReader(m);
						r.Close();
						r.Close();
				}
				catch (IOException e)
				{
					errorThrown =3D true;
				}
				catch (Exception e)
				{
				   Fail("#0F-1 Unexpected Exception Trown");
				}
				Assert("#0F No Exception Trown",errorThrown);
			}
		}

		//TODO: (TestRead*) Verify Read Method
		public void TestReadBoolean()
		{
			{
					bool[] a =3D {true,true,false};
					byte[] arr_a =3D new byte[3];
					int i =3D 0;
					foreach(bool a1 in a)
					{
							  arr_a[i] =3D Convert.ToByte(a1);
							  i++;
					}
							 =20
					bool b;
					MemoryStream m =3D new MemoryStream(arr_a);
				try
				{=09
					BinaryReader r =3D new BinaryReader(m);
					b =3D 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 =3D {0,2,3,1,5,2};
				byte b;
				MemoryStream m =3D new MemoryStream(a);
				try
				{
					BinaryReader r =3D new BinaryReader(m);
					b =3D r.ReadByte();
					AssertEquals("#13 No well readed byte: ",a[0],b);
				}
				catch (Exception e)
				{
					Fail("#14 Unexpeted Exception thrown: " + e.ToString());
				}
			}
		}
		public void TestReadChar()
		{
			{
				char[] a =3D {'a','b','c','d','e'};
				byte[] arr_a =3D new byte[6];
				int i =3D 0;
				char c;
            foreach(char a1 in a)
            {
 	           arr_a[i] =3D Convert.ToByte(a1);
 	           i++;
            }
				MemoryStream m =3D new MemoryStream(arr_a);
				BinaryReader r =3D new BinaryReader(m);
				try
				{
					c =3D r.ReadChar();
					AssertEquals("#15 No well readed Char",a[0],c);
				}
				catch(Exception e)
				{
					Fail("#16 Unexpeted Exception: " + e.ToString());
				}
			}
		}

		//-TODO: (TestRead[Type]*) Verify the ReadBoolean, ReadByte ....
		// ReadBoolean, ReadByte, ReadChar Done
	=09
		//TODO: (TestFillBuffer*) Verify the FillBuffer Method
		public void TestPeekChar()
		{
			{
					char char1,char2;
					char[] b =3D {'A','B','C'};
					byte[] arr_b =3D new byte[3];
					int i =3D 0;
					foreach (char b1 in b)
					{
							  arr_b[i] =3D Convert.ToByte(b1);
							  i++;
					}
					 =20
					MemoryStream m =3D new MemoryStream(arr_b);
					BinaryReader r =3D new BinaryReader(m);
				try
				{=09
					char1 =3D (char)r.PeekChar();
					char2 =3D (char)r.PeekChar();
					AssertEquals("#13 the stream pointer have been altered in peek", char1=
,char2);
				} catch (Exception e) {
					Fail ("#14 Unexpected exception thrown: " + e.ToString());
				}
			}
		}
	}
}



--=-h0fD3FRx2KF1bMAZst8A--