move GCLatencyMode to System.Runtime
[mono.git] / mcs / class / corlib / Test / System.Text / UTF8EncodingTest.cs
1 //
2 // UTF8EncodingTest.cs - NUnit Test Cases for System.Text.UTF8Encoding
3 //
4 // Authors:
5 //      Patrick Kalkman  kalkman@cistron.nl
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2003 Patrick Kalkman
9 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14 using System.Text;
15
16 #if NET_2_0
17 using DecoderException = System.Text.DecoderFallbackException;
18 #else
19 using DecoderException = System.ArgumentException;
20 #endif
21
22 using AssertType = NUnit.Framework.Assert;
23
24 namespace MonoTests.System.Text
25 {
26         [TestFixture]
27         public class UTF8EncodingTest : Assertion
28         {
29                 private UTF8Encoding utf8;
30
31                 [SetUp]
32                 public void Create () 
33                 {
34                         utf8 = new UTF8Encoding (true, true);
35                 }
36
37                 [Test]
38                 public void IsBrowserDisplay ()
39                 {
40                         Assert (utf8.IsBrowserDisplay);
41                 }
42
43                 [Test]
44                 public void IsBrowserSave ()
45                 {
46                         Assert (utf8.IsBrowserSave);
47                 }
48
49                 [Test]
50                 public void IsMailNewsDisplay ()
51                 {
52                         Assert (utf8.IsMailNewsDisplay);
53                 }
54
55                 [Test]
56                 public void IsMailNewsSave ()
57                 {
58                         Assert (utf8.IsMailNewsSave);
59                 }
60
61                 [Test]
62                 public void TestEncodingGetBytes1()
63                 {
64                         UTF8Encoding utf8Enc = new UTF8Encoding ();
65                         string UniCode = "\u0041\u2262\u0391\u002E";
66
67                         // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E 
68                         // see (RFC 2044)
69                         byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
70
71                         Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]);
72                         Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]);
73                         Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]);
74                         Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]);
75                         Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]);
76                         Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]);
77                         Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]);
78                 }
79
80                 [Test]
81                 public void TestEncodingGetBytes2()
82                 {
83                         UTF8Encoding utf8Enc = new UTF8Encoding ();
84                         string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
85
86                         // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 
87                         // see (RFC 2044)
88                         byte[] utf8Bytes = new byte [11];
89
90                         int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
91                         Assertion.AssertEquals ("UTF #1", 11, ByteCnt);
92                         Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]);
93                         Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]);
94                         Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]);
95                         Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]);
96                         Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]);
97                         Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]);
98                         Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]);
99                         Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]);
100                         Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]);
101                         Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]);
102                         Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]);
103                 }
104
105                 [Test]
106                 public void TestDecodingGetChars1()
107                 {
108                         UTF8Encoding utf8Enc = new UTF8Encoding ();
109                         // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>." 
110                         // see (RFC 2044)
111                         byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
112                         char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
113
114                         Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]);
115                         Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]);
116                         Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]);
117                         Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]);
118                 }
119
120                 [Test]
121 #if NET_2_0
122                 [Category ("NotWorking")]
123 #endif
124                 public void TestMaxCharCount()
125                 {
126                         UTF8Encoding UTF8enc = new UTF8Encoding ();
127 #if NET_2_0
128                         // hmm, where is this extra 1 coming from?
129                         Assertion.AssertEquals ("UTF #1", 51, UTF8enc.GetMaxCharCount(50));
130 #else
131                         Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50));
132 #endif
133                 }
134
135                 [Test]
136 #if NET_2_0
137                 [Category ("NotWorking")]
138 #endif
139                 public void TestMaxByteCount()
140                 {
141                         UTF8Encoding UTF8enc = new UTF8Encoding ();
142 #if NET_2_0
143                         // maybe under .NET 2.0 insufficient surrogate pair is
144                         // just not handled, and 3 is Preamble size.
145                         Assertion.AssertEquals ("UTF #1", 153, UTF8enc.GetMaxByteCount(50));
146 #else
147                         Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
148 #endif
149                 }
150
151                 // regression for bug #59648
152                 [Test]
153                 public void TestThrowOnInvalid ()
154                 {
155                         UTF8Encoding u = new UTF8Encoding (true, false);
156
157                         byte[] data = new byte [] { 0xC0, 0xAF };
158 #if NET_2_0
159                         AssertEquals ("#A0", 2, u.GetCharCount (data));
160                         string s = u.GetString (data);
161                         AssertEquals ("#A1", "\uFFFD\uFFFD", s);
162 #else
163                         AssertEquals ("#A0", 0, u.GetCharCount (data));
164                         string s = u.GetString (data);
165                         AssertEquals ("#A1", String.Empty, s);
166 #endif
167
168                         data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
169                         s = u.GetString (data);
170 #if NET_2_0
171                         AssertEquals ("#B1", 6, s.Length);
172                         AssertEquals ("#B2", 0x30, (int) s [0]);
173                         AssertEquals ("#B3", 0x31, (int) s [1]);
174                         AssertEquals ("#B4", 0xFFFD, (int) s [2]);
175                         AssertEquals ("#B5", 0xFFFD, (int) s [3]);
176                         AssertEquals ("#B6", 0x30, (int) s [4]);
177                         AssertEquals ("#B7", 0x32, (int) s [5]);
178 #else
179                         AssertEquals ("#B1", 4, s.Length);
180                         AssertEquals ("#B2", 0x30, (int) s [0]);
181                         AssertEquals ("#B3", 0x31, (int) s [1]);
182                         AssertEquals ("#B4", 0x30, (int) s [2]);
183                         AssertEquals ("#B5", 0x32, (int) s [3]);
184 #endif
185                 }
186
187                 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
188
189                 [Test]
190                 public void T1_Correct_GreekWord_kosme () 
191                 {
192                         byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
193                         string s = utf8.GetString (data);
194                         // cute but saving source code in unicode can be problematic
195                         // so we just ensure we can re-encode this
196                         AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)));
197                 }
198
199                 [Test]
200                 public void T2_Boundary_1_FirstPossibleSequence_Pass () 
201                 {
202                         byte[] data211 = { 0x00 };
203                         string s = utf8.GetString (data211);
204                         AssertEquals ("1 byte  (U-00000000)", "\0", s);
205                         AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)));
206
207                         byte[] data212 = { 0xC2, 0x80 };
208                         s = utf8.GetString (data212);
209                         AssertEquals ("2 bytes (U-00000080)", 128, s [0]);
210                         AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)));
211
212                         byte[] data213 = { 0xE0, 0xA0, 0x80 };
213                         s = utf8.GetString (data213);
214                         AssertEquals ("3 bytes (U-00000800)", 2048, s [0]);
215                         AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)));
216
217                         byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
218                         s = utf8.GetString (data214);
219                         AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]);
220                         AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]);
221                         AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)));
222                 }
223
224                 [Test]
225                 // Fail on MS Fx 1.1
226                 [ExpectedException (typeof (DecoderException))]
227                 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 () 
228                 {
229                         byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
230                         string s = utf8.GetString (data215);
231                         AssertNull ("5 bytes (U-00200000)", s);
232                         AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)));
233                 }
234
235                 [Test]
236                 // Fail on MS Fx 1.1
237                 [ExpectedException (typeof (DecoderException))]
238                 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 () 
239                 {
240                         byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
241                         string s = utf8.GetString (data216);
242                         AssertNull ("6 bytes (U-04000000)", s);
243                         AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)));
244                 }
245
246                 [Test]
247                 public void T2_Boundary_2_LastPossibleSequence_Pass () 
248                 {
249                         byte[] data221 = { 0x7F };
250                         string s = utf8.GetString (data221);
251                         AssertEquals ("1 byte  (U-0000007F)", 127, s [0]);
252                         AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)));
253
254                         byte[] data222 = { 0xDF, 0xBF };
255                         s = utf8.GetString (data222);
256                         AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]);
257                         AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)));
258
259                         byte[] data223 = { 0xEF, 0xBF, 0xBF };
260                         s = utf8.GetString (data223);
261                         AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]);
262                         AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)));
263
264                 }
265
266                 [Test]
267                 // Fail on MS Fx 1.1
268                 [ExpectedException (typeof (DecoderException))]
269                 public void T2_Boundary_2_LastPossibleSequence_Fail_4 () 
270                 {
271                         byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
272                         string s = utf8.GetString (data224);
273                         AssertNull ("4 bytes (U-001FFFFF)", s);
274                         AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)));
275                 }
276
277                 [Test]
278                 // Fail on MS Fx 1.1
279                 [ExpectedException (typeof (DecoderException))]
280                 public void T2_Boundary_2_LastPossibleSequence_Fail_5 () 
281                 {
282                         byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
283                         string s = utf8.GetString (data225);
284                         AssertNull ("5 bytes (U-03FFFFFF)", s);
285                         AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)));
286                 }
287
288                 [Test]
289                 // Fail on MS Fx 1.1
290                 [ExpectedException (typeof (DecoderException))]
291                 public void T2_Boundary_2_LastPossibleSequence_Fail_6 () 
292                 {
293                         byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
294                         string s = utf8.GetString (data226);
295                         AssertNull ("6 bytes (U-7FFFFFFF)", s);
296                         AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)));
297                 }
298
299                 [Test]
300                 public void T2_Boundary_3_Other_Pass () 
301                 {
302                         byte[] data231 = { 0xED, 0x9F, 0xBF };
303                         string s = utf8.GetString (data231);
304                         AssertEquals ("U-0000D7FF", 55295, s [0]);
305                         AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)));
306
307                         byte[] data232 = { 0xEE, 0x80, 0x80 };
308                         s = utf8.GetString (data232);
309                         AssertEquals ("U-0000E000", 57344, s [0]);
310                         AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)));
311
312                         byte[] data233 = { 0xEF, 0xBF, 0xBD };
313                         s = utf8.GetString (data233);
314                         AssertEquals ("U-0000FFFD", 65533, s [0]);
315                         AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)));
316
317                         byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
318                         s = utf8.GetString (data234);
319                         AssertEquals ("U-0010FFFF-0", 56319, s [0]);
320                         AssertEquals ("U-0010FFFF-1", 57343, s [1]);
321                         AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)));
322                 }
323
324                 [Test]
325                 // Fail on MS Fx 1.1
326                 [ExpectedException (typeof (DecoderException))]
327                 public void T2_Boundary_3_Other_Fail_5 () 
328                 {
329                         byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
330                         string s = utf8.GetString (data235);
331                         AssertNull ("U-00110000", s);
332                         AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)));
333                 }
334
335                 [Test]
336                 [ExpectedException (typeof (DecoderException))]
337                 public void T3_Malformed_1_UnexpectedContinuation_311 () 
338                 {
339                         byte[] data = { 0x80 };
340                         string s = utf8.GetString (data);
341                         // exception is "really" expected here
342                 }
343
344                 [Test]
345                 [ExpectedException (typeof (DecoderException))]
346                 public void T3_Malformed_1_UnexpectedContinuation_312 () 
347                 {
348                         byte[] data = { 0xBF };
349                         string s = utf8.GetString (data);
350                         // exception is "really" expected here
351                 }
352
353                 [Test]
354                 [ExpectedException (typeof (DecoderException))]
355                 public void T3_Malformed_1_UnexpectedContinuation_313 () 
356                 {
357                         byte[] data = { 0x80, 0xBF };
358                         string s = utf8.GetString (data);
359                         // exception is "really" expected here
360                 }
361
362                 [Test]
363                 [ExpectedException (typeof (DecoderException))]
364                 public void T3_Malformed_1_UnexpectedContinuation_314 () 
365                 {
366                         byte[] data = { 0x80, 0xBF, 0x80 };
367                         string s = utf8.GetString (data);
368                         // exception is "really" expected here
369                 }
370
371                 [Test]
372                 [ExpectedException (typeof (DecoderException))]
373                 public void T3_Malformed_1_UnexpectedContinuation_315 () 
374                 {
375                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
376                         string s = utf8.GetString (data);
377                         // exception is "really" expected here
378                 }
379
380                 [Test]
381                 [ExpectedException (typeof (DecoderException))]
382                 public void T3_Malformed_1_UnexpectedContinuation_316 () 
383                 {
384                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
385                         string s = utf8.GetString (data);
386                         // exception is "really" expected here
387                 }
388
389                 [Test]
390                 [ExpectedException (typeof (DecoderException))]
391                 public void T3_Malformed_1_UnexpectedContinuation_317 () 
392                 {
393                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
394                         string s = utf8.GetString (data);
395                         // exception is "really" expected here
396                 }
397
398                 [Test]
399                 [ExpectedException (typeof (DecoderException))]
400                 public void T3_Malformed_1_UnexpectedContinuation_318 () 
401                 {
402                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
403                         string s = utf8.GetString (data);
404                         // exception is "really" expected here
405                 }
406
407                 [Test]
408                 [ExpectedException (typeof (DecoderException))]
409                 public void T3_Malformed_1_UnexpectedContinuation_319 () 
410                 {
411                         // 64 different continuation characters
412                         byte[] data = {
413                                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 
414                                 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 
415                                 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 
416                                 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
417                         string s = utf8.GetString (data);
418                         // exception is "really" expected here
419                 }
420
421                 [Test]
422                 [ExpectedException (typeof (DecoderException))]
423                 public void T3_Malformed_2_LonelyStart_321 ()
424                 {
425                         byte[] data = { 
426                                 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20, 
427                                 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20, 
428                                 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20, 
429                                 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
430                         string s = utf8.GetString (data);
431                         // exception is "really" expected here
432                 }
433
434                 [Test]
435                 [ExpectedException (typeof (DecoderException))]
436                 public void T3_Malformed_2_LonelyStart_322 () 
437                 {
438                         byte[] data = { 
439                                 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20, 
440                                 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
441                         string s = utf8.GetString (data);
442                         // exception is "really" expected here
443                 }
444
445                 [Test]
446                 [ExpectedException (typeof (DecoderException))]
447                 public void T3_Malformed_2_LonelyStart_323 () 
448                 {
449                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
450                         string s = utf8.GetString (data);
451                         // exception is "really" expected here
452                 }
453
454                 [Test]
455                 [ExpectedException (typeof (DecoderException))]
456                 public void T3_Malformed_2_LonelyStart_324 () 
457                 {
458                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
459                         string s = utf8.GetString (data);
460                         // exception is "really" expected here
461                 }
462
463                 [Test]
464                 [ExpectedException (typeof (DecoderException))]
465                 public void T3_Malformed_2_LonelyStart_325 () 
466                 {
467                         byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
468                         string s = utf8.GetString (data);
469                         // exception is "really" expected here
470                 }
471
472                 [Test]
473                 [ExpectedException (typeof (DecoderException))]
474                 public void T3_Malformed_3_LastContinuationMissing_331 () 
475                 {
476                         byte[] data = { 0xC0 };
477                         string s = utf8.GetString (data);
478                         // exception is "really" expected here
479                 }
480
481                 [Test]
482                 [ExpectedException (typeof (DecoderException))]
483                 public void T3_Malformed_3_LastContinuationMissing_332 () 
484                 {
485                         byte[] data = { 0xE0, 0x80 };
486                         string s = utf8.GetString (data);
487                         // exception is "really" expected here
488                 }
489
490                 [Test]
491                 [ExpectedException (typeof (DecoderException))]
492                 public void T3_Malformed_3_LastContinuationMissing_333 () 
493                 {
494                         byte[] data = { 0xF0, 0x80, 0x80 };
495                         string s = utf8.GetString (data);
496                         // exception is "really" expected here
497                 }
498
499                 [Test]
500                 [ExpectedException (typeof (DecoderException))]
501                 public void T3_Malformed_3_LastContinuationMissing_334 () 
502                 {
503                         byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
504                         string s = utf8.GetString (data);
505                         // exception is "really" expected here
506                 }
507
508                 [Test]
509                 [ExpectedException (typeof (DecoderException))]
510                 public void T3_Malformed_3_LastContinuationMissing_335 () 
511                 {
512                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
513                         string s = utf8.GetString (data);
514                         // exception is "really" expected here
515                 }
516
517                 [Test]
518 // MS Fx 1.1 accept this
519 //              [ExpectedException (typeof (DecoderException))]
520                 public void T3_Malformed_3_LastContinuationMissing_336 () 
521                 {
522                         byte[] data = { 0xDF };
523                         try {
524                                 string s = utf8.GetString (data);
525                                 // exception is "really" expected here
526                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
527                         }
528                         catch (DecoderException) {
529                                 // but Mono doesn't - better stick to the standard
530                         }
531                 }
532
533                 [Test]
534                 // MS Fx 1.1 accept this
535 //              [ExpectedException (typeof (DecoderException))]
536                 public void T3_Malformed_3_LastContinuationMissing_337 () 
537                 {
538                         byte[] data = { 0xEF, 0xBF };
539                         try {
540                                 string s = utf8.GetString (data);
541                                 // exception is "really" expected here
542                                 AssertEquals ("MS FX 1.1 behaviour", String.Empty, s);
543                         }
544                         catch (DecoderException) {
545                                 // but Mono doesn't - better stick to the standard
546                         }
547                 }
548
549                 [Test]
550                 [ExpectedException (typeof (DecoderException))]
551                 public void T3_Malformed_3_LastContinuationMissing_338 () 
552                 {
553                         byte[] data = { 0xF7, 0xBF, 0xBF };
554                         string s = utf8.GetString (data);
555                         // exception is "really" expected here
556                 }
557
558                 [Test]
559                 [ExpectedException (typeof (DecoderException))]
560                 public void T3_Malformed_3_LastContinuationMissing_339 () 
561                 {
562                         byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
563                         string s = utf8.GetString (data);
564                         // exception is "really" expected here
565                 }
566
567                 [Test]
568                 [ExpectedException (typeof (DecoderException))]
569                 public void T3_Malformed_3_LastContinuationMissing_3310 () 
570                 {
571                         byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
572                         string s = utf8.GetString (data);
573                         // exception is "really" expected here
574                 }
575
576                 [Test]
577                 [ExpectedException (typeof (DecoderException))]
578                 public void T3_Malformed_4_ConcatenationImcomplete () 
579                 {
580                         byte[] data = {
581                                 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF, 
582                                 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
583                         string s = utf8.GetString (data);
584                         // exception is "really" expected here
585                 }
586
587                 [Test]
588                 [ExpectedException (typeof (DecoderException))]
589                 public void T3_Malformed_5_ImpossibleBytes_351 () 
590                 {
591                         byte[] data = { 0xFE };
592                         string s = utf8.GetString (data);
593                         // exception is "really" expected here
594                 }
595
596                 [Test]
597                 [ExpectedException (typeof (DecoderException))]
598                 public void T3_Malformed_5_ImpossibleBytes_352 () 
599                 {
600                         byte[] data = { 0xFF };
601                         string s = utf8.GetString (data);
602                         // exception is "really" expected here
603                 }
604
605                 [Test]
606                 [ExpectedException (typeof (DecoderException))]
607                 public void T3_Malformed_5_ImpossibleBytes_353 () 
608                 {
609                         byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
610                         string s = utf8.GetString (data);
611                         // exception is "really" expected here
612                 }
613
614                 // Overlong == dangereous -> "safe" decoder should reject them
615
616                 [Test]
617                 [ExpectedException (typeof (DecoderException))]
618                 public void T4_Overlong_1_ASCII_Slash_411 () 
619                 {
620                         byte[] data = { 0xC0, 0xAF };
621                         string s = utf8.GetString (data);
622                         // exception is "really" expected here
623                 }
624
625                 [Test]
626                 [ExpectedException (typeof (DecoderException))]
627                 public void T4_Overlong_1_ASCII_Slash_412 () 
628                 {
629                         byte[] data = { 0xE0, 0x80, 0xAF };
630                         string s = utf8.GetString (data);
631                         // exception is "really" expected here
632                 }
633
634                 [Test]
635                 [ExpectedException (typeof (DecoderException))]
636                 public void T4_Overlong_1_ASCII_Slash_413 () 
637                 {
638                         byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
639                         string s = utf8.GetString (data);
640                         // exception is "really" expected here
641                 }
642
643                 [Test]
644                 [ExpectedException (typeof (DecoderException))]
645                 public void T4_Overlong_1_ASCII_Slash_414 () 
646                 {
647                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
648                         string s = utf8.GetString (data);
649                         // exception is "really" expected here
650                 }
651
652                 [Test]
653                 [ExpectedException (typeof (DecoderException))]
654                 public void T4_Overlong_1_ASCII_Slash_415 () 
655                 {
656                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
657                         string s = utf8.GetString (data);
658                         // exception is "really" expected here
659                 }
660
661                 [Test]
662                 [ExpectedException (typeof (DecoderException))]
663                 public void T4_Overlong_2_MaximumBoundary_421 () 
664                 {
665                         byte[] data = { 0xC1, 0xBF };
666                         string s = utf8.GetString (data);
667                         // exception is "really" expected here
668                 }
669
670                 [Test]
671                 [ExpectedException (typeof (DecoderException))]
672                 public void T4_Overlong_2_MaximumBoundary_422 () 
673                 {
674                         byte[] data = { 0xE0, 0x9F, 0xBF };
675                         string s = utf8.GetString (data);
676                         // exception is "really" expected here
677                 }
678
679                 [Test]
680                 [ExpectedException (typeof (DecoderException))]
681                 public void T4_Overlong_2_MaximumBoundary_423 () 
682                 {
683                         byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
684                         string s = utf8.GetString (data);
685                         // exception is "really" expected here
686                 }
687
688                 [Test]
689                 [ExpectedException (typeof (DecoderException))]
690                 public void T4_Overlong_2_MaximumBoundary_424 () 
691                 {
692                         byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
693                         string s = utf8.GetString (data);
694                         // exception is "really" expected here
695                 }
696
697                 [Test]
698                 [ExpectedException (typeof (DecoderException))]
699                 public void T4_Overlong_2_MaximumBoundary_425 () 
700                 {
701                         byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
702                         string s = utf8.GetString (data);
703                         // exception is "really" expected here
704                 }
705
706                 [Test]
707                 [ExpectedException (typeof (DecoderException))]
708                 public void T4_Overlong_3_NUL_431 () 
709                 {
710                         byte[] data = { 0xC0, 0x80 };
711                         string s = utf8.GetString (data);
712                         // exception is "really" expected here
713                 }
714
715                 [Test]
716                 [ExpectedException (typeof (DecoderException))]
717                 public void T4_Overlong_3_NUL_432 () 
718                 {
719                         byte[] data = { 0xE0, 0x80, 0x80 };
720                         string s = utf8.GetString (data);
721                         // exception is "really" expected here
722                 }
723
724                 [Test]
725                 [ExpectedException (typeof (DecoderException))]
726                 public void T4_Overlong_3_NUL_433 () 
727                 {
728                         byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
729                         string s = utf8.GetString (data);
730                         // exception is "really" expected here
731                 }
732
733                 [Test]
734                 [ExpectedException (typeof (DecoderException))]
735                 public void T4_Overlong_3_NUL_434 () 
736                 {
737                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
738                         string s = utf8.GetString (data);
739                         // exception is "really" expected here
740                 }
741
742                 [Test]
743                 [ExpectedException (typeof (DecoderException))]
744                 public void T4_Overlong_3_NUL_435 () 
745                 {
746                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
747                         string s = utf8.GetString (data);
748                         // exception is "really" expected here
749                 }
750
751                 [Test]
752 #if NET_2_0
753                 [ExpectedException (typeof (DecoderFallbackException))]
754 #else
755 // MS Fx 1.1 accept this
756                 [Category ("NotDotNet")]
757                 [ExpectedException (typeof (DecoderException))]
758 #endif
759                         public void T5_IllegalCodePosition_1_UTF16Surrogates_511 () 
760                 {
761                         byte[] data = { 0xED, 0xA0, 0x80 };
762                         string s = utf8.GetString (data);
763                         // exception is "really" expected here
764                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
765                 }
766
767                 [Test]
768 #if NET_2_0
769                 [ExpectedException (typeof (DecoderFallbackException))]
770 #else
771 // MS Fx 1.1 accept this
772                 [Category ("NotDotNet")]
773                 [ExpectedException (typeof (DecoderException))]
774 #endif
775                 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 () 
776                 {
777                         byte[] data = { 0xED, 0xAD, 0xBF };
778                         string s = utf8.GetString (data);
779                         // exception is "really" expected here
780                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
781                 }
782
783                 [Test]
784 #if NET_2_0
785                 [ExpectedException (typeof (DecoderFallbackException))]
786 #else
787 // MS Fx 1.1 accept this
788                 [Category ("NotDotNet")]
789                 [ExpectedException (typeof (DecoderException))]
790 #endif
791                 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
792                 {
793                         byte[] data = { 0xED, 0xAE, 0x80 };
794                         string s = utf8.GetString (data);
795                         // exception is "really" expected here
796                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
797                 }
798
799                 [Test]
800 #if NET_2_0
801                 [ExpectedException (typeof (DecoderFallbackException))]
802 #else
803 // MS Fx 1.1 accept this
804                 [Category ("NotDotNet")]
805                 [ExpectedException (typeof (DecoderException))]
806 #endif
807                 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 () 
808                 {
809                         byte[] data = { 0xED, 0xAF, 0xBF };
810                         string s = utf8.GetString (data);
811                         // exception is "really" expected here
812                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
813                 }
814
815                 [Test]
816 #if NET_2_0
817                 [ExpectedException (typeof (DecoderFallbackException))]
818 #else
819 // MS Fx 1.1 accept this
820                 [Category ("NotDotNet")]
821                 [ExpectedException (typeof (DecoderException))]
822 #endif
823                 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
824                 {
825                         byte[] data = { 0xED, 0xB0, 0x80 };
826                         string s = utf8.GetString (data);
827                         // exception is "really" expected here
828                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]);
829                 }
830
831                 [Test]
832 #if NET_2_0
833                 [ExpectedException (typeof (DecoderFallbackException))]
834 #else
835 // MS Fx 1.1 accept this
836                 [Category ("NotDotNet")]
837                 [ExpectedException (typeof (DecoderException))]
838 #endif
839                 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 () 
840                 {
841                         byte[] data = { 0xED, 0xBE, 0x80 };
842                         string s = utf8.GetString (data);
843                         // exception is "really" expected here
844                         AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]);
845                 }
846
847                 [Test]
848 #if NET_2_0
849                 [ExpectedException (typeof (DecoderFallbackException))]
850 #else
851 // MS Fx 1.1 accept this
852                 [Category ("NotDotNet")]
853                 [ExpectedException (typeof (DecoderException))]
854 #endif
855                 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 () 
856                 {
857                         byte[] data = { 0xED, 0xBF, 0xBF };
858                         string s = utf8.GetString (data);
859                         // exception is "really" expected here
860                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]);
861                 }
862
863                 [Test]
864 #if NET_2_0
865                 [ExpectedException (typeof (DecoderFallbackException))]
866 #else
867 // MS Fx 1.1 accept this
868                 [Category ("NotDotNet")]
869                 [ExpectedException (typeof (DecoderException))]
870 #endif
871                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 () 
872                 {
873                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
874                         string s = utf8.GetString (data);
875                         // exception is "really" expected here
876                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
877                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
878                 }
879
880                 [Test]
881 #if NET_2_0
882                 [ExpectedException (typeof (DecoderFallbackException))]
883 #else
884 // MS Fx 1.1 accept this
885                 [Category ("NotDotNet")]
886                 [ExpectedException (typeof (DecoderException))]
887 #endif
888                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 () 
889                 {
890                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
891                         string s = utf8.GetString (data);
892                         // exception is "really" expected here
893                         AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]);
894                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
895                 }
896
897                 [Test]
898 #if NET_2_0
899                 [ExpectedException (typeof (DecoderFallbackException))]
900 #else
901 // MS Fx 1.1 accept this
902                 [Category ("NotDotNet")]
903                 [ExpectedException (typeof (DecoderException))]
904 #endif
905                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 () 
906                 {
907                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
908                         string s = utf8.GetString (data);
909                         // exception is "really" expected here
910                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
911                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
912                 }
913
914                 [Test]
915 #if NET_2_0
916                 [ExpectedException (typeof (DecoderFallbackException))]
917 #else
918 // MS Fx 1.1 accept this
919                 [Category ("NotDotNet")]
920                 [ExpectedException (typeof (DecoderException))]
921 #endif
922                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 () 
923                 {
924                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
925                         string s = utf8.GetString (data);
926                         // exception is "really" expected here
927                         AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]);
928                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
929                 }
930
931                 [Test]
932 #if NET_2_0
933                 [ExpectedException (typeof (DecoderFallbackException))]
934 #else
935 // MS Fx 1.1 accept this
936                 [Category ("NotDotNet")]
937                 [ExpectedException (typeof (DecoderException))]
938 #endif
939                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 () 
940                 {
941                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
942                         string s = utf8.GetString (data);
943                         // exception is "really" expected here
944                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
945                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
946                 }
947
948                 [Test]
949 #if NET_2_0
950                 [ExpectedException (typeof (DecoderFallbackException))]
951 #else
952 // MS Fx 1.1 accept this
953                 [Category ("NotDotNet")]
954                 [ExpectedException (typeof (DecoderException))]
955 #endif
956                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 () 
957                 {
958                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
959                         string s = utf8.GetString (data);
960                         // exception is "really" expected here
961                         AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]);
962                         AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]);
963                 }
964
965                 [Test]
966 #if NET_2_0
967                 [ExpectedException (typeof (DecoderFallbackException))]
968 #else
969 // MS Fx 1.1 accept this
970                 [Category ("NotDotNet")]
971                 [ExpectedException (typeof (DecoderException))]
972 #endif
973                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 () 
974                 {
975                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
976                         string s = utf8.GetString (data);
977                         // exception is "really" expected here
978                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
979                         AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]);
980                 }
981
982                 [Test]
983 #if NET_2_0
984                 [ExpectedException (typeof (DecoderFallbackException))]
985 #else
986 // MS Fx 1.1 accept this
987                 [Category ("NotDotNet")]
988                 [ExpectedException (typeof (DecoderException))]
989 #endif
990                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 () 
991                 {
992                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
993                         string s = utf8.GetString (data);
994                         // exception is "really" expected here
995                         AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]);
996                         AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]);
997                 }
998
999                 [Test]
1000 // MS Fx 1.1 accept this
1001 //              [ExpectedException (typeof (DecoderException))]
1002                 public void T5_IllegalCodePosition_3_Other_531 () 
1003                 {
1004                         byte[] data = { 0xEF, 0xBF, 0xBE };
1005                         string s = utf8.GetString (data);
1006                         // exception is "really" expected here
1007                         AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]);
1008                 }
1009
1010                 [Test]
1011 // MS Fx 1.1 accept this
1012 //              [ExpectedException (typeof (DecoderException))]
1013                 public void T5_IllegalCodePosition_3_Other_532 () 
1014                 {
1015                         byte[] data = { 0xEF, 0xBF, 0xBF };
1016                         string s = utf8.GetString (data);
1017                         // exception is "really" expected here
1018                         AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]);
1019                 }
1020
1021                 [Test]
1022                 // bug #75065 and #73086.
1023                 public void GetCharsFEFF ()
1024                 {
1025                         byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
1026                         Encoding enc = new UTF8Encoding (false, true);
1027                         string s = enc.GetString (data);
1028                         AssertEquals ("\uFEFF", s);
1029
1030                         Encoding utf = Encoding.UTF8;
1031                         char[] testChars = {'\uFEFF','A'};
1032
1033                         byte[] bytes = utf.GetBytes(testChars);
1034                         char[] chars = utf.GetChars(bytes);
1035                         AssertEquals ("#1", '\uFEFF', chars [0]);
1036                         AssertEquals ("#2", 'A', chars [1]);
1037                 }
1038
1039 #if NET_2_0
1040                 [Test]
1041                 public void CloneNotReadOnly ()
1042                 {
1043                         Encoding e = Encoding.GetEncoding (65001).Clone ()
1044                                 as Encoding;
1045                         AssertEquals (false, e.IsReadOnly);
1046                         e.EncoderFallback = new EncoderExceptionFallback ();
1047                 }
1048 #endif
1049
1050                 [Test]
1051 #if NET_2_0
1052                 [ExpectedException (typeof (DecoderFallbackException))]
1053 #else
1054                 [ExpectedException (typeof (ArgumentException))]
1055                 [Category ("NotDotNet")] // MS Bug
1056 #endif
1057                 public void Bug77315 ()
1058                 {
1059                         new UTF8Encoding (false, true).GetString (
1060                                 new byte [] {0xED, 0xA2, 0x8C});
1061                 }
1062
1063                 [Test]
1064                 public void SufficientByteArray ()
1065                 {
1066                         Encoder e = Encoding.UTF8.GetEncoder ();
1067                         byte [] bytes = new byte [0];
1068
1069                         char [] chars = new char [] {'\uD800'};
1070                         e.GetBytes (chars, 0, 1, bytes, 0, false);
1071                         try {
1072                                 int ret = e.GetBytes (chars, 1, 0, bytes, 0, true);
1073 #if NET_2_0
1074                                 AssertEquals ("drop insufficient char in 2.0: char[]", 0, ret);
1075 #else
1076                                 Fail ("ArgumentException is expected: char[]");
1077 #endif
1078                         } catch (ArgumentException) {
1079                         }
1080
1081                         string s = "\uD800";
1082                         try {
1083                                 int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0);
1084 #if NET_2_0
1085                                 AssertEquals ("drop insufficient char in 2.0: string", 0, ret);
1086 #else
1087                                 Fail ("ArgumentException is expected: string");
1088 #endif
1089                         } catch (ArgumentException) {
1090                         }
1091                 }
1092
1093 #if NET_2_0
1094                 [Test] // bug #77550
1095                 public void DecoderFallbackSimple ()
1096                 {
1097                         UTF8Encoding e = new UTF8Encoding (false, false);
1098                         AssertType.AreEqual (1, e.GetDecoder ().GetCharCount (
1099                                         new byte [] {(byte) 183}, 0, 1),
1100                                         "#1");
1101                         AssertType.AreEqual (1, e.GetDecoder().GetChars (
1102                                         new byte [] {(byte) 183}, 0, 1,
1103                                         new char [100], 0),
1104                                         "#2");
1105                         AssertType.AreEqual (1, e.GetString (new byte [] {(byte) 183}).Length,
1106                                         "#3");
1107                 }
1108
1109                 [Test]
1110                 public void FallbackDefaultEncodingUTF8 ()
1111                 {
1112                         DecoderReplacementFallbackBuffer b =
1113                                 Encoding.UTF8.DecoderFallback.CreateFallbackBuffer ()
1114                                 as DecoderReplacementFallbackBuffer;
1115                         AssertType.IsTrue (b.Fallback (new byte [] {}, 0), "#1");
1116                         AssertType.IsFalse (b.MovePrevious (), "#2");
1117                         AssertType.AreEqual (1, b.Remaining, "#3");
1118                         AssertType.AreEqual ('\uFFFD', b.GetNextChar (), "#4");
1119                 }
1120 #endif
1121         }
1122 }