5 // Atsushi Enomoto <atsushi@ximian.com>
7 // (C) 2006 Novell, Inc.
13 namespace MonoTests.System.Text
16 public class EncoderTest
19 [ExpectedException (typeof (ArgumentNullException))]
20 public void ConvertNullChars ()
22 int bytesUsed, charsUsed;
24 Encoding.UTF8.GetEncoder ().Convert (
25 null, 0, 100, new byte [100], 0, 100, false,
26 out bytesUsed, out charsUsed, out done);
30 [ExpectedException (typeof (ArgumentNullException))]
31 public void ConvertNullBytes ()
33 int bytesUsed, charsUsed;
35 Encoding.UTF8.GetEncoder ().Convert (
36 new char [100], 0, 100, null, 0, 100, false,
37 out bytesUsed, out charsUsed, out done);
41 public void ConvertLimitedDestination ()
43 byte [] bytes = new byte [10000];
44 char [] chars = new char [10000];
46 Encoder conv = new ExposedEncoder ();
47 int bytesUsed, charsUsed;
50 conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
51 out bytesUsed, out charsUsed, out done);
53 Assert.IsFalse (done, "#1");
54 Assert.AreEqual (625, bytesUsed, "#2");
55 Assert.AreEqual (625, charsUsed, "#3");
59 public void ConvertLimitedDestinationUTF8 ()
61 byte [] bytes = new byte [10000];
62 char [] chars = new char [10000];
64 Encoder conv = Encoding.UTF8.GetEncoder ();
65 var type = conv.GetType ();
66 int bytesUsed, charsUsed;
69 conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
70 out bytesUsed, out charsUsed, out done);
72 Assert.IsFalse (done, "#1");
73 Assert.AreEqual (1000, bytesUsed, "#2");
74 Assert.AreEqual (1000, charsUsed, "#3");
78 public void CustomEncodingGetEncoder ()
80 var encoding = new CustomEncoding ();
81 var encoder = encoding.GetEncoder ();
82 Assert.IsNotNull (encoder);
86 public void ConvertZeroCharacters ()
88 int charsUsed, bytesUsed;
90 byte [] bytes = new byte [0];
92 Encoding.UTF8.GetEncoder ().Convert (
93 new char[0], 0, 0, bytes, 0, bytes.Length, true,
94 out charsUsed, out bytesUsed, out completed);
96 Assert.IsTrue (completed, "#1");
97 Assert.AreEqual (0, charsUsed, "#2");
98 Assert.AreEqual (0, bytesUsed, "#3");
101 class ExposedEncoder : Encoder {
102 public override int GetByteCount (char [] chars, int index, int count, bool flush)
104 return Encoding.UTF8.GetEncoder ().GetByteCount (chars, index, count, flush);
107 public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex, bool flush)
109 return Encoding.UTF8.GetEncoder ().GetBytes (chars, charIndex, charCount, bytes, byteIndex, flush);
113 class CustomEncoding : Encoding {
115 public override int GetByteCount (char [] chars, int index, int count)
117 throw new NotSupportedException ();
120 public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
122 throw new NotSupportedException ();
125 public override int GetCharCount (byte [] bytes, int index, int count)
127 throw new NotSupportedException ();
130 public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
132 throw new NotSupportedException ();
135 public override int GetMaxByteCount (int charCount)
137 throw new NotSupportedException ();
140 public override int GetMaxCharCount (int byteCount)
142 throw new NotSupportedException ();