Test Encoder.Convert and UTF8Encoder.Convert behaviors.
authorMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 4 Aug 2014 14:26:53 +0000 (15:26 +0100)
committerMarcos Henrich <marcos.henrich@xamarin.com>
Mon, 4 Aug 2014 14:26:53 +0000 (15:26 +0100)
Added class ExposedEncoder to test the virtual method Encoder.Convert behavior.
Test UTF8Encoder.Convert override.

mcs/class/corlib/Test/System.Text/DecoderTest.cs
mcs/class/corlib/Test/System.Text/EncoderTest.cs

index 6cf0306d823257437d538dd85defcf95b6710516..e48566f5403112ce68ed532343fe124c0fd7e44a 100644 (file)
@@ -44,7 +44,7 @@ namespace MonoTests.System.Text
                        char [] chars = new char [10000];
                        byte [] bytes = new byte [10000];
 
-                       Decoder conv = Encoding.UTF8.GetDecoder ();
+                       Decoder conv = new ExposedDecoder ();
                        int charsUsed, bytesUsed;
                        bool done;
 
@@ -56,6 +56,24 @@ namespace MonoTests.System.Text
                        Assert.AreEqual (625, bytesUsed, "#3");
                }
 
+               [Test]
+               public void ConvertLimitedDestinationUTF8 ()
+               {
+                       char [] chars = new char [10000];
+                       byte [] bytes = new byte [10000];
+
+                       Decoder conv = Encoding.UTF8.GetDecoder ();
+                       int charsUsed, bytesUsed;
+                       bool done;
+
+                       conv.Convert (bytes, 0, 10000, chars, 0, 1000, true,
+                                     out charsUsed, out bytesUsed, out done);
+
+                       Assert.IsFalse (done, "#1");
+                       Assert.AreEqual (1000, charsUsed, "#2");
+                       Assert.AreEqual (1000, bytesUsed, "#3");
+               }
+
 
                [Test]
                public void CustomEncodingGetDecoder ()
@@ -65,6 +83,18 @@ namespace MonoTests.System.Text
                        Assert.IsNotNull (decoder);
                }
 
+               class ExposedDecoder : Decoder {
+                       public override int GetCharCount (byte [] bytes, int index, int count)
+                       {
+                               return Encoding.UTF8.GetDecoder ().GetCharCount (bytes, index, count);
+                       }
+
+                       public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
+                       {
+                               return Encoding.UTF8.GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex);
+                       }
+               }
+
                class CustomEncoding : Encoding {
 
                        public override int GetByteCount (char [] chars, int index, int count)
index 27a8d4eeb4b21022dc7098bd1b1234305d11ba41..4273efc3d3a0a1bc052adfab225e82e692f6043c 100644 (file)
@@ -44,7 +44,7 @@ namespace MonoTests.System.Text
                        byte [] bytes = new byte [10000];
                        char [] chars = new char [10000];
 
-                       Encoder conv = Encoding.UTF8.GetEncoder ();
+                       Encoder conv = new ExposedEncoder ();
                        int bytesUsed, charsUsed;
                        bool done;
 
@@ -56,6 +56,25 @@ namespace MonoTests.System.Text
                        Assert.AreEqual (625, charsUsed, "#3");
                }
 
+               [Test]
+               public void ConvertLimitedDestinationUTF8 ()
+               {
+                       byte [] bytes = new byte [10000];
+                       char [] chars = new char [10000];
+
+                       Encoder conv = Encoding.UTF8.GetEncoder ();
+                       var type = conv.GetType ();
+                       int bytesUsed, charsUsed;
+                       bool done;
+
+                       conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
+                                     out bytesUsed, out charsUsed, out done);
+
+                       Assert.IsFalse (done, "#1");
+                       Assert.AreEqual (1000, bytesUsed, "#2");
+                       Assert.AreEqual (1000, charsUsed, "#3");
+               }
+
                [Test]
                public void CustomEncodingGetEncoder ()
                {
@@ -80,6 +99,18 @@ namespace MonoTests.System.Text
                        Assert.AreEqual (0, bytesUsed, "#3");
                }
 
+               class ExposedEncoder : Encoder {
+                       public override int GetByteCount (char [] chars, int index, int count, bool flush)
+                       {
+                               return Encoding.UTF8.GetEncoder ().GetByteCount (chars, index, count, flush);
+                       }
+
+                       public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex, bool flush)
+                       {
+                               return Encoding.UTF8.GetEncoder ().GetBytes (chars, charIndex, charCount, bytes, byteIndex, flush);
+                       }
+               }
+
                class CustomEncoding : Encoding {
 
                        public override int GetByteCount (char [] chars, int index, int count)