Merge pull request #271 from pruiz/xamarin-bug-4108
[mono.git] / mcs / class / corlib / Test / System.Globalization / CultureInfoTest.cs
1 //
2 // System.Globalization.CultureInfo Test Cases
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (c) 2005 Novell, Inc. (http://www.novell.com)
8 //
9
10 using System;
11 using System.Globalization;
12 using System.IO;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using System.Threading;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System.Globalization
19 {
20         [TestFixture]
21         public class CultureInfoTest
22         {
23                 CultureInfo old_culture;
24
25                 [SetUp]
26                 public void Setup ()
27                 {
28                         old_culture = Thread.CurrentThread.CurrentCulture;
29                 }
30
31                 [TearDown]
32                 public void TearDown ()
33                 {
34                         Thread.CurrentThread.CurrentCulture = old_culture;
35                 }
36
37                 [Test]
38                 public void Constructor0 ()
39                 {
40                         CultureInfo ci = new CultureInfo (2067);
41                         Assert.IsFalse (ci.IsReadOnly, "#1");
42                         Assert.AreEqual (2067, ci.LCID, "#2");
43                         Assert.AreEqual ("nl-BE", ci.Name, "#3");
44                         Assert.IsTrue (ci.UseUserOverride, "#4");
45                 }
46
47                 [Test]
48                 public void Constructor0_Identifier_Negative ()
49                 {
50                         try {
51                                 new CultureInfo (-1);
52                                 Assert.Fail ("#1");
53                         } catch (ArgumentOutOfRangeException ex) {
54                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
55                                 Assert.IsNull (ex.InnerException, "#3");
56                                 Assert.IsNotNull (ex.Message, "#4");
57                                 Assert.IsNotNull (ex.ParamName, "#5");
58                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
59                         }
60                 }
61
62                 [Test]
63                 public void Constructor1 ()
64                 {
65                         CultureInfo ci = new CultureInfo ("nl-BE");
66                         Assert.IsFalse (ci.IsReadOnly, "#1");
67                         Assert.AreEqual (2067, ci.LCID, "#2");
68                         Assert.AreEqual ("nl-BE", ci.Name, "#3");
69                         Assert.IsTrue (ci.UseUserOverride, "#4");
70                 }
71
72                 [Test]
73                 public void Constructor1_Name_Null ()
74                 {
75                         try {
76                                 new CultureInfo ((string) null);
77                                 Assert.Fail ("#1");
78                         } catch (ArgumentNullException ex) {
79                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
80                                 Assert.IsNull (ex.InnerException, "#3");
81                                 Assert.IsNotNull (ex.Message, "#4");
82                                 Assert.IsNotNull (ex.ParamName, "#5");
83                                 Assert.AreEqual ("name", ex.ParamName, "#6");
84                         }
85                 }
86
87                 [Test]
88                 public void Constructor2 ()
89                 {
90                         CultureInfo ci = new CultureInfo (2067, false);
91                         Assert.IsFalse (ci.IsReadOnly, "#A1");
92                         Assert.AreEqual (2067, ci.LCID, "#A2");
93                         Assert.AreEqual ("nl-BE", ci.Name, "#A3");
94                         Assert.IsFalse (ci.UseUserOverride, "#A4");
95
96                         ci = new CultureInfo (2067, true);
97                         Assert.IsFalse (ci.IsReadOnly, "#B1");
98                         Assert.AreEqual (2067, ci.LCID, "#B2");
99                         Assert.AreEqual ("nl-BE", ci.Name, "#B3");
100                         Assert.IsTrue (ci.UseUserOverride, "#B4");
101                 }
102
103                 [Test]
104                 public void Constructor2_Identifier_Negative ()
105                 {
106                         try {
107                                 new CultureInfo (-1, false);
108                                 Assert.Fail ("#1");
109                         } catch (ArgumentOutOfRangeException ex) {
110                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
111                                 Assert.IsNull (ex.InnerException, "#3");
112                                 Assert.IsNotNull (ex.Message, "#4");
113                                 Assert.IsNotNull (ex.ParamName, "#5");
114                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
115                         }
116                 }
117
118                 [Test]
119                 public void Constructor3 ()
120                 {
121                         CultureInfo ci = new CultureInfo ("nl-BE", false);
122                         Assert.IsFalse (ci.IsReadOnly, "#A1");
123                         Assert.AreEqual (2067, ci.LCID, "#A2");
124                         Assert.AreEqual ("nl-BE", ci.Name, "#A3");
125                         Assert.IsFalse (ci.UseUserOverride, "#A4");
126
127                         ci = new CultureInfo ("nl-BE", true);
128                         Assert.IsFalse (ci.IsReadOnly, "#B1");
129                         Assert.AreEqual (2067, ci.LCID, "#B2");
130                         Assert.AreEqual ("nl-BE", ci.Name, "#B3");
131                         Assert.IsTrue (ci.UseUserOverride, "#B4");
132                 }
133
134                 [Test]
135                 public void Constructor3_Name_Null ()
136                 {
137                         try {
138                                 new CultureInfo ((string) null, false);
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.IsNotNull (ex.ParamName, "#5");
145                                 Assert.AreEqual ("name", ex.ParamName, "#6");
146                         }
147                 }
148
149                 [Test]
150                 public void DateTimeFormat_Neutral_Culture ()
151                 {
152                         CultureInfo ci = new CultureInfo ("nl");
153                         try {
154                                 DateTimeFormatInfo dfi = ci.DateTimeFormat;
155 #if NET_4_0
156                                 Assert.IsNotNull (dfi, "#1");
157 #else
158                                 Assert.Fail ("#1:" + (dfi != null));
159 #endif
160                         } catch (NotSupportedException ex) {
161                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
162                                 Assert.IsNull (ex.InnerException, "#3");
163                                 Assert.IsNotNull (ex.Message, "#4");
164                         }
165                 }
166
167                 [Test] // bug #72081
168                 public void GetAllCulturesInvariant ()
169                 {
170                         CultureInfo invariant = CultureInfo.InvariantCulture;
171                         CultureInfo [] infos = CultureInfo.GetCultures (CultureTypes.AllCultures);
172                         foreach (CultureInfo culture in infos) {
173                                 if (culture.Equals (invariant))
174                                         return;
175                         }
176
177                         Assert.Fail ("InvariantCulture not found in the array from GetCultures()");
178                 }
179
180                 [Test]
181 #if !NET_4_0
182                 [ExpectedException (typeof (NotSupportedException))]
183 #endif
184                 public void TrySetNeutralCultureNotInvariant ()
185                 {
186                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("ar");
187                 }
188
189                 [Test]
190                 [Category ("TargetJvmNotWorking")] //OptionalCalendars not yet supported for TARGET_JVM.
191                 // make sure that all CultureInfo holds non-null calendars.
192                 public void OptionalCalendars ()
193                 {
194                         foreach (CultureInfo ci in CultureInfo.GetCultures (
195                                 CultureTypes.AllCultures))
196                                 Assert.IsNotNull (ci.OptionalCalendars, String.Format ("{0} {1}",
197                                         ci.LCID, ci.Name));
198                 }
199
200                 [Test] // bug #77347
201                 public void CloneNeutral ()
202                 {
203                         CultureInfo culture = new CultureInfo ("en");
204                         CultureInfo cultureClone = culture.Clone () as CultureInfo;
205                         Assert.IsTrue (culture.Equals (cultureClone));
206                 }
207
208                 [Test]
209                 public void IsNeutral ()
210                 {
211                         var ci = new CultureInfo (0x6C1A);
212                         Assert.IsTrue (ci.IsNeutralCulture, "#1");
213                         Assert.AreEqual ("srp", ci.ThreeLetterISOLanguageName, "#2");
214
215                         ci = new CultureInfo ("en-US");
216                         Assert.IsFalse (ci.IsNeutralCulture, "#1a");
217                         Assert.AreEqual ("eng", ci.ThreeLetterISOLanguageName, "#2a");
218                 }
219
220                 [Test] // bug #81930
221                 public void IsReadOnly ()
222                 {
223                         CultureInfo ci;
224
225                         ci = new CultureInfo ("en-US");
226                         Assert.IsFalse (ci.IsReadOnly, "#A1");
227                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A2");
228                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A3");
229                         ci.NumberFormat.NumberGroupSeparator = ",";
230                         ci.NumberFormat = new NumberFormatInfo ();
231                         ci.DateTimeFormat.DateSeparator = "/";
232                         ci.DateTimeFormat = new DateTimeFormatInfo ();
233                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A4");
234                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A5");
235
236                         ci = new CultureInfo (CultureInfo.InvariantCulture.LCID);
237                         Assert.IsFalse (ci.IsReadOnly, "#B1");
238                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B2");
239                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B3");
240                         ci.NumberFormat.NumberGroupSeparator = ",";
241                         ci.NumberFormat = new NumberFormatInfo ();
242                         ci.DateTimeFormat.DateSeparator = "/";
243                         ci.DateTimeFormat = new DateTimeFormatInfo ();
244                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B4");
245                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B5");
246
247                         ci = CultureInfo.CurrentCulture;
248                         Assert.IsTrue (ci.IsReadOnly, "#C1:" + ci.DisplayName);
249                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#C2");
250                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#C3");
251                         try {
252                                 ci.NumberFormat.NumberGroupSeparator = ",";
253                                 Assert.Fail ("#C4");
254                         } catch (InvalidOperationException ex) {
255                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C5");
256                                 Assert.IsNull (ex.InnerException, "#C6");
257                                 Assert.IsNotNull (ex.Message, "#C7");
258                         }
259
260                         ci = CultureInfo.CurrentUICulture;
261                         Assert.IsTrue (ci.IsReadOnly, "#D1");
262                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#D2");
263                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#D3");
264                         try {
265                                 ci.NumberFormat.NumberGroupSeparator = ",";
266                                 Assert.Fail ("#D4");
267                         } catch (InvalidOperationException ex) {
268                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D5");
269                                 Assert.IsNull (ex.InnerException, "#D6");
270                                 Assert.IsNotNull (ex.Message, "#D7");
271                         }
272
273                         ci = CultureInfo.InvariantCulture;
274                         Assert.IsTrue (ci.IsReadOnly, "#F1");
275                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#F2");
276                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#F3");
277                         try {
278                                 ci.NumberFormat.NumberGroupSeparator = ",";
279                                 Assert.Fail ("#F4");
280                         } catch (InvalidOperationException ex) {
281                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F5");
282                                 Assert.IsNull (ex.InnerException, "#F6");
283                                 Assert.IsNotNull (ex.Message, "#F7");
284                         }
285
286                         ci = new CultureInfo (string.Empty);
287                         Assert.IsFalse (ci.IsReadOnly, "#G1");
288                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G2");
289                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G3");
290                         ci.NumberFormat.NumberGroupSeparator = ",";
291                         ci.NumberFormat = new NumberFormatInfo ();
292                         ci.DateTimeFormat.DateSeparator = "/";
293                         ci.DateTimeFormat = new DateTimeFormatInfo ();
294                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G4");
295                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G5");
296                 }
297
298                 [Test]
299                 public void IsReadOnly_GetCultures ()
300                 {
301                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
302                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
303                                 Assert.IsFalse (ci.IsReadOnly, "#1:" + cultureMsg);
304                                 if (ci.IsNeutralCulture)
305                                         continue;
306                                 Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
307                                 Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
308                         }
309                 }
310
311                 [Test]
312                 [Category ("NotWorking")]
313                 public void IsReadOnly_InstalledUICulture ()
314                 {
315                         CultureInfo ci = CultureInfo.InstalledUICulture;
316                         Assert.IsTrue (ci.IsReadOnly, "#1");
317                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#2");
318                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#3");
319                         try {
320                                 ci.NumberFormat.NumberGroupSeparator = ",";
321                                 Assert.Fail ("#4");
322                         } catch (InvalidOperationException ex) {
323                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
324                                 Assert.IsNull (ex.InnerException, "#6");
325                                 Assert.IsNotNull (ex.Message, "#7");
326                         }
327                 }
328
329                 [Test] // bug #69652
330                 public void Norwegian ()
331                 {
332                         new CultureInfo ("no");
333                         new CultureInfo ("nb-NO");
334                         new CultureInfo ("nn-NO");
335                 }
336
337                 [Test]
338                 public void NumberFormat_Neutral_Culture ()
339                 {
340                         CultureInfo ci = new CultureInfo ("nl");
341                         try {
342                                 NumberFormatInfo nfi = ci.NumberFormat;
343 #if NET_4_0
344                                 Assert.IsNotNull (nfi, "#1");
345 #else
346                                 Assert.Fail ("#1:" + (nfi != null));
347 #endif
348                         } catch (NotSupportedException ex) {
349                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
350                                 Assert.IsNull (ex.InnerException, "#3");
351                                 Assert.IsNotNull (ex.Message, "#4");
352                         }
353                 }
354
355                 [Test]
356                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
357                 public void GetCultureInfo_Identifier ()
358                 {
359                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
360                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
361                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.LCID);
362                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
363                                 if (culture.IsNeutralCulture)
364                                         continue;
365                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
366                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
367                         }
368                 }
369
370                 [Test]
371                 public void GetCultureInfo_Identifier_Negative ()
372                 {
373                         try {
374                                 CultureInfo.GetCultureInfo (-1);
375                                 Assert.Fail ("#1");
376                         } catch (ArgumentOutOfRangeException ex) {
377                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
378                                 Assert.IsNull (ex.InnerException, "#3");
379                                 Assert.IsNotNull (ex.Message, "#4");
380                                 Assert.IsNotNull (ex.ParamName, "#5");
381                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
382                         }
383                 }
384
385                 [Test]
386                 public void GetCultureInfo_Identifier_NotSupported ()
387                 {
388                         try {
389                                 CultureInfo.GetCultureInfo (666);
390                                 Assert.Fail ("#1");
391                         } catch (ArgumentException ex) {
392 #if NET_4_0
393                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
394 #else
395                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
396 #endif
397                                 Assert.IsNull (ex.InnerException, "#3");
398                                 Assert.IsNotNull (ex.Message, "#4");
399                                 Assert.IsNotNull (ex.ParamName, "#5");
400                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
401                         }
402                 }
403
404                 [Test]
405                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
406                 public void GetCultureInfo_Name ()
407                 {
408                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
409                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
410                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.Name);
411                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
412                                 if (culture.IsNeutralCulture)
413                                         continue;
414                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
415                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
416                         }
417                 }
418
419                 [Test]
420                 public void GetCultureInfo_Name_NotSupported ()
421                 {
422                         try {
423                                 CultureInfo.GetCultureInfo ("666");
424                                 Assert.Fail ("#1");
425                         } catch (ArgumentException ex) {
426 #if NET_4_0
427                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
428 #else
429                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
430 #endif
431                                 Assert.IsNull (ex.InnerException, "#3");
432                                 Assert.IsNotNull (ex.Message, "#4");
433                                 Assert.IsNotNull (ex.ParamName, "#5");
434                                 Assert.AreEqual ("name", ex.ParamName, "#6");
435                         }
436                 }
437
438                 [Test]
439                 public void GetCultureInfo_Name_Null ()
440                 {
441                         try {
442                                 CultureInfo.GetCultureInfo ((string) null);
443                                 Assert.Fail ("#1");
444                         } catch (ArgumentNullException ex) {
445                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
446                                 Assert.IsNull (ex.InnerException, "#3");
447                                 Assert.IsNotNull (ex.Message, "#4");
448                                 Assert.IsNotNull (ex.ParamName, "#5");
449                                 Assert.AreEqual ("name", ex.ParamName, "#6");
450                         }
451                 }
452
453                 [Test]
454                 public void UseUserOverride_CurrentCulture ()
455                 {
456                         CultureInfo ci = CultureInfo.CurrentCulture;
457                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
458                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
459
460                         ci = (CultureInfo) ci.Clone ();
461                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
462                 }
463
464                 [Test]
465                 public void UseUserOverride_CurrentUICulture ()
466                 {
467                         CultureInfo ci = CultureInfo.CurrentCulture;
468                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
469                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
470
471                         ci = (CultureInfo) ci.Clone ();
472                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
473                 }
474
475                 [Test]
476                 public void UseUserOverride_GetCultureInfo ()
477                 {
478                         CultureInfo culture;
479
480                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
481                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
482                                 culture = CultureInfo.GetCultureInfo (ci.Name);
483                                 Assert.IsFalse (culture.UseUserOverride, "#1: " + cultureMsg);
484                                 culture = CultureInfo.GetCultureInfo (ci.LCID);
485                                 Assert.IsFalse (culture.UseUserOverride, "#2: " + cultureMsg);
486                         }
487                 }
488
489                 [Test]
490                 public void UseUserOverride_GetCultures ()
491                 {
492                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
493                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
494                                 if (ci.LCID == CultureInfo.InvariantCulture.LCID)
495                                         Assert.IsFalse (ci.UseUserOverride, cultureMsg);
496                                 else
497                                         Assert.IsTrue (ci.UseUserOverride, cultureMsg);
498                         }
499                 }
500
501                 [Test]
502                 public void UseUserOverride_InvariantCulture ()
503                 {
504                         CultureInfo ci = CultureInfo.InvariantCulture;
505                         Assert.IsFalse (ci.UseUserOverride, "#21");
506
507                         ci = (CultureInfo) ci.Clone ();
508                         Assert.IsFalse (ci.UseUserOverride, "#2");
509                 }
510
511                 [Test]
512                 public void Bug402128 ()
513                 {
514                         var culture = new CultureInfo ("en-US");
515                         var ms = new MemoryStream ();
516                         var formatter = new BinaryFormatter ();
517                         formatter.Serialize (ms, culture);
518                         ms.Seek (0, SeekOrigin.Begin);
519                         var deserializedCulture = (CultureInfo) formatter.Deserialize (ms);
520                 }
521
522                 [Test]
523                 public void ZhHant ()
524                 {
525                         Assert.AreEqual (31748, new CultureInfo ("zh-Hant").LCID);
526                         Assert.AreEqual (31748, CultureInfo.GetCultureInfo ("zh-Hant").LCID);
527                 }
528         }
529 }