* UTF32EncodingTest.cs: Added tests for GetByteCount overloads.
[mono.git] / mcs / class / corlib / Test / System.Text / UTF32EncodingTest.cs
1 #if NET_2_0
2 using System;
3 using System.Text;
4
5 using NUnit.Framework;
6
7 namespace MonoTests.System.Text
8 {
9         [TestFixture]
10         public class UTF32EncodingTest
11         {
12                 [Test] // GetByteCount (Char [])
13                 [Category ("NotDotNet")] // A1/B1 return 24 on MS
14                 public void GetByteCount1 ()
15                 {
16                         char [] chars = new char[] { 'z', 'a', '\u0306',
17                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
18
19                         UTF32Encoding le = new UTF32Encoding (false, true);
20                         Assert.AreEqual (28, le.GetByteCount (chars), "#A1");
21                         Assert.AreEqual (0, le.GetByteCount (new char [0]), "#A2");
22
23                         UTF32Encoding be = new UTF32Encoding (true, true);
24                         Assert.AreEqual (28, be.GetByteCount (chars), "#B1");
25                         Assert.AreEqual (0, be.GetByteCount (new char [0]), "#B2");
26                 }
27
28                 [Test] // GetByteCount (Char [])
29                 public void GetByteCount1_Chars_Null ()
30                 {
31                         UTF32Encoding enc = new UTF32Encoding ();
32                         try {
33                                 enc.GetByteCount ((Char []) null);
34                                 Assert.Fail ("#1");
35                         } catch (ArgumentNullException ex) {
36                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
37                                 Assert.IsNull (ex.InnerException, "#3");
38                                 Assert.IsNotNull (ex.Message, "#4");
39                                 Assert.AreEqual ("chars", ex.ParamName, "#5");
40                         }
41                 }
42
43                 [Test] // GetByteCount (String)
44                 [Category ("NotDotNet")] // A1/B1 return 24 on MS
45                 public void GetByteCount2 ()
46                 {
47                         string s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
48
49                         UTF32Encoding le = new UTF32Encoding (false, true);
50                         Assert.AreEqual (28, le.GetByteCount (s), "#A1");
51                         Assert.AreEqual (0, le.GetByteCount (string.Empty), "#A2");
52
53                         UTF32Encoding be = new UTF32Encoding (true, true);
54                         Assert.AreEqual (28, be.GetByteCount (s), "#B1");
55                         Assert.AreEqual (0, be.GetByteCount (string.Empty), "#B2");
56                 }
57
58                 [Test] // GetByteCount (String)
59                 public void GetByteCount2_S_Null ()
60                 {
61                         UTF32Encoding enc = new UTF32Encoding ();
62                         try {
63                                 enc.GetByteCount ((string) null);
64                                 Assert.Fail ("#1");
65                         } catch (ArgumentNullException ex) {
66                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
67                                 Assert.IsNull (ex.InnerException, "#3");
68                                 Assert.IsNotNull (ex.Message, "#4");
69                                 Assert.AreEqual ("s", ex.ParamName, "#5");
70                         }
71                 }
72
73                 [Test] // GetByteCount (Char *)
74                 public unsafe void GetByteCount3 ()
75                 {
76                         char [] chars = new char[] { 'z', 'a', '\u0306',
77                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
78
79                         fixed (char* cp = chars) {
80                                 UTF32Encoding le = new UTF32Encoding (false, true);
81                                 Assert.AreEqual (12, le.GetByteCount (cp, 3), "#A1");
82                                 Assert.AreEqual (4, le.GetByteCount (cp, 1), "#A2");
83                                 Assert.AreEqual (0, le.GetByteCount (cp, 0), "#A3");
84                                 Assert.AreEqual (24, le.GetByteCount (cp, 6), "#A4");
85                                 //Assert.AreEqual (24, le.GetByteCount (cp, 7), "#A5");
86
87                                 UTF32Encoding be = new UTF32Encoding (true, true);
88                                 Assert.AreEqual (12, be.GetByteCount (cp, 3), "#B1");
89                                 Assert.AreEqual (4, be.GetByteCount (cp, 1), "#B2");
90                                 Assert.AreEqual (0, be.GetByteCount (cp, 0), "#B3");
91                                 Assert.AreEqual (24, be.GetByteCount (cp, 6), "#B4");
92                                 //Assert.AreEqual (24, be.GetByteCount (cp, 7), "#B5");
93                         }
94                 }
95
96                 [Test] // GetByteCount (Char *)
97                 public unsafe void GetByteCount3_Chars_Null ()
98                 {
99                         UTF32Encoding enc = new UTF32Encoding ();
100                         try {
101                                 enc.GetByteCount ((char *) null, 1);
102                                 Assert.Fail ("#1");
103                         } catch (ArgumentNullException ex) {
104                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
105                                 Assert.IsNull (ex.InnerException, "#3");
106                                 Assert.IsNotNull (ex.Message, "#4");
107                                 Assert.AreEqual ("chars", ex.ParamName, "#5");
108                         }
109                 }
110
111                 [Test] // GetByteCount (Char [], Int32, Int32)
112                 public void GetByteCount4 ()
113                 {
114                         char [] chars = new char[] { 'z', 'a', '\u0306',
115                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
116
117                         UTF32Encoding le = new UTF32Encoding (false, true);
118                         Assert.AreEqual (12, le.GetByteCount (chars, 0, 3), "#A1");
119                         Assert.AreEqual (16, le.GetByteCount (chars, 2, 4), "#A2");
120                         Assert.AreEqual (4, le.GetByteCount (chars, 4, 1), "#A3");
121                         Assert.AreEqual (4, le.GetByteCount (chars, 6, 1), "#A4");
122                         Assert.AreEqual (0, le.GetByteCount (chars, 6, 0), "#A5");
123                         Assert.AreEqual (24, le.GetByteCount (chars, 0, 6), "#A6");
124                         //Assert.AreEqual (24, le.GetByteCount (chars, 0, 7), "#A7");
125
126                         UTF32Encoding be = new UTF32Encoding (true, true);
127                         Assert.AreEqual (12, be.GetByteCount (chars, 0, 3), "#B1");
128                         Assert.AreEqual (16, be.GetByteCount (chars, 2, 4), "#B2");
129                         Assert.AreEqual (4, be.GetByteCount (chars, 4, 1), "#B3");
130                         Assert.AreEqual (4, be.GetByteCount (chars, 6, 1), "#B4");
131                         Assert.AreEqual (0, be.GetByteCount (chars, 6, 0), "#B5");
132                         Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B6");
133                         //Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B7");
134                 }
135
136                 [Test] // GetByteCount (Char [], Int32, Int32)
137                 public void GetByteCount4_Chars_Null ()
138                 {
139                         UTF32Encoding enc = new UTF32Encoding ();
140                         try {
141                                 enc.GetByteCount ((Char []) null, 0, 1);
142                                 Assert.Fail ("#1");
143                         } catch (ArgumentNullException ex) {
144                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
145                                 Assert.IsNull (ex.InnerException, "#3");
146                                 Assert.IsNotNull (ex.Message, "#4");
147                                 Assert.AreEqual ("chars", ex.ParamName, "#5");
148                         }
149                 }
150
151                 [Test] // GetByteCount (Char [], Int32, Int32)
152                 public void GetByteCount4_Count_Negative ()
153                 {
154                         char [] chars = new char[] { 'z', 'a', '\u0306',
155                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
156
157                         UTF32Encoding enc = new UTF32Encoding ();
158                         try {
159                                 enc.GetByteCount (chars, 1, -1);
160                                 Assert.Fail ("#1");
161                         } catch (ArgumentOutOfRangeException ex) {
162                                 // Non-negative number required
163                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
164                                 Assert.IsNull (ex.InnerException, "#3");
165                                 Assert.IsNotNull (ex.Message, "#4");
166                                 Assert.AreEqual ("count", ex.ParamName, "#5");
167                         }
168                 }
169
170                 [Test] // GetByteCount (Char [], Int32, Int32)
171                 public void GetByteCount4_Count_Overflow ()
172                 {
173                         char [] chars = new char[] { 'z', 'a', '\u0306',
174                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
175
176                         UTF32Encoding enc = new UTF32Encoding ();
177                         try {
178                                 enc.GetByteCount (chars, 6, 2);
179                                 Assert.Fail ("#1");
180                         } catch (ArgumentOutOfRangeException ex) {
181                                 // Index and count must refer to a location
182                                 // within the buffer
183                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
184                                 Assert.IsNull (ex.InnerException, "#3");
185                                 Assert.IsNotNull (ex.Message, "#4");
186                                 //Assert.AreEqual ("chars", ex.ParamName, "#5");
187                         }
188                 }
189
190                 [Test] // GetByteCount (Char [], Int32, Int32)
191                 public void GetByteCount4_Index_Negative ()
192                 {
193                         char [] chars = new char[] { 'z', 'a', '\u0306',
194                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
195
196                         UTF32Encoding enc = new UTF32Encoding ();
197                         try {
198                                 enc.GetByteCount (chars, -1, 1);
199                                 Assert.Fail ("#1");
200                         } catch (ArgumentOutOfRangeException ex) {
201                                 // Non-negative number required
202                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
203                                 Assert.IsNull (ex.InnerException, "#3");
204                                 Assert.IsNotNull (ex.Message, "#4");
205                                 Assert.AreEqual ("index", ex.ParamName, "#5");
206                         }
207                 }
208
209                 [Test] // GetByteCount (Char [], Int32, Int32)
210                 public void GetByteCount4_Index_Overflow ()
211                 {
212                         char [] chars = new char[] { 'z', 'a', '\u0306',
213                                 '\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
214
215                         UTF32Encoding enc = new UTF32Encoding ();
216                         try {
217                                 enc.GetByteCount (chars, 7, 1);
218                                 Assert.Fail ("#1");
219                         } catch (ArgumentOutOfRangeException ex) {
220                                 // Index and count must refer to a location
221                                 // within the buffer
222                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
223                                 Assert.IsNull (ex.InnerException, "#3");
224                                 Assert.IsNotNull (ex.Message, "#4");
225                                 //Assert.AreEqual ("chars", ex.ParamName, "#5");
226                         }
227                 }
228
229                 [Test]
230                 public void GetPreamble ()
231                 {
232                         byte[] lePreamble = new UTF32Encoding(false, true).GetPreamble();
233                         Assert.AreEqual (new byte [] { 0xff, 0xfe, 0, 0 }, lePreamble, "#1");
234
235                         byte[] bePreamble = new UTF32Encoding(true, true).GetPreamble();
236                         Assert.AreEqual (new byte [] { 0, 0, 0xfe, 0xff }, bePreamble, "#2");
237                 }
238
239                 [Test]
240                 public void IsBrowserDisplay ()
241                 {
242                         UTF32Encoding le = new UTF32Encoding (false, true);
243                         Assert.IsFalse (le.IsBrowserDisplay, "#1");
244
245                         UTF32Encoding be = new UTF32Encoding (true, true);
246                         Assert.IsFalse (be.IsBrowserDisplay, "#2");
247                 }
248
249                 [Test]
250                 public void IsBrowserSave ()
251                 {
252                         UTF32Encoding le = new UTF32Encoding (false, true);
253                         Assert.IsFalse (le.IsBrowserSave);
254
255                         UTF32Encoding be = new UTF32Encoding (true, true);
256                         Assert.IsFalse (be.IsBrowserSave, "#2");
257                 }
258
259                 [Test]
260                 public void IsMailNewsDisplay ()
261                 {
262                         UTF32Encoding le = new UTF32Encoding (false, true);
263                         Assert.IsFalse (le.IsMailNewsDisplay);
264
265                         UTF32Encoding be = new UTF32Encoding (true, true);
266                         Assert.IsFalse (be.IsMailNewsDisplay, "#2");
267                 }
268
269                 [Test]
270                 public void IsMailNewsSave ()
271                 {
272                         UTF32Encoding le = new UTF32Encoding (false, true);
273                         Assert.IsFalse (le.IsMailNewsSave);
274
275                         UTF32Encoding be = new UTF32Encoding (true, true);
276                         Assert.IsFalse (be.IsMailNewsSave, "#2");
277                 }
278         }
279 }
280 #endif