Merge pull request #1516 from DavidKarlas/getThreadsHang
[mono.git] / mcs / class / corlib / Test / System.Text / EncoderTest.cs
1 //
2 // EncoderTest.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 EncoderTest
17         {
18                 [Test]
19                 [ExpectedException (typeof (ArgumentNullException))]
20                 public void ConvertNullChars ()
21                 {
22                         int bytesUsed, charsUsed;
23                         bool done;
24                         Encoding.UTF8.GetEncoder ().Convert (
25                                 null, 0, 100, new byte [100], 0, 100, false,
26                                 out bytesUsed, out charsUsed, out done);
27                 }
28
29                 [Test]
30                 [ExpectedException (typeof (ArgumentNullException))]
31                 public void ConvertNullBytes ()
32                 {
33                         int bytesUsed, charsUsed;
34                         bool done;
35                         Encoding.UTF8.GetEncoder ().Convert (
36                                 new char [100], 0, 100, null, 0, 100, false,
37                                 out bytesUsed, out charsUsed, out done);
38                 }
39
40                 [Test]
41                 public void ConvertLimitedDestination ()
42                 {
43                         byte [] bytes = new byte [10000];
44                         char [] chars = new char [10000];
45
46                         Encoder conv = new ExposedEncoder ();
47                         int bytesUsed, charsUsed;
48                         bool done;
49
50                         conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
51                                       out bytesUsed, out charsUsed, out done);
52
53                         Assert.IsFalse (done, "#1");
54                         Assert.AreEqual (625, bytesUsed, "#2");
55                         Assert.AreEqual (625, charsUsed, "#3");
56                 }
57
58                 [Test]
59                 public void ConvertLimitedDestinationUTF8 ()
60                 {
61                         byte [] bytes = new byte [10000];
62                         char [] chars = new char [10000];
63
64                         Encoder conv = Encoding.UTF8.GetEncoder ();
65                         var type = conv.GetType ();
66                         int bytesUsed, charsUsed;
67                         bool done;
68
69                         conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
70                                       out bytesUsed, out charsUsed, out done);
71
72                         Assert.IsFalse (done, "#1");
73                         Assert.AreEqual (1000, bytesUsed, "#2");
74                         Assert.AreEqual (1000, charsUsed, "#3");
75                 }
76
77                 [Test]
78                 public void CustomEncodingGetEncoder ()
79                 {
80                         var encoding = new CustomEncoding ();
81                         var encoder = encoding.GetEncoder ();
82                         Assert.IsNotNull (encoder);
83                 }
84
85                 [Test]
86                 public void ConvertZeroCharacters ()
87                 {
88                         int charsUsed, bytesUsed;
89                         bool completed;
90                         byte [] bytes = new byte [0];
91
92                         Encoding.UTF8.GetEncoder ().Convert (
93                                 new char[0], 0, 0, bytes, 0, bytes.Length, true,
94                                 out charsUsed, out bytesUsed, out completed);
95
96                         Assert.IsTrue (completed, "#1");
97                         Assert.AreEqual (0, charsUsed, "#2");
98                         Assert.AreEqual (0, bytesUsed, "#3");
99                 }
100
101                 class ExposedEncoder : Encoder {
102                         public override int GetByteCount (char [] chars, int index, int count, bool flush)
103                         {
104                                 return Encoding.UTF8.GetEncoder ().GetByteCount (chars, index, count, flush);
105                         }
106
107                         public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex, bool flush)
108                         {
109                                 return Encoding.UTF8.GetEncoder ().GetBytes (chars, charIndex, charCount, bytes, byteIndex, flush);
110                         }
111                 }
112
113                 class CustomEncoding : Encoding {
114
115                         public override int GetByteCount (char [] chars, int index, int count)
116                         {
117                                 throw new NotSupportedException ();
118                         }
119
120                         public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
121                         {
122                                 throw new NotSupportedException ();
123                         }
124
125                         public override int GetCharCount (byte [] bytes, int index, int count)
126                         {
127                                 throw new NotSupportedException ();
128                         }
129
130                         public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
131                         {
132                                 throw new NotSupportedException ();
133                         }
134
135                         public override int GetMaxByteCount (int charCount)
136                         {
137                                 throw new NotSupportedException ();
138                         }
139
140                         public override int GetMaxCharCount (int byteCount)
141                         {
142                                 throw new NotSupportedException ();
143                         }
144                 }
145         }
146 }