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