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 GetBytes(string)
123 public void TestGetBytes7 ()
125 var latin1_encoding = Encoding.GetEncoding ("latin1");
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");
131 expected = new byte [] { 0x3F, 0x3F };
132 actual = latin1_encoding.GetBytes("\ud83d\ude0a"); // surrogate pair replacement
133 Assert.AreEqual (expected, actual, "#2");
135 expected = new byte [] { 0x3F, 0x3F, 0x20 };
136 actual = latin1_encoding.GetBytes("\ud83d\ude0a "); // surrogate pair replacement
137 Assert.AreEqual (expected, actual, "#3");
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");
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");
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");
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");
156 [Test] // Test GetChars(byte[])
157 public void TestGetChars1 ()
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]);
165 [Test] // Test GetChars(byte[], int, int)
166 public void TestGetChars2 ()
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");
174 [Test] // Test non-ASCII char in byte[]
175 public void TestGetChars3 ()
177 Encoding ascii_encoding = Encoding.ASCII;
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");
186 [Test] // Test GetChars(byte[], int, int, char[], int)
187 public void TestGetChars4 ()
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");
196 [Test] // Test GetString(char[])
197 public void TestGetString1 ()
199 Encoding ascii_encoding = Encoding.ASCII;
200 string str = ascii_encoding.GetString(testbytes);
201 Assert.AreEqual ("Test", str);
204 [Test] // Test GetString(char[], int, int)
205 public void TestGetString2 ()
207 Encoding ascii_encoding = Encoding.ASCII;
208 string str = ascii_encoding.GetString(testbytes, 1, 2);
209 Assert.AreEqual ("es", str);
212 [Test] // Test invalid byte handling
213 public void TestGetString3 ()
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);
221 [Test] // Test Decoder
222 public void TestDecoder ()
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");
231 [Test] // Test Decoder
232 public void TestEncoder ()
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");
242 public void TestZero ()
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");
250 [ExpectedException (typeof (EncoderFallbackException))]
251 public void EncoderFallback ()
253 Encoding e = Encoding.ASCII.Clone () as Encoding;
254 e.EncoderFallback = new EncoderExceptionFallback ();
255 e.GetBytes ("\u24c8");
259 [ExpectedException (typeof (DecoderFallbackException))]
260 public void DecoderFallback ()
262 Encoding e = Encoding.ASCII.Clone () as Encoding;
263 e.DecoderFallback = new DecoderExceptionFallback ();
264 e.GetChars (new byte [] {0x80});
268 // [ExpectedException (typeof (ArgumentException))]
269 public void DecoderFallback2 ()
271 var bytes = new byte[] {
272 0x30, 0xa0, 0x31, 0xa8
274 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
275 enc.DecoderFallback = new TestFallbackDecoder ();
277 var chars = new char [7];
278 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
279 Console.WriteLine (ret);
281 for (int i = 0; i < chars.Length; i++) {
282 Console.Write ("{0:x2} ", (int)chars [i]);
284 Console.WriteLine ();
288 public void DecoderFallback3 ()
290 var bytes = new byte[] {
291 0x30, 0xa0, 0x31, 0xa8
293 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
294 enc.DecoderFallback = new TestFallbackDecoder ();
296 var chars = new char[] { '9', '8', '7', '6', '5' };
297 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
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]");
307 class TestFallbackDecoder : DecoderFallback {
310 public override int MaxCharCount {
311 get { return count; }
314 public override DecoderFallbackBuffer CreateFallbackBuffer ()
316 return new Buffer ();
319 class Buffer : DecoderFallbackBuffer {
323 public override int Remaining {
325 return queue.Length - index;
329 public override char GetNextChar ()
331 return index < queue.Length ? queue [index++] : '\0';
334 public override bool Fallback (byte[] bytes, int unused)
336 queue = new char[bytes.Length * count];
338 for (int i = 0; i < bytes.Length; i++) {
339 for (int j = 0; j < count; j++)
340 queue [index++] = (char)(bytes [i]+j);
345 public override bool MovePrevious ()
347 throw new NotImplementedException ();
350 public override void Reset ()