[Mono-list] First NUnit contribution
Michael Riegger
mikeriegger@yahoo.ca
Tue, 28 May 2002 15:25:19 -0400 (EDT)
This is the first NUnit test case I've written, it is for the UnicodeEncoding class in System.Text
and I was hoping to get some feedback on it. It runs without problems on both windows and cygwin
mono. The major thing it is missing is testing for invalid surrogates, as I am having trouble
finding a case that would get the Windows library to throw one when encountered. Any advice you
can give is appreciated.
Michael Riegger
// UnicodeEncodingTest - NUnit Test Cases for the System.Text.UnicodeEncoding class
//
// Author: Michael Riegger <mikeriegger@yahoo.ca>
//
// <c> 2002 Michael Riegger
using NUnit.Framework;
using System.Text;
using System;
namespace MonoTests.System.Text
{
public class UnicodeEncodingTest : TestCase
{
public UnicodeEncodingTest (string name) : base (name) {}
public static ITest Suite
{
get
{
TestSuite suite = new TestSuite ();
suite.AddTest (new UnicodeEncodingTest ("Test1"));
suite.AddTest (new UnicodeEncodingTest ("Test2"));
return suite;
}
}
public void Test1 ()
{
string englishString = "Latin text.";
byte [] englishByteArrayBE = new byte []
{
0,(byte)'L',
0,(byte)'a',
0,(byte)'t',
0,(byte)'i',
0,(byte)'n',
0,(byte)' ',
0,(byte)'t',
0,(byte)'e',
0,(byte)'x',
0,(byte)'t',
0,(byte)'.',
};
Test(englishString,englishByteArrayBE);
}
public void Test2 ()
{
string hiraganaString = "\u3070\u3060\u3073\u3075\u3092";
byte [] hiraganaByteArrayBE = new byte []
{
0x30,0x70,0x30,0x60,0x30,0x73,0x30,0x75,0x30,0x92
};
Test(hiraganaString,hiraganaByteArrayBE);
}
public void Test (string unicode, byte [] byteArray)
{
// Run through the overridden functions testing with a unicode string and its corresponding
byte array
// Default constructor should result in little-endian with byte order mark
UnicodeEncoding DefaultEncoding = new UnicodeEncoding();
TestByteOrderMark ( unicode, byteArray, DefaultEncoding,false );
TestGetByteCount( unicode, byteArray, DefaultEncoding );
TestGetCharCount( unicode, byteArray, DefaultEncoding );
TestGetBytes1 (unicode, byteArray, DefaultEncoding, false);
TestGetBytes2 (unicode, byteArray, DefaultEncoding, false);
TestGetBytes3 (unicode, byteArray, DefaultEncoding, false);
TestGetChars (unicode, byteArray, DefaultEncoding, false);
TestGetMaxByteCount (DefaultEncoding);
TestGetMaxCharCount (DefaultEncoding);
TestGetEncoder (unicode, DefaultEncoding);
TestGetDecoder (byteArray, DefaultEncoding);
// Test big endian encoding
UnicodeEncoding BigEndianEncoding = new UnicodeEncoding(true,true);
TestByteOrderMark ( unicode, byteArray, BigEndianEncoding,true );
TestGetByteCount( unicode, byteArray, BigEndianEncoding );
TestGetCharCount( unicode, byteArray, BigEndianEncoding );
TestGetBytes1 (unicode, byteArray, BigEndianEncoding, true);
TestGetBytes2 (unicode, byteArray, BigEndianEncoding, true);
TestGetBytes3 (unicode, byteArray, BigEndianEncoding, true);
TestGetChars (unicode, byteArray, BigEndianEncoding, true);
TestGetMaxByteCount (BigEndianEncoding);
TestGetMaxCharCount (BigEndianEncoding);
TestGetEncoder (unicode, BigEndianEncoding);
TestGetDecoder (byteArray, BigEndianEncoding);
// Test little endian encoding
UnicodeEncoding LittleEndianEncoding = new UnicodeEncoding(false,true);
TestByteOrderMark ( unicode, byteArray, LittleEndianEncoding,false );
TestGetByteCount( unicode, byteArray, LittleEndianEncoding );
TestGetCharCount( unicode, byteArray, LittleEndianEncoding );
TestGetBytes1 (unicode, byteArray, LittleEndianEncoding, false);
TestGetBytes2 (unicode, byteArray, LittleEndianEncoding, false);
TestGetBytes3 (unicode, byteArray, LittleEndianEncoding, false);
TestGetChars (unicode, byteArray, LittleEndianEncoding, false);
TestGetMaxByteCount (LittleEndianEncoding);
TestGetMaxCharCount (LittleEndianEncoding);
TestGetEncoder (unicode, LittleEndianEncoding);
TestGetDecoder (byteArray, LittleEndianEncoding);
}
public void TestByteOrderMark ( string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding, bool isBigEndian )
{
byte [] testByteOrderMark = encoding.GetPreamble ();
if ( isBigEndian )
{
AssertEquals ( testByteOrderMark[0],(byte)0xFE );
AssertEquals ( testByteOrderMark[1],(byte)0xFF );
}
else
{
AssertEquals ( testByteOrderMark[0],(byte)0xFF );
AssertEquals ( testByteOrderMark[1],(byte)0xFE );
}
}
public void TestGetByteCount ( string unicodeString, byte [] unicodeByteArray,UnicodeEncoding
encoding )
{
// Test method - public override int GetByteCount(string);
AssertEquals ( unicodeByteArray.Length, encoding.GetByteCount( unicodeString ) );
// Test the execption handling of "public override int GetByteCount( string s );"
// There are two exceptions that this can throw: ArgumentNullException and ArgumentException
// Test for null exception thrown
bool wasNullExceptionCaught = false;
try
{
string nullString = null;
int byteCount = encoding.GetByteCount ( nullString );
}
catch( ArgumentNullException )
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Incorrect exception thrown at 1: " + e.ToString ());
}
AssertEquals ( wasNullExceptionCaught, true );
// Test the 2nd overridden method - public override int GetByteCount(char[], int index, int
count);
for (int index = -1 ; index < unicodeString.Length + 1 ; index++)
{
for (int count = -1 ; count < unicodeString.Length + 1 ; count++)
{
int byteCount = 0;
bool wasArgumentOutOfRangeExceptionCaught = false;
try
{
byteCount = encoding.GetByteCount (unicodeString.ToCharArray(), index, count );
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Incorrect exception thrown at 2: " +e.ToString ());
}
// Test to determine whether an execption should have been thrown
if ( index < 0 || count < 0 || index + count > unicodeString.Length )
AssertEquals (wasArgumentOutOfRangeExceptionCaught,true);
else
{
// Make sure an exception was not thrown, and the value returned was correct
AssertEquals (wasArgumentOutOfRangeExceptionCaught,false);
AssertEquals (byteCount, count * 2);
}
}
}
// Test the method to make sure it throws a null exception
wasNullExceptionCaught = false;
try
{
char [] nullCharArray = null;
int byteCount = encoding.GetByteCount ( nullCharArray, 0, 0 );
}
catch( ArgumentNullException )
{
wasNullExceptionCaught = true;
}
AssertEquals ( wasNullExceptionCaught, true );
}
public void TestGetCharCount ( string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding)
{
// Test for a combination of paramters, some of which would be invalid and should throw
exceptions
for (int index = -1 ; index < unicodeByteArray.Length + 1 ; index++)
{
for (int count = -1 ; count < unicodeByteArray.Length + 1 ; count ++)
{
int charCount = 0;
bool wasArgumentOutOfRangeExceptionCaught = false;
try
{
charCount = encoding.GetCharCount (unicodeByteArray, index, count);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Incorrect exception thrown at 1: " + e.ToString ());
}
if (index < 0 || count < 0 || index + count > unicodeByteArray.Length)
AssertEquals (wasArgumentOutOfRangeExceptionCaught, true);
else
{
AssertEquals (wasArgumentOutOfRangeExceptionCaught, false);
AssertEquals (charCount , count / 2);
}
}
}
// Test for a null exception
bool wasNullExceptionThrown = false;
try
{
byte [] nullByteArray = null;
int charCount = encoding.GetCharCount (nullByteArray, 0, 0);
}
catch (ArgumentNullException)
{
wasNullExceptionThrown = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString ());
}
AssertEquals (wasNullExceptionThrown, true);
}
private bool areByteArraysEquivalent (byte [] array1, int arrayIndex1, byte [] array2, int
arrayIndex2, int lengthtoTest, bool isTestByteArrayBigEndian)
{
for (int i = arrayIndex1, j = arrayIndex2 ; i < arrayIndex1 + lengthtoTest ; i+=2, j+=2)
{
// The function takes the correct byte array in bigendian format
// If we are testing a little endian Encoding, reverse the byte sequence
if (isTestByteArrayBigEndian)
{
if (array1[i] != array2[j] || array1[i+1] != array2[j+1])
return false;
}
else
{
if (array1[i+1] != array2[j] || array1[i] != array2[j+1])
return false;
}
}
return true;
}
public void TestGetBytes1 (string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding, bool isBigEndian)
{
// Test public override byte[] GetBytes(string);
// Test for null exception handling
bool wasNullExceptionCaught = false;
try
{
string nullString = null;
encoding.GetBytes (nullString);
}
catch (ArgumentNullException)
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 1: " + e.ToString ());
}
finally
{
AssertEquals (wasNullExceptionCaught, true);
byte[] testByteArray = encoding.GetBytes ( unicodeString );
AssertEquals (areByteArraysEquivalent (unicodeByteArray, 0, testByteArray, 0, 0, isBigEndian),
true);
}
}
public void TestGetBytes2 (string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding, bool isBigEndian)
{
// Test the 2nd overloaded overridden method "public override int GetBytes(char[], int
charIndex, int charCount, byte[], int byteIndex);"
// Test to make sure that a nullexception is thrown if either chars or bytes is null
// Try with a valid charArray and a null byteArray
bool wasNullExceptionCaught = false;
try
{
byte [] nullByteArray = null;
encoding.GetBytes (unicodeString.ToCharArray (), 0, 0, nullByteArray ,0);
}
catch (ArgumentNullException)
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString ());
}
AssertEquals (wasNullExceptionCaught, true);
// Now try with a null charArray
wasNullExceptionCaught = false;
try
{
char [] nullCharArray = null;
byte [] resultByteArray = new byte [10];
encoding.GetBytes (nullCharArray, 0, 0, resultByteArray ,0);
}
catch (ArgumentNullException)
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 3: " + e.ToString ());
}
AssertEquals (wasNullExceptionCaught, true);
// Test for ArgumentOutOfRangeException
for (int charIndex = -1 ; charIndex < unicodeString.Length + 1; charIndex++)
{
for (int charCount = -1 ; charCount < unicodeString.Length + 1 ; charCount++)
{
for (int byteIndex = -1; byteIndex < encoding.GetByteCount (unicodeString) + 1; byteIndex++)
{
bool wasArgumentExceptionCaught = false;
bool wasArgumentOutOfRangeExceptionCaught = false;
byte [] testByteArray = new byte [unicodeByteArray.Length];
try
{
encoding.GetBytes (unicodeString.ToCharArray (), charIndex, charCount, testByteArray,
byteIndex);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (ArgumentException)
{
wasArgumentExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 4: " + e.ToString ());
}
// Did we give GetBytes () bad data? Check to make sure that an exception was thrown
if (charIndex < 0 || charCount < 0 || byteIndex < 0 || charIndex + charCount >
unicodeString.Length || byteIndex > testByteArray.Length)
{
AssertEquals (wasArgumentOutOfRangeExceptionCaught, true);
}
// byteIndex is equal to the length of bytes OR No bytes have been stored into bytes. (i.e.
there is enough room in the given array for the characters)
else if (byteIndex == testByteArray.Length || byteIndex + charCount * 2 >
testByteArray.Length)
{
if (charCount != 0)
AssertEquals (wasArgumentExceptionCaught, true);
}
else
{
AssertEquals (wasArgumentExceptionCaught, false);
AssertEquals (wasArgumentOutOfRangeExceptionCaught, false);
// Valid data, check to make sure that the unicode string was copied correctly
Assert (areByteArraysEquivalent(unicodeByteArray, charIndex * 2, testByteArray, byteIndex,
charCount * 2, isBigEndian));
}
}
}
}
}
public void TestGetBytes3 (string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding, bool isBigEndian)
{
// Test the 3rd overloaded overridden method "public override int GetBytes(string, int
charIndex, int charCount, byte[], int byteIndex);"
// Test to make sure that a nullexception is thrown if either chars or bytes is null
// Try with a valid charArray and a null byteArray
bool wasNullExceptionCaught = false;
try
{
byte [] nullByteArray = null;
encoding.GetBytes (unicodeString, 0, 0, nullByteArray ,0);
}
catch (ArgumentNullException)
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString ());
}
AssertEquals (wasNullExceptionCaught, true);
// Now try with a null charArray
wasNullExceptionCaught = false;
try
{
string nullString = null;
byte [] resultByteArray = new byte [10];
encoding.GetBytes (nullString, 0, 0, resultByteArray ,0);
}
catch (ArgumentNullException)
{
wasNullExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 3: " + e.ToString ());
}
AssertEquals (wasNullExceptionCaught, true);
// Test for ArgumentOutOfRangeException
for (int charIndex = -1 ; charIndex < unicodeString.Length + 1; charIndex++)
{
for (int charCount = -1 ; charCount < unicodeString.Length + 1 ; charCount++)
{
for (int byteIndex = -1; byteIndex < encoding.GetByteCount (unicodeString) + 1; byteIndex++)
{
bool wasArgumentExceptionCaught = false;
bool wasArgumentOutOfRangeExceptionCaught = false;
byte [] testByteArray = new byte [unicodeByteArray.Length];
try
{
encoding.GetBytes (unicodeString, charIndex, charCount, testByteArray, byteIndex);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (ArgumentException)
{
wasArgumentExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 4: " + e.ToString ());
}
// Did we give GetBytes () bad data? Check to make sure that an exception was thrown
if (charIndex < 0 || charCount < 0 || byteIndex < 0 || charIndex + charCount >
unicodeString.Length || byteIndex > testByteArray.Length)
{
AssertEquals (wasArgumentOutOfRangeExceptionCaught, true);
}
// byteIndex is equal to the length of bytes OR No bytes have been stored into bytes. (i.e.
there is enough room in the given array for the characters)
else if (byteIndex == testByteArray.Length || byteIndex + charCount * 2 >
testByteArray.Length)
{
if (charCount != 0)
AssertEquals (wasArgumentExceptionCaught, true);
}
else
{
AssertEquals (wasArgumentExceptionCaught, false);
AssertEquals (wasArgumentOutOfRangeExceptionCaught, false);
// Valid data, check to make sure that the unicode string was copied correctly
AssertEquals (areByteArraysEquivalent(unicodeByteArray, charIndex * 2, testByteArray,
byteIndex, charCount * 2, isBigEndian), true);
}
}
}
}
}
public void TestGetChars (string unicodeString, byte [] unicodeByteArray, UnicodeEncoding
encoding, bool isBigEndian)
{
// Test overridden method GetChars for proper null exception handling
// public override int GetChars(byte[] bytes,int byteIndex,int byteCount,char[] chars,int
charIndex);
// Test to make sure that a null exception is thrown if bytes is null
{
bool wasArgumentNullExceptionThrown = false;
try
{
char [] validCharArray = new char [10];
encoding.GetChars (null, 0,0, validCharArray, 0);
}
catch (ArgumentNullException)
{
wasArgumentNullExceptionThrown = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 1:" + e.ToString ());
}
Assert (wasArgumentNullExceptionThrown);
}
// Test to make sure that a null exception is thrown if the charArray is null
{
bool wasArgumentNullExceptionThrown = false;
try
{
byte [] validByteArray = new byte [10];
char [] nullCharArray = null;
encoding.GetChars (validByteArray, 0,0, nullCharArray, 0);
}
catch (ArgumentNullException)
{
wasArgumentNullExceptionThrown = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2:" + e.ToString ());
}
Assert (wasArgumentNullExceptionThrown);
}
{
// Test for ArgumentOutOfRangeException
for (int byteIndex = -1 ; byteIndex < unicodeByteArray.Length + 1; byteIndex++)
{
for (int byteCount = -1 ; byteCount < unicodeByteArray.Length + 1 ; byteCount++)
{
for (int charIndex = -1; charIndex < unicodeString.Length + 1; charIndex++)
{
bool wasArgumentExceptionCaught = false;
bool wasArgumentOutOfRangeExceptionCaught = false;
char [] testCharArray = new char [unicodeString.Length];
try
{
encoding.GetChars (unicodeByteArray, byteIndex, byteCount, testCharArray, charIndex);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (ArgumentException)
{
wasArgumentExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 3: " + e.ToString ());
}
// Did we give GetChar () bad data? Check to make sure that an exception was thrown
if (byteIndex < 0 || byteCount < 0 || charIndex < 0 || byteIndex + byteCount >
unicodeByteArray.Length /*|| byteIndex > testByteArray.Length*/)
{
AssertEquals (wasArgumentOutOfRangeExceptionCaught, true);
}
// charIndex is greater than the length of chars.
else if (charIndex > testCharArray.Length || charIndex + byteCount/2 >
testCharArray.Length)
{
AssertEquals (wasArgumentExceptionCaught, true);
}
else
{
Assert (!wasArgumentExceptionCaught);
Assert (!wasArgumentOutOfRangeExceptionCaught);
// Verify that unicodeString and testCharArray are equivalent
for (int i = byteIndex/2, j = charIndex ; j < byteIndex + byteCount ; i++, j++)
{
// AssertEquals (unicodeString[i], testCharArray[j]);
}
}
}
}
}
}
}
public void TestGetMaxByteCount (UnicodeEncoding encoding)
{
for (int charCount = -1 ; charCount < 100 ; charCount++)
{
bool wasArgumentOutOfRangeExceptionCaught = false;
try
{
int maxBytesRequired = encoding.GetMaxByteCount (charCount);
AssertEquals (maxBytesRequired, charCount * 2);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception: 1" + e.ToString ());
}
if (charCount < 0)
Assert (wasArgumentOutOfRangeExceptionCaught);
}
}
public void TestGetMaxCharCount (UnicodeEncoding encoding)
{
for (int byteCount = -1 ; byteCount < 100 ; byteCount++)
{
bool wasArgumentOutOfRangeExceptionCaught = true;
try
{
int maxCharCount = encoding.GetMaxCharCount (byteCount);
AssertEquals (maxCharCount, (byteCount + 1) / 2);
}
catch (ArgumentOutOfRangeException)
{
wasArgumentOutOfRangeExceptionCaught = true;
}
catch (Exception e)
{
Fail ("Invalid exception thrown: 1" + e.ToString ());
}
if (byteCount < 0)
Assert (wasArgumentOutOfRangeExceptionCaught);
}
}
public void TestGetDecoder (byte [] byteArrayToTest, UnicodeEncoding encoding)
{
Decoder decoder = encoding.GetDecoder ();
int decoderResultLength = decoder.GetCharCount (byteArrayToTest, 0, byteArrayToTest.Length);
char [] decoderCharArrayResult = new char [decoderResultLength];
int numCharsDecoded = decoder.GetChars (byteArrayToTest, 0, byteArrayToTest.Length,
decoderCharArrayResult, 0);
AssertEquals (numCharsDecoded, decoderResultLength);
char [] encodingCharArrayResult = encoding.GetChars (byteArrayToTest);
AssertEquals (encodingCharArrayResult.Length , decoderCharArrayResult.Length);
for (int i=0;i < encodingCharArrayResult.Length ;i++)
AssertEquals (encodingCharArrayResult[i], decoderCharArrayResult[i]);
}
public void TestGetEncoder (string unicodeString, UnicodeEncoding encoding)
{
byte [] encodingResult = encoding.GetBytes (unicodeString);
Encoder encoder = encoding.GetEncoder ();
int encoderResultLength = encoder.GetByteCount (unicodeString.ToCharArray (),
0,unicodeString.Length, true);
byte [] encoderResult = new byte [ encoderResultLength ];
int numBytesEncoded = encoder.GetBytes (unicodeString.ToCharArray (),0,unicodeString.Length,
encoderResult, 0, true);
AssertEquals (numBytesEncoded, encoderResultLength);
AssertEquals (encodingResult.Length ,encoderResult.Length);
for (int i=0;i < encoderResult.Length ;i++)
AssertEquals (encoderResult[i], encodingResult[i]);
}
}
}
______________________________________________________________________
Find, Connect, Date! http://personals.yahoo.ca