2 // Latin1EncodingTest.cs
5 // Alexander Köplinger (alexander.koeplinger@xamarin.com)
7 // Copyright (C) 2016 Xamarin, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
33 using NUnit.Framework.Constraints;
35 namespace MonoTests.System.Text
38 public class Latin1EncodingTest
40 private char[] testchars;
41 private byte[] testbytes;
46 testchars = new char[4];
51 testbytes = new byte[4];
52 testbytes[0] = (byte) 'T';
53 testbytes[1] = (byte) 'e';
54 testbytes[2] = (byte) 's';
55 testbytes[3] = (byte) 't';
59 public void IsBrowserDisplay ()
61 Assert.IsTrue (Encoding.GetEncoding ("latin1").IsBrowserDisplay);
65 public void IsBrowserSave ()
67 Assert.IsTrue (Encoding.GetEncoding ("latin1").IsBrowserSave);
71 public void IsMailNewsDisplay ()
73 Assert.IsTrue (Encoding.GetEncoding ("latin1").IsMailNewsDisplay);
77 public void IsMailNewsSave ()
79 Assert.IsTrue (Encoding.GetEncoding ("latin1").IsMailNewsSave);
82 [Test] // Test GetBytes(char[])
83 public void TestGetBytes1 ()
85 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
86 byte[] bytes = latin1_encoding.GetBytes(testchars);
87 for (int i = 0; i < testchars.Length; i++)
88 Assert.AreEqual (testchars[i], (char) bytes[i]);
91 [Test] // Test GetBytes(char[], int, int)
92 public void TestGetBytes2 ()
94 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
95 byte[] bytes = latin1_encoding.GetBytes(testchars, 1, 1);
96 Assert.AreEqual (1, bytes.Length, "#1");
97 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
100 [Test] // Test non-Latin1 char in char[]
101 public void TestGetBytes3 ()
103 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
104 testchars[2] = (char) 0x100;
105 byte[] bytes = latin1_encoding.GetBytes(testchars);
106 Assert.AreEqual ('T', (char) bytes [0], "#1");
107 Assert.AreEqual ('e', (char) bytes [1], "#2");
108 Assert.AreEqual ('?', (char) bytes [2], "#3");
109 Assert.AreEqual ('t', (char) bytes [3], "#4");
112 [Test] // Test GetBytes(char[], int, int, byte[], int)
113 public void TestGetBytes4 ()
115 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
116 byte[] bytes = new Byte[1];
117 int cnt = latin1_encoding.GetBytes(testchars, 1, 1, bytes, 0);
118 Assert.AreEqual (1, cnt, "#1");
119 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
122 [Test] // Test GetBytes(string, int, int, byte[], int)
123 public void TestGetBytes5 ()
125 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
126 byte[] bytes = new Byte[1];
127 int cnt = latin1_encoding.GetBytes("Test", 1, 1, bytes, 0);
128 Assert.AreEqual ('e', (char) bytes [0], "#1");
131 [Test] // Test GetBytes(string)
132 public void TestGetBytes6 ()
134 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
135 byte[] bytes = latin1_encoding.GetBytes("Test");
136 for (int i = 0; i < testchars.Length; i++)
137 Assert.AreEqual (testchars [i], (char) bytes [i]);
140 [Test] // Test GetBytes(string)
141 public void TestGetBytes7 ()
143 var latin1_encoding = Encoding.GetEncoding ("latin1");
145 var expected = new byte [] { 0x3F, 0x20, 0x3F, 0x20, 0x3F };
146 var actual = latin1_encoding.GetBytes("\u24c8 \u2075 \u221e"); // normal replacement
147 Assert.AreEqual (expected, actual, "#1");
149 expected = new byte [] { 0x3F, 0x3F };
150 actual = latin1_encoding.GetBytes("\ud83d\ude0a"); // surrogate pair replacement
151 Assert.AreEqual (expected, actual, "#2");
153 expected = new byte [] { 0x3F, 0x3F, 0x20 };
154 actual = latin1_encoding.GetBytes("\ud83d\ude0a "); // surrogate pair replacement
155 Assert.AreEqual (expected, actual, "#3");
157 expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
158 actual = latin1_encoding.GetBytes(" \ud83d\ude0a "); // surrogate pair replacement
159 Assert.AreEqual (expected, actual, "#4");
161 expected = new byte [] { 0x20, 0x20, 0x3F, 0x3F, 0x20, 0x20 };
162 actual = latin1_encoding.GetBytes(" \ud834\udd1e "); // surrogate pair replacement
163 Assert.AreEqual (expected, actual, "#5");
165 expected = new byte [] { 0x41, 0x42, 0x43, 0x00, 0x41, 0x42, 0x43 };
166 actual = latin1_encoding.GetBytes("ABC\0ABC"); // embedded zero byte not replaced
167 Assert.AreEqual (expected, actual, "#6");
169 expected = new byte [] { 0x20, 0x20, 0x3F, 0x20, 0x20 };
170 actual = latin1_encoding.GetBytes(" \ud834 "); // invalid surrogate pair replacement
171 Assert.AreEqual (expected, actual, "#7");
174 [Test] // Test GetChars(byte[])
175 public void TestGetChars1 ()
177 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
178 char[] chars = latin1_encoding.GetChars(testbytes);
179 for (int i = 0; i < testbytes.Length; i++)
180 Assert.AreEqual (testbytes[i], (byte) chars[i]);
183 [Test] // Test GetChars(byte[], int, int)
184 public void TestGetChars2 ()
186 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
187 char[] chars = latin1_encoding.GetChars(testbytes, 1, 1);
188 Assert.AreEqual (1, chars.Length, "#1");
189 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
192 [Test] // Test GetChars(byte[], int, int, char[], int)
193 public void TestGetChars4 ()
195 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
196 char[] chars = new char[1];
197 int cnt = latin1_encoding.GetChars(testbytes, 1, 1, chars, 0);
198 Assert.AreEqual (1, cnt, "#1");
199 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
202 [Test] // Test GetString(char[])
203 public void TestGetString1 ()
205 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
206 string str = latin1_encoding.GetString(testbytes);
207 Assert.AreEqual ("Test", str);
210 [Test] // Test GetString(char[], int, int)
211 public void TestGetString2 ()
213 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
214 string str = latin1_encoding.GetString(testbytes, 1, 2);
215 Assert.AreEqual ("es", str);
218 [Test] // Test Decoder
219 public void TestDecoder ()
221 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
222 char[] chars = new char[1];
223 int cnt = latin1_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
224 Assert.AreEqual (1, cnt, "#1");
225 Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
228 [Test] // Test Decoder
229 public void TestEncoder ()
231 Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
232 byte[] bytes = new Byte[1];
233 int cnt = latin1_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
234 Assert.AreEqual (1, cnt, "#1");
235 Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
239 public void TestZero ()
241 Encoding encoding = Encoding.GetEncoding ("latin1");
242 Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
243 Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
247 [ExpectedException (typeof (EncoderFallbackException))]
248 public void EncoderFallback ()
250 Encoding e = Encoding.GetEncoding ("latin1").Clone () as Encoding;
251 e.EncoderFallback = new EncoderExceptionFallback ();
252 e.GetBytes ("\u24c8");
256 // [ExpectedException (typeof (ArgumentException))]
257 public void DecoderFallback2 ()
259 var bytes = new byte[] {
260 0x30, 0xa0, 0x31, 0xa8
262 var enc = (Encoding)Encoding.GetEncoding ("latin1").Clone ();
263 enc.DecoderFallback = new TestFallbackDecoder ();
265 var chars = new char [7];
266 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
267 Console.WriteLine (ret);
269 for (int i = 0; i < chars.Length; i++) {
270 Console.Write ("{0:x2} ", (int)chars [i]);
272 Console.WriteLine ();
276 public void DecoderFallback3 ()
278 var bytes = new byte[] {
279 0x30, 0xa0, 0x31, 0xa8
281 var enc = (Encoding)Encoding.GetEncoding ("latin1").Clone ();
282 enc.DecoderFallback = new TestFallbackDecoder ();
284 var chars = new char[] { '9', '8', '7', '6', '5' };
285 var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
287 Assert.That (ret, Is.EqualTo (4), "ret");
288 Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
289 Assert.That (chars [1], Is.EqualTo ((char)0xA0), "chars[1]");
290 Assert.That (chars [2], Is.EqualTo ('1'), "chars[2]");
291 Assert.That (chars [3], Is.EqualTo ((char)0xA8), "chars[3]");
292 Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
295 class TestFallbackDecoder : DecoderFallback {
298 public override int MaxCharCount {
299 get { return count; }
302 public override DecoderFallbackBuffer CreateFallbackBuffer ()
304 return new Buffer ();
307 class Buffer : DecoderFallbackBuffer {
311 public override int Remaining {
313 return queue.Length - index;
317 public override char GetNextChar ()
319 return index < queue.Length ? queue [index++] : '\0';
322 public override bool Fallback (byte[] bytes, int unused)
324 queue = new char[bytes.Length * count];
326 for (int i = 0; i < bytes.Length; i++) {
327 for (int j = 0; j < count; j++)
328 queue [index++] = (char)(bytes [i]+j);
333 public override bool MovePrevious ()
335 throw new NotImplementedException ();
338 public override void Reset ()