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;
13 namespace MonoTests.System.Text
16 public class ASCIIEncodingTest
18 private char[] testchars;
19 private byte[] testbytes;
24 testchars = new char[4];
29 testbytes = new byte[4];
30 testbytes[0] = (byte) 'T';
31 testbytes[1] = (byte) 'e';
32 testbytes[2] = (byte) 's';
33 testbytes[3] = (byte) 't';
37 public void IsBrowserDisplay ()
39 Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
43 public void IsBrowserSave ()
45 Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
49 public void IsMailNewsDisplay ()
51 Assert.IsTrue (Encoding.ASCII.IsMailNewsDisplay);
55 public void IsMailNewsSave ()
57 Assert.IsTrue (Encoding.ASCII.IsMailNewsSave);
60 [Test] // Test GetBytes(char[])
61 public void TestGetBytes1 ()
63 Encoding ascii_encoding = Encoding.ASCII;
64 byte[] bytes = ascii_encoding.GetBytes(testchars);
65 for (int i = 0; i < testchars.Length; i++)
66 Assert.AreEqual (testchars[i], (char) bytes[i]);
69 [Test] // Test GetBytes(char[], int, int)
70 public void TestGetBytes2 ()
72 Encoding ascii_encoding = Encoding.ASCII;
73 byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
74 Assert.AreEqual (1, bytes.Length, "#1");
75 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
78 [Test] // Test non-ASCII char in char[]
79 public void TestGetBytes3 ()
81 Encoding ascii_encoding = Encoding.ASCII;
82 testchars[2] = (char) 0x80;
83 byte[] bytes = ascii_encoding.GetBytes(testchars);
84 Assert.AreEqual ('T', (char) bytes [0], "#1");
85 Assert.AreEqual ('e', (char) bytes [1], "#2");
86 Assert.AreEqual ('?', (char) bytes [2], "#3");
87 Assert.AreEqual ('t', (char) bytes [3], "#4");
90 [Test] // Test GetBytes(char[], int, int, byte[], int)
91 public void TestGetBytes4 ()
93 Encoding ascii_encoding = Encoding.ASCII;
94 byte[] bytes = new Byte[1];
95 int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
96 Assert.AreEqual (1, cnt, "#1");
97 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
100 [Test] // Test GetBytes(string, int, int, byte[], int)
101 public void TestGetBytes5 ()
103 Encoding ascii_encoding = Encoding.ASCII;
104 byte[] bytes = new Byte[1];
105 int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
106 Assert.AreEqual ('e', (char) bytes [0], "#1");
109 [Test] // Test GetBytes(string)
110 public void TestGetBytes6 ()
112 Encoding ascii_encoding = Encoding.ASCII;
113 byte[] bytes = ascii_encoding.GetBytes("Test");
114 for (int i = 0; i < testchars.Length; i++)
115 Assert.AreEqual (testchars [i], (char) bytes [i]);
118 [Test] // Test GetBytes(string)
119 public void TestGetBytes7 ()
121 var latin1_encoding = Encoding.GetEncoding ("latin1");
123 var expected = new byte [] { 0x3F, 0x20, 0x3F, 0x20, 0x3F };
124 var actual = latin1_encoding.GetBytes("\u24c8 \u2075 \u221e"); // normal replacement
125 Assert.AreEqual (expected, actual, "#1");
127 expected = new byte [] { 0x3F, 0x3F };
128 actual = latin1_encoding.GetBytes("\ud83d\ude0a"); // surrogate pair replacement
129 Assert.AreEqual (expected, actual, "#2");
131 expected = new byte [] { 0x3F, 0x3F, 0x20 };
132 actual = latin1_encoding.GetBytes("\ud83d\ude0a "); // surrogate pair replacement
133 Assert.AreEqual (expected, actual, "#3");
135 expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
136 actual = latin1_encoding.GetBytes(" \ud83d\ude0a "); // surrogate pair replacement
137 Assert.AreEqual (expected, actual, "#4");
139 expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
140 actual = latin1_encoding.GetBytes(" \ud834\udd1e "); // surrogate pair replacement
141 Assert.AreEqual (expected, actual, "#5");
143 expected = new byte [] { 0x41, 0x42, 0x43, 0x00, 0x41, 0x42, 0x43 };
144 actual = latin1_encoding.GetBytes("ABC\0ABC"); // embedded zero byte not replaced
145 Assert.AreEqual (expected, actual, "#6");
147 expected = new byte [] { 0x20, 0x20, 0x3F, 0x20, 0x20 };
148 actual = latin1_encoding.GetBytes(" \ud834 "); // invalid surrogate pair replacement
149 Assert.AreEqual (expected, actual, "#7");
152 [Test] // Test GetChars(byte[])
153 public void TestGetChars1 ()
155 Encoding ascii_encoding = Encoding.ASCII;
156 char[] chars = ascii_encoding.GetChars(testbytes);
157 for (int i = 0; i < testbytes.Length; i++)
158 Assert.AreEqual (testbytes[i], (byte) chars[i]);
161 [Test] // Test GetChars(byte[], int, int)
162 public void TestGetChars2 ()
164 Encoding ascii_encoding = Encoding.ASCII;
165 char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
166 Assert.AreEqual (1, chars.Length, "#1");
167 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
170 [Test] // Test non-ASCII char in byte[]
171 public void TestGetChars3 ()
173 Encoding ascii_encoding = Encoding.ASCII;
175 char[] chars = ascii_encoding.GetChars(testbytes);
176 Assert.AreEqual ('T', chars [0], "#1");
177 Assert.AreEqual ('e', chars [1], "#2");
178 Assert.AreEqual ('?', chars [2], "#3");
179 Assert.AreEqual ('t', chars [3], "#4");
182 [Test] // Test GetChars(byte[], int, int, char[], int)
183 public void TestGetChars4 ()
185 Encoding ascii_encoding = Encoding.ASCII;
186 char[] chars = new char[1];
187 int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
188 Assert.AreEqual (1, cnt, "#1");
189 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
192 [Test] // Test GetString(char[])
193 public void TestGetString1 ()
195 Encoding ascii_encoding = Encoding.ASCII;
196 string str = ascii_encoding.GetString(testbytes);
197 Assert.AreEqual ("Test", str);
200 [Test] // Test GetString(char[], int, int)
201 public void TestGetString2 ()
203 Encoding ascii_encoding = Encoding.ASCII;
204 string str = ascii_encoding.GetString(testbytes, 1, 2);
205 Assert.AreEqual ("es", str);
208 [Test] // Test invalid byte handling
209 public void TestGetString3 ()
211 Encoding encoding = Encoding.ASCII;
212 byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
213 string s = encoding.GetString (bytes, 0, 3);
214 Assert.AreEqual ("a??", s);
217 [Test] // Test Decoder
218 public void TestDecoder ()
220 Encoding ascii_encoding = Encoding.ASCII;
221 char[] chars = new char[1];
222 int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
223 Assert.AreEqual (1, cnt, "#1");
224 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
227 [Test] // Test Decoder
228 public void TestEncoder ()
230 Encoding ascii_encoding = Encoding.ASCII;
231 byte[] bytes = new Byte[1];
232 int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
233 Assert.AreEqual (1, cnt, "#1");
234 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
238 public void TestZero ()
240 Encoding encoding = Encoding.ASCII;
241 Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
242 Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
246 [ExpectedException (typeof (EncoderFallbackException))]
247 public void EncoderFallback ()
249 Encoding e = Encoding.ASCII.Clone () as Encoding;
250 e.EncoderFallback = new EncoderExceptionFallback ();
251 e.GetBytes ("\u24c8");
255 [ExpectedException (typeof (DecoderFallbackException))]
256 public void DecoderFallback ()
258 Encoding e = Encoding.ASCII.Clone () as Encoding;
259 e.DecoderFallback = new DecoderExceptionFallback ();
260 e.GetChars (new byte [] {0x80});
264 // [ExpectedException (typeof (ArgumentException))]
265 public void DecoderFallback2 ()
267 var bytes = new byte[] {
268 0x30, 0xa0, 0x31, 0xa8
270 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
271 enc.DecoderFallback = new TestFallbackDecoder ();
273 var chars = new char [7];
274 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
275 Console.WriteLine (ret);
277 for (int i = 0; i < chars.Length; i++) {
278 Console.Write ("{0:x2} ", (int)chars [i]);
280 Console.WriteLine ();
284 public void DecoderFallback3 ()
286 var bytes = new byte[] {
287 0x30, 0xa0, 0x31, 0xa8
289 var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
290 enc.DecoderFallback = new TestFallbackDecoder ();
292 var chars = new char[] { '9', '8', '7', '6', '5' };
293 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
295 Assert.That (ret, Is.EqualTo (2), "ret");
296 Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
297 Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
298 Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
299 Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
300 Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
303 class TestFallbackDecoder : DecoderFallback {
306 public override int MaxCharCount {
307 get { return count; }
310 public override DecoderFallbackBuffer CreateFallbackBuffer ()
312 return new Buffer ();
315 class Buffer : DecoderFallbackBuffer {
319 public override int Remaining {
321 return queue.Length - index;
325 public override char GetNextChar ()
327 return index < queue.Length ? queue [index++] : '\0';
330 public override bool Fallback (byte[] bytes, int unused)
332 queue = new char[bytes.Length * count];
334 for (int i = 0; i < bytes.Length; i++) {
335 for (int j = 0; j < count; j++)
336 queue [index++] = (char)(bytes [i]+j);
341 public override bool MovePrevious ()
343 throw new NotImplementedException ();
346 public override void Reset ()