X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2Fcorlib%2FTest%2FSystem.Text%2FUTF8EncodingTest.cs;h=fd161035cb18f9575cb85f03947d143087420a92;hb=694ab7d8477ba306f75ee1cb71ec8acd87788f67;hp=f5a2e452fc05649e1b09def55b88f92e4ddfe63c;hpb=669beaed8380fa592533c8755f72593b4422d01d;p=mono.git diff --git a/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs b/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs index f5a2e452fc0..fd161035cb1 100644 --- a/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs +++ b/mcs/class/corlib/Test/System.Text/UTF8EncodingTest.cs @@ -11,6 +11,7 @@ using NUnit.Framework; using System; +using System.IO; using System.Text; #if NET_2_0 @@ -21,11 +22,11 @@ using DecoderException = System.ArgumentException; using AssertType = NUnit.Framework.Assert; -namespace MonoTests.System.Text { - +namespace MonoTests.System.Text +{ [TestFixture] - public class UTF8EncodingTest : Assertion { - + public class UTF8EncodingTest + { private UTF8Encoding utf8; [SetUp] @@ -34,95 +35,125 @@ namespace MonoTests.System.Text { utf8 = new UTF8Encoding (true, true); } - [Test] - public void TestEncodingGetBytes1() - { - UTF8Encoding utf8Enc = new UTF8Encoding (); - string UniCode = "\u0041\u2262\u0391\u002E"; - - // "A." may be encoded as 41 E2 89 A2 CE 91 2E - // see (RFC 2044) - byte[] utf8Bytes = utf8Enc.GetBytes (UniCode); - - Assertion.AssertEquals ("UTF #1", 0x41, utf8Bytes [0]); - Assertion.AssertEquals ("UTF #2", 0xE2, utf8Bytes [1]); - Assertion.AssertEquals ("UTF #3", 0x89, utf8Bytes [2]); - Assertion.AssertEquals ("UTF #4", 0xA2, utf8Bytes [3]); - Assertion.AssertEquals ("UTF #5", 0xCE, utf8Bytes [4]); - Assertion.AssertEquals ("UTF #6", 0x91, utf8Bytes [5]); - Assertion.AssertEquals ("UTF #7", 0x2E, utf8Bytes [6]); - } - - [Test] - public void TestEncodingGetBytes2() - { - UTF8Encoding utf8Enc = new UTF8Encoding (); - string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021"; - - // "Hi Mom !" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 - // see (RFC 2044) - byte[] utf8Bytes = new byte [11]; - - int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0); - - Assertion.AssertEquals ("UTF #1", 11, ByteCnt); - Assertion.AssertEquals ("UTF #2", 0x48, utf8Bytes [0]); - Assertion.AssertEquals ("UTF #3", 0x69, utf8Bytes [1]); - Assertion.AssertEquals ("UTF #4", 0x20, utf8Bytes [2]); - Assertion.AssertEquals ("UTF #5", 0x4D, utf8Bytes [3]); - Assertion.AssertEquals ("UTF #6", 0x6F, utf8Bytes [4]); - Assertion.AssertEquals ("UTF #7", 0x6D, utf8Bytes [5]); - Assertion.AssertEquals ("UTF #8", 0x20, utf8Bytes [6]); - Assertion.AssertEquals ("UTF #9", 0xE2, utf8Bytes [7]); - Assertion.AssertEquals ("UTF #10", 0x98, utf8Bytes [8]); - Assertion.AssertEquals ("UTF #11", 0xBA, utf8Bytes [9]); - Assertion.AssertEquals ("UTF #12", 0x21, utf8Bytes [10]); - } - - [Test] - public void TestDecodingGetChars1() - { - UTF8Encoding utf8Enc = new UTF8Encoding (); - // 41 E2 89 A2 CE 91 2E may be decoded as "A." - // see (RFC 2044) - byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E}; - char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes); - - Assertion.AssertEquals ("UTF #1", 0x0041, UniCodeChars [0]); - Assertion.AssertEquals ("UTF #2", 0x2262, UniCodeChars [1]); - Assertion.AssertEquals ("UTF #3", 0x0391, UniCodeChars [2]); - Assertion.AssertEquals ("UTF #4", 0x002E, UniCodeChars [3]); - } - - [Test] + [Test] + public void IsBrowserDisplay () + { + Assert.IsTrue (utf8.IsBrowserDisplay); + } + + [Test] + public void IsBrowserSave () + { + Assert.IsTrue (utf8.IsBrowserSave); + } + + [Test] + public void IsMailNewsDisplay () + { + Assert.IsTrue (utf8.IsMailNewsDisplay); + } + + [Test] + public void IsMailNewsSave () + { + Assert.IsTrue (utf8.IsMailNewsSave); + } + + [Test] + public void TestCompat () + { + Assert.IsTrue (new UTF8Encoding ().Equals (new UTF8Encoding ())); + } + + [Test] + public void TestEncodingGetBytes1() + { + UTF8Encoding utf8Enc = new UTF8Encoding (); + string UniCode = "\u0041\u2262\u0391\u002E"; + + // "A." may be encoded as 41 E2 89 A2 CE 91 2E + // see (RFC 2044) + byte[] utf8Bytes = utf8Enc.GetBytes (UniCode); + + Assert.AreEqual (0x41, utf8Bytes [0], "UTF #1"); + Assert.AreEqual (0xE2, utf8Bytes [1], "UTF #2"); + Assert.AreEqual (0x89, utf8Bytes [2], "UTF #3"); + Assert.AreEqual (0xA2, utf8Bytes [3], "UTF #4"); + Assert.AreEqual (0xCE, utf8Bytes [4], "UTF #5"); + Assert.AreEqual (0x91, utf8Bytes [5], "UTF #6"); + Assert.AreEqual (0x2E, utf8Bytes [6], "UTF #7"); + } + + [Test] + public void TestEncodingGetBytes2() + { + UTF8Encoding utf8Enc = new UTF8Encoding (); + string UniCode = "\u0048\u0069\u0020\u004D\u006F\u006D\u0020\u263A\u0021"; + + // "Hi Mom !" may be encoded as 48 69 20 4D 6F 6D 20 E2 98 BA 21 + // see (RFC 2044) + byte[] utf8Bytes = new byte [11]; + + int ByteCnt = utf8Enc.GetBytes (UniCode.ToCharArray(), 0, UniCode.Length, utf8Bytes, 0); + Assert.AreEqual (11, ByteCnt, "UTF #1"); + Assert.AreEqual (0x48, utf8Bytes [0], "UTF #2"); + Assert.AreEqual (0x69, utf8Bytes [1], "UTF #3"); + Assert.AreEqual (0x20, utf8Bytes [2], "UTF #4"); + Assert.AreEqual (0x4D, utf8Bytes [3], "UTF #5"); + Assert.AreEqual (0x6F, utf8Bytes [4], "UTF #6"); + Assert.AreEqual (0x6D, utf8Bytes [5], "UTF #7"); + Assert.AreEqual (0x20, utf8Bytes [6], "UTF #8"); + Assert.AreEqual (0xE2, utf8Bytes [7], "UTF #9"); + Assert.AreEqual (0x98, utf8Bytes [8], "UTF #10"); + Assert.AreEqual (0xBA, utf8Bytes [9], "UTF #11"); + Assert.AreEqual (0x21, utf8Bytes [10], "UTF #12"); + } + + [Test] + public void TestDecodingGetChars1() + { + UTF8Encoding utf8Enc = new UTF8Encoding (); + // 41 E2 89 A2 CE 91 2E may be decoded as "A." + // see (RFC 2044) + byte[] utf8Bytes = new byte [] {0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E}; + char[] UniCodeChars = utf8Enc.GetChars(utf8Bytes); + + Assert.AreEqual (0x0041, UniCodeChars [0], "UTF #1"); + Assert.AreEqual (0x2262, UniCodeChars [1], "UTF #2"); + Assert.AreEqual (0x0391, UniCodeChars [2], "UTF #3"); + Assert.AreEqual (0x002E, UniCodeChars [3], "UTF #4"); + } + + [Test] #if NET_2_0 - [Category ("NotWorking")] + [Category ("NotWorking")] #endif - public void TestMaxCharCount() - { - UTF8Encoding UTF8enc = new UTF8Encoding (); + public void TestMaxCharCount() + { + UTF8Encoding UTF8enc = new UTF8Encoding (); #if NET_2_0 - // hmm, where is this extra 1 coming from? - Assertion.AssertEquals ("UTF #1", 51, UTF8enc.GetMaxCharCount(50)); + // hmm, where is this extra 1 coming from? + Assert.AreEqual (51, UTF8enc.GetMaxCharCount(50), "UTF #1"); #else - Assertion.AssertEquals ("UTF #1", 50, UTF8enc.GetMaxCharCount(50)); + Assert.AreEqual (50, UTF8enc.GetMaxCharCount(50), "UTF #1"); #endif - } - - [Test] + } + + [Test] #if NET_2_0 - [Category ("NotWorking")] + [Category ("NotWorking")] #endif - public void TestMaxByteCount() - { - UTF8Encoding UTF8enc = new UTF8Encoding (); + public void TestMaxByteCount() + { + UTF8Encoding UTF8enc = new UTF8Encoding (); #if NET_2_0 - // maybe under .NET 2.0 insufficient surrogate pair is just not handled, and 3 is Preamble size. - Assertion.AssertEquals ("UTF #1", 153, UTF8enc.GetMaxByteCount(50)); + // maybe under .NET 2.0 insufficient surrogate pair is + // just not handled, and 3 is Preamble size. + Assert.AreEqual (153, UTF8enc.GetMaxByteCount(50), "UTF #1"); #else - Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50)); + Assert.AreEqual (200, UTF8enc.GetMaxByteCount(50), "UTF #1"); #endif - } + } // regression for bug #59648 [Test] @@ -131,16 +162,33 @@ namespace MonoTests.System.Text { UTF8Encoding u = new UTF8Encoding (true, false); byte[] data = new byte [] { 0xC0, 0xAF }; +#if NET_2_0 + Assert.AreEqual (2, u.GetCharCount (data), "#A0"); + string s = u.GetString (data); + Assert.AreEqual ("\uFFFD\uFFFD", s, "#A1"); +#else + Assert.AreEqual (0, u.GetCharCount (data), "#A0"); string s = u.GetString (data); - AssertEquals (0, s.Length); + Assert.AreEqual (String.Empty, s, "#A1"); +#endif data = new byte [] { 0x30, 0x31, 0xC0, 0xAF, 0x30, 0x32 }; s = u.GetString (data); - AssertEquals (4, s.Length); - AssertEquals (0x30, (int) s [0]); - AssertEquals (0x31, (int) s [1]); - AssertEquals (0x30, (int) s [2]); - AssertEquals (0x32, (int) s [3]); +#if NET_2_0 + Assert.AreEqual (6, s.Length, "#B1"); + Assert.AreEqual (0x30, (int) s [0], "#B2"); + Assert.AreEqual (0x31, (int) s [1], "#B3"); + Assert.AreEqual (0xFFFD, (int) s [2], "#B4"); + Assert.AreEqual (0xFFFD, (int) s [3], "#B5"); + Assert.AreEqual (0x30, (int) s [4], "#B6"); + Assert.AreEqual (0x32, (int) s [5], "#B7"); +#else + Assert.AreEqual (4, s.Length, "#B1"); + Assert.AreEqual (0x30, (int) s [0], "#B2"); + Assert.AreEqual (0x31, (int) s [1], "#B3"); + Assert.AreEqual (0x30, (int) s [2], "#B4"); + Assert.AreEqual (0x32, (int) s [3], "#B5"); +#endif } // UTF8 decoding tests from http://www.cl.cam.ac.uk/~mgk25/ @@ -152,7 +200,7 @@ namespace MonoTests.System.Text { string s = utf8.GetString (data); // cute but saving source code in unicode can be problematic // so we just ensure we can re-encode this - AssertEquals ("Reconverted", BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (BitConverter.ToString (data), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted"); } [Test] @@ -160,24 +208,24 @@ namespace MonoTests.System.Text { { byte[] data211 = { 0x00 }; string s = utf8.GetString (data211); - AssertEquals ("1 byte (U-00000000)", "\0", s); - AssertEquals ("Reconverted-1", BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual ("\0", s, "1 byte (U-00000000)"); + Assert.AreEqual (BitConverter.ToString (data211), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1"); byte[] data212 = { 0xC2, 0x80 }; s = utf8.GetString (data212); - AssertEquals ("2 bytes (U-00000080)", 128, s [0]); - AssertEquals ("Reconverted-2", BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (128, s [0], "2 bytes (U-00000080)"); + Assert.AreEqual (BitConverter.ToString (data212), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2"); byte[] data213 = { 0xE0, 0xA0, 0x80 }; s = utf8.GetString (data213); - AssertEquals ("3 bytes (U-00000800)", 2048, s [0]); - AssertEquals ("Reconverted-3", BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (2048, s [0], "3 bytes (U-00000800)"); + Assert.AreEqual (BitConverter.ToString (data213), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3"); byte[] data214 = { 0xF0, 0x90, 0x80, 0x80 }; s = utf8.GetString (data214); - AssertEquals ("4 bytes (U-00010000)-0", 55296, s [0]); - AssertEquals ("4 bytes (U-00010000)-1", 56320, s [1]); - AssertEquals ("Reconverted-4", BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (55296, s [0], "4 bytes (U-00010000)-0"); + Assert.AreEqual (56320, s [1], "4 bytes (U-00010000)-1"); + Assert.AreEqual (BitConverter.ToString (data214), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4"); } [Test] @@ -187,8 +235,8 @@ namespace MonoTests.System.Text { { byte[] data215 = { 0xF8, 0x88, 0x80, 0x80, 0x80 }; string s = utf8.GetString (data215); - AssertNull ("5 bytes (U-00200000)", s); - AssertEquals ("Reconverted-5", BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "5 bytes (U-00200000)"); + Assert.AreEqual (BitConverter.ToString (data215), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5"); } [Test] @@ -198,8 +246,8 @@ namespace MonoTests.System.Text { { byte[] data216 = { 0xFC, 0x84, 0x80, 0x80, 0x80, 0x80 }; string s = utf8.GetString (data216); - AssertNull ("6 bytes (U-04000000)", s); - AssertEquals ("Reconverted-6", BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "6 bytes (U-04000000)"); + Assert.AreEqual (BitConverter.ToString (data216), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-6"); } [Test] @@ -207,18 +255,18 @@ namespace MonoTests.System.Text { { byte[] data221 = { 0x7F }; string s = utf8.GetString (data221); - AssertEquals ("1 byte (U-0000007F)", 127, s [0]); - AssertEquals ("Reconverted-1", BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (127, s [0], "1 byte (U-0000007F)"); + Assert.AreEqual (BitConverter.ToString (data221), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1"); byte[] data222 = { 0xDF, 0xBF }; s = utf8.GetString (data222); - AssertEquals ("2 bytes (U-000007FF)", 2047, s [0]); - AssertEquals ("Reconverted-2", BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (2047, s [0], "2 bytes (U-000007FF)"); + Assert.AreEqual (BitConverter.ToString (data222), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2"); byte[] data223 = { 0xEF, 0xBF, 0xBF }; s = utf8.GetString (data223); - AssertEquals ("3 bytes (U-0000FFFF)", 65535, s [0]); - AssertEquals ("Reconverted-3", BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (65535, s [0], "3 bytes (U-0000FFFF)"); + Assert.AreEqual (BitConverter.ToString (data223), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3"); } @@ -229,8 +277,8 @@ namespace MonoTests.System.Text { { byte[] data224 = { 0x7F, 0xBF, 0xBF, 0xBF }; string s = utf8.GetString (data224); - AssertNull ("4 bytes (U-001FFFFF)", s); - AssertEquals ("Reconverted-4", BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "4 bytes (U-001FFFFF)"); + Assert.AreEqual (BitConverter.ToString (data224), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4"); } [Test] @@ -240,8 +288,8 @@ namespace MonoTests.System.Text { { byte[] data225 = { 0xFB, 0xBF, 0xBF, 0xBF, 0xBF }; string s = utf8.GetString (data225); - AssertNull ("5 bytes (U-03FFFFFF)", s); - AssertEquals ("Reconverted-5", BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "5 bytes (U-03FFFFFF)"); + Assert.AreEqual (BitConverter.ToString (data225), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5"); } [Test] @@ -251,8 +299,8 @@ namespace MonoTests.System.Text { { byte[] data226 = { 0xFD, 0xBF, 0xBF, 0xBF, 0xBF, 0xBF }; string s = utf8.GetString (data226); - AssertNull ("6 bytes (U-7FFFFFFF)", s); - AssertEquals ("Reconverted-6", BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "6 bytes (U-7FFFFFFF)"); + Assert.AreEqual (BitConverter.ToString (data226), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-6"); } [Test] @@ -260,24 +308,24 @@ namespace MonoTests.System.Text { { byte[] data231 = { 0xED, 0x9F, 0xBF }; string s = utf8.GetString (data231); - AssertEquals ("U-0000D7FF", 55295, s [0]); - AssertEquals ("Reconverted-1", BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (55295, s [0], "U-0000D7FF"); + Assert.AreEqual (BitConverter.ToString (data231), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-1"); byte[] data232 = { 0xEE, 0x80, 0x80 }; s = utf8.GetString (data232); - AssertEquals ("U-0000E000", 57344, s [0]); - AssertEquals ("Reconverted-2", BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (57344, s [0], "U-0000E000"); + Assert.AreEqual (BitConverter.ToString (data232), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-2"); byte[] data233 = { 0xEF, 0xBF, 0xBD }; s = utf8.GetString (data233); - AssertEquals ("U-0000FFFD", 65533, s [0]); - AssertEquals ("Reconverted-3", BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (65533, s [0], "U-0000FFFD"); + Assert.AreEqual (BitConverter.ToString (data233), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-3"); byte[] data234 = { 0xF4, 0x8F, 0xBF, 0xBF }; s = utf8.GetString (data234); - AssertEquals ("U-0010FFFF-0", 56319, s [0]); - AssertEquals ("U-0010FFFF-1", 57343, s [1]); - AssertEquals ("Reconverted-4", BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s))); + Assert.AreEqual (56319, s [0], "U-0010FFFF-0"); + Assert.AreEqual (57343, s [1], "U-0010FFFF-1"); + Assert.AreEqual (BitConverter.ToString (data234), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-4"); } [Test] @@ -287,8 +335,8 @@ namespace MonoTests.System.Text { { byte[] data235 = { 0xF4, 0x90, 0x80, 0x80 }; string s = utf8.GetString (data235); - AssertNull ("U-00110000", s); - AssertEquals ("Reconverted-5", BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s))); + Assert.IsNull (s, "U-00110000"); + Assert.AreEqual (BitConverter.ToString (data235), BitConverter.ToString (utf8.GetBytes (s)), "Reconverted-5"); } [Test] @@ -482,7 +530,7 @@ namespace MonoTests.System.Text { try { string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", String.Empty, s); + Assert.AreEqual (String.Empty, s, "MS FX 1.1 behaviour"); } catch (DecoderException) { // but Mono doesn't - better stick to the standard @@ -490,7 +538,7 @@ namespace MonoTests.System.Text { } [Test] -// MS Fx 1.1 accept this + // MS Fx 1.1 accept this // [ExpectedException (typeof (DecoderException))] public void T3_Malformed_3_LastContinuationMissing_337 () { @@ -498,7 +546,7 @@ namespace MonoTests.System.Text { try { string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", String.Empty, s); + Assert.AreEqual (String.Empty, s, "MS FX 1.1 behaviour"); } catch (DecoderException) { // but Mono doesn't - better stick to the standard @@ -720,7 +768,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xA0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]); + Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -736,7 +784,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAD, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]); + Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -752,7 +800,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAE, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]); + Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -768,7 +816,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]); + Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -784,7 +832,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xB0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56320, s [0]); + Assert.AreEqual (56320, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -800,7 +848,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xBE, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 57216, s [0]); + Assert.AreEqual (57216, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -816,7 +864,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xBF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 57343, s [0]); + Assert.AreEqual (57343, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -832,8 +880,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]); + Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -849,8 +897,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xA0, 0x80, 0xED, 0xBF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 55296, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]); + Assert.AreEqual (55296, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -866,8 +914,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xB0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]); + Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -883,8 +931,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAD, 0xBF, 0xED, 0xBF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56191, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]); + Assert.AreEqual (56191, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -900,8 +948,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xB0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]); + Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -917,8 +965,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAE, 0x80, 0xED, 0xBF, 0x8F }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56192, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 57295, s [1]); + Assert.AreEqual (56192, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (57295, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -934,8 +982,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xB0, 0x80 }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 56320, s [1]); + Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (56320, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -951,8 +999,8 @@ namespace MonoTests.System.Text { byte[] data = { 0xED, 0xAF, 0xBF, 0xED, 0xBF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 56319, s [0]); - AssertEquals ("MS FX 1.1 behaviour", 57343, s [1]); + Assert.AreEqual (56319, s [0], "MS FX 1.1 behaviour"); + Assert.AreEqual (57343, s [1], "MS FX 1.1 behaviour"); } [Test] @@ -963,7 +1011,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xEF, 0xBF, 0xBE }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 65534, s [0]); + Assert.AreEqual (65534, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -974,7 +1022,7 @@ namespace MonoTests.System.Text { byte[] data = { 0xEF, 0xBF, 0xBF }; string s = utf8.GetString (data); // exception is "really" expected here - AssertEquals ("MS FX 1.1 behaviour", 65535, s [0]); + Assert.AreEqual (65535, s [0], "MS FX 1.1 behaviour"); } [Test] @@ -984,15 +1032,15 @@ namespace MonoTests.System.Text { byte [] data = new byte [] {0xEF, 0xBB, 0xBF}; Encoding enc = new UTF8Encoding (false, true); string s = enc.GetString (data); - AssertEquals ("\uFEFF", s); + Assert.AreEqual (s, "\uFEFF"); Encoding utf = Encoding.UTF8; char[] testChars = {'\uFEFF','A'}; byte[] bytes = utf.GetBytes(testChars); char[] chars = utf.GetChars(bytes); - AssertEquals ("#1", '\uFEFF', chars [0]); - AssertEquals ("#2", 'A', chars [1]); + Assert.AreEqual ('\uFEFF', chars [0], "#1"); + Assert.AreEqual ('A', chars [1], "#2"); } #if NET_2_0 @@ -1001,7 +1049,7 @@ namespace MonoTests.System.Text { { Encoding e = Encoding.GetEncoding (65001).Clone () as Encoding; - AssertEquals (false, e.IsReadOnly); + Assert.AreEqual (false, e.IsReadOnly); e.EncoderFallback = new EncoderExceptionFallback (); } #endif @@ -1030,9 +1078,9 @@ namespace MonoTests.System.Text { try { int ret = e.GetBytes (chars, 1, 0, bytes, 0, true); #if NET_2_0 - AssertEquals ("drop insufficient char in 2.0: char[]", 0, ret); + Assert.AreEqual (0, ret, "drop insufficient char in 2.0: char[]"); #else - Fail ("ArgumentException is expected: char[]"); + Assert.Fail ("ArgumentException is expected: char[]"); #endif } catch (ArgumentException) { } @@ -1041,29 +1089,79 @@ namespace MonoTests.System.Text { try { int ret = Encoding.UTF8.GetBytes (s, 0, 1, bytes, 0); #if NET_2_0 - AssertEquals ("drop insufficient char in 2.0: string", 0, ret); + Assert.AreEqual (0, ret, "drop insufficient char in 2.0: string"); #else - Fail ("ArgumentException is expected: string"); + Assert.Fail ("ArgumentException is expected: string"); #endif } catch (ArgumentException) { } } + + [Test] // bug #565129 + public void SufficientByteArray2 () + { + var u = Encoding.UTF8; + Assert.AreEqual (3, u.GetByteCount ("\uFFFD"), "#1-1"); + Assert.AreEqual (3, u.GetByteCount ("\uD800"), "#1-2"); + Assert.AreEqual (3, u.GetByteCount ("\uDC00"), "#1-3"); + Assert.AreEqual (4, u.GetByteCount ("\uD800\uDC00"), "#1-4"); + byte [] bytes = new byte [10]; + Assert.AreEqual (3, u.GetBytes ("\uDC00", 0, 1, bytes, 0), "#1-5"); // was bogus + + Assert.AreEqual (3, u.GetBytes ("\uFFFD").Length, "#2-1"); + Assert.AreEqual (3, u.GetBytes ("\uD800").Length, "#2-2"); + Assert.AreEqual (3, u.GetBytes ("\uDC00").Length, "#2-3"); + Assert.AreEqual (4, u.GetBytes ("\uD800\uDC00").Length, "#2-4"); + + for (char c = char.MinValue; c < char.MaxValue; c++) { + byte [] bIn; + bIn = u.GetBytes (c.ToString ()); + } + + try { + new UTF8Encoding (false, true).GetBytes (new char [] {'\uDF45', '\uD808'}, 0, 2); + Assert.Fail ("EncoderFallbackException is expected"); + } catch (EncoderFallbackException) { + } + } #if NET_2_0 [Test] // bug #77550 public void DecoderFallbackSimple () { UTF8Encoding e = new UTF8Encoding (false, false); - AssertType.AreEqual (0, e.GetDecoder ().GetCharCount ( + AssertType.AreEqual (1, e.GetDecoder ().GetCharCount ( new byte [] {(byte) 183}, 0, 1), "#1"); - AssertType.AreEqual (0, e.GetDecoder().GetChars ( + AssertType.AreEqual (1, e.GetDecoder().GetChars ( new byte [] {(byte) 183}, 0, 1, new char [100], 0), "#2"); - AssertType.AreEqual (0, e.GetString (new byte [] {(byte) 183}).Length, + AssertType.AreEqual (1, e.GetString (new byte [] {(byte) 183}).Length, "#3"); } + + [Test] + public void FallbackDefaultEncodingUTF8 () + { + DecoderReplacementFallbackBuffer b = + Encoding.UTF8.DecoderFallback.CreateFallbackBuffer () + as DecoderReplacementFallbackBuffer; + AssertType.IsTrue (b.Fallback (new byte [] {}, 0), "#1"); + AssertType.IsFalse (b.MovePrevious (), "#2"); + AssertType.AreEqual (1, b.Remaining, "#3"); + AssertType.AreEqual ('\uFFFD', b.GetNextChar (), "#4"); + } + + [Test] + public void Bug415628 () + { + using (var f = File.Open ("Test/resources/415628.bin", FileMode.Open)) { + BinaryReader br = new BinaryReader (f); + byte [] buf = br.ReadBytes (8000); + Encoding.UTF8.GetString(buf); + } + } #endif } }