Excellent, thats just the sort of comments I was looking for. I've gone through and
made the
changes listed.
> I'll just make a quick comment (or two), since I don't have time to
> carefully read the whole thing.
>
> 1) Use a string message as the first parameter of your Assert*() methods
> when there is more than one per Test method. This will make it much easier
> to tell where the test is failing.
>
> 2) The order of the parameters is important. The expected value comes
> before the actual value. NUnit gives a nice message when the assert fails,
> like "Expected <42> but got <69>".
>
> So, you should have something like: AssertEquals("A#45", 42,
> myAnswerToLifeEtc)
>
> Regards,
> Nick D.
// UnicodeEncodingTest - NUnit Test Cases for the System.Text.UnicodeEncoding class
//
// Author: Michael Riegger <[EMAIL PROTECTED]>
//
// <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 ("1st byte in preamble (big endian)
should be 0xFE", (byte)0xFE,
testByteOrderMark[0] );
AssertEquals ("2nd byte in preamble (big endian)
should be 0xFF", (byte)0xFF,
testByteOrderMark[1] );
}
else
{
AssertEquals ("1st byte in preamble (little endian)
should be 0xFF", (byte)0xFF,
testByteOrderMark[0]);
AssertEquals ("2nd byte in preamble (little endian)
should be 0xFE", (byte)0xFE,
testByteOrderMark[1] );
}
}
public void TestGetByteCount ( string unicodeString, byte []
unicodeByteArray,UnicodeEncoding
encoding )
{
// Test method - public override int GetByteCount(string);
AssertEquals ("GetByteCount error", 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
try
{
string nullString = null;
int byteCount = encoding.GetByteCount ( nullString );
Fail ("GetByteCount (string) failed to thrown null
exception");
}
catch( ArgumentNullException )
{
}
catch (Exception e)
{
Fail ("Incorrect exception thrown at 1: " + e.ToString
());
}
// 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 )
Assert ("GetByteCount(string) failed
to throw OutOfRange exception",
wasArgumentOutOfRangeExceptionCaught);
else
{
// Make sure an exception was not
thrown, and the value returned was correct
Assert ("GetByteCount(string)
OutOfRange exception should not have been thrown",
!wasArgumentOutOfRangeExceptionCaught);
AssertEquals ("GetByteCount(string)
incorrect returnval",count * 2, byteCount);
}
}
}
// Test the method to make sure it throws a null exception
try
{
char [] nullCharArray = null;
int byteCount = encoding.GetByteCount ( nullCharArray,
0, 0 );
Fail ("GetByteCount (char[]) did not thrown null
exception");
}
catch ( ArgumentNullException )
{
}
catch (Exception e)
{
Fail ("Incorrect exception thrown at 3" + e.ToString
());
}
}
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)
Assert("Failed to throw OutOfRange
exception", wasArgumentOutOfRangeExceptionCaught);
else
{
Assert ("Incorrect exception thrown",
!wasArgumentOutOfRangeExceptionCaught);
AssertEquals ("Incorrect return
value", count / 2, charCount);
}
}
}
// Test for a null exception
try
{
byte [] nullByteArray = null;
int charCount = encoding.GetCharCount (nullByteArray,
0, 0);
Fail ("ArgumentNullException not thrown!");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString
());
}
}
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
try
{
string nullString = null;
encoding.GetBytes (nullString);
Fail ("GetBytes failed to throw null exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 1: " + e.ToString
());
}
byte[] testByteArray = encoding.GetBytes ( unicodeString );
Assert ("GetBytes returned incorrect result",
areByteArraysEquivalent (unicodeByteArray, 0,
testByteArray, 0, 0, isBigEndian));
}
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
try
{
byte [] nullByteArray = null;
encoding.GetBytes (unicodeString.ToCharArray (), 0, 0,
nullByteArray ,0);
Fail ("GetBytes (byte [] null) should have thrown
exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString
());
}
// Now try with a null charArray
try
{
char [] nullCharArray = null;
byte [] resultByteArray = new byte [10];
encoding.GetBytes (nullCharArray, 0, 0,
resultByteArray ,0);
Fail ("GetBytes (char [] null) should have thrown
exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 3: " + e.ToString
());
}
// 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)
{
Assert ("OutOfRange Exception
not thrown", wasArgumentOutOfRangeExceptionCaught);
}
// 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)
Assert
("ArgumentException should have been thrown",wasArgumentExceptionCaught);
}
else
{
Assert ("ArgumentException
should not have been thrown", !wasArgumentExceptionCaught);
Assert
("ArgumentOutOfRangeException should not have been thrown",
!wasArgumentOutOfRangeExceptionCaught);
// Valid data, check to make
sure that the unicode string was copied correctly
Assert ("Incorrect data
returned", 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
try
{
byte [] nullByteArray = null;
encoding.GetBytes (unicodeString, 0, 0, nullByteArray
,0);
Fail ("null string did not result in exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2: " + e.ToString
());
}
// Now try with a null charArray
try
{
string nullString = null;
byte [] resultByteArray = new byte [10];
encoding.GetBytes (nullString, 0, 0, resultByteArray
,0);
Fail ("null char array did not result in exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 3: " + e.ToString
());
}
// 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)
{
Assert ("OutOfRangeException
not thrown", wasArgumentOutOfRangeExceptionCaught);
}
// 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)
Assert
("ArgumentException not thrown", wasArgumentExceptionCaught);
}
else
{
Assert ("ArgumentException
should not have been thrown", !wasArgumentExceptionCaught);
Assert
("ArgumentOutOfRangException should not have been thrown",
!wasArgumentOutOfRangeExceptionCaught);
// Valid data, check to make
sure that the unicode string was copied correctly
Assert ("Incorrect data
returned", areByteArraysEquivalent(unicodeByteArray, charIndex * 2,
testByteArray, byteIndex, charCount * 2, isBigEndian));
}
}
}
}
}
public void TestGetChars (string unicodeString, byte []
unicodeByteArrayBE, UnicodeEncoding
encoding, bool isBigEndian)
{
byte [] unicodeByteArray;
if (!isBigEndian)
{
unicodeByteArray = new byte
[unicodeByteArrayBE.Length];
for (int i = 0 ; i < unicodeByteArray.Length - 1;i+=2)
{
unicodeByteArray[i] = unicodeByteArrayBE[i+1];
unicodeByteArray[i+1] = unicodeByteArrayBE[i];
}
}
else
unicodeByteArray = unicodeByteArrayBE;
// 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
try
{
char [] validCharArray = new char [10];
encoding.GetChars (null, 0,0, validCharArray, 0);
Fail ("null byte array did not result in exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 1:" + e.ToString
());
}
// Test to make sure that a null exception is thrown if the
charArray is null
try
{
byte [] validByteArray = new byte [10];
char [] nullCharArray = null;
encoding.GetChars (validByteArray, 0,0, nullCharArray,
0);
Fail ("null char array did not result in exception");
}
catch (ArgumentNullException)
{
}
catch (Exception e)
{
Fail ("Invalid exception thrown at 2:" + e.ToString
());
}
// Test for ArgumentOutOfRangeException
for (int byteIndex = -2 ; byteIndex < unicodeByteArray.Length
+ 1; byteIndex+=2)
{
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];
int numCharsStored = 0;
try
{
numCharsStored =
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*/)
{
Assert ("ArgumentOutOfRange
exception not thrown", wasArgumentOutOfRangeExceptionCaught);
}
// charIndex is greater than the
length of chars.
else if (charIndex >
testCharArray.Length || charIndex + byteCount/2 > testCharArray.Length)
{
Assert ("ArgumentException not
thrown", wasArgumentExceptionCaught);
}
else
{
Assert ("ArgumentException
should not have been thrown", !wasArgumentExceptionCaught);
Assert
("ArgumentOutOfRangeException should not have been thrown",
!wasArgumentOutOfRangeExceptionCaught);
// Verify that unicodeString
and testCharArray are equivalent
int stringIndex = byteIndex /
2;
int testIndex = charIndex;
for (int i=0; i <
numCharsStored; i++)
{
AssertEquals
("Incorrect data returned", unicodeString[stringIndex],
testCharArray[testIndex]);
stringIndex++;
testIndex++;
}
}
}
}
}
}
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 ("OutOfRange exception not thrown",
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 ("OutOfRange exception not thrown",
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 ("Decoder GetChars/GetCharCount length error",
decoderResultLength,
numCharsDecoded);
char [] encodingCharArrayResult = encoding.GetChars
(byteArrayToTest);
AssertEquals ("Decoder GetCharCount error",
encodingCharArrayResult.Length ,
decoderCharArrayResult.Length);
for (int i=0;i < encodingCharArrayResult.Length ;i++)
AssertEquals ("Decoder data error",
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 ("Encoder GetBytes/GetByteCount length error",
numBytesEncoded,
encoderResultLength);
AssertEquals ("Encoder GetByteCount error",
encodingResult.Length ,encoderResult.Length);
for (int i=0;i < encoderResult.Length ;i++)
AssertEquals ("Encoder data error", encoderResult[i],
encodingResult[i]);
}
}
}
______________________________________________________________________
Find, Connect, Date! http://personals.yahoo.ca
_______________________________________________
Mono-list maillist - [EMAIL PROTECTED]
http://lists.ximian.com/mailman/listinfo/mono-list