2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / Test / System.Text / UnicodeEncodingTest.cs
1 //
2 // UnicodeEncodingTest.cs - NUnit Test Cases for System.Text.UnicodeEncoding
3 //
4 // Author:
5 //     Patrick Kalkman  kalkman@cistron.nl
6 //
7 // (C) 2003 Patrick Kalkman
8 // 
9 using NUnit.Framework;
10 using System;
11 using System.Text;
12
13 namespace MonoTests.System.Text
14 {
15         [TestFixture]
16         public class UnicodeEncodingTest 
17         {
18                 [Test]
19                 public void IsBrowserDisplay ()
20                 {
21                         UnicodeEncoding enc = new UnicodeEncoding ();
22                         Assert.IsFalse (enc.IsBrowserDisplay);
23                 }
24
25                 [Test]
26                 public void IsBrowserSave ()
27                 {
28                         UnicodeEncoding enc = new UnicodeEncoding ();
29                         Assert.IsTrue (enc.IsBrowserSave);
30                 }
31
32                 [Test]
33                 public void IsMailNewsDisplay ()
34                 {
35                         UnicodeEncoding enc = new UnicodeEncoding ();
36                         Assert.IsFalse (enc.IsMailNewsDisplay);
37                 }
38
39                 [Test]
40                 public void IsMailNewsSave ()
41                 {
42                         UnicodeEncoding enc = new UnicodeEncoding ();
43                         Assert.IsFalse (enc.IsMailNewsSave);
44                 }
45
46                 [Test]
47                 public void TestEncodingGetBytes1()
48                 {
49                         //pi and sigma in unicode
50                         string Unicode = "\u03a0\u03a3";
51                         byte[] UniBytes;
52                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
53                         UniBytes = UnicodeEnc.GetBytes (Unicode);
54                         
55                         Assert.AreEqual (0xA0, UniBytes [0], "Uni #1");
56                         Assert.AreEqual (0x03, UniBytes [1], "Uni #2");
57                         Assert.AreEqual (0xA3, UniBytes [2], "Uni #3");
58                         Assert.AreEqual (0x03, UniBytes [3], "Uni #4");
59                 }
60         
61                 [Test]
62                 public void TestEncodingGetBytes2()
63                 {
64                         //pi and sigma in unicode
65                         string Unicode = "\u03a0\u03a3";
66                         byte[] UniBytes;
67                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
68                         UniBytes = UnicodeEnc.GetBytes (Unicode);
69                         
70                         Assert.AreEqual (0x03, UniBytes [0], "Uni #1");
71                         Assert.AreEqual (0xA0, UniBytes [1], "Uni #2");
72                         Assert.AreEqual (0x03, UniBytes [2], "Uni #3");
73                         Assert.AreEqual (0xA3, UniBytes [3], "Uni #4");
74                 }
75
76                 [Test]
77                 public void TestEncodingGetBytes3()
78                 {
79                         //pi and sigma in unicode
80                         string Unicode = "\u03a0\u03a3";
81                         byte[] UniBytes = new byte [4];
82                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian 
83                         int Cnt = UnicodeEnc.GetBytes (Unicode.ToCharArray(), 0, Unicode.Length, UniBytes, 0);
84                         
85                         Assert.AreEqual (4, Cnt, "Uni #1");
86                         Assert.AreEqual (0xA0, UniBytes [0], "Uni #2");
87                         Assert.AreEqual (0x03, UniBytes [1], "Uni #3");
88                         Assert.AreEqual (0xA3, UniBytes [2], "Uni #4");
89                         Assert.AreEqual (0x03, UniBytes [3], "Uni #5");
90                 }
91         
92                 [Test]
93                 public void TestEncodingDecodingGetBytes1()
94                 {
95                         //pi and sigma in unicode
96                         string Unicode = "\u03a0\u03a3";
97                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian 
98                         //Encode the unicode string.
99                         byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
100                         //Decode the bytes to a unicode char array.
101                         char[] UniChars = UnicodeEnc.GetChars (UniBytes);
102                         string Result = new string(UniChars);
103                         
104                         Assert.AreEqual (Unicode, Result, "Uni #1");
105                 }
106
107                 [Test]
108                 public void TestEncodingDecodingGetBytes2()
109                 {
110                         //pi and sigma in unicode
111                         string Unicode = "\u03a0\u03a3";
112                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian 
113                         //Encode the unicode string.
114                         byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
115                         //Decode the bytes to a unicode char array.
116                         char[] UniChars = UnicodeEnc.GetChars (UniBytes);
117                         string Result = new string(UniChars);
118                         
119                         Assert.AreEqual (Unicode, Result, "Uni #1");
120                 }
121
122                 [Test]
123                 public void TestEncodingGetCharCount ()
124                 {
125                         byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0};
126                         UnicodeEncoding encoding = new UnicodeEncoding ();
127
128                         Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2), 
129                                                          "GetCharCount #1");
130                 }
131
132         
133                 
134                 [Test]
135                 public void TestPreamble1()
136                 {
137                         //litle-endian with byte order mark.
138                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, true); 
139                         byte[] PreAmble = UnicodeEnc.GetPreamble();
140
141                         Assert.AreEqual (0xFF, PreAmble [0], "Uni #1");
142                         Assert.AreEqual (0xFE, PreAmble [1], "Uni #2");
143                 }
144
145                 [Test]
146                 public void TestPreamble2()
147                 {
148                         //big-endian with byte order mark.
149                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); 
150                         byte[] PreAmble = UnicodeEnc.GetPreamble();
151
152                         Assert.AreEqual (0xFE, PreAmble [0], "Uni #1");
153                         Assert.AreEqual (0xFF, PreAmble [1], "Uni #2");
154                 }
155
156                 [Test]
157                 public void TestPreamble3()
158                 {
159                         //little-endian without byte order mark.
160                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, false); 
161                         byte[] PreAmble = UnicodeEnc.GetPreamble();
162
163                         Assert.AreEqual (0, PreAmble.Length, "Uni #1");
164                 }
165                 
166                 [Test]
167 #if NET_2_0
168                 [Category ("NotWorking")]
169 #endif
170                 public void TestMaxCharCount()
171                 {
172                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
173 #if NET_2_0
174                         // where is this extra 1 coming from?
175                         Assert.AreEqual (26, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
176                         Assert.AreEqual (27, UnicodeEnc.GetMaxCharCount(51), "UTF #2");
177 #else
178                         Assert.AreEqual (25, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
179 #endif
180                 }
181         
182                 [Test]
183 #if NET_2_0
184                 [Category ("NotWorking")]
185 #endif
186                 public void TestMaxByteCount()
187                 {
188                         UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
189 #if NET_2_0
190                         // is this extra 2 BOM?
191                         Assert.AreEqual (102, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
192 #else
193                         Assert.AreEqual (100, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
194 #endif
195                 }
196
197                 [Test]
198                 public void ZeroLengthArrays ()
199                 {
200                         UnicodeEncoding encoding = new UnicodeEncoding ();
201                         encoding.GetCharCount (new byte [0]);
202                         encoding.GetChars (new byte [0]);
203                         encoding.GetCharCount (new byte [0], 0, 0);
204                         encoding.GetChars (new byte [0], 0, 0);
205                         encoding.GetChars (new byte [0], 0, 0, new char [0], 0);
206                         encoding.GetByteCount (new char [0]);
207                         encoding.GetBytes (new char [0]);
208                         encoding.GetByteCount (new char [0], 0, 0);
209                         encoding.GetBytes (new char [0], 0, 0);
210                         encoding.GetBytes (new char [0], 0, 0, new byte [0], 0);
211                         encoding.GetByteCount ("");
212                         encoding.GetBytes ("");
213                 }
214
215                 [Test]
216                 public void ByteOrderMark ()
217                 {
218                         string littleEndianString = "\ufeff\u0042\u004f\u004d";
219                         string bigEndianString = "\ufffe\u4200\u4f00\u4d00";
220                         byte [] littleEndianBytes = new byte [] {0xff, 0xfe, 0x42, 0x00, 0x4f, 0x00, 0x4d, 0x00};
221                         byte [] bigEndianBytes = new byte [] {0xfe, 0xff, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4d};
222                         UnicodeEncoding encoding;
223                         
224                         encoding = new UnicodeEncoding (false, true);
225                         Assert.AreEqual (encoding.GetBytes (littleEndianString), littleEndianBytes, "BOM #1");
226                         Assert.AreEqual (encoding.GetBytes (bigEndianString), bigEndianBytes, "BOM #2");
227                         Assert.AreEqual (encoding.GetString (littleEndianBytes), littleEndianString, "BOM #3");
228                         Assert.AreEqual (encoding.GetString (bigEndianBytes), bigEndianString, "BOM #4");
229
230                         encoding = new UnicodeEncoding (true, true);
231                         Assert.AreEqual (encoding.GetBytes (littleEndianString), bigEndianBytes, "BOM #5");
232                         Assert.AreEqual (encoding.GetBytes (bigEndianString), littleEndianBytes, "BOM #6");
233                         Assert.AreEqual (encoding.GetString (littleEndianBytes), bigEndianString, "BOM #7");
234                         Assert.AreEqual (encoding.GetString (bigEndianBytes), littleEndianString, "BOM #8");
235                 }
236         }
237 }