2 // UnicodeEncodingTest.cs - NUnit Test Cases for System.Text.UnicodeEncoding
5 // Patrick Kalkman kalkman@cistron.nl
7 // (C) 2003 Patrick Kalkman
13 namespace MonoTests.System.Text
16 public class UnicodeEncodingTest
19 public void IsBrowserDisplay ()
21 UnicodeEncoding enc = new UnicodeEncoding ();
22 Assert.IsFalse (enc.IsBrowserDisplay);
26 public void IsBrowserSave ()
28 UnicodeEncoding enc = new UnicodeEncoding ();
29 Assert.IsTrue (enc.IsBrowserSave);
33 public void IsMailNewsDisplay ()
35 UnicodeEncoding enc = new UnicodeEncoding ();
36 Assert.IsFalse (enc.IsMailNewsDisplay);
40 public void IsMailNewsSave ()
42 UnicodeEncoding enc = new UnicodeEncoding ();
43 Assert.IsFalse (enc.IsMailNewsSave);
47 public void TestEncodingGetBytes1()
49 //pi and sigma in unicode
50 string Unicode = "\u03a0\u03a3";
52 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
53 UniBytes = UnicodeEnc.GetBytes (Unicode);
55 Assert.AreEqual (0xA0, UniBytes [0], "Uni #1");
56 Assert.AreEqual (0x03, UniBytes [1], "Uni #2");
57 Assert.AreEqual (0xA3, UniBytes [2], "Uni #3");
58 Assert.AreEqual (0x03, UniBytes [3], "Uni #4");
62 public void TestEncodingGetBytes2()
64 //pi and sigma in unicode
65 string Unicode = "\u03a0\u03a3";
67 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
68 UniBytes = UnicodeEnc.GetBytes (Unicode);
70 Assert.AreEqual (0x03, UniBytes [0], "Uni #1");
71 Assert.AreEqual (0xA0, UniBytes [1], "Uni #2");
72 Assert.AreEqual (0x03, UniBytes [2], "Uni #3");
73 Assert.AreEqual (0xA3, UniBytes [3], "Uni #4");
77 public void TestEncodingGetBytes3()
79 //pi and sigma in unicode
80 string Unicode = "\u03a0\u03a3";
81 byte[] UniBytes = new byte [4];
82 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
83 int Cnt = UnicodeEnc.GetBytes (Unicode.ToCharArray(), 0, Unicode.Length, UniBytes, 0);
85 Assert.AreEqual (4, Cnt, "Uni #1");
86 Assert.AreEqual (0xA0, UniBytes [0], "Uni #2");
87 Assert.AreEqual (0x03, UniBytes [1], "Uni #3");
88 Assert.AreEqual (0xA3, UniBytes [2], "Uni #4");
89 Assert.AreEqual (0x03, UniBytes [3], "Uni #5");
93 public void TestEncodingDecodingGetBytes1()
95 //pi and sigma in unicode
96 string Unicode = "\u03a0\u03a3";
97 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
98 //Encode the unicode string.
99 byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
100 //Decode the bytes to a unicode char array.
101 char[] UniChars = UnicodeEnc.GetChars (UniBytes);
102 string Result = new string(UniChars);
104 Assert.AreEqual (Unicode, Result, "Uni #1");
108 public void TestEncodingDecodingGetBytes2()
110 //pi and sigma in unicode
111 string Unicode = "\u03a0\u03a3";
112 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
113 //Encode the unicode string.
114 byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
115 //Decode the bytes to a unicode char array.
116 char[] UniChars = UnicodeEnc.GetChars (UniBytes);
117 string Result = new string(UniChars);
119 Assert.AreEqual (Unicode, Result, "Uni #1");
123 public void TestEncodingGetCharCount ()
125 byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0};
126 UnicodeEncoding encoding = new UnicodeEncoding ();
128 Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2),
135 public void TestPreamble1()
137 //litle-endian with byte order mark.
138 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, true);
139 byte[] PreAmble = UnicodeEnc.GetPreamble();
141 Assert.AreEqual (0xFF, PreAmble [0], "Uni #1");
142 Assert.AreEqual (0xFE, PreAmble [1], "Uni #2");
146 public void TestPreamble2()
148 //big-endian with byte order mark.
149 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true);
150 byte[] PreAmble = UnicodeEnc.GetPreamble();
152 Assert.AreEqual (0xFE, PreAmble [0], "Uni #1");
153 Assert.AreEqual (0xFF, PreAmble [1], "Uni #2");
157 public void TestPreamble3()
159 //little-endian without byte order mark.
160 UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, false);
161 byte[] PreAmble = UnicodeEnc.GetPreamble();
163 Assert.AreEqual (0, PreAmble.Length, "Uni #1");
168 [Category ("NotWorking")]
170 public void TestMaxCharCount()
172 UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
174 // where is this extra 1 coming from?
175 Assert.AreEqual (26, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
176 Assert.AreEqual (27, UnicodeEnc.GetMaxCharCount(51), "UTF #2");
178 Assert.AreEqual (25, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
184 [Category ("NotWorking")]
186 public void TestMaxByteCount()
188 UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
190 // is this extra 2 BOM?
191 Assert.AreEqual (102, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
193 Assert.AreEqual (100, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
198 public void ZeroLengthArrays ()
200 UnicodeEncoding encoding = new UnicodeEncoding ();
201 encoding.GetCharCount (new byte [0]);
202 encoding.GetChars (new byte [0]);
203 encoding.GetCharCount (new byte [0], 0, 0);
204 encoding.GetChars (new byte [0], 0, 0);
205 encoding.GetChars (new byte [0], 0, 0, new char [0], 0);
206 encoding.GetByteCount (new char [0]);
207 encoding.GetBytes (new char [0]);
208 encoding.GetByteCount (new char [0], 0, 0);
209 encoding.GetBytes (new char [0], 0, 0);
210 encoding.GetBytes (new char [0], 0, 0, new byte [0], 0);
211 encoding.GetByteCount ("");
212 encoding.GetBytes ("");
216 public void ByteOrderMark ()
218 string littleEndianString = "\ufeff\u0042\u004f\u004d";
219 string bigEndianString = "\ufffe\u4200\u4f00\u4d00";
220 byte [] littleEndianBytes = new byte [] {0xff, 0xfe, 0x42, 0x00, 0x4f, 0x00, 0x4d, 0x00};
221 byte [] bigEndianBytes = new byte [] {0xfe, 0xff, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4d};
222 UnicodeEncoding encoding;
224 encoding = new UnicodeEncoding (false, true);
225 Assert.AreEqual (encoding.GetBytes (littleEndianString), littleEndianBytes, "BOM #1");
226 Assert.AreEqual (encoding.GetBytes (bigEndianString), bigEndianBytes, "BOM #2");
227 Assert.AreEqual (encoding.GetString (littleEndianBytes), littleEndianString, "BOM #3");
228 Assert.AreEqual (encoding.GetString (bigEndianBytes), bigEndianString, "BOM #4");
230 encoding = new UnicodeEncoding (true, true);
231 Assert.AreEqual (encoding.GetBytes (littleEndianString), bigEndianBytes, "BOM #5");
232 Assert.AreEqual (encoding.GetBytes (bigEndianString), littleEndianBytes, "BOM #6");
233 Assert.AreEqual (encoding.GetString (littleEndianBytes), bigEndianString, "BOM #7");
234 Assert.AreEqual (encoding.GetString (bigEndianBytes), littleEndianString, "BOM #8");