1 // ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
3 // Author: Mike Kestner <mkestner@speakeasy.net>
5 // <c> 2002 Mike Kestner
10 using NUnit.Framework;
11 using NUnit.Framework.Constraints;
14 using NUnit.Framework.SyntaxHelpers;
17 namespace MonoTests.System.Text
20 public class ASCIIEncodingTest
22 private char[] testchars;
23 private byte[] testbytes;
28 testchars = new char[4];
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';
41 public void IsBrowserDisplay ()
43 Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
47 public void IsBrowserSave ()
49 Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
53 public void IsMailNewsDisplay ()
55 Assert.IsTrue (Encoding.ASCII.IsMailNewsDisplay);
59 public void IsMailNewsSave ()
61 Assert.IsTrue (Encoding.ASCII.IsMailNewsSave);
64 [Test] // Test GetBytes(char[])
65 public void TestGetBytes1 ()
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]);
73 [Test] // Test GetBytes(char[], int, int)
74 public void TestGetBytes2 ()
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");
82 [Test] // Test non-ASCII char in char[]
83 public void TestGetBytes3 ()
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");
94 [Test] // Test GetBytes(char[], int, int, byte[], int)
95 public void TestGetBytes4 ()
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");
104 [Test] // Test GetBytes(string, int, int, byte[], int)
105 public void TestGetBytes5 ()
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");
113 [Test] // Test GetBytes(string)
114 public void TestGetBytes6 ()
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]);
122 [Test] // Test GetChars(byte[])
123 public void TestGetChars1 ()
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]);
131 [Test] // Test GetChars(byte[], int, int)
132 public void TestGetChars2 ()
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");
140 [Test] // Test non-ASCII char in byte[]
141 public void TestGetChars3 ()
143 Encoding ascii_encoding = Encoding.ASCII;
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");
152 [Test] // Test GetChars(byte[], int, int, char[], int)
153 public void TestGetChars4 ()
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");
162 [Test] // Test GetString(char[])
163 public void TestGetString1 ()
165 Encoding ascii_encoding = Encoding.ASCII;
166 string str = ascii_encoding.GetString(testbytes);
167 Assert.AreEqual ("Test", str);
170 [Test] // Test GetString(char[], int, int)
171 public void TestGetString2 ()
173 Encoding ascii_encoding = Encoding.ASCII;
174 string str = ascii_encoding.GetString(testbytes, 1, 2);
175 Assert.AreEqual ("es", str);
178 [Test] // Test invalid byte handling
179 public void TestGetString3 ()
181 Encoding encoding = Encoding.ASCII;
182 byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
183 string s = encoding.GetString (bytes, 0, 3);
185 Assert.AreEqual ("a??", s);
187 Assert.AreEqual ("aab", s);
191 [Test] // Test Decoder
192 public void TestDecoder ()
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");
201 [Test] // Test Decoder
202 public void TestEncoder ()
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");
212 public void TestZero ()
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");
220 [ExpectedException (typeof (DecoderFallbackException))]
221 public void DecoderFallback ()
223 Encoding e = Encoding.ASCII.Clone () as Encoding;
224 e.DecoderFallback = new DecoderExceptionFallback ();
225 e.GetChars (new byte [] {0x80});
229 // [ExpectedException (typeof (ArgumentException))]
230 public void DecoderFallback2 ()
232 var bytes = new byte[] {
233 0x30, 0xa0, 0x31, 0xa8
235 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
236 enc.DecoderFallback = new TestFallbackDecoder ();
238 var chars = new char [7];
239 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
240 Console.WriteLine (ret);
242 for (int i = 0; i < chars.Length; i++) {
243 Console.Write ("{0:x2} ", (int)chars [i]);
245 Console.WriteLine ();
249 public void DecoderFallback3 ()
251 var bytes = new byte[] {
252 0x30, 0xa0, 0x31, 0xa8
254 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
255 enc.DecoderFallback = new TestFallbackDecoder ();
257 var chars = new char[] { '9', '8', '7', '6', '5' };
258 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
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]");
268 class TestFallbackDecoder : DecoderFallback {
271 public override int MaxCharCount {
272 get { return count; }
275 public override DecoderFallbackBuffer CreateFallbackBuffer ()
277 return new Buffer ();
280 class Buffer : DecoderFallbackBuffer {
284 public override int Remaining {
286 return queue.Length - index;
290 public override char GetNextChar ()
292 return index < queue.Length ? queue [index++] : '\0';
295 public override bool Fallback (byte[] bytes, int unused)
297 queue = new char[bytes.Length * count];
299 for (int i = 0; i < bytes.Length; i++) {
300 for (int j = 0; j < count; j++)
301 queue [index++] = (char)(bytes [i]+j);
306 public override bool MovePrevious ()
308 throw new NotImplementedException ();
311 public override void Reset ()