/// <summary>
///This is a test class for EncodingHelperTest and is intended
///to contain all EncodingHelperTest Unit Tests
///</summary>
[TestClass()]
public class EncodingHelperTest
{
/// <summary>
/// Normal test for this method.
///</summary>
[TestMethod()]
public void IsTextUTF8Test()
{
for ( int i = 0; i < 1000; i++)
{
List<Char> chars = new List< char >();
chars.Add( ‘中‘ );
List<UnicodeCategory> temp = new List<UnicodeCategory>();
Random rd = new Random(( int )(DateTime.Now.Ticks & 0x7FFFFFFF));
for ( int j = 0; j < 255; j++)
{
char ch = ( char )rd.Next(0xFFFF);
UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(ch);
if (uc == UnicodeCategory.Surrogate || // Single surrogate could not be encoding correctly.
uc == UnicodeCategory.PrivateUse || // Private use blocks should be excluded.
uc == UnicodeCategory.OtherNotAssigned
)
{
j--;
}
else
{
chars.Add(ch);
temp.Add(uc);
}
}
string str = new string (chars.ToArray());
byte [] inputStream = Encoding.UTF8.GetBytes(str);
bool expected = true ;
bool actual;
actual = EncodingHelper.IsTextUTF8( ref inputStream);
Assert.AreEqual(expected, actual, string .Format( "UTF8_Assert Fails at:{0}" , str));
inputStream = Encoding.GetEncoding(932).GetBytes(str);
expected = false ;
actual = EncodingHelper.IsTextUTF8( ref inputStream);
Assert.AreEqual(expected, actual, string .Format( "ShiftJIS_Assert Fails at:{0}" , str));
}
}
/// <summary>
/// Check with All ASCII chars
/// </summary>
[TestMethod]
public void IsTextUTF8Test_AllASCII()
{
string str = "ABCDEFGHKLHSJKLDFHJKLHAJKLSHJKLHAJKLSHDJKLAHSDJKLHAJKLSDHJKLASHDJKLHASJKLDHJKLASD" ;
byte [] inputStream = Encoding.UTF8.GetBytes(str);
bool expected = false ;
bool actual;
actual = EncodingHelper.IsTextUTF8( ref inputStream);
Assert.AreEqual(expected, actual, string .Format( "UTF8_Assert Fails at:{0}" , str));
}
}
|