Merge pull request #3796 from ntherning/windows-backend-for-MemoryMappedFile
[mono.git] / mcs / class / corlib / Test / System.Text / Latin1EncodingTest.cs
1 //
2 // Latin1EncodingTest.cs
3 //
4 // Author:
5 //      Alexander Köplinger (alexander.koeplinger@xamarin.com)
6 //
7 // Copyright (C) 2016 Xamarin, Inc.
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Text;
31
32 using NUnit.Framework;
33 using NUnit.Framework.Constraints;
34
35 namespace MonoTests.System.Text
36 {
37         [TestFixture]
38         public class Latin1EncodingTest
39         {
40                 private char[] testchars;
41                 private byte[] testbytes;
42
43                 [SetUp]
44                 public void SetUp ()
45                 {
46                         testchars = new char[4];
47                         testchars[0] = 'T';
48                         testchars[1] = 'e';
49                         testchars[2] = 's';
50                         testchars[3] = 't';
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';
56                 }
57
58                 [Test]
59                 public void IsBrowserDisplay ()
60                 {
61                         Assert.IsTrue (Encoding.GetEncoding ("latin1").IsBrowserDisplay);
62                 }
63
64                 [Test]
65                 public void IsBrowserSave ()
66                 {
67                         Assert.IsTrue (Encoding.GetEncoding ("latin1").IsBrowserSave);
68                 }
69
70                 [Test]
71                 public void IsMailNewsDisplay ()
72                 {
73                         Assert.IsTrue (Encoding.GetEncoding ("latin1").IsMailNewsDisplay);
74                 }
75
76                 [Test]
77                 public void IsMailNewsSave ()
78                 {
79                         Assert.IsTrue (Encoding.GetEncoding ("latin1").IsMailNewsSave);
80                 }
81
82                 [Test] // Test GetBytes(char[])
83                 public void TestGetBytes1 () 
84                 {
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]);
89                 }
90
91                 [Test] // Test GetBytes(char[], int, int)
92                 public void TestGetBytes2 () 
93                 {
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");
98                 }
99
100                 [Test] // Test non-Latin1 char in char[]
101                 public void TestGetBytes3 () 
102                 {
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");
110                 }
111
112                 [Test] // Test GetBytes(char[], int, int, byte[], int)
113                 public void TestGetBytes4 () 
114                 {
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");
120                 }
121
122                 [Test] // Test GetBytes(string, int, int, byte[], int)
123                 public void TestGetBytes5 () 
124                 {
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");
129                 }
130
131                 [Test] // Test GetBytes(string)
132                 public void TestGetBytes6 () 
133                 {
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]);
138                 }
139
140                 [Test] // Test GetBytes(string)
141                 public void TestGetBytes7 ()
142                 {
143                         var latin1_encoding = Encoding.GetEncoding ("latin1");
144
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");
148
149                         expected = new byte [] { 0x3F, 0x3F };
150                         actual = latin1_encoding.GetBytes("\ud83d\ude0a"); // surrogate pair replacement
151                         Assert.AreEqual (expected, actual, "#2");
152
153                         expected = new byte [] { 0x3F, 0x3F, 0x20 };
154                         actual = latin1_encoding.GetBytes("\ud83d\ude0a "); // surrogate pair replacement
155                         Assert.AreEqual (expected, actual, "#3");
156
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");
160
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");
164
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");
168
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");
172                 }
173
174                 [Test] // Test GetChars(byte[])
175                 public void TestGetChars1 () 
176                 {
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]);
181                 }
182
183                 [Test] // Test GetChars(byte[], int, int)
184                 public void TestGetChars2 () 
185                 {
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");
190                 }
191
192                 [Test] // Test GetChars(byte[], int, int, char[], int)
193                 public void TestGetChars4 () 
194                 {
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");
200                 }
201
202                 [Test] // Test GetString(char[])
203                 public void TestGetString1 () 
204                 {
205                         Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
206                         string str = latin1_encoding.GetString(testbytes);
207                         Assert.AreEqual ("Test", str);
208                 }
209
210                 [Test] // Test GetString(char[], int, int)
211                 public void TestGetString2 () 
212                 {
213                         Encoding latin1_encoding = Encoding.GetEncoding ("latin1");
214                         string str = latin1_encoding.GetString(testbytes, 1, 2);
215                         Assert.AreEqual ("es", str);
216                 }
217
218                 [Test] // Test Decoder
219                 public void TestDecoder ()
220                 {
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");
226                 }
227
228                 [Test] // Test Decoder
229                 public void TestEncoder ()
230                 {
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");
236                 }
237
238                 [Test]
239                 public void TestZero ()
240                 {
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");
244                 }
245
246                 [Test]
247                 [ExpectedException (typeof (EncoderFallbackException))]
248                 public void EncoderFallback ()
249                 {
250                         Encoding e = Encoding.GetEncoding ("latin1").Clone () as Encoding;
251                         e.EncoderFallback = new EncoderExceptionFallback ();
252                         e.GetBytes ("\u24c8");
253                 }
254
255                 [Test]
256         //      [ExpectedException (typeof (ArgumentException))]
257                 public void DecoderFallback2 ()
258                 {
259                         var bytes = new byte[] {
260                                 0x30, 0xa0, 0x31, 0xa8
261                         };
262                         var enc = (Encoding)Encoding.GetEncoding ("latin1").Clone ();
263                         enc.DecoderFallback = new TestFallbackDecoder ();
264                         
265                         var chars = new char [7];
266                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
267                         Console.WriteLine (ret);
268                         
269                         for (int i = 0; i < chars.Length; i++) {
270                                 Console.Write ("{0:x2} ", (int)chars [i]);
271                         }
272                         Console.WriteLine ();
273                 }
274                 
275                 [Test]
276                 public void DecoderFallback3 ()
277                 {
278                         var bytes = new byte[] {
279                                 0x30, 0xa0, 0x31, 0xa8
280                         };
281                         var enc = (Encoding)Encoding.GetEncoding ("latin1").Clone ();
282                         enc.DecoderFallback = new TestFallbackDecoder ();
283                         
284                         var chars = new char[] { '9', '8', '7', '6', '5' };
285                         var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
286                         
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]");
293                 }
294                 
295                 class TestFallbackDecoder : DecoderFallback {
296                         const int count = 2;
297                         
298                         public override int MaxCharCount {
299                                 get { return count; }
300                         }
301                         
302                         public override DecoderFallbackBuffer CreateFallbackBuffer ()
303                         {
304                                 return new Buffer ();
305                         }
306                         
307                         class Buffer : DecoderFallbackBuffer {
308                                 char[] queue;
309                                 int index;
310                                 
311                                 public override int Remaining {
312                                         get {
313                                                 return queue.Length - index;
314                                         }
315                                 }
316                                 
317                                 public override char GetNextChar ()
318                                 {
319                                         return index < queue.Length ? queue [index++] : '\0';
320                                 }
321                                 
322                                 public override bool Fallback (byte[] bytes, int unused)
323                                 {
324                                         queue = new char[bytes.Length * count];
325                                         index = 0;
326                                         for (int i = 0; i < bytes.Length; i++) {
327                                                 for (int j = 0; j < count; j++)
328                                                         queue [index++] = (char)(bytes [i]+j);
329                                         }
330                                         return true;
331                                 }
332                                 
333                                 public override bool MovePrevious ()
334                                 {
335                                         throw new NotImplementedException ();
336                                 }
337                                 
338                                 public override void Reset ()
339                                 {
340                                         base.Reset ();
341                                 }
342                         }
343                 }
344
345         }
346 }