Reverted changes of commit 6ad7f75a3777652a535ef4356e6d193eed4ed612
[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.IO;
15 using System.Text;
16
17 #if NET_2_0
18 using DecoderException = System.Text.DecoderFallbackException;
19 #else
20 using DecoderException = System.ArgumentException;
21 #endif
22
23 using AssertType = NUnit.Framework.Assert;
24
25 namespace MonoTests.System.Text
26 {
27         [TestFixture]
28         public class UTF8EncodingTest 
29         {
30                 private UTF8Encoding utf8;
31
32                 [SetUp]
33                 public void Create () 
34                 {
35                         utf8 = new UTF8Encoding (true, true);
36                 }
37
38                 [Test]
39                 public void IsBrowserDisplay ()
40                 {
41                         Assert.IsTrue (utf8.IsBrowserDisplay);
42                 }
43
44                 [Test]
45                 public void IsBrowserSave ()
46                 {
47                         Assert.IsTrue (utf8.IsBrowserSave);
48                 }
49
50                 [Test]
51                 public void IsMailNewsDisplay ()
52                 {
53                         Assert.IsTrue (utf8.IsMailNewsDisplay);
54                 }
55
56                 [Test]
57                 public void IsMailNewsSave ()
58                 {
59                         Assert.IsTrue (utf8.IsMailNewsSave);
60                 }
61
62                 [Test]
63                 public void TestCompat ()
64                 {
65                         Assert.IsTrue (new UTF8Encoding ().Equals (new UTF8Encoding ()));
66                 }
67                 
68                 [Test]
69                 public void TestEncodingGetBytes1()
70                 {
71                         UTF8Encoding utf8Enc = new UTF8Encoding ();
72                         string UniCode = "\u0041\u2262\u0391\u002E";
73
74                         // "A<NOT IDENTICAL TO><ALPHA>." may be encoded as 41 E2 89 A2 CE 91 2E 
75                         // see (RFC 2044)
76                         byte[] utf8Bytes = utf8Enc.GetBytes (UniCode);
77
78                         Assert.AreEqual (0x41, utf8Bytes [0], "UTF #1");
79                         Assert.AreEqual (0xE2, utf8Bytes [1], "UTF #2");
80                         Assert.AreEqual (0x89, utf8Bytes [2], "UTF #3");
81                         Assert.AreEqual (0xA2, utf8Bytes [3], "UTF #4");
82                         Assert.AreEqual (0xCE, utf8Bytes [4], "UTF #5");
83                         Assert.AreEqual (0x91, utf8Bytes [5], "UTF #6");
84                         Assert.AreEqual (0x2E, utf8Bytes [6], "UTF #7");
85                 }
86
87                 [Test]
88                 public void TestEncodingGetBytes2()
89                 {
90                         UTF8Encoding utf8Enc = new UTF8Encoding ();
91                         string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021";
92
93                         // "Hi Mom <WHITE SMILING FACE>!" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 
94                         // see (RFC 2044)
95                         byte[] utf8Bytes = new byte [11];
96
97                         int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0);
98                         Assert.AreEqual (11, ByteCnt, "UTF #1");
99                         Assert.AreEqual (0x48, utf8Bytes [0], "UTF #2");
100                         Assert.AreEqual (0x69, utf8Bytes [1], "UTF #3");
101                         Assert.AreEqual (0x20, utf8Bytes [2], "UTF #4");
102                         Assert.AreEqual (0x4D, utf8Bytes [3], "UTF #5");
103                         Assert.AreEqual (0x6F, utf8Bytes [4], "UTF #6");
104                         Assert.AreEqual (0x6D, utf8Bytes [5], "UTF #7");
105                         Assert.AreEqual (0x20, utf8Bytes [6], "UTF #8");
106                         Assert.AreEqual (0xE2, utf8Bytes [7], "UTF #9");
107                         Assert.AreEqual (0x98, utf8Bytes [8], "UTF #10");
108                         Assert.AreEqual (0xBA, utf8Bytes [9], "UTF #11");
109                         Assert.AreEqual (0x21, utf8Bytes [10], "UTF #12");
110                 }
111
112                 [Test]
113                 public void TestDecodingGetChars1()
114                 {
115                         UTF8Encoding utf8Enc = new UTF8Encoding ();
116                         // 41 E2 89 A2 CE 91 2E may be decoded as "A<NOT IDENTICAL TO><ALPHA>." 
117                         // see (RFC 2044)
118                         byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E};
119                         char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes);
120
121                         Assert.AreEqual (0x0041, UniCodeChars [0], "UTF #1");
122                         Assert.AreEqual (0x2262, UniCodeChars [1], "UTF #2");
123                         Assert.AreEqual (0x0391, UniCodeChars [2], "UTF #3");
124                         Assert.AreEqual (0x002E, UniCodeChars [3], "UTF #4");
125                 }
126
127                 [Test]
128 #if NET_2_0
129                 [Category ("NotWorking")]
130 #endif
131                 public void TestMaxCharCount()
132                 {
133                         UTF8Encoding UTF8enc = new UTF8Encoding ();
134 #if NET_2_0
135                         // hmm, where is this extra 1 coming from?
136                         Assert.AreEqual (51, UTF8enc.GetMaxCharCount(50), "UTF #1");
137 #else
138                         Assert.AreEqual (50, UTF8enc.GetMaxCharCount(50), "UTF #1");
139 #endif
140                 }
141
142                 [Test]
143 #if NET_2_0
144                 [Category ("NotWorking")]
145 #endif
146                 public void TestMaxByteCount()
147                 {
148                         UTF8Encoding UTF8enc = new UTF8Encoding ();
149 #if NET_2_0
150                         // maybe under .NET 2.0 insufficient surrogate pair is
151                         // just not handled, and 3 is Preamble size.
152                         Assert.AreEqual (153, UTF8enc.GetMaxByteCount(50), "UTF #1");
153 #else
154                         Assert.AreEqual (200, UTF8enc.GetMaxByteCount(50), "UTF #1");
155 #endif
156                 }
157
158                 // regression for bug #59648
159                 [Test]
160                 public void TestThrowOnInvalid ()
161                 {
162                         UTF8Encoding u = new UTF8Encoding (true, false);
163
164                         byte[] data = new byte [] { 0xC0, 0xAF };
165 #if NET_2_0
166                         Assert.AreEqual (2, u.GetCharCount (data), "#A0");
167                         string s = u.GetString (data);
168                         Assert.AreEqual ("\uFFFD\uFFFD", s, "#A1");
169 #else
170                         Assert.AreEqual (0, u.GetCharCount (data), "#A0");
171                         string s = u.GetString (data);
172                         Assert.AreEqual (String.Empty, s, "#A1");
173 #endif
174
175                         data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 };
176                         s = u.GetString (data);
177 #if NET_2_0
178                         Assert.AreEqual (6, s.Length, "#B1");
179                         Assert.AreEqual (0x30, (int) s [0], "#B2");
180                         Assert.AreEqual (0x31, (int) s [1], "#B3");
181                         Assert.AreEqual (0xFFFD, (int) s [2], "#B4");
182                         Assert.AreEqual (0xFFFD, (int) s [3], "#B5");
183                         Assert.AreEqual (0x30, (int) s [4], "#B6");
184                         Assert.AreEqual (0x32, (int) s [5], "#B7");
185 #else
186                         Assert.AreEqual (4, s.Length, "#B1");
187                         Assert.AreEqual (0x30, (int) s [0], "#B2");
188                         Assert.AreEqual (0x31, (int) s [1], "#B3");
189                         Assert.AreEqual (0x30, (int) s [2], "#B4");
190                         Assert.AreEqual (0x32, (int) s [3], "#B5");
191 #endif
192                 }
193
194                 // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/
195
196                 [Test]
197                 public void T1_Correct_GreekWord_kosme () 
198                 {
199                         byte[] data = { 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0xCF, 0x83, 0xCE, 0xBC, 0xCE, 0xB5 };
200                         string s = utf8.GetString (data);
201                         // cute but saving source code in unicode can be problematic
202                         // so we just ensure we can re-encode this
203                         Assert.AreEqual (BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted");
204                 }
205
206                 [Test]
207                 public void T2_Boundary_1_FirstPossibleSequence_Pass () 
208                 {
209                         byte[] data211 = { 0x00 };
210                         string s = utf8.GetString (data211);
211                         Assert.AreEqual ("\0", s, "1 byte  (U-00000000)");
212                         Assert.AreEqual (BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1");
213
214                         byte[] data212 = { 0xC2, 0x80 };
215                         s = utf8.GetString (data212);
216                         Assert.AreEqual (128, s [0], "2 bytes (U-00000080)");
217                         Assert.AreEqual (BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2");
218
219                         byte[] data213 = { 0xE0, 0xA0, 0x80 };
220                         s = utf8.GetString (data213);
221                         Assert.AreEqual (2048, s [0], "3 bytes (U-00000800)");
222                         Assert.AreEqual (BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3");
223
224                         byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 };
225                         s = utf8.GetString (data214);
226                         Assert.AreEqual (55296, s [0], "4 bytes (U-00010000)-0");
227                         Assert.AreEqual (56320, s [1], "4 bytes (U-00010000)-1");
228                         Assert.AreEqual (BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4");
229                 }
230
231                 [Test]
232                 // Fail on MS Fx 1.1
233                 [ExpectedException (typeof (DecoderException))]
234                 public void T2_Boundary_1_FirstPossibleSequence_Fail_5 () 
235                 {
236                         byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 };
237                         string s = utf8.GetString (data215);
238                         Assert.IsNull (s, "5 bytes (U-00200000)");
239                         Assert.AreEqual (BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5");
240                 }
241
242                 [Test]
243                 // Fail on MS Fx 1.1
244                 [ExpectedException (typeof (DecoderException))]
245                 public void T2_Boundary_1_FirstPossibleSequence_Fail_6 () 
246                 {
247                         byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 };
248                         string s = utf8.GetString (data216);
249                         Assert.IsNull (s, "6 bytes (U-04000000)");
250                         Assert.AreEqual (BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-6");
251                 }
252
253                 [Test]
254                 public void T2_Boundary_2_LastPossibleSequence_Pass () 
255                 {
256                         byte[] data221 = { 0x7F };
257                         string s = utf8.GetString (data221);
258                         Assert.AreEqual (127, s [0], "1 byte  (U-0000007F)");
259                         Assert.AreEqual (BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1");
260
261                         byte[] data222 = { 0xDF, 0xBF };
262                         s = utf8.GetString (data222);
263                         Assert.AreEqual (2047, s [0], "2 bytes (U-000007FF)");
264                         Assert.AreEqual (BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2");
265
266                         byte[] data223 = { 0xEF, 0xBF, 0xBF };
267                         s = utf8.GetString (data223);
268                         Assert.AreEqual (65535, s [0], "3 bytes (U-0000FFFF)");
269                         Assert.AreEqual (BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3");
270
271                 }
272
273                 [Test]
274                 // Fail on MS Fx 1.1
275                 [ExpectedException (typeof (DecoderException))]
276                 public void T2_Boundary_2_LastPossibleSequence_Fail_4 () 
277                 {
278                         byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF };
279                         string s = utf8.GetString (data224);
280                         Assert.IsNull (s, "4 bytes (U-001FFFFF)");
281                         Assert.AreEqual (BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4");
282                 }
283
284                 [Test]
285                 // Fail on MS Fx 1.1
286                 [ExpectedException (typeof (DecoderException))]
287                 public void T2_Boundary_2_LastPossibleSequence_Fail_5 () 
288                 {
289                         byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF };
290                         string s = utf8.GetString (data225);
291                         Assert.IsNull (s, "5 bytes (U-03FFFFFF)");
292                         Assert.AreEqual (BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5");
293                 }
294
295                 [Test]
296                 // Fail on MS Fx 1.1
297                 [ExpectedException (typeof (DecoderException))]
298                 public void T2_Boundary_2_LastPossibleSequence_Fail_6 () 
299                 {
300                         byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF };
301                         string s = utf8.GetString (data226);
302                         Assert.IsNull (s, "6 bytes (U-7FFFFFFF)");
303                         Assert.AreEqual (BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-6");
304                 }
305
306                 [Test]
307                 public void T2_Boundary_3_Other_Pass () 
308                 {
309                         byte[] data231 = { 0xED, 0x9F, 0xBF };
310                         string s = utf8.GetString (data231);
311                         Assert.AreEqual (55295, s [0], "U-0000D7FF");
312                         Assert.AreEqual (BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1");
313
314                         byte[] data232 = { 0xEE, 0x80, 0x80 };
315                         s = utf8.GetString (data232);
316                         Assert.AreEqual (57344, s [0], "U-0000E000");
317                         Assert.AreEqual (BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2");
318
319                         byte[] data233 = { 0xEF, 0xBF, 0xBD };
320                         s = utf8.GetString (data233);
321                         Assert.AreEqual (65533, s [0], "U-0000FFFD");
322                         Assert.AreEqual (BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3");
323
324                         byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF };
325                         s = utf8.GetString (data234);
326                         Assert.AreEqual (56319, s [0], "U-0010FFFF-0");
327                         Assert.AreEqual (57343, s [1], "U-0010FFFF-1");
328                         Assert.AreEqual (BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4");
329                 }
330
331                 [Test]
332                 // Fail on MS Fx 1.1
333                 [ExpectedException (typeof (DecoderException))]
334                 public void T2_Boundary_3_Other_Fail_5 () 
335                 {
336                         byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 };
337                         string s = utf8.GetString (data235);
338                         Assert.IsNull (s, "U-00110000");
339                         Assert.AreEqual (BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5");
340                 }
341
342                 [Test]
343                 [ExpectedException (typeof (DecoderException))]
344                 public void T3_Malformed_1_UnexpectedContinuation_311 () 
345                 {
346                         byte[] data = { 0x80 };
347                         string s = utf8.GetString (data);
348                         // exception is "really" expected here
349                 }
350
351                 [Test]
352                 [ExpectedException (typeof (DecoderException))]
353                 public void T3_Malformed_1_UnexpectedContinuation_312 () 
354                 {
355                         byte[] data = { 0xBF };
356                         string s = utf8.GetString (data);
357                         // exception is "really" expected here
358                 }
359
360                 [Test]
361                 [ExpectedException (typeof (DecoderException))]
362                 public void T3_Malformed_1_UnexpectedContinuation_313 () 
363                 {
364                         byte[] data = { 0x80, 0xBF };
365                         string s = utf8.GetString (data);
366                         // exception is "really" expected here
367                 }
368
369                 [Test]
370                 [ExpectedException (typeof (DecoderException))]
371                 public void T3_Malformed_1_UnexpectedContinuation_314 () 
372                 {
373                         byte[] data = { 0x80, 0xBF, 0x80 };
374                         string s = utf8.GetString (data);
375                         // exception is "really" expected here
376                 }
377
378                 [Test]
379                 [ExpectedException (typeof (DecoderException))]
380                 public void T3_Malformed_1_UnexpectedContinuation_315 () 
381                 {
382                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF };
383                         string s = utf8.GetString (data);
384                         // exception is "really" expected here
385                 }
386
387                 [Test]
388                 [ExpectedException (typeof (DecoderException))]
389                 public void T3_Malformed_1_UnexpectedContinuation_316 () 
390                 {
391                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80 };
392                         string s = utf8.GetString (data);
393                         // exception is "really" expected here
394                 }
395
396                 [Test]
397                 [ExpectedException (typeof (DecoderException))]
398                 public void T3_Malformed_1_UnexpectedContinuation_317 () 
399                 {
400                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF };
401                         string s = utf8.GetString (data);
402                         // exception is "really" expected here
403                 }
404
405                 [Test]
406                 [ExpectedException (typeof (DecoderException))]
407                 public void T3_Malformed_1_UnexpectedContinuation_318 () 
408                 {
409                         byte[] data = { 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF, 0x80 };
410                         string s = utf8.GetString (data);
411                         // exception is "really" expected here
412                 }
413
414                 [Test]
415                 [ExpectedException (typeof (DecoderException))]
416                 public void T3_Malformed_1_UnexpectedContinuation_319 () 
417                 {
418                         // 64 different continuation characters
419                         byte[] data = {
420                                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 
421                                 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 
422                                 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 
423                                 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF };
424                         string s = utf8.GetString (data);
425                         // exception is "really" expected here
426                 }
427
428                 [Test]
429                 [ExpectedException (typeof (DecoderException))]
430                 public void T3_Malformed_2_LonelyStart_321 ()
431                 {
432                         byte[] data = { 
433                                 0xC0, 0x20, 0xC1, 0x20, 0xC2, 0x20, 0xC3, 0x20, 0xC4, 0x20, 0xC5, 0x20, 0xC6, 0x20, 0xC7, 0x20, 
434                                 0xC8, 0x20, 0xC9, 0x20, 0xCA, 0x20, 0xCB, 0x20, 0xCC, 0x20, 0xCD, 0x20, 0xCE, 0x20, 0xCF, 0x20, 
435                                 0xD0, 0x20, 0xD1, 0x20, 0xD2, 0x20, 0xD3, 0x20, 0xD4, 0x20, 0xD5, 0x20, 0xD6, 0x20, 0xD7, 0x20, 
436                                 0xD8, 0x20, 0xD9, 0x20, 0xDA, 0x20, 0xDB, 0x20, 0xDC, 0x20, 0xDD, 0x20, 0xDE, 0x20, 0xDF, 0x20 };
437                         string s = utf8.GetString (data);
438                         // exception is "really" expected here
439                 }
440
441                 [Test]
442                 [ExpectedException (typeof (DecoderException))]
443                 public void T3_Malformed_2_LonelyStart_322 () 
444                 {
445                         byte[] data = { 
446                                 0xE0, 0x20, 0xE1, 0x20, 0xE2, 0x20, 0xE3, 0x20, 0xE4, 0x20, 0xE5, 0x20, 0xE6, 0x20, 0xE7, 0x20, 
447                                 0xE8, 0x20, 0xE9, 0x20, 0xEA, 0x20, 0xEB, 0x20, 0xEC, 0x20, 0xED, 0x20, 0xEE, 0x20, 0xEF, 0x20 };
448                         string s = utf8.GetString (data);
449                         // exception is "really" expected here
450                 }
451
452                 [Test]
453                 [ExpectedException (typeof (DecoderException))]
454                 public void T3_Malformed_2_LonelyStart_323 () 
455                 {
456                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
457                         string s = utf8.GetString (data);
458                         // exception is "really" expected here
459                 }
460
461                 [Test]
462                 [ExpectedException (typeof (DecoderException))]
463                 public void T3_Malformed_2_LonelyStart_324 () 
464                 {
465                         byte[] data = { 0xF0, 0x20, 0xF1, 0x20, 0xF2, 0x20, 0xF3, 0x20, 0xF4, 0x20, 0xF5, 0x20, 0xF6, 0x20, 0xF7, 0x20 };
466                         string s = utf8.GetString (data);
467                         // exception is "really" expected here
468                 }
469
470                 [Test]
471                 [ExpectedException (typeof (DecoderException))]
472                 public void T3_Malformed_2_LonelyStart_325 () 
473                 {
474                         byte[] data = { 0xFC, 0x20, 0xFD, 0x20 };
475                         string s = utf8.GetString (data);
476                         // exception is "really" expected here
477                 }
478
479                 [Test]
480                 [ExpectedException (typeof (DecoderException))]
481                 public void T3_Malformed_3_LastContinuationMissing_331 () 
482                 {
483                         byte[] data = { 0xC0 };
484                         string s = utf8.GetString (data);
485                         // exception is "really" expected here
486                 }
487
488                 [Test]
489                 [ExpectedException (typeof (DecoderException))]
490                 public void T3_Malformed_3_LastContinuationMissing_332 () 
491                 {
492                         byte[] data = { 0xE0, 0x80 };
493                         string s = utf8.GetString (data);
494                         // exception is "really" expected here
495                 }
496
497                 [Test]
498                 [ExpectedException (typeof (DecoderException))]
499                 public void T3_Malformed_3_LastContinuationMissing_333 () 
500                 {
501                         byte[] data = { 0xF0, 0x80, 0x80 };
502                         string s = utf8.GetString (data);
503                         // exception is "really" expected here
504                 }
505
506                 [Test]
507                 [ExpectedException (typeof (DecoderException))]
508                 public void T3_Malformed_3_LastContinuationMissing_334 () 
509                 {
510                         byte[] data = { 0xF8, 0x80, 0x80, 0x80 };
511                         string s = utf8.GetString (data);
512                         // exception is "really" expected here
513                 }
514
515                 [Test]
516                 [ExpectedException (typeof (DecoderException))]
517                 public void T3_Malformed_3_LastContinuationMissing_335 () 
518                 {
519                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80 };
520                         string s = utf8.GetString (data);
521                         // exception is "really" expected here
522                 }
523
524                 [Test]
525 // MS Fx 1.1 accept this
526 //              [ExpectedException (typeof (DecoderException))]
527                 public void T3_Malformed_3_LastContinuationMissing_336 () 
528                 {
529                         byte[] data = { 0xDF };
530                         try {
531                                 string s = utf8.GetString (data);
532                                 // exception is "really" expected here
533                                 Assert.AreEqual (String.Empty, s, "MS FX 1.1 behaviour");
534                         }
535                         catch (DecoderException) {
536                                 // but Mono doesn't - better stick to the standard
537                         }
538                 }
539
540                 [Test]
541                 // MS Fx 1.1 accept this
542 //              [ExpectedException (typeof (DecoderException))]
543                 public void T3_Malformed_3_LastContinuationMissing_337 () 
544                 {
545                         byte[] data = { 0xEF, 0xBF };
546                         try {
547                                 string s = utf8.GetString (data);
548                                 // exception is "really" expected here
549                                 Assert.AreEqual (String.Empty, s, "MS FX 1.1 behaviour");
550                         }
551                         catch (DecoderException) {
552                                 // but Mono doesn't - better stick to the standard
553                         }
554                 }
555
556                 [Test]
557                 [ExpectedException (typeof (DecoderException))]
558                 public void T3_Malformed_3_LastContinuationMissing_338 () 
559                 {
560                         byte[] data = { 0xF7, 0xBF, 0xBF };
561                         string s = utf8.GetString (data);
562                         // exception is "really" expected here
563                 }
564
565                 [Test]
566                 [ExpectedException (typeof (DecoderException))]
567                 public void T3_Malformed_3_LastContinuationMissing_339 () 
568                 {
569                         byte[] data = { 0xF, 0xBF, 0xBF, 0xBF };
570                         string s = utf8.GetString (data);
571                         // exception is "really" expected here
572                 }
573
574                 [Test]
575                 [ExpectedException (typeof (DecoderException))]
576                 public void T3_Malformed_3_LastContinuationMissing_3310 () 
577                 {
578                         byte[] data = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
579                         string s = utf8.GetString (data);
580                         // exception is "really" expected here
581                 }
582
583                 [Test]
584                 [ExpectedException (typeof (DecoderException))]
585                 public void T3_Malformed_4_ConcatenationImcomplete () 
586                 {
587                         byte[] data = {
588                                 0xC0, 0xE0, 0x80, 0xF0, 0x80, 0x80, 0xF8, 0x80, 0x80, 0x80, 0xFC, 0x80, 0x80, 0x80, 0x80, 0xDF, 
589                                 0xEF, 0xBF, 0xF7, 0xBF, 0xBF, 0xFB, 0xBF, 0xBF, 0xBF, 0xFD, 0xBF, 0xBF, 0xBF, 0xBF };
590                         string s = utf8.GetString (data);
591                         // exception is "really" expected here
592                 }
593
594                 [Test]
595                 [ExpectedException (typeof (DecoderException))]
596                 public void T3_Malformed_5_ImpossibleBytes_351 () 
597                 {
598                         byte[] data = { 0xFE };
599                         string s = utf8.GetString (data);
600                         // exception is "really" expected here
601                 }
602
603                 [Test]
604                 [ExpectedException (typeof (DecoderException))]
605                 public void T3_Malformed_5_ImpossibleBytes_352 () 
606                 {
607                         byte[] data = { 0xFF };
608                         string s = utf8.GetString (data);
609                         // exception is "really" expected here
610                 }
611
612                 [Test]
613                 [ExpectedException (typeof (DecoderException))]
614                 public void T3_Malformed_5_ImpossibleBytes_353 () 
615                 {
616                         byte[] data = { 0xFE, 0xFE, 0xFF, 0xFF };
617                         string s = utf8.GetString (data);
618                         // exception is "really" expected here
619                 }
620
621                 // Overlong == dangereous -> "safe" decoder should reject them
622
623                 [Test]
624                 [ExpectedException (typeof (DecoderException))]
625                 public void T4_Overlong_1_ASCII_Slash_411 () 
626                 {
627                         byte[] data = { 0xC0, 0xAF };
628                         string s = utf8.GetString (data);
629                         // exception is "really" expected here
630                 }
631
632                 [Test]
633                 [ExpectedException (typeof (DecoderException))]
634                 public void T4_Overlong_1_ASCII_Slash_412 () 
635                 {
636                         byte[] data = { 0xE0, 0x80, 0xAF };
637                         string s = utf8.GetString (data);
638                         // exception is "really" expected here
639                 }
640
641                 [Test]
642                 [ExpectedException (typeof (DecoderException))]
643                 public void T4_Overlong_1_ASCII_Slash_413 () 
644                 {
645                         byte[] data = { 0xF0, 0x80, 0x80, 0xAF };
646                         string s = utf8.GetString (data);
647                         // exception is "really" expected here
648                 }
649
650                 [Test]
651                 [ExpectedException (typeof (DecoderException))]
652                 public void T4_Overlong_1_ASCII_Slash_414 () 
653                 {
654                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0xAF };
655                         string s = utf8.GetString (data);
656                         // exception is "really" expected here
657                 }
658
659                 [Test]
660                 [ExpectedException (typeof (DecoderException))]
661                 public void T4_Overlong_1_ASCII_Slash_415 () 
662                 {
663                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0xAF };
664                         string s = utf8.GetString (data);
665                         // exception is "really" expected here
666                 }
667
668                 [Test]
669                 [ExpectedException (typeof (DecoderException))]
670                 public void T4_Overlong_2_MaximumBoundary_421 () 
671                 {
672                         byte[] data = { 0xC1, 0xBF };
673                         string s = utf8.GetString (data);
674                         // exception is "really" expected here
675                 }
676
677                 [Test]
678                 [ExpectedException (typeof (DecoderException))]
679                 public void T4_Overlong_2_MaximumBoundary_422 () 
680                 {
681                         byte[] data = { 0xE0, 0x9F, 0xBF };
682                         string s = utf8.GetString (data);
683                         // exception is "really" expected here
684                 }
685
686                 [Test]
687                 [ExpectedException (typeof (DecoderException))]
688                 public void T4_Overlong_2_MaximumBoundary_423 () 
689                 {
690                         byte[] data = { 0xF0, 0x8F, 0xBF, 0xBF };
691                         string s = utf8.GetString (data);
692                         // exception is "really" expected here
693                 }
694
695                 [Test]
696                 [ExpectedException (typeof (DecoderException))]
697                 public void T4_Overlong_2_MaximumBoundary_424 () 
698                 {
699                         byte[] data = { 0xF8, 0x87, 0xBF, 0xBF, 0xBF };
700                         string s = utf8.GetString (data);
701                         // exception is "really" expected here
702                 }
703
704                 [Test]
705                 [ExpectedException (typeof (DecoderException))]
706                 public void T4_Overlong_2_MaximumBoundary_425 () 
707                 {
708                         byte[] data = { 0xFC, 0x83, 0xBF, 0xBF, 0xBF, 0xBF };
709                         string s = utf8.GetString (data);
710                         // exception is "really" expected here
711                 }
712
713                 [Test]
714                 [ExpectedException (typeof (DecoderException))]
715                 public void T4_Overlong_3_NUL_431 () 
716                 {
717                         byte[] data = { 0xC0, 0x80 };
718                         string s = utf8.GetString (data);
719                         // exception is "really" expected here
720                 }
721
722                 [Test]
723                 [ExpectedException (typeof (DecoderException))]
724                 public void T4_Overlong_3_NUL_432 () 
725                 {
726                         byte[] data = { 0xE0, 0x80, 0x80 };
727                         string s = utf8.GetString (data);
728                         // exception is "really" expected here
729                 }
730
731                 [Test]
732                 [ExpectedException (typeof (DecoderException))]
733                 public void T4_Overlong_3_NUL_433 () 
734                 {
735                         byte[] data = { 0xF0, 0x80, 0x80, 0x80 };
736                         string s = utf8.GetString (data);
737                         // exception is "really" expected here
738                 }
739
740                 [Test]
741                 [ExpectedException (typeof (DecoderException))]
742                 public void T4_Overlong_3_NUL_434 () 
743                 {
744                         byte[] data = { 0xF8, 0x80, 0x80, 0x80, 0x80 };
745                         string s = utf8.GetString (data);
746                         // exception is "really" expected here
747                 }
748
749                 [Test]
750                 [ExpectedException (typeof (DecoderException))]
751                 public void T4_Overlong_3_NUL_435 () 
752                 {
753                         byte[] data = { 0xFC, 0x80, 0x80, 0x80, 0x80, 0x80 };
754                         string s = utf8.GetString (data);
755                         // exception is "really" expected here
756                 }
757
758                 [Test]
759 #if NET_2_0
760                 [ExpectedException (typeof (DecoderFallbackException))]
761 #else
762 // MS Fx 1.1 accept this
763                 [Category ("NotDotNet")]
764                 [ExpectedException (typeof (DecoderException))]
765 #endif
766                         public void T5_IllegalCodePosition_1_UTF16Surrogates_511 () 
767                 {
768                         byte[] data = { 0xED, 0xA0, 0x80 };
769                         string s = utf8.GetString (data);
770                         // exception is "really" expected here
771                         Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour");
772                 }
773
774                 [Test]
775 #if NET_2_0
776                 [ExpectedException (typeof (DecoderFallbackException))]
777 #else
778 // MS Fx 1.1 accept this
779                 [Category ("NotDotNet")]
780                 [ExpectedException (typeof (DecoderException))]
781 #endif
782                 public void T5_IllegalCodePosition_1_UTF16Surrogates_512 () 
783                 {
784                         byte[] data = { 0xED, 0xAD, 0xBF };
785                         string s = utf8.GetString (data);
786                         // exception is "really" expected here
787                         Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour");
788                 }
789
790                 [Test]
791 #if NET_2_0
792                 [ExpectedException (typeof (DecoderFallbackException))]
793 #else
794 // MS Fx 1.1 accept this
795                 [Category ("NotDotNet")]
796                 [ExpectedException (typeof (DecoderException))]
797 #endif
798                 public void T5_IllegalCodePosition_1_UTF16Surrogates_513 ()
799                 {
800                         byte[] data = { 0xED, 0xAE, 0x80 };
801                         string s = utf8.GetString (data);
802                         // exception is "really" expected here
803                         Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour");
804                 }
805
806                 [Test]
807 #if NET_2_0
808                 [ExpectedException (typeof (DecoderFallbackException))]
809 #else
810 // MS Fx 1.1 accept this
811                 [Category ("NotDotNet")]
812                 [ExpectedException (typeof (DecoderException))]
813 #endif
814                 public void T5_IllegalCodePosition_1_UTF16Surrogates_514 () 
815                 {
816                         byte[] data = { 0xED, 0xAF, 0xBF };
817                         string s = utf8.GetString (data);
818                         // exception is "really" expected here
819                         Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour");
820                 }
821
822                 [Test]
823 #if NET_2_0
824                 [ExpectedException (typeof (DecoderFallbackException))]
825 #else
826 // MS Fx 1.1 accept this
827                 [Category ("NotDotNet")]
828                 [ExpectedException (typeof (DecoderException))]
829 #endif
830                 public void T5_IllegalCodePosition_1_UTF16Surrogates_515 ()
831                 {
832                         byte[] data = { 0xED, 0xB0, 0x80 };
833                         string s = utf8.GetString (data);
834                         // exception is "really" expected here
835                         Assert.AreEqual (56320, s [0], "MS FX 1.1 behaviour");
836                 }
837
838                 [Test]
839 #if NET_2_0
840                 [ExpectedException (typeof (DecoderFallbackException))]
841 #else
842 // MS Fx 1.1 accept this
843                 [Category ("NotDotNet")]
844                 [ExpectedException (typeof (DecoderException))]
845 #endif
846                 public void T5_IllegalCodePosition_1_UTF16Surrogates_516 () 
847                 {
848                         byte[] data = { 0xED, 0xBE, 0x80 };
849                         string s = utf8.GetString (data);
850                         // exception is "really" expected here
851                         Assert.AreEqual (57216, s [0], "MS FX 1.1 behaviour");
852                 }
853
854                 [Test]
855 #if NET_2_0
856                 [ExpectedException (typeof (DecoderFallbackException))]
857 #else
858 // MS Fx 1.1 accept this
859                 [Category ("NotDotNet")]
860                 [ExpectedException (typeof (DecoderException))]
861 #endif
862                 public void T5_IllegalCodePosition_1_UTF16Surrogates_517 () 
863                 {
864                         byte[] data = { 0xED, 0xBF, 0xBF };
865                         string s = utf8.GetString (data);
866                         // exception is "really" expected here
867                         Assert.AreEqual (57343, s [0], "MS FX 1.1 behaviour");
868                 }
869
870                 [Test]
871 #if NET_2_0
872                 [ExpectedException (typeof (DecoderFallbackException))]
873 #else
874 // MS Fx 1.1 accept this
875                 [Category ("NotDotNet")]
876                 [ExpectedException (typeof (DecoderException))]
877 #endif
878                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_521 () 
879                 {
880                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 };
881                         string s = utf8.GetString (data);
882                         // exception is "really" expected here
883                         Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour");
884                         Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour");
885                 }
886
887                 [Test]
888 #if NET_2_0
889                 [ExpectedException (typeof (DecoderFallbackException))]
890 #else
891 // MS Fx 1.1 accept this
892                 [Category ("NotDotNet")]
893                 [ExpectedException (typeof (DecoderException))]
894 #endif
895                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_522 () 
896                 {
897                         byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF };
898                         string s = utf8.GetString (data);
899                         // exception is "really" expected here
900                         Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour");
901                         Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour");
902                 }
903
904                 [Test]
905 #if NET_2_0
906                 [ExpectedException (typeof (DecoderFallbackException))]
907 #else
908 // MS Fx 1.1 accept this
909                 [Category ("NotDotNet")]
910                 [ExpectedException (typeof (DecoderException))]
911 #endif
912                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_523 () 
913                 {
914                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 };
915                         string s = utf8.GetString (data);
916                         // exception is "really" expected here
917                         Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour");
918                         Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour");
919                 }
920
921                 [Test]
922 #if NET_2_0
923                 [ExpectedException (typeof (DecoderFallbackException))]
924 #else
925 // MS Fx 1.1 accept this
926                 [Category ("NotDotNet")]
927                 [ExpectedException (typeof (DecoderException))]
928 #endif
929                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_524 () 
930                 {
931                         byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF };
932                         string s = utf8.GetString (data);
933                         // exception is "really" expected here
934                         Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour");
935                         Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour");
936                 }
937
938                 [Test]
939 #if NET_2_0
940                 [ExpectedException (typeof (DecoderFallbackException))]
941 #else
942 // MS Fx 1.1 accept this
943                 [Category ("NotDotNet")]
944                 [ExpectedException (typeof (DecoderException))]
945 #endif
946                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_525 () 
947                 {
948                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 };
949                         string s = utf8.GetString (data);
950                         // exception is "really" expected here
951                         Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour");
952                         Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour");
953                 }
954
955                 [Test]
956 #if NET_2_0
957                 [ExpectedException (typeof (DecoderFallbackException))]
958 #else
959 // MS Fx 1.1 accept this
960                 [Category ("NotDotNet")]
961                 [ExpectedException (typeof (DecoderException))]
962 #endif
963                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_526 () 
964                 {
965                         byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F };
966                         string s = utf8.GetString (data);
967                         // exception is "really" expected here
968                         Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour");
969                         Assert.AreEqual (57295, s [1], "MS FX 1.1 behaviour");
970                 }
971
972                 [Test]
973 #if NET_2_0
974                 [ExpectedException (typeof (DecoderFallbackException))]
975 #else
976 // MS Fx 1.1 accept this
977                 [Category ("NotDotNet")]
978                 [ExpectedException (typeof (DecoderException))]
979 #endif
980                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_527 () 
981                 {
982                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 };
983                         string s = utf8.GetString (data);
984                         // exception is "really" expected here
985                         Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour");
986                         Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour");
987                 }
988
989                 [Test]
990 #if NET_2_0
991                 [ExpectedException (typeof (DecoderFallbackException))]
992 #else
993 // MS Fx 1.1 accept this
994                 [Category ("NotDotNet")]
995                 [ExpectedException (typeof (DecoderException))]
996 #endif
997                 public void T5_IllegalCodePosition_2_PairedUTF16Surrogates_528 () 
998                 {
999                         byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF };
1000                         string s = utf8.GetString (data);
1001                         // exception is "really" expected here
1002                         Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour");
1003                         Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour");
1004                 }
1005
1006                 [Test]
1007 // MS Fx 1.1 accept this
1008 //              [ExpectedException (typeof (DecoderException))]
1009                 public void T5_IllegalCodePosition_3_Other_531 () 
1010                 {
1011                         byte[] data = { 0xEF, 0xBF, 0xBE };
1012                         string s = utf8.GetString (data);
1013                         // exception is "really" expected here
1014                         Assert.AreEqual (65534, s [0], "MS FX 1.1 behaviour");
1015                 }
1016
1017                 [Test]
1018 // MS Fx 1.1 accept this
1019 //              [ExpectedException (typeof (DecoderException))]
1020                 public void T5_IllegalCodePosition_3_Other_532 () 
1021                 {
1022                         byte[] data = { 0xEF, 0xBF, 0xBF };
1023                         string s = utf8.GetString (data);
1024                         // exception is "really" expected here
1025                         Assert.AreEqual (65535, s [0], "MS FX 1.1 behaviour");
1026                 }
1027
1028                 [Test]
1029                 // bug #75065 and #73086.
1030                 public void GetCharsFEFF ()
1031                 {
1032                         byte [] data = new byte [] {0xEF, 0xBB, 0xBF};
1033                         Encoding enc = new UTF8Encoding (false, true);
1034                         string s = enc.GetString (data);
1035                         Assert.AreEqual (s, "\uFEFF");
1036
1037                         Encoding utf = Encoding.UTF8;
1038                         char[] testChars = {'\uFEFF','A'};
1039
1040                         byte[] bytes = utf.GetBytes(testChars);
1041                         char[] chars = utf.GetChars(bytes);
1042                         Assert.AreEqual ('\uFEFF', chars [0], "#1");
1043                         Assert.AreEqual ('A', chars [1], "#2");
1044                 }
1045
1046 #if NET_2_0
1047                 [Test]
1048                 public void CloneNotReadOnly ()
1049                 {
1050                         Encoding e = Encoding.GetEncoding (65001).Clone ()
1051                                 as Encoding;
1052                         Assert.AreEqual (false, e.IsReadOnly);
1053                         e.EncoderFallback = new EncoderExceptionFallback ();
1054                 }
1055 #endif
1056
1057                 [Test]
1058 #if NET_2_0
1059                 [ExpectedException (typeof (DecoderFallbackException))]
1060 #else
1061                 [ExpectedException (typeof (ArgumentException))]
1062                 [Category ("NotDotNet")] // MS Bug
1063 #endif
1064                 public void Bug77315 ()
1065                 {
1066                         new UTF8Encoding (false, true).GetString (
1067                                 new byte [] {0xED, 0xA2, 0x8C});
1068                 }
1069
1070                 [Test]
1071                 public void SufficientByteArray ()
1072                 {
1073                         Encoder e = Encoding.UTF8.GetEncoder ();
1074                         byte [] bytes = new byte [0];
1075
1076                         char [] chars = new char [] {'\uD800'};
1077                         e.GetBytes (chars, 0, 1, bytes, 0, false);
1078                         try {
1079                                 int ret = e.GetBytes (chars, 1, 0, bytes, 0, true);
1080 #if NET_2_0
1081                                 Assert.AreEqual (0, ret, "drop insufficient char in 2.0: char[]");
1082 #else
1083                                 Assert.Fail ("ArgumentException is expected: char[]");
1084 #endif
1085                         } catch (ArgumentException) {
1086                         }
1087
1088                         string s = "\uD800";
1089                         try {
1090                                 int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0);
1091 #if NET_2_0
1092                                 Assert.AreEqual (0, ret, "drop insufficient char in 2.0: string");
1093 #else
1094                                 Assert.Fail ("ArgumentException is expected: string");
1095 #endif
1096                         } catch (ArgumentException) {
1097                         }
1098                 }
1099                 
1100                 [Test] // bug #565129
1101                 public void SufficientByteArray2 ()
1102                 {
1103                         var u = Encoding.UTF8;
1104                         Assert.AreEqual (3, u.GetByteCount ("\uFFFD"), "#1-1");
1105                         Assert.AreEqual (3, u.GetByteCount ("\uD800"), "#1-2");
1106                         Assert.AreEqual (3, u.GetByteCount ("\uDC00"), "#1-3");
1107                         Assert.AreEqual (4, u.GetByteCount ("\uD800\uDC00"), "#1-4");
1108                         byte [] bytes = new byte [10];
1109                         Assert.AreEqual (3, u.GetBytes ("\uDC00", 0, 1, bytes, 0), "#1-5"); // was bogus
1110
1111                         Assert.AreEqual (3, u.GetBytes ("\uFFFD").Length, "#2-1");
1112                         Assert.AreEqual (3, u.GetBytes ("\uD800").Length, "#2-2");
1113                         Assert.AreEqual (3, u.GetBytes ("\uDC00").Length, "#2-3");
1114                         Assert.AreEqual (4, u.GetBytes ("\uD800\uDC00").Length, "#2-4");
1115
1116                         for (char c = char.MinValue; c < char.MaxValue; c++) {
1117                                 byte [] bIn;
1118                                 bIn = u.GetBytes (c.ToString ());
1119                         }
1120
1121                         try {
1122                                 new UTF8Encoding (false, true).GetBytes (new char [] {'\uDF45', '\uD808'}, 0, 2);
1123                                 Assert.Fail ("EncoderFallbackException is expected");
1124                         } catch (EncoderFallbackException) {
1125                         }
1126                 }
1127
1128 #if NET_2_0
1129                 [Test] // bug #77550
1130                 public void DecoderFallbackSimple ()
1131                 {
1132                         UTF8Encoding e = new UTF8Encoding (false, false);
1133                         AssertType.AreEqual (1, e.GetDecoder ().GetCharCount (
1134                                         new byte [] {(byte) 183}, 0, 1),
1135                                         "#1");
1136                         AssertType.AreEqual (1, e.GetDecoder().GetChars (
1137                                         new byte [] {(byte) 183}, 0, 1,
1138                                         new char [100], 0),
1139                                         "#2");
1140                         AssertType.AreEqual (1, e.GetString (new byte [] {(byte) 183}).Length,
1141                                         "#3");
1142                 }
1143
1144                 [Test]
1145                 public void FallbackDefaultEncodingUTF8 ()
1146                 {
1147                         DecoderReplacementFallbackBuffer b =
1148                                 Encoding.UTF8.DecoderFallback.CreateFallbackBuffer ()
1149                                 as DecoderReplacementFallbackBuffer;
1150                         AssertType.IsTrue (b.Fallback (new byte [] {}, 0), "#1");
1151                         AssertType.IsFalse (b.MovePrevious (), "#2");
1152                         AssertType.AreEqual (1, b.Remaining, "#3");
1153                         AssertType.AreEqual ('\uFFFD', b.GetNextChar (), "#4");
1154                 }
1155
1156                 [Test]
1157                 [Category ("MobileNotWorking")]
1158                 public void Bug415628 ()
1159                 {
1160                         using (var f = File.Open ("Test/resources/415628.bin", FileMode.Open)) {
1161                                 BinaryReader br = new BinaryReader (f);
1162                                 byte [] buf = br.ReadBytes (8000);
1163                                 Encoding.UTF8.GetString(buf);
1164                         }
1165                 }
1166 #endif
1167
1168                 [Test]
1169                 [ExpectedException (typeof (ArgumentException))]
1170                 public void Bug10788()
1171                 {
1172                         byte[] bytes = new byte[4096];
1173                         char[] chars = new char[10];
1174
1175                         Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 4096, chars, 9, false);
1176                 }
1177
1178                 [Test]
1179                 public void Bug10789()
1180                 {
1181                         byte[] bytes = new byte[4096];
1182                         char[] chars = new char[10];
1183
1184                         try {
1185                                 Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 1, chars, 10, false);
1186                                 Assert.Fail ("ArgumentException is expected #1");
1187                         } catch (ArgumentException) {
1188                         }
1189
1190                         try {
1191                                 Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 1, chars, 11, false);
1192                                 Assert.Fail ("ArgumentOutOfRangeException is expected #2");
1193                         } catch (ArgumentOutOfRangeException) {
1194                         }
1195
1196                         int charactersWritten = Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 0, chars, 10, false);
1197                         Assert.AreEqual (0, charactersWritten, "#3");
1198                 }
1199
1200                 // DecoderFallbackExceptionTest
1201                 //   This struct describes a DecoderFallbackExceptions' test. It
1202                 //   contains the expected indexes (eindex) and bad-bytes lengths
1203                 //   (elen) delivered by the first and subsequent
1204                 //   DecoderFallbackException throwed when the utf8 conversion routines
1205                 //   are exposed by the array of bytes (bytes) contained in this test.
1206                 //   It also has a nice description (description) for documentation and
1207                 //   debugging.
1208                 //
1209                 //   The hardcoded 'eindex' and 'elen' info is the output that you will
1210                 //   got if you run this strings on the MS.NET platform.
1211                 struct DecoderFallbackExceptionTest
1212                 {
1213                         public string description;
1214                         public byte [] bytes;
1215                         public int [] eindex;
1216                         public int [] elen;
1217                         public DecoderFallbackExceptionTest (
1218                                         string description,
1219                                         int [] eindex,
1220                                         int [] elen,
1221                                         byte [] bytes)
1222                         {
1223                                 this.description = description;
1224                                 this.bytes = bytes;
1225                                 if (eindex.Length != elen.Length)
1226                                         throw new ApplicationException ("eindex.Length != elen.Length in test '" + description + "'");
1227                                 this.eindex = eindex;
1228                                 this.elen = elen;
1229                         }
1230                 }
1231
1232                 // try to convert the all current test's bytes with Getchars()
1233                 // in only one step
1234                 private void DecoderFallbackExceptions_GetChars (
1235                         char [] chars,
1236                         int testno,
1237                         Decoder dec,
1238                         DecoderFallbackExceptionTest t)
1239                 {
1240                         try {
1241                                 dec.GetChars (t.bytes, 0, t.bytes.Length, chars, 0, true);
1242                                         Assert.IsTrue (
1243                                                 t.eindex.Length == 0,
1244                                                 String.Format (
1245                                                         "test#{0}-1: UNEXPECTED SUCCESS",
1246                                                         testno));
1247                         } catch(DecoderFallbackException ex) {
1248                                 Assert.IsTrue (
1249                                         t.eindex.Length > 0,
1250                                         String.Format (
1251                                                 "test#{0}-1: UNEXPECTED FAIL",
1252                                                 testno));
1253                                 Assert.IsTrue (
1254                                         ex.Index == t.eindex[0],
1255                                         String.Format (
1256                                                 "test#{0}-1: Expected exception at {1} not {2}.",
1257                                                 testno,
1258                                                 t.eindex[0],
1259                                                 ex.Index));
1260                                 Assert.IsTrue (
1261                                         ex.BytesUnknown.Length == t.elen[0],
1262                                         String.Format (
1263                                                 "test#{0}-1: Expected BytesUnknown.Length of {1} not {2}.",
1264                                                 testno,
1265                                                 t.elen[0],
1266                                                 ex.BytesUnknown.Length));
1267                                 for (int i = 0; i < ex.BytesUnknown.Length; i++)
1268                                         Assert.IsTrue (
1269                                                 ex.BytesUnknown[i] == t.bytes[ex.Index + i],
1270                                                 String.Format (
1271                                                         "test#{0}-1: expected byte {1:X} not {2:X} at {3}.",
1272                                                         testno,
1273                                                         t.bytes[ex.Index + i],
1274                                                         ex.BytesUnknown[i],
1275                                                         ex.Index + i));
1276                                 dec.Reset ();
1277                         }
1278                 }
1279
1280                 // convert bytes to string using a fixed blocksize.
1281                 // If something bad happens, try to recover using the
1282                 // DecoderFallbackException info.
1283                 private void DecoderFallbackExceptions_Convert (
1284                         char [] chars,
1285                         int testno,
1286                         Decoder dec,
1287                         DecoderFallbackExceptionTest t,
1288                         int block_size)
1289                 {
1290                         int charsUsed, bytesUsed;
1291                         bool completed;
1292
1293                         int ce = 0; // current exception
1294                         for (int c = 0; c < t.bytes.Length; ) {
1295                                 try {
1296                                         int bu = c + block_size > t.bytes.Length
1297                                                         ? t.bytes.Length - c
1298                                                         : block_size;
1299                                         dec.Convert (
1300                                                 t.bytes, c, bu,
1301                                                 chars, 0, chars.Length,
1302                                                 c + bu >= t.bytes.Length,
1303                                                 out bytesUsed, out charsUsed,
1304                                                 out completed);
1305                                         c += bytesUsed;
1306                                 } catch (DecoderFallbackException ex) {
1307                                         Assert.IsTrue (
1308                                                 t.eindex.Length > ce,
1309                                                 String.Format (
1310                                                         "test#{0}-2-{1}#{2}: UNEXPECTED FAIL (c={3}, eIndex={4}, eBytesUnknwon={5})",
1311                                                         testno, block_size, ce, c,
1312                                                         ex.Index,
1313                                                         ex.BytesUnknown.Length));
1314                                         Assert.IsTrue (
1315                                                 ex.Index + c == t.eindex[ce],
1316                                                 String.Format (
1317                                                         "test#{0}-2-{1}#{2}: Expected at {3} not {4}.",
1318                                                         testno, block_size, ce,
1319                                                         t.eindex[ce],
1320                                                         ex.Index + c));
1321                                         Assert.IsTrue (
1322                                                 ex.BytesUnknown.Length == t.elen[ce],
1323                                                 String.Format (
1324                                                         "test#{0}-2-{1}#{2}: Expected BytesUnknown.Length of {3} not {4} @{5}.",
1325                                                         testno, block_size, ce,
1326                                                         t.elen[0], ex.BytesUnknown.Length, c));
1327                                         for (int i = 0; i < ex.BytesUnknown.Length; i++)
1328                                                 Assert.IsTrue (
1329                                                         ex.BytesUnknown[i] == t.bytes[ex.Index + i + c],
1330                                                         String.Format (
1331                                                                 "test#{0}-2-{1}#{2}: Expected byte {3:X} not {4:X} at {5}.",
1332                                                                 testno, block_size, ce,
1333                                                                 t.bytes[ex.Index + i + c],
1334                                                                 ex.BytesUnknown[i],
1335                                                                 ex.Index + i));
1336                                         c += ex.BytesUnknown.Length + ex.Index;
1337                                         dec.Reset ();
1338                                         ce++;
1339                                 }
1340                         }
1341                         Assert.IsTrue (
1342                                 ce == t.eindex.Length,
1343                                 String.Format (
1344                                         "test#{0}-2-{1}: UNEXPECTED SUCCESS (expected {2} exceptions, but happened {3})",
1345                                         testno, block_size, t.eindex.Length, ce));
1346                 }
1347
1348                 [Test]
1349                 public void DecoderFallbackExceptions ()
1350                 {
1351
1352                         DecoderFallbackExceptionTest [] tests = new DecoderFallbackExceptionTest []
1353                         {
1354                                 /* #1  */
1355                                 new DecoderFallbackExceptionTest (
1356                                         "Greek word 'kosme'",
1357                                         new int [] { },
1358                                         new int [] { },
1359                                         new byte [] {
1360                                                 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf,
1361                                                 0x83, 0xce, 0xbc, 0xce, 0xb5 }),
1362                                 /* #2  */
1363                                 new DecoderFallbackExceptionTest (
1364                                         "First possible sequence of 1 byte",
1365                                         new int [] { },
1366                                         new int [] { },
1367                                         new byte [] { 0x00 }),
1368                                 /* #3  */
1369                                 new DecoderFallbackExceptionTest (
1370                                         "First possible sequence of 2 bytes",
1371                                         new int [] { },
1372                                         new int [] { },
1373                                         new byte [] { 0xc2, 0x80 }),
1374                                 /* #4  */
1375                                 new DecoderFallbackExceptionTest (
1376                                         "First possible sequence of 3 bytes",
1377                                         new int [] { },
1378                                         new int [] { },
1379                                         new byte [] { 0xe0, 0xa0, 0x80 }),
1380                                 /* #5  */
1381                                 new DecoderFallbackExceptionTest (
1382                                         "First possible sequence of 4 bytes",
1383                                         new int [] { },
1384                                         new int [] { },
1385                                         new byte [] { 0xf0, 0x90, 0x80, 0x80 }),
1386                                 /* #6  */
1387                                 new DecoderFallbackExceptionTest (
1388                                         "First possible sequence of 5 bytes",
1389                                         new int [] { 0, 1, 2, 3, 4 },
1390                                         new int [] { 1, 1, 1, 1, 1 },
1391                                         new byte [] { 0xf8, 0x88, 0x80, 0x80, 0x80 }),
1392                                 /* #7  */
1393                                 new DecoderFallbackExceptionTest (
1394                                         "First possible sequence of 6 bytes",
1395                                         new int [] { 0, 1, 2, 3, 4, 5 },
1396                                         new int [] { 1, 1, 1, 1, 1, 1 },
1397                                         new byte [] {
1398                                                 0xfc, 0x84, 0x80, 0x80, 0x80, 0x80 }),
1399                                 /* #8  */
1400                                 new DecoderFallbackExceptionTest (
1401                                         "Last possible sequence of 1 byte",
1402                                         new int [] { },
1403                                         new int [] { },
1404                                         new byte [] { 0x7f }),
1405                                 /* #9  */
1406                                 new DecoderFallbackExceptionTest (
1407                                         "Last possible sequence of 2 bytes",
1408                                         new int [] { },
1409                                         new int [] { },
1410                                         new byte [] { 0xdf, 0xbf }),
1411                                 /* #10 */
1412                                 new DecoderFallbackExceptionTest (
1413                                         "Last possible sequence of 3 bytes",
1414                                         new int [] { },
1415                                         new int [] { },
1416                                         new byte [] { 0xef, 0xbf, 0xbf }),
1417                                 /* #11 */
1418                                 new DecoderFallbackExceptionTest (
1419                                         "Last possible sequence of 4 bytes",
1420                                         new int [] { 0, 1, 2, 3 },
1421                                         new int [] { 1, 1, 1, 1 },
1422                                         new byte [] { 0xf7, 0xbf, 0xbf, 0xbf }),
1423                                 /* #12 */
1424                                 new DecoderFallbackExceptionTest (
1425                                         "Last possible sequence of 5 bytes",
1426                                         new int [] { 0, 1, 2, 3, 4 },
1427                                         new int [] { 1, 1, 1, 1, 1 },
1428                                         new byte [] { 0xfb, 0xbf, 0xbf, 0xbf, 0xbf }),
1429                                 /* #13 */
1430                                 new DecoderFallbackExceptionTest (
1431                                         "Last possible sequence of 6 bytes",
1432                                         new int [] { 0, 1, 2, 3, 4, 5 },
1433                                         new int [] { 1, 1, 1, 1, 1, 1 },
1434                                         new byte [] { 0xfd, 0xbf, 0xbf, 0xbf, 0xbf, 0xbf }),
1435                                 /* #14 */
1436                                 new DecoderFallbackExceptionTest (
1437                                         "U-0000D7FF = ed 9f bf",
1438                                         new int [] { },
1439                                         new int [] { },
1440                                         new byte [] { 0xed, 0x9f, 0xbf }),
1441                                 /* #15 */
1442                                 new DecoderFallbackExceptionTest (
1443                                         "U-0000E000 = ee 80 80",
1444                                         new int [] { },
1445                                         new int [] { },
1446                                         new byte [] { 0xee, 0x80, 0x80 }),
1447                                 /* #16 */
1448                                 new DecoderFallbackExceptionTest (
1449                                         "U-0000FFFD = ef bf bd",
1450                                         new int [] { },
1451                                         new int [] { },
1452                                         new byte [] { 0xef, 0xbf, 0xbd }),
1453                                 /* #17 */
1454                                 new DecoderFallbackExceptionTest (
1455                                         "U-0010FFFF = f4 8f bf bf",
1456                                         new int [] { },
1457                                         new int [] { },
1458                                         new byte [] { 0xf4, 0x8f, 0xbf, 0xbf }),
1459                                 /* #18 */
1460                                 new DecoderFallbackExceptionTest (
1461                                         "U-00110000 = f4 90 80 80",
1462                                         new int [] { 0, 2, 3 },
1463                                         new int [] { 2, 1, 1 },
1464                                         new byte [] { 0xf4, 0x90, 0x80, 0x80 }),
1465                                 /* #19 */
1466                                 new DecoderFallbackExceptionTest (
1467                                         "First continuation byte 0x80",
1468                                         new int [] { 0 },
1469                                         new int [] { 1 },
1470                                         new byte [] { 0x80 }),
1471                                 /* #20 */
1472                                 new DecoderFallbackExceptionTest (
1473                                         "Last  continuation byte 0xbf",
1474                                         new int [] { 0 },
1475                                         new int [] { 1 },
1476                                         new byte [] { 0xbf }),
1477                                 /* #21 */
1478                                 new DecoderFallbackExceptionTest (
1479                                         "2 continuation bytes",
1480                                         new int [] { 0, 1 },
1481                                         new int [] { 1, 1 },
1482                                         new byte [] { 0x80, 0xbf }),
1483                                 /* #22 */
1484                                 new DecoderFallbackExceptionTest (
1485                                         "3 continuation bytes",
1486                                         new int [] { 0, 1, 2 },
1487                                         new int [] { 1, 1, 1 },
1488                                         new byte [] { 0x80, 0xbf, 0x80 }),
1489                                 /* #23 */
1490                                 new DecoderFallbackExceptionTest (
1491                                         "4 continuation bytes",
1492                                         new int [] { 0, 1, 2, 3 },
1493                                         new int [] { 1, 1, 1, 1 },
1494                                         new byte [] { 0x80, 0xbf, 0x80, 0xbf }),
1495                                 /* #24 */
1496                                 new DecoderFallbackExceptionTest (
1497                                         "5 continuation bytes",
1498                                         new int [] { 0, 1, 2, 3, 4 },
1499                                         new int [] { 1, 1, 1, 1, 1 },
1500                                         new byte [] { 0x80, 0xbf, 0x80, 0xbf, 0x80 }),
1501                                 /* #25 */
1502                                 new DecoderFallbackExceptionTest (
1503                                         "6 continuation bytes",
1504                                         new int [] { 0, 1, 2, 3, 4, 5 },
1505                                         new int [] { 1, 1, 1, 1, 1, 1 },
1506                                         new byte [] {
1507                                                 0x80, 0xbf, 0x80, 0xbf, 0x80, 0xbf }),
1508                                 /* #26 */
1509                                 new DecoderFallbackExceptionTest (
1510                                         "7 continuation bytes",
1511                                         new int [] { 0, 1, 2, 3, 4, 5, 6 },
1512                                         new int [] { 1, 1, 1, 1, 1, 1, 1 },
1513                                         new byte [] {
1514                                                 0x80, 0xbf, 0x80, 0xbf, 0x80, 0xbf,
1515                                                 0x80 }),
1516                                 /* #27 */
1517                                 new DecoderFallbackExceptionTest (
1518                                         "Sequence of all 64 continuation bytes",
1519                                         new int [] {
1520                                                  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
1521                                                 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1522                                                 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1523                                                 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
1524                                                 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
1525                                                 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
1526                                                 60, 61, 62, 63 },
1527                                         new int [] {
1528                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1529                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1530                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1531                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1532                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1533                                                 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1534                                                 1, 1, 1, 1 },
1535                                         new byte [] {
1536                                                 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
1537                                                 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b,
1538                                                 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91,
1539                                                 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
1540                                                 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d,
1541                                                 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3,
1542                                                 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
1543                                                 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
1544                                                 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5,
1545                                                 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb,
1546                                                 0xbc, 0xbd, 0xbe, 0xbf }),
1547                                 /* #28 */
1548                                 new DecoderFallbackExceptionTest (
1549                                         "All 32 first bytes of 2-byte sequences (0xc0-0xdf), each followed by a space character",
1550                                         new int [] {
1551                                                  0,  2,  4,  6,  8,
1552                                                 10, 12, 14, 16, 18,
1553                                                 20, 22, 24, 26, 28,
1554                                                 30, 32, 34, 36, 38,
1555                                                 40, 42, 44, 46, 48,
1556                                                 50, 52, 54, 56, 58,
1557                                                 60, 62 },
1558                                         new int [] {
1559                                                 1, 1, 1, 1, 1,
1560                                                 1, 1, 1, 1, 1,
1561                                                 1, 1, 1, 1, 1,
1562                                                 1, 1, 1, 1, 1,
1563                                                 1, 1, 1, 1, 1,
1564                                                 1, 1, 1, 1, 1,
1565                                                 1, 1 },
1566                                         new byte [] {
1567                                                 0xc0, 0x20, 0xc1, 0x20, 0xc2, 0x20,
1568                                                 0xc3, 0x20, 0xc4, 0x20, 0xc5, 0x20,
1569                                                 0xc6, 0x20, 0xc7, 0x20, 0xc8, 0x20,
1570                                                 0xc9, 0x20, 0xca, 0x20, 0xcb, 0x20,
1571                                                 0xcc, 0x20, 0xcd, 0x20, 0xce, 0x20,
1572                                                 0xcf, 0x20, 0xd0, 0x20, 0xd1, 0x20,
1573                                                 0xd2, 0x20, 0xd3, 0x20, 0xd4, 0x20,
1574                                                 0xd5, 0x20, 0xd6, 0x20, 0xd7, 0x20,
1575                                                 0xd8, 0x20, 0xd9, 0x20, 0xda, 0x20,
1576                                                 0xdb, 0x20, 0xdc, 0x20, 0xdd, 0x20,
1577                                                 0xde, 0x20, 0xdf, 0x20 }),
1578                                 /* #29 */
1579                                 new DecoderFallbackExceptionTest (
1580                                         "All 16 first bytes of 3-byte sequences (0xe0-0xef), each followed by a space character",
1581                                         new int [] {
1582                                                  0,  2,  4,  6,  8,
1583                                                 10, 12, 14, 16, 18,
1584                                                 20, 22, 24, 26, 28,
1585                                                 30 },
1586                                         new int [] {
1587                                                 1, 1, 1, 1, 1,
1588                                                 1, 1, 1, 1, 1,
1589                                                 1, 1, 1, 1, 1,
1590                                                 1 },
1591                                         new byte [] {
1592                                                 0xe0, 0x20, 0xe1, 0x20, 0xe2, 0x20,
1593                                                 0xe3, 0x20, 0xe4, 0x20, 0xe5, 0x20,
1594                                                 0xe6, 0x20, 0xe7, 0x20, 0xe8, 0x20,
1595                                                 0xe9, 0x20, 0xea, 0x20, 0xeb, 0x20,
1596                                                 0xec, 0x20, 0xed, 0x20, 0xee, 0x20,
1597                                                 0xef, 0x20 }),
1598                                 /* #30 */
1599                                 new DecoderFallbackExceptionTest (
1600                                         "All 8 first bytes of 4-byte sequences (0xf0-0xf7), each followed by a space character",
1601                                         new int [] { 0,  2,  4,  6,  8, 10, 12, 14 },
1602                                         new int [] { 1, 1, 1, 1, 1, 1, 1, 1 },
1603                                         new byte [] {
1604                                                 0xf0, 0x20, 0xf1, 0x20, 0xf2, 0x20,
1605                                                 0xf3, 0x20, 0xf4, 0x20, 0xf5, 0x20,
1606                                                 0xf6, 0x20, 0xf7, 0x20 }),
1607                                 /* #31 */
1608                                 new DecoderFallbackExceptionTest (
1609                                         "All 4 first bytes of 5-byte sequences (0xf8-0xfb), each followed by a space character",
1610                                         new int [] { 0, 2, 4, 6 },
1611                                         new int [] { 1, 1, 1, 1 },
1612                                         new byte [] {
1613                                                 0xf8, 0x20, 0xf9, 0x20, 0xfa, 0x20,
1614                                                 0xfb, 0x20 }),
1615                                 /* #32 */
1616                                 new DecoderFallbackExceptionTest (
1617                                         "All 2 first bytes of 6-byte sequences (0xfc-0xfd), each followed by a space character",
1618                                         new int [] { 0, 2 },
1619                                         new int [] { 1, 1 },
1620                                         new byte [] { 0xfc, 0x20, 0xfd, 0x20 }),
1621                                 /* #33 */
1622                                 new DecoderFallbackExceptionTest (
1623                                         "2-byte sequence with last byte missing",
1624                                         new int [] { 0 },
1625                                         new int [] { 1 },
1626                                         new byte [] { 0xc0 }),
1627                                 /* #34 */
1628                                 new DecoderFallbackExceptionTest (
1629                                         "3-byte sequence with last byte missing",
1630                                         new int [] { 0 },
1631                                         new int [] { 2 },
1632                                         new byte [] { 0xe0, 0x80 }),
1633                                 /* #35 */
1634                                 new DecoderFallbackExceptionTest (
1635                                         "4-byte sequence with last byte missing",
1636                                         new int [] { 0, 2 },
1637                                         new int [] { 2, 1 },
1638                                         new byte [] { 0xf0, 0x80, 0x80 }),
1639                                 /* #36 */
1640                                 new DecoderFallbackExceptionTest (
1641                                         "5-byte sequence with last byte missing",
1642                                         new int [] { 0, 1, 2, 3 },
1643                                         new int [] { 1, 1, 1, 1 },
1644                                         new byte [] { 0xf8, 0x80, 0x80, 0x80 }),
1645                                 /* #37 */
1646                                 new DecoderFallbackExceptionTest (
1647                                         "6-byte sequence with last byte missing",
1648                                         new int [] { 0, 1, 2, 3, 4 },
1649                                         new int [] { 1, 1, 1, 1, 1 },
1650                                         new byte [] { 0xfc, 0x80, 0x80, 0x80, 0x80 }),
1651                                 /* #38 */
1652                                 new DecoderFallbackExceptionTest (
1653                                         "2-byte sequence with last byte missing",
1654                                         new int [] { 0 },
1655                                         new int [] { 1 },
1656                                         new byte [] { 0xdf }),
1657                                 /* #39 */
1658                                 new DecoderFallbackExceptionTest (
1659                                         "3-byte sequence with last byte missing",
1660                                         new int [] { 0 },
1661                                         new int [] { 2 },
1662                                         new byte [] { 0xef, 0xbf }),
1663                                 /* #40 */
1664                                 new DecoderFallbackExceptionTest (
1665                                         "4-byte sequence with last byte missing",
1666                                         new int [] { 0, 1, 2 },
1667                                         new int [] { 1, 1, 1 },
1668                                         new byte [] { 0xf7, 0xbf, 0xbf }),
1669                                 /* #41 */
1670                                 new DecoderFallbackExceptionTest (
1671                                         "5-byte sequence with last byte missing",
1672                                         new int [] { 0, 1, 2, 3 },
1673                                         new int [] { 1, 1, 1, 1 },
1674                                         new byte [] { 0xfb, 0xbf, 0xbf, 0xbf }),
1675                                 /* #42 */
1676                                 new DecoderFallbackExceptionTest (
1677                                         "6-byte sequence with last byte missing",
1678                                         new int [] { 0, 1, 2, 3, 4 },
1679                                         new int [] { 1, 1, 1, 1, 1 },
1680                                         new byte [] { 0xfd, 0xbf, 0xbf, 0xbf, 0xbf }),
1681                                 /* #43 */
1682                                 new DecoderFallbackExceptionTest (
1683                                         "All the 10 sequences of 3.3 concatenated",
1684                                         new int [] {
1685                                                  0,  1,      3,
1686                                                  5,  6,  7,  8,  9,
1687                                                 10, 11, 12, 13, 14,
1688                                                 15, 16,     18, 19,
1689                                                 20, 21, 22, 23, 24,
1690                                                 25, 26, 27, 28, 29 },
1691                                         new int [] {
1692                                                  1,  2,      2,
1693                                                  1,  1,  1,  1,  1,
1694                                                  1,  1,  1,  1,  1,
1695                                                  1,  2,      1,  1,
1696                                                  1,  1,  1,  1,  1,
1697                                                  1,  1,  1,  1,  1 },
1698                                         new byte [] {
1699                                                 0xc0, 0xe0, 0x80, 0xf0, 0x80, 0x80,
1700                                                 0xf8, 0x80, 0x80, 0x80, 0xfc, 0x80,
1701                                                 0x80, 0x80, 0x80, 0xdf, 0xef, 0xbf,
1702                                                 0xf7, 0xbf, 0xbf, 0xfb, 0xbf, 0xbf,
1703                                                 0xbf, 0xfd, 0xbf, 0xbf, 0xbf, 0xbf }),
1704                                 /* #44 */
1705                                 new DecoderFallbackExceptionTest (
1706                                         "Bad chars fe",
1707                                         new int [] { 0 },
1708                                         new int [] { 1 },
1709                                         new byte [] { 0xfe }),
1710                                 /* #45 */
1711                                 new DecoderFallbackExceptionTest (
1712                                         "Bad chars ff",
1713                                         new int [] { 0 },
1714                                         new int [] { 1 },
1715                                         new byte [] { 0xff }),
1716                                 /* #46 */
1717                                 new DecoderFallbackExceptionTest (
1718                                         "Bad chars fe fe ff ff",
1719                                         new int [] { 0, 1, 2, 3 },
1720                                         new int [] { 1, 1, 1, 1 },
1721                                         new byte [] { 0xfe, 0xfe, 0xff, 0xff }),
1722                                 /* #47 */
1723                                 new DecoderFallbackExceptionTest (
1724                                         "Overlong U+002F = c0 af",
1725                                         new int [] { 0, 1 },
1726                                         new int [] { 1, 1 },
1727                                         new byte [] { 0xc0, 0xaf }),
1728                                 /* #48 */
1729                                 new DecoderFallbackExceptionTest (
1730                                         "Overlong U+002F = e0 80 af",
1731                                         new int [] { 0, 2 },
1732                                         new int [] { 2, 1 },
1733                                         new byte [] { 0xe0, 0x80, 0xaf }),
1734                                 /* #49 */
1735                                 new DecoderFallbackExceptionTest (
1736                                         "Overlong U+002F = f0 80 80 af",
1737                                         new int [] { 0, 2, 3 },
1738                                         new int [] { 2, 1, 1 },
1739                                         new byte [] { 0xf0, 0x80, 0x80, 0xaf }),
1740                                 /* #50 */
1741                                 new DecoderFallbackExceptionTest (
1742                                         "Overlong U+002F = f8 80 80 80 af",
1743                                         new int [] { 0, 1, 2, 3, 4 },
1744                                         new int [] { 1, 1, 1, 1, 1 },
1745                                         new byte [] { 0xf8, 0x80, 0x80, 0x80, 0xaf }),
1746                                 /* #51 */
1747                                 new DecoderFallbackExceptionTest (
1748                                         "Overlong U+002F = fc 80 80 80 80 af",
1749                                         new int [] { 0, 1, 2, 3, 4, 5 },
1750                                         new int [] { 1, 1, 1, 1, 1, 1 },
1751                                         new byte [] {
1752                                                 0xfc, 0x80, 0x80, 0x80, 0x80, 0xaf }),
1753                                 /* #52 */
1754                                 new DecoderFallbackExceptionTest (
1755                                         "Maximum overlong U-0000007F",
1756                                         new int [] { 0, 1 },
1757                                         new int [] { 1, 1 },
1758                                         new byte [] { 0xc1, 0xbf }),
1759                                 /* #53 */
1760                                 new DecoderFallbackExceptionTest (
1761                                         "Maximum overlong U-000007FF",
1762                                         new int [] { 0, 2 },
1763                                         new int [] { 2, 1, },
1764                                         new byte [] { 0xe0, 0x9f, 0xbf }),
1765                                 /* #54 */
1766                                 new DecoderFallbackExceptionTest (
1767                                         "Maximum overlong U-0000FFFF",
1768                                         new int [] { 0, 2, 3 },
1769                                         new int [] { 2, 1, 1 },
1770                                         new byte [] { 0xf0, 0x8f, 0xbf, 0xbf }),
1771                                 /* #55 */
1772                                 new DecoderFallbackExceptionTest (      
1773                                         "Maximum overlong U-001FFFFF",
1774                                         new int [] { 0, 1, 2, 3, 4 },
1775                                         new int [] { 1, 1, 1, 1, 1 },
1776                                         new byte [] { 0xf8, 0x87, 0xbf, 0xbf, 0xbf }),
1777                                 /* #56 */
1778                                 new DecoderFallbackExceptionTest (
1779                                         "Maximum overlong U-03FFFFFF",
1780                                         new int [] { 0, 1, 2, 3, 4, 5 },
1781                                         new int [] { 1, 1, 1, 1, 1, 1 },
1782                                         new byte [] {
1783                                                 0xfc, 0x83, 0xbf, 0xbf, 0xbf, 0xbf }),
1784                                 /* #57 */
1785                                 new DecoderFallbackExceptionTest (
1786                                         "Null overlong c0 80",
1787                                         new int [] { 0, 1 },
1788                                         new int [] { 1, 1 },
1789                                         new byte [] { 0xc0, 0x80, 0x22 }),
1790                                 /* #58 */
1791                                 new DecoderFallbackExceptionTest (
1792                                         "Null overlong e0 80 80",
1793                                         new int [] { 0, 2 },
1794                                         new int [] { 2, 1 },
1795                                         new byte [] { 0xe0, 0x80, 0x80 }),
1796                                 /* #59 */
1797                                 new DecoderFallbackExceptionTest (
1798                                         "Null overlong f0 80 80 80",
1799                                         new int [] { 0, 2, 3 },
1800                                         new int [] { 2, 1, 1 },
1801                                         new byte [] { 0xf0, 0x80, 0x80, 0x80 }),
1802                                 /* #60 */
1803                                 new DecoderFallbackExceptionTest (
1804                                         "Null overlong f8 80 80 80 80",
1805                                         new int [] { 0, 1, 2, 3, 4 },
1806                                         new int [] { 1, 1, 1, 1, 1 },
1807                                         new byte [] { 0xf8, 0x80, 0x80, 0x80, 0x80 }),
1808                                 /* #61 */
1809                                 new DecoderFallbackExceptionTest (
1810                                         "Null overlong fc 80 80 80 80 80",
1811                                         new int [] { 0, 1, 2, 3, 4, 5 },
1812                                         new int [] { 1, 1, 1, 1, 1, 1 },
1813                                         new byte [] {
1814                                                 0xfc, 0x80, 0x80, 0x80, 0x80, 0x80 }),
1815                                 /* #62 */
1816                                 new DecoderFallbackExceptionTest (
1817                                         "Single UTF-16 surrogate U+D800",
1818                                         new int [] { 0, 2 },
1819                                         new int [] { 2, 1 },
1820                                         new byte [] { 0xed, 0xa0, 0x80 }),
1821                                 /* #63 */
1822                                 new DecoderFallbackExceptionTest (
1823                                         "Single UTF-16 surrogate U+DB7F",
1824                                         new int [] { 0, 2 },
1825                                         new int [] { 2, 1 },
1826                                         new byte [] { 0xed, 0xad, 0xbf }),
1827                                 /* #64 */
1828                                 new DecoderFallbackExceptionTest (
1829                                         "Single UTF-16 surrogate U+DB80",
1830                                         new int [] { 0, 2 },
1831                                         new int [] { 2, 1 },
1832                                         new byte [] { 0xed, 0xae, 0x80 }),
1833                                 /* #65 */
1834                                 new DecoderFallbackExceptionTest (
1835                                         "Single UTF-16 surrogate U+DBFF",
1836                                         new int [] { 0, 2 },
1837                                         new int [] { 2, 1 },
1838                                         new byte [] { 0xed, 0xaf, 0xbf }),
1839                                 /* #66 */
1840                                 new DecoderFallbackExceptionTest (
1841                                         "Single UTF-16 surrogate U+DC00",
1842                                         new int [] { 0, 2 },
1843                                         new int [] { 2, 1 },
1844                                         new byte [] { 0xed, 0xb0, 0x80 }),
1845                                 /* #67 */
1846                                 new DecoderFallbackExceptionTest (
1847                                         "Single UTF-16 surrogate U+DF80",
1848                                         new int [] { 0, 2 },
1849                                         new int [] { 2, 1 },
1850                                         new byte [] { 0xed, 0xbe, 0x80 }),
1851                                 /* #68 */
1852                                 new DecoderFallbackExceptionTest (
1853                                         "Single UTF-16 surrogate U+DFFF",
1854                                         new int [] { 0, 2 },
1855                                         new int [] { 2, 1 },
1856                                         new byte [] { 0xed, 0xbf, 0xbf }),
1857                                 /* #69 */
1858                                 new DecoderFallbackExceptionTest (
1859                                         "Paired UTF-16 surrogate U+D800 U+DC00",
1860                                         new int [] { 0, 2, 3, 5 },
1861                                         new int [] { 2, 1, 2, 1 },
1862                                         new byte [] {
1863                                                 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80 }),
1864                                 /* #70 */
1865                                 new DecoderFallbackExceptionTest (
1866                                         "Paired UTF-16 surrogate U+D800 U+DFFF",
1867                                         new int [] { 0, 2, 3, 5 },
1868                                         new int [] { 2, 1, 2, 1 },
1869                                         new byte [] {
1870                                                 0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf }),
1871                                 /* #71 */
1872                                 new DecoderFallbackExceptionTest (
1873                                         "Paired UTF-16 surrogate U+DB7F U+DC00",
1874                                         new int [] { 0, 2, 3, 5 },
1875                                         new int [] { 2, 1, 2, 1 },
1876                                         new byte [] {
1877                                                 0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80 }),
1878                                 /* #72 */
1879                                 new DecoderFallbackExceptionTest (
1880                                         "Paired UTF-16 surrogate U+DB7F U+DFFF",
1881                                         new int [] { 0, 2, 3, 5 },
1882                                         new int [] { 2, 1, 2, 1 },
1883                                         new byte [] {
1884                                                 0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf }),
1885                                 /* #73 */
1886                                 new DecoderFallbackExceptionTest (
1887                                         "Paired UTF-16 surrogate U+DB80 U+DC00",
1888                                         new int [] { 0, 2, 3, 5 },
1889                                         new int [] { 2, 1, 2, 1 },
1890                                         new byte [] {
1891                                                 0xed, 0xae, 0x80, 0xed, 0xb0, 0x80 }),
1892                                 /* #74 */
1893                                 new DecoderFallbackExceptionTest (
1894                                         "Paired UTF-16 surrogate U+DB80 U+DFFF",
1895                                         new int [] { 0, 2, 3, 5 },
1896                                         new int [] { 2, 1, 2, 1 },
1897                                         new byte [] {
1898                                                 0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf }),
1899                                 /* #75 */
1900                                 new DecoderFallbackExceptionTest (
1901                                         "Paired UTF-16 surrogate U+DBFF U+DC00",
1902                                         new int [] { 0, 2, 3, 5 },
1903                                         new int [] { 2, 1, 2, 1 },
1904                                         new byte [] {
1905                                                 0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80 }),
1906                                 /* #76 */
1907                                 new DecoderFallbackExceptionTest (
1908                                         "Paired UTF-16 surrogate U+DBFF U+DFFF",
1909                                         new int [] { 0, 2, 3, 5 },
1910                                         new int [] { 2, 1, 2, 1 },
1911                                         new byte [] {
1912                                                 0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf }),
1913                                 /* #77 */
1914                                 new DecoderFallbackExceptionTest (
1915                                         "Illegal code position U+FFFE",
1916                                         new int [] { },
1917                                         new int [] { },
1918                                         new byte [] { 0xef, 0xbf, 0xbe }),
1919                                 /* #78 */
1920                                 new DecoderFallbackExceptionTest (
1921                                         "Illegal code position U+FFFF",
1922                                         new int [] { },
1923                                         new int [] { },
1924                                         new byte [] { 0xef, 0xbf, 0xbf }),
1925                         };
1926                         Encoding utf8 = Encoding.GetEncoding (
1927                                                 "utf-8",
1928                                                 new EncoderExceptionFallback(),
1929                                                 new DecoderExceptionFallback());
1930                         Decoder dec = utf8.GetDecoder ();
1931                         char [] chars;
1932
1933                         for(int t = 0; t < tests.Length; t++) {
1934                                 chars = new char [utf8.GetMaxCharCount (tests[t].bytes.Length)];
1935
1936                                 // #1 complete conversion
1937                                 DecoderFallbackExceptions_GetChars (chars, t+1, dec, tests[t]);
1938
1939                                 // #2 convert with several block_sizes
1940                                 for (int bs = 1; bs <= tests[t].bytes.Length; bs++)
1941                                         DecoderFallbackExceptions_Convert (chars, t+1, dec, tests[t], bs);
1942                         }
1943                 }
1944
1945                 // EncoderFallbackExceptionTest
1946                 //   This struct describes a EncoderFallbackExceptions' test.
1947                 //   It contains an array (index_fail) which is void if it is a
1948                 //   valid UTF16 string.
1949                 //   If it is an invalid string this array contains indexes
1950                 //   (in 'index_fail') which point to the invalid chars in
1951                 //   'str'.
1952                 //   This array is hardcoded in each tests and it contains the
1953                 //   absolute positions found in a sequence of
1954                 //   EncoderFallbackException exceptions thrown if you convert
1955                 //   this strings on a MS.NET platform.
1956                 struct EncoderFallbackExceptionTest
1957                 {
1958                         public string str;
1959                         public int [] eindex;
1960                         public EncoderFallbackExceptionTest (
1961                                         string str,
1962                                         int [] eindex)
1963                         {
1964                                 this.str = str;
1965                                 this.eindex = eindex;
1966                         }
1967                 }
1968
1969                 // try to encode some bytes at once with GetBytes
1970                 private void EncoderFallbackExceptions_GetBytes (
1971                         byte [] bytes,
1972                         int testno,
1973                         Encoder enc,
1974                         EncoderFallbackExceptionTest t)
1975                 {
1976                         try {
1977                                 enc.GetBytes (
1978                                         t.str.ToCharArray (), 0, t.str.Length,
1979                                         bytes, 0, true);
1980                                 Assert.IsTrue (
1981                                         t.eindex.Length == 0,
1982                                         String.Format (
1983                                                 "test#{0}-1: UNEXPECTED SUCCESS",
1984                                                 testno));
1985                         } catch(EncoderFallbackException ex) {
1986                                 Assert.IsTrue (
1987                                         t.eindex.Length > 0,
1988                                         String.Format (
1989                                                 "test#{0}-1: UNEXPECTED FAIL",
1990                                                 testno));
1991                                 Assert.IsTrue (
1992                                         ex.Index == t.eindex[0],
1993                                         String.Format (
1994                                                 "test#{0}-1: Expected exception at {1} not {2}.",
1995                                                 testno, t.eindex[0], ex.Index));
1996                                 Assert.IsTrue (
1997                                         !ex.IsUnknownSurrogate (),
1998                                         String.Format (
1999                                                 "test#{0}-1: Expected false not {1} in IsUnknownSurrogate().",
2000                                                 testno,
2001                                                 ex.IsUnknownSurrogate ()));
2002                                 // NOTE: I know that in the previous check we
2003                                 // have asserted that ex.IsUnknownSurrogate()
2004                                 // is always false, but this does not mean that
2005                                 // we don't have to take in consideration its
2006                                 // real value for the next check.
2007                                 if (ex.IsUnknownSurrogate ())
2008                                         Assert.IsTrue (
2009                                                 ex.CharUnknownHigh == t.str[ex.Index]
2010                                                 && ex.CharUnknownLow == t.str[ex.Index + 1],
2011                                                 String.Format (
2012                                                         "test#{0}-1: expected ({1:X}, {2:X}) not ({3:X}, {4:X}).",
2013                                                         testno,
2014                                                         t.str[ex.Index],
2015                                                         t.str[ex.Index + 1],
2016                                                         ex.CharUnknownHigh,
2017                                                         ex.CharUnknownLow));
2018                                 else
2019                                         Assert.IsTrue (
2020                                                 ex.CharUnknown == t.str[ex.Index],
2021                                                 String.Format (
2022                                                         "test#{0}-1: expected ({1:X}) not ({2:X}).",
2023                                                         testno,
2024                                                         t.str[ex.Index],
2025                                                         ex.CharUnknown));
2026                                 enc.Reset ();
2027                         }
2028                 }
2029
2030                 private void EncoderFallbackExceptions_Convert (
2031                         byte [] bytes,
2032                         int testno,
2033                         Encoder enc,
2034                         EncoderFallbackExceptionTest t,
2035                         int block_size)
2036                 {
2037                         int charsUsed, bytesUsed;
2038                         bool completed;
2039
2040                         int ce = 0; // current exception
2041
2042                         for (int c = 0; c < t.str.Length; ) {
2043                                 //Console.WriteLine ("test#{0}-2-{1}: c={2}", testno, block_size, c);
2044                                 try {
2045                                         int bu = c + block_size > t.str.Length
2046                                                         ? t.str.Length - c
2047                                                         : block_size;
2048                                         enc.Convert (
2049                                                 t.str.ToCharArray (), c, bu,
2050                                                 bytes, 0, bytes.Length,
2051                                                 c + bu >= t.str.Length,
2052                                                 out charsUsed, out bytesUsed,
2053                                                 out completed);
2054                                         c += charsUsed;
2055                                 } catch (EncoderFallbackException ex) {
2056                                         //Console.WriteLine (
2057                                         //      "test#{0}-2-{1}#{2}: Exception (Index={3}, UnknownSurrogate={4})",
2058                                         //      testno, block_size, ce,
2059                                         //      ex.Index, ex.IsUnknownSurrogate ());
2060                                         Assert.IsTrue (
2061                                                 ce < t.eindex.Length,
2062                                                 String.Format (
2063                                                         "test#{0}-2-{1}#{2}: UNEXPECTED EXCEPTION (Index={3}, UnknownSurrogate={4})",
2064                                                         testno, block_size, ce,
2065                                                         ex.Index,
2066                                                         ex.IsUnknownSurrogate ()));
2067                                         Assert.IsTrue (
2068                                                 ex.Index + c == t.eindex[ce],
2069                                                 String.Format (
2070                                                         "test#{0}-2-{1}#{2}: Expected exception at {3} not {4}.",
2071                                                         testno, block_size, ce,
2072                                                         t.eindex[ce],
2073                                                         ex.Index + c));
2074                                         Assert.IsTrue (
2075                                                 !ex.IsUnknownSurrogate (),
2076                                                 String.Format (
2077                                                         "test#{0}-2-{1}#{2}: Expected false not {3} in IsUnknownSurrogate().",
2078                                                         testno, block_size, ce,
2079                                                         ex.IsUnknownSurrogate ()));
2080                                         if (ex.IsUnknownSurrogate ()) {
2081                                                 Assert.IsTrue (
2082                                                         ex.CharUnknownHigh == t.str[ex.Index + c]
2083                                                         && ex.CharUnknownLow == t.str[ex.Index + c + 1],
2084                                                         String.Format (
2085                                                                 "test#{0}-2-{1}#{2}: expected ({3:X}, {4:X}) not ({5:X}, {6:X}).",
2086                                                                 testno, block_size, ce,
2087                                                                 t.str[ex.Index + c], t.str[ex.Index + c + 1],
2088                                                                 ex.CharUnknownHigh, ex.CharUnknownLow));
2089                                                 c += ex.Index + 2;
2090                                         } else {
2091                                                 Assert.IsTrue (
2092                                                         ex.CharUnknown == t.str[ex.Index + c],
2093                                                         String.Format (
2094                                                                 "test#{0}-2-{1}#{2}: expected ({3:X}) not ({4:X}).",
2095                                                                 testno, block_size, ce,
2096                                                                 t.str[ex.Index + c],
2097                                                                 ex.CharUnknown));
2098                                                 c += ex.Index + 1;
2099                                         }
2100                                         enc.Reset ();
2101                                         ce++;
2102                                 }
2103                         }
2104                         Assert.IsTrue (
2105                                 ce == t.eindex.Length,
2106                                 String.Format (
2107                                         "test#{0}-2-{1}: UNEXPECTED SUCCESS (expected {2} exceptions, but happened {3})",
2108                                         testno, block_size, t.eindex.Length, ce));
2109                 }
2110
2111                 [Test]
2112                 public void EncoderFallbackExceptions ()
2113                 {
2114
2115                         EncoderFallbackExceptionTest [] tests = new EncoderFallbackExceptionTest []
2116                         {
2117                                 /* #1  */ new EncoderFallbackExceptionTest ( "Zero \u0000.",                                   new int [] { }),
2118                                 /* #2  */ new EncoderFallbackExceptionTest ( "Last before leads \uD7FF.",                      new int [] { }),
2119                                 /* #3  */ new EncoderFallbackExceptionTest ( "Using lead \uD800 without a surrogate.",         new int [] { 11 }),
2120                                 /* #4  */ new EncoderFallbackExceptionTest ( "Using lead \uD877 without a surrogate.",         new int [] { 11 }),
2121                                 /* #5  */ new EncoderFallbackExceptionTest ( "Using lead \uDBFF without a surrogate.",         new int [] { 11 }),
2122                                 /* #6  */ new EncoderFallbackExceptionTest ( "Using trail \uDC00 without a lead.",             new int [] { 12 }),
2123                                 /* #7  */ new EncoderFallbackExceptionTest ( "Using trail \uDBFF without a lead.",             new int [] { 12 }),
2124                                 /* #8  */ new EncoderFallbackExceptionTest ( "First-plane 2nd block \uE000.",                  new int [] { }),
2125                                 /* #9  */ new EncoderFallbackExceptionTest ( "First-plane 2nd block \uFFFF.",                  new int [] { }),
2126                                 /* #10 */ new EncoderFallbackExceptionTest ( "Playing with first surrogate \uD800\uDC00.",     new int [] { }),
2127                                 /* #11 */ new EncoderFallbackExceptionTest ( "Playing before first surrogate \uD800\uDBFF.",   new int [] { 31, 32 }),
2128                                 /* #12 */ new EncoderFallbackExceptionTest ( "Playing with last of first plane \uD800\uDFFF.", new int [] { }),
2129                                 /* #13 */ new EncoderFallbackExceptionTest ( "Playing with first of last plane \uDBFF\uDC00.", new int [] { }),
2130                                 /* #14 */ new EncoderFallbackExceptionTest ( "Playing with last surrogate \uDBFF\uDFFF.",      new int [] { }),
2131                                 /* #15 */ new EncoderFallbackExceptionTest ( "Playing after last surrogate \uDBFF\uE000.",     new int [] { 29 }),
2132                                 /* #16 */ new EncoderFallbackExceptionTest ( "Incomplete string \uD800",                       new int [] { 18 }),
2133                                 /* #17 */ new EncoderFallbackExceptionTest ( "Horrible thing \uD800\uD800.",                   new int [] { 15, 16 }),
2134                         };
2135                         Encoding utf8 = Encoding.GetEncoding (
2136                                                 "utf-8",
2137                                                 new EncoderExceptionFallback(),
2138                                                 new DecoderExceptionFallback());
2139                         Encoder enc = utf8.GetEncoder ();
2140                         byte [] bytes;
2141
2142                         for(int t = 0; t < tests.Length; t++) {
2143                                 bytes = new byte [utf8.GetMaxByteCount (tests[t].str.Length)];
2144
2145                                 // #1 complete conversion
2146                                 EncoderFallbackExceptions_GetBytes (bytes, t+1, enc, tests[t]);
2147
2148                                 // #2 convert in two rounds
2149                                 for (int bs = 1; bs <= tests[t].str.Length; bs++)
2150                                         EncoderFallbackExceptions_Convert (bytes, t+1, enc, tests[t], bs);
2151                         }
2152                 }
2153         }
2154 }