1 // 2 // DecoderTest.cs 3 // 4 // Author: 5 // Atsushi Enomoto <atsushi@ximian.com> 6 // 7 // (C) 2006 Novell, Inc. 8 // 9 using NUnit.Framework; 10 using System; 11 using System.Text; 12 13 namespace MonoTests.System.Text 14 { 15 [TestFixture] 16 public class DecoderTest 17 { 18 [Test] 19 [ExpectedException (typeof (ArgumentNullException))] ConvertNullChars()20 public void ConvertNullChars () 21 { 22 int charsUsed, bytesUsed; 23 bool done; 24 Encoding.UTF8.GetDecoder ().Convert ( 25 null, 0, 100, new char [100], 0, 100, false, 26 out charsUsed, out bytesUsed, out done); 27 } 28 29 [Test] 30 [ExpectedException (typeof (ArgumentNullException))] ConvertNullBytes()31 public void ConvertNullBytes () 32 { 33 int charsUsed, bytesUsed; 34 bool done; 35 Encoding.UTF8.GetDecoder ().Convert ( 36 new byte [100], 0, 100, null, 0, 100, false, 37 out charsUsed, out bytesUsed, out done); 38 } 39 40 [Test] ConvertLimitedDestination()41 public void ConvertLimitedDestination () 42 { 43 char [] chars = new char [10000]; 44 byte [] bytes = new byte [10000]; 45 46 Decoder conv = new ExposedDecoder (); 47 int charsUsed, bytesUsed; 48 bool done; 49 50 conv.Convert (bytes, 0, 10000, chars, 0, 1000, true, 51 out charsUsed, out bytesUsed, out done); 52 53 Assert.IsFalse (done, "#1"); 54 Assert.AreEqual (625, charsUsed, "#2"); 55 Assert.AreEqual (625, bytesUsed, "#3"); 56 } 57 58 [Test] ConvertLimitedDestinationUTF8()59 public void ConvertLimitedDestinationUTF8 () 60 { 61 char [] chars = new char [10000]; 62 byte [] bytes = new byte [10000]; 63 64 Decoder conv = Encoding.UTF8.GetDecoder (); 65 int charsUsed, bytesUsed; 66 bool done; 67 68 conv.Convert (bytes, 0, 10000, chars, 0, 1000, true, 69 out charsUsed, out bytesUsed, out done); 70 71 Assert.IsFalse (done, "#1"); 72 Assert.AreEqual (1000, charsUsed, "#2"); 73 Assert.AreEqual (1000, bytesUsed, "#3"); 74 } 75 76 77 [Test] CustomEncodingGetDecoder()78 public void CustomEncodingGetDecoder () 79 { 80 var encoding = new CustomEncoding (); 81 var decoder = encoding.GetDecoder (); 82 Assert.IsNotNull (decoder); 83 } 84 85 class ExposedDecoder : Decoder { GetCharCount(byte [] bytes, int index, int count)86 public override int GetCharCount (byte [] bytes, int index, int count) 87 { 88 return Encoding.UTF8.GetDecoder ().GetCharCount (bytes, index, count); 89 } 90 GetChars(byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)91 public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex) 92 { 93 return Encoding.UTF8.GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex); 94 } 95 } 96 97 class CustomEncoding : Encoding { 98 GetByteCount(char [] chars, int index, int count)99 public override int GetByteCount (char [] chars, int index, int count) 100 { 101 throw new NotSupportedException (); 102 } 103 GetBytes(char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)104 public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex) 105 { 106 throw new NotSupportedException (); 107 } 108 GetCharCount(byte [] bytes, int index, int count)109 public override int GetCharCount (byte [] bytes, int index, int count) 110 { 111 throw new NotSupportedException (); 112 } 113 GetChars(byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)114 public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex) 115 { 116 throw new NotSupportedException (); 117 } 118 GetMaxByteCount(int charCount)119 public override int GetMaxByteCount (int charCount) 120 { 121 throw new NotSupportedException (); 122 } 123 GetMaxCharCount(int byteCount)124 public override int GetMaxCharCount (int byteCount) 125 { 126 throw new NotSupportedException (); 127 } 128 } 129 130 [Test] Bug10789()131 public void Bug10789 () 132 { 133 byte[] bytes = new byte[100]; 134 char[] chars = new char[100]; 135 136 Decoder conv = Encoding.UTF8.GetDecoder (); 137 int charsUsed, bytesUsed; 138 bool completed; 139 140 conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed); 141 142 Assert.IsTrue (completed, "#1"); 143 Assert.AreEqual (0, charsUsed, "#2"); 144 Assert.AreEqual (0, bytesUsed, "#3"); 145 } 146 } 147 } 148