Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[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                         Assert.AreEqual ("a??", s);
185                 }
186
187                 [Test] // Test Decoder
188                 public void TestDecoder ()
189                 {
190                         Encoding ascii_encoding = Encoding.ASCII;
191                         char[] chars = new char[1];
192                         int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
193                         Assert.AreEqual (1, cnt, "#1");
194                         Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
195                 }
196
197                 [Test] // Test Decoder
198                 public void TestEncoder ()
199                 {
200                         Encoding ascii_encoding = Encoding.ASCII;
201                         byte[] bytes = new Byte[1];
202                         int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
203                         Assert.AreEqual (1, cnt, "#1");
204                         Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
205                 }
206
207                 [Test]
208                 public void TestZero ()
209                 {
210                         Encoding encoding = Encoding.ASCII;
211                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
212                         Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (DecoderFallbackException))]
217                 public void DecoderFallback ()
218                 {
219                         Encoding e = Encoding.ASCII.Clone () as Encoding;
220                         e.DecoderFallback = new DecoderExceptionFallback ();
221                         e.GetChars (new byte [] {0x80});
222                 }
223
224                 [Test]
225         //      [ExpectedException (typeof (ArgumentException))]
226                 public void DecoderFallback2 ()
227                 {
228                         var bytes = new byte[] {
229                                 0x30, 0xa0, 0x31, 0xa8
230                         };
231                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
232                         enc.DecoderFallback = new TestFallbackDecoder ();
233                         
234                         var chars = new char [7];
235                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
236                         Console.WriteLine (ret);
237                         
238                         for (int i = 0; i < chars.Length; i++) {
239                                 Console.Write ("{0:x2} ", (int)chars [i]);
240                         }
241                         Console.WriteLine ();
242                 }
243                 
244                 [Test]
245                 public void DecoderFallback3 ()
246                 {
247                         var bytes = new byte[] {
248                                 0x30, 0xa0, 0x31, 0xa8
249                         };
250                         var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
251                         enc.DecoderFallback = new TestFallbackDecoder ();
252                         
253                         var chars = new char[] { '9', '8', '7', '6', '5' };
254                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
255                         
256                         Assert.That (ret, Is.EqualTo (2), "ret");
257                         Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
258                         Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
259                         Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
260                         Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
261                         Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
262                 }
263                 
264                 class TestFallbackDecoder : DecoderFallback {
265                         const int count = 2;
266                         
267                         public override int MaxCharCount {
268                                 get { return count; }
269                         }
270                         
271                         public override DecoderFallbackBuffer CreateFallbackBuffer ()
272                         {
273                                 return new Buffer ();
274                         }
275                         
276                         class Buffer : DecoderFallbackBuffer {
277                                 char[] queue;
278                                 int index;
279                                 
280                                 public override int Remaining {
281                                         get {
282                                                 return queue.Length - index;
283                                         }
284                                 }
285                                 
286                                 public override char GetNextChar ()
287                                 {
288                                         return index < queue.Length ? queue [index++] : '\0';
289                                 }
290                                 
291                                 public override bool Fallback (byte[] bytes, int unused)
292                                 {
293                                         queue = new char[bytes.Length * count];
294                                         index = 0;
295                                         for (int i = 0; i < bytes.Length; i++) {
296                                                 for (int j = 0; j < count; j++)
297                                                         queue [index++] = (char)(bytes [i]+j);
298                                         }
299                                         return true;
300                                 }
301                                 
302                                 public override bool MovePrevious ()
303                                 {
304                                         throw new NotImplementedException ();
305                                 }
306                                 
307                                 public override void Reset ()
308                                 {
309                                         base.Reset ();
310                                 }
311                         }
312                 }
313                 
314
315         }
316 }