Merge pull request #569 from knocte/fix_cairo_profile_versions_problem
[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 GetChars(byte[])
123                 public void TestGetChars1 () 
124                 {
125                         Encoding ascii_encoding = Encoding.ASCII;
126                         char[] chars = ascii_encoding.GetChars(testbytes);
127                         for (int i = 0; i < testbytes.Length; i++)
128                                 Assert.AreEqual (testbytes[i], (byte) chars[i]);
129                 }
130
131                 [Test] // Test GetChars(byte[], int, int)
132                 public void TestGetChars2 () 
133                 {
134                         Encoding ascii_encoding = Encoding.ASCII;
135                         char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
136                         Assert.AreEqual (1, chars.Length, "#1");
137                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
138                 }
139
140                 [Test] // Test non-ASCII char in byte[]
141                 public void TestGetChars3 () 
142                 {
143                         Encoding ascii_encoding = Encoding.ASCII;
144                         testbytes[2] = 0x80;
145                         char[] chars = ascii_encoding.GetChars(testbytes);
146                         Assert.AreEqual ('T', chars [0], "#1");
147                         Assert.AreEqual ('e', chars [1], "#2");
148                         Assert.AreEqual ('?', chars [2], "#3");
149                         Assert.AreEqual ('t', chars [3], "#4");
150                 }
151
152                 [Test] // Test GetChars(byte[], int, int, char[], int)
153                 public void TestGetChars4 () 
154                 {
155                         Encoding ascii_encoding = Encoding.ASCII;
156                         char[] chars = new char[1];
157                         int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
158                         Assert.AreEqual (1, cnt, "#1");
159                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
160                 }
161
162                 [Test] // Test GetString(char[])
163                 public void TestGetString1 () 
164                 {
165                         Encoding ascii_encoding = Encoding.ASCII;
166                         string str = ascii_encoding.GetString(testbytes);
167                         Assert.AreEqual ("Test", str);
168                 }
169
170                 [Test] // Test GetString(char[], int, int)
171                 public void TestGetString2 () 
172                 {
173                         Encoding ascii_encoding = Encoding.ASCII;
174                         string str = ascii_encoding.GetString(testbytes, 1, 2);
175                         Assert.AreEqual ("es", str);
176                 }
177
178                 [Test] // Test invalid byte handling
179                 public void TestGetString3 () 
180                 {
181                         Encoding encoding = Encoding.ASCII;
182                         byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
183                         string s = encoding.GetString (bytes, 0, 3);
184 #if NET_2_0
185                         Assert.AreEqual ("a??", s);
186 #else
187                         Assert.AreEqual ("aab", s);
188 #endif
189                 }
190
191                 [Test] // Test Decoder
192                 public void TestDecoder ()
193                 {
194                         Encoding ascii_encoding = Encoding.ASCII;
195                         char[] chars = new char[1];
196                         int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
197                         Assert.AreEqual (1, cnt, "#1");
198                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
199                 }
200
201                 [Test] // Test Decoder
202                 public void TestEncoder ()
203                 {
204                         Encoding ascii_encoding = Encoding.ASCII;
205                         byte[] bytes = new Byte[1];
206                         int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
207                         Assert.AreEqual (1, cnt, "#1");
208                         Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
209                 }
210
211                 [Test]
212                 public void TestZero ()
213                 {
214                         Encoding encoding = Encoding.ASCII;
215                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
216                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
217                 }
218
219                 [Test]
220                 [ExpectedException (typeof (DecoderFallbackException))]
221                 public void DecoderFallback ()
222                 {
223                         Encoding e = Encoding.ASCII.Clone () as Encoding;
224                         e.DecoderFallback = new DecoderExceptionFallback ();
225                         e.GetChars (new byte [] {0x80});
226                 }
227
228                 [Test]
229         //      [ExpectedException (typeof (ArgumentException))]
230                 public void DecoderFallback2 ()
231                 {
232                         var bytes = new byte[] {
233                                 0x30, 0xa0, 0x31, 0xa8
234                         };
235                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
236                         enc.DecoderFallback = new TestFallbackDecoder ();
237                         
238                         var chars = new char [7];
239                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
240                         Console.WriteLine (ret);
241                         
242                         for (int i = 0; i < chars.Length; i++) {
243                                 Console.Write ("{0:x2} ", (int)chars [i]);
244                         }
245                         Console.WriteLine ();
246                 }
247                 
248                 [Test]
249                 public void DecoderFallback3 ()
250                 {
251                         var bytes = new byte[] {
252                                 0x30, 0xa0, 0x31, 0xa8
253                         };
254                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
255                         enc.DecoderFallback = new TestFallbackDecoder ();
256                         
257                         var chars = new char[] { '9', '8', '7', '6', '5' };
258                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
259                         
260                         Assert.That (ret, Is.EqualTo (4), "ret"); // FIXME: Wrong it should be 2
261                         Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
262                         Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
263                         Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
264                         Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
265                         Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
266                 }
267                 
268                 class TestFallbackDecoder : DecoderFallback {
269                         const int count = 2;
270                         
271                         public override int MaxCharCount {
272                                 get { return count; }
273                         }
274                         
275                         public override DecoderFallbackBuffer CreateFallbackBuffer ()
276                         {
277                                 return new Buffer ();
278                         }
279                         
280                         class Buffer : DecoderFallbackBuffer {
281                                 char[] queue;
282                                 int index;
283                                 
284                                 public override int Remaining {
285                                         get {
286                                                 return queue.Length - index;
287                                         }
288                                 }
289                                 
290                                 public override char GetNextChar ()
291                                 {
292                                         return index < queue.Length ? queue [index++] : '\0';
293                                 }
294                                 
295                                 public override bool Fallback (byte[] bytes, int unused)
296                                 {
297                                         queue = new char[bytes.Length * count];
298                                         index = 0;
299                                         for (int i = 0; i < bytes.Length; i++) {
300                                                 for (int j = 0; j < count; j++)
301                                                         queue [index++] = (char)(bytes [i]+j);
302                                         }
303                                         return true;
304                                 }
305                                 
306                                 public override bool MovePrevious ()
307                                 {
308                                         throw new NotImplementedException ();
309                                 }
310                                 
311                                 public override void Reset ()
312                                 {
313                                         base.Reset ();
314                                 }
315                         }
316                 }
317                 
318
319         }
320 }