Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / corlib / Test / System.Text / ASCIIEncodingTest.cs
1 // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
2 // 
3 // Author: Mike Kestner <mkestner@speakeasy.net>
4 //
5 // <c> 2002 Mike Kestner
6
7 using System;
8 using System.Text;
9
10 using NUnit.Framework;
11 using NUnit.Framework.Constraints;
12
13 namespace MonoTests.System.Text
14 {
15         [TestFixture]
16         public class ASCIIEncodingTest
17         {
18                 private char[] testchars;
19                 private byte[] testbytes;
20
21                 [SetUp]
22                 public void SetUp ()
23                 {
24                         testchars = new char[4];
25                         testchars[0] = 'T';
26                         testchars[1] = 'e';
27                         testchars[2] = 's';
28                         testchars[3] = 't';
29                         testbytes = new byte[4];
30                         testbytes[0] = (byte) 'T';
31                         testbytes[1] = (byte) 'e';
32                         testbytes[2] = (byte) 's';
33                         testbytes[3] = (byte) 't';
34                 }
35
36                 [Test]
37                 public void IsBrowserDisplay ()
38                 {
39                         Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
40                 }
41
42                 [Test]
43                 public void IsBrowserSave ()
44                 {
45                         Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
46                 }
47
48                 [Test]
49                 public void IsMailNewsDisplay ()
50                 {
51                         Assert.IsTrue (Encoding.ASCII.IsMailNewsDisplay);
52                 }
53
54                 [Test]
55                 public void IsMailNewsSave ()
56                 {
57                         Assert.IsTrue (Encoding.ASCII.IsMailNewsSave);
58                 }
59
60                 [Test] // Test GetBytes(char[])
61                 public void TestGetBytes1 () 
62                 {
63                         Encoding ascii_encoding = Encoding.ASCII;
64                         byte[] bytes = ascii_encoding.GetBytes(testchars);
65                         for (int i = 0; i < testchars.Length; i++)
66                                 Assert.AreEqual (testchars[i], (char) bytes[i]);
67                 }
68
69                 [Test] // Test GetBytes(char[], int, int)
70                 public void TestGetBytes2 () 
71                 {
72                         Encoding ascii_encoding = Encoding.ASCII;
73                         byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
74                         Assert.AreEqual (1, bytes.Length, "#1");
75                         Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
76                 }
77
78                 [Test] // Test non-ASCII char in char[]
79                 public void TestGetBytes3 () 
80                 {
81                         Encoding ascii_encoding = Encoding.ASCII;
82                         testchars[2] = (char) 0x80;
83                         byte[] bytes = ascii_encoding.GetBytes(testchars);
84                         Assert.AreEqual ('T', (char) bytes [0], "#1");
85                         Assert.AreEqual ('e', (char) bytes [1], "#2");
86                         Assert.AreEqual ('?', (char) bytes [2], "#3");
87                         Assert.AreEqual ('t', (char) bytes [3], "#4");
88                 }
89
90                 [Test] // Test GetBytes(char[], int, int, byte[], int)
91                 public void TestGetBytes4 () 
92                 {
93                         Encoding ascii_encoding = Encoding.ASCII;
94                         byte[] bytes = new Byte[1];
95                         int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
96                         Assert.AreEqual (1, cnt, "#1");
97                         Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
98                 }
99
100                 [Test] // Test GetBytes(string, int, int, byte[], int)
101                 public void TestGetBytes5 () 
102                 {
103                         Encoding ascii_encoding = Encoding.ASCII;
104                         byte[] bytes = new Byte[1];
105                         int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
106                         Assert.AreEqual ('e', (char) bytes [0], "#1");
107                 }
108
109                 [Test] // Test GetBytes(string)
110                 public void TestGetBytes6 () 
111                 {
112                         Encoding ascii_encoding = Encoding.ASCII;
113                         byte[] bytes = ascii_encoding.GetBytes("Test");
114                         for (int i = 0; i < testchars.Length; i++)
115                                 Assert.AreEqual (testchars [i], (char) bytes [i]);
116                 }
117
118                 [Test] // Test GetBytes(string)
119                 public void TestGetBytes7 ()
120                 {
121                         var latin1_encoding = Encoding.GetEncoding ("latin1");
122
123                         var expected = new byte [] { 0x3F, 0x20, 0x3F, 0x20, 0x3F };
124                         var actual = latin1_encoding.GetBytes("\u24c8 \u2075 \u221e"); // normal replacement
125                         Assert.AreEqual (expected, actual, "#1");
126
127                         expected = new byte [] { 0x3F, 0x3F };
128                         actual = latin1_encoding.GetBytes("\ud83d\ude0a"); // surrogate pair replacement
129                         Assert.AreEqual (expected, actual, "#2");
130
131                         expected = new byte [] { 0x3F, 0x3F, 0x20 };
132                         actual = latin1_encoding.GetBytes("\ud83d\ude0a "); // surrogate pair replacement
133                         Assert.AreEqual (expected, actual, "#3");
134
135                         expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
136                         actual = latin1_encoding.GetBytes("  \ud83d\ude0a  "); // surrogate pair replacement
137                         Assert.AreEqual (expected, actual, "#4");
138
139                         expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
140                         actual = latin1_encoding.GetBytes("  \ud834\udd1e  "); // surrogate pair replacement
141                         Assert.AreEqual (expected, actual, "#5");
142
143                         expected = new byte [] { 0x41, 0x42, 0x43, 0x00, 0x41, 0x42, 0x43 };
144                         actual = latin1_encoding.GetBytes("ABC\0ABC"); // embedded zero byte not replaced
145                         Assert.AreEqual (expected, actual, "#6");
146
147                         expected = new byte [] { 0x20, 0x20, 0x3F, 0x20, 0x20 };
148                         actual = latin1_encoding.GetBytes("  \ud834  "); // invalid surrogate pair replacement
149                         Assert.AreEqual (expected, actual, "#7");
150                 }
151
152                 [Test] // Test GetChars(byte[])
153                 public void TestGetChars1 () 
154                 {
155                         Encoding ascii_encoding = Encoding.ASCII;
156                         char[] chars = ascii_encoding.GetChars(testbytes);
157                         for (int i = 0; i < testbytes.Length; i++)
158                                 Assert.AreEqual (testbytes[i], (byte) chars[i]);
159                 }
160
161                 [Test] // Test GetChars(byte[], int, int)
162                 public void TestGetChars2 () 
163                 {
164                         Encoding ascii_encoding = Encoding.ASCII;
165                         char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
166                         Assert.AreEqual (1, chars.Length, "#1");
167                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
168                 }
169
170                 [Test] // Test non-ASCII char in byte[]
171                 public void TestGetChars3 () 
172                 {
173                         Encoding ascii_encoding = Encoding.ASCII;
174                         testbytes[2] = 0x80;
175                         char[] chars = ascii_encoding.GetChars(testbytes);
176                         Assert.AreEqual ('T', chars [0], "#1");
177                         Assert.AreEqual ('e', chars [1], "#2");
178                         Assert.AreEqual ('?', chars [2], "#3");
179                         Assert.AreEqual ('t', chars [3], "#4");
180                 }
181
182                 [Test] // Test GetChars(byte[], int, int, char[], int)
183                 public void TestGetChars4 () 
184                 {
185                         Encoding ascii_encoding = Encoding.ASCII;
186                         char[] chars = new char[1];
187                         int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
188                         Assert.AreEqual (1, cnt, "#1");
189                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
190                 }
191
192                 [Test] // Test GetString(char[])
193                 public void TestGetString1 () 
194                 {
195                         Encoding ascii_encoding = Encoding.ASCII;
196                         string str = ascii_encoding.GetString(testbytes);
197                         Assert.AreEqual ("Test", str);
198                 }
199
200                 [Test] // Test GetString(char[], int, int)
201                 public void TestGetString2 () 
202                 {
203                         Encoding ascii_encoding = Encoding.ASCII;
204                         string str = ascii_encoding.GetString(testbytes, 1, 2);
205                         Assert.AreEqual ("es", str);
206                 }
207
208                 [Test] // Test invalid byte handling
209                 public void TestGetString3 () 
210                 {
211                         Encoding encoding = Encoding.ASCII;
212                         byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
213                         string s = encoding.GetString (bytes, 0, 3);
214                         Assert.AreEqual ("a??", s);
215                 }
216
217                 [Test] // Test Decoder
218                 public void TestDecoder ()
219                 {
220                         Encoding ascii_encoding = Encoding.ASCII;
221                         char[] chars = new char[1];
222                         int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
223                         Assert.AreEqual (1, cnt, "#1");
224                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
225                 }
226
227                 [Test] // Test Decoder
228                 public void TestEncoder ()
229                 {
230                         Encoding ascii_encoding = Encoding.ASCII;
231                         byte[] bytes = new Byte[1];
232                         int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
233                         Assert.AreEqual (1, cnt, "#1");
234                         Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
235                 }
236
237                 [Test]
238                 public void TestZero ()
239                 {
240                         Encoding encoding = Encoding.ASCII;
241                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
242                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
243                 }
244
245                 [Test]
246                 [ExpectedException (typeof (EncoderFallbackException))]
247                 public void EncoderFallback ()
248                 {
249                         Encoding e = Encoding.ASCII.Clone () as Encoding;
250                         e.EncoderFallback = new EncoderExceptionFallback ();
251                         e.GetBytes ("\u24c8");
252                 }
253
254                 [Test]
255                 [ExpectedException (typeof (DecoderFallbackException))]
256                 public void DecoderFallback ()
257                 {
258                         Encoding e = Encoding.ASCII.Clone () as Encoding;
259                         e.DecoderFallback = new DecoderExceptionFallback ();
260                         e.GetChars (new byte [] {0x80});
261                 }
262
263                 [Test]
264         //      [ExpectedException (typeof (ArgumentException))]
265                 public void DecoderFallback2 ()
266                 {
267                         var bytes = new byte[] {
268                                 0x30, 0xa0, 0x31, 0xa8
269                         };
270                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
271                         enc.DecoderFallback = new TestFallbackDecoder ();
272                         
273                         var chars = new char [7];
274                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
275                         Console.WriteLine (ret);
276                         
277                         for (int i = 0; i < chars.Length; i++) {
278                                 Console.Write ("{0:x2} ", (int)chars [i]);
279                         }
280                         Console.WriteLine ();
281                 }
282                 
283                 [Test]
284                 public void DecoderFallback3 ()
285                 {
286                         var bytes = new byte[] {
287                                 0x30, 0xa0, 0x31, 0xa8
288                         };
289                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
290                         enc.DecoderFallback = new TestFallbackDecoder ();
291                         
292                         var chars = new char[] { '9', '8', '7', '6', '5' };
293                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
294                         
295                         Assert.That (ret, Is.EqualTo (2), "ret");
296                         Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
297                         Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
298                         Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
299                         Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
300                         Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
301                 }
302                 
303                 class TestFallbackDecoder : DecoderFallback {
304                         const int count = 2;
305                         
306                         public override int MaxCharCount {
307                                 get { return count; }
308                         }
309                         
310                         public override DecoderFallbackBuffer CreateFallbackBuffer ()
311                         {
312                                 return new Buffer ();
313                         }
314                         
315                         class Buffer : DecoderFallbackBuffer {
316                                 char[] queue;
317                                 int index;
318                                 
319                                 public override int Remaining {
320                                         get {
321                                                 return queue.Length - index;
322                                         }
323                                 }
324                                 
325                                 public override char GetNextChar ()
326                                 {
327                                         return index < queue.Length ? queue [index++] : '\0';
328                                 }
329                                 
330                                 public override bool Fallback (byte[] bytes, int unused)
331                                 {
332                                         queue = new char[bytes.Length * count];
333                                         index = 0;
334                                         for (int i = 0; i < bytes.Length; i++) {
335                                                 for (int j = 0; j < count; j++)
336                                                         queue [index++] = (char)(bytes [i]+j);
337                                         }
338                                         return true;
339                                 }
340                                 
341                                 public override bool MovePrevious ()
342                                 {
343                                         throw new NotImplementedException ();
344                                 }
345                                 
346                                 public override void Reset ()
347                                 {
348                                         base.Reset ();
349                                 }
350                         }
351                 }
352                 
353
354         }
355 }