UTF8EncodingTest.cs 33 KB

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