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