1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
//
// UnicodeEncodingTest.cs - NUnit Test Cases for System.Text.UnicodeEncoding
//
// Author:
// Patrick Kalkman kalkman@cistron.nl
//
// (C) 2003 Patrick Kalkman
//
using NUnit.Framework;
using System;
using System.Text;
namespace MonoTests.System.Text
{
[TestFixture]
public class UnicodeEncodingTest
{
[Test]
public void IsBrowserDisplay ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsBrowserDisplay);
}
[Test]
public void IsBrowserSave ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsTrue (enc.IsBrowserSave);
}
[Test]
public void IsMailNewsDisplay ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsMailNewsDisplay);
}
[Test]
public void IsMailNewsSave ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsMailNewsSave);
}
[Test]
public void TestEncodingGetBytes1()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes;
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
UniBytes = UnicodeEnc.GetBytes (Unicode);
Assert.AreEqual (0xA0, UniBytes [0], "Uni #1");
Assert.AreEqual (0x03, UniBytes [1], "Uni #2");
Assert.AreEqual (0xA3, UniBytes [2], "Uni #3");
Assert.AreEqual (0x03, UniBytes [3], "Uni #4");
}
[Test]
public void TestEncodingGetBytes2()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes;
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
UniBytes = UnicodeEnc.GetBytes (Unicode);
Assert.AreEqual (0x03, UniBytes [0], "Uni #1");
Assert.AreEqual (0xA0, UniBytes [1], "Uni #2");
Assert.AreEqual (0x03, UniBytes [2], "Uni #3");
Assert.AreEqual (0xA3, UniBytes [3], "Uni #4");
}
[Test]
public void TestEncodingGetBytes3()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes = new byte [4];
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
int Cnt = UnicodeEnc.GetBytes (Unicode.ToCharArray(), 0, Unicode.Length, UniBytes, 0);
Assert.AreEqual (4, Cnt, "Uni #1");
Assert.AreEqual (0xA0, UniBytes [0], "Uni #2");
Assert.AreEqual (0x03, UniBytes [1], "Uni #3");
Assert.AreEqual (0xA3, UniBytes [2], "Uni #4");
Assert.AreEqual (0x03, UniBytes [3], "Uni #5");
}
[Test]
public void TestEncodingDecodingGetBytes1()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
//Encode the unicode string.
byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
//Decode the bytes to a unicode char array.
char[] UniChars = UnicodeEnc.GetChars (UniBytes);
string Result = new string(UniChars);
Assert.AreEqual (Unicode, Result, "Uni #1");
}
[Test]
public void TestEncodingDecodingGetBytes2()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
//Encode the unicode string.
byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
//Decode the bytes to a unicode char array.
char[] UniChars = UnicodeEnc.GetChars (UniBytes);
string Result = new string(UniChars);
Assert.AreEqual (Unicode, Result, "Uni #1");
}
[Test]
public void TestEncodingGetCharCount ()
{
byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0};
UnicodeEncoding encoding = new UnicodeEncoding ();
Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2),
"GetCharCount #1");
}
[Test]
public void TestPreamble1()
{
//litle-endian with byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, true);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0xFF, PreAmble [0], "Uni #1");
Assert.AreEqual (0xFE, PreAmble [1], "Uni #2");
}
[Test]
public void TestPreamble2()
{
//big-endian with byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0xFE, PreAmble [0], "Uni #1");
Assert.AreEqual (0xFF, PreAmble [1], "Uni #2");
}
[Test]
public void TestPreamble3()
{
//little-endian without byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, false);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0, PreAmble.Length, "Uni #1");
}
[Test]
public void TestMaxCharCount()
{
UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
Assert.AreEqual (26, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
Assert.AreEqual (27, UnicodeEnc.GetMaxCharCount(51), "UTF #2");
}
[Test]
public void TestMaxByteCount()
{
UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
Assert.AreEqual (102, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
}
[Test]
public void ZeroLengthArrays ()
{
UnicodeEncoding encoding = new UnicodeEncoding ();
encoding.GetCharCount (new byte [0]);
encoding.GetChars (new byte [0]);
encoding.GetCharCount (new byte [0], 0, 0);
encoding.GetChars (new byte [0], 0, 0);
encoding.GetChars (new byte [0], 0, 0, new char [0], 0);
encoding.GetByteCount (new char [0]);
encoding.GetBytes (new char [0]);
encoding.GetByteCount (new char [0], 0, 0);
encoding.GetBytes (new char [0], 0, 0);
encoding.GetBytes (new char [0], 0, 0, new byte [0], 0);
encoding.GetByteCount ("");
encoding.GetBytes ("");
}
[Test]
public void ByteOrderMark ()
{
string littleEndianString = "\ufeff\u0042\u004f\u004d";
string bigEndianString = "\ufffe\u4200\u4f00\u4d00";
byte [] littleEndianBytes = new byte [] {0xff, 0xfe, 0x42, 0x00, 0x4f, 0x00, 0x4d, 0x00};
byte [] bigEndianBytes = new byte [] {0xfe, 0xff, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4d};
UnicodeEncoding encoding;
encoding = new UnicodeEncoding (false, true);
Assert.AreEqual (encoding.GetBytes (littleEndianString), littleEndianBytes, "BOM #1");
Assert.AreEqual (encoding.GetBytes (bigEndianString), bigEndianBytes, "BOM #2");
Assert.AreEqual (encoding.GetString (littleEndianBytes), littleEndianString, "BOM #3");
Assert.AreEqual (encoding.GetString (bigEndianBytes), bigEndianString, "BOM #4");
encoding = new UnicodeEncoding (true, true);
Assert.AreEqual (encoding.GetBytes (littleEndianString), bigEndianBytes, "BOM #5");
Assert.AreEqual (encoding.GetBytes (bigEndianString), littleEndianBytes, "BOM #6");
Assert.AreEqual (encoding.GetString (littleEndianBytes), bigEndianString, "BOM #7");
Assert.AreEqual (encoding.GetString (bigEndianBytes), littleEndianString, "BOM #8");
}
[Test]
public void GetString_Odd_Count_0 ()
{
byte [] array = new byte [3];
string s = Encoding.Unicode.GetString (array, 0, 3);
Assert.AreEqual (0, (int) s [0], "0");
Assert.AreEqual (2, s.Length, "Length");
Assert.AreEqual (65533, (int) s [1], "1");
}
[Test]
public void GetString_Odd_Count_ff ()
{
byte [] array = new byte [3] { 0xff, 0xff, 0xff };
string s = Encoding.Unicode.GetString (array, 0, 3);
Assert.AreEqual (65535, (int) s [0], "0");
Assert.AreEqual (2, s.Length, "Length");
Assert.AreEqual (65533, (int) s [1], "1");
}
[Test]
public void GetMaxByteCountIncludesBOM ()
{
Assert.AreEqual (2, Encoding.Unicode.GetMaxByteCount (0), "#1");
Assert.AreEqual (4, Encoding.Unicode.GetMaxByteCount (1), "#2");
Assert.AreEqual (6, Encoding.Unicode.GetMaxByteCount (2), "#3");
Assert.AreEqual (10, Encoding.Unicode.GetMaxByteCount (4), "#4");
Assert.AreEqual (20, Encoding.Unicode.GetMaxByteCount (9), "#5");
Assert.AreEqual (22, Encoding.Unicode.GetMaxByteCount (10), "#6");
}
[Test]
public void GetMaxCharCountRoundsCorrectly ()
{
Assert.AreEqual (1, Encoding.Unicode.GetMaxCharCount (0), "#1");
Assert.AreEqual (2, Encoding.Unicode.GetMaxCharCount (1), "#2");
Assert.AreEqual (2, Encoding.Unicode.GetMaxCharCount (2), "#3");
Assert.AreEqual (3, Encoding.Unicode.GetMaxCharCount (4), "#4");
Assert.AreEqual (6, Encoding.Unicode.GetMaxCharCount (9), "#5");
Assert.AreEqual (6, Encoding.Unicode.GetMaxCharCount (10), "#6");
}
}
}
|