[SRE] Improved token fixups processing.
[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 ClearCachedData ()
151                 {
152                         var dt = DateTime.Now;
153                         old_culture.ClearCachedData (); // It can be any culture instance as the method should be static
154                         dt = DateTime.Now;
155                 }
156
157                 [Test]
158                 public void CreateSpecificCulture ()
159                 {
160                         var ci = CultureInfo.CreateSpecificCulture ("en");
161                         Assert.AreEqual ("en-US", ci.Name, "#1");
162
163                         ci = CultureInfo.CreateSpecificCulture ("en-GB");
164                         Assert.AreEqual ("en-GB", ci.Name, "#2");
165
166                         ci = CultureInfo.CreateSpecificCulture ("en-----");
167                         Assert.AreEqual ("en-US", ci.Name, "#3");
168
169                         ci = CultureInfo.CreateSpecificCulture ("en-GB-");
170                         Assert.AreEqual ("en-US", ci.Name, "#4");
171
172                         ci = CultureInfo.CreateSpecificCulture ("");
173                         Assert.AreEqual (CultureInfo.InvariantCulture, ci, "#5");
174                 }
175
176                 [Test]
177                 public void CreateSpecificCulture_Invalid ()
178                 {
179                         try {
180                                 CultureInfo.CreateSpecificCulture ("uy32");
181                                 Assert.Fail ("#1");
182 #if NET_4_0
183                         } catch (CultureNotFoundException) {
184 #else
185                         } catch (ArgumentException) {
186 #endif
187                         }
188
189                         try {
190                                 CultureInfo.CreateSpecificCulture (null);
191                                 Assert.Fail ("#2");
192                         } catch (ArgumentNullException) {
193                                 // .NET throws NRE which is lame
194                         }
195                 }
196
197                 [Test]
198                 public void DateTimeFormat_Neutral_Culture ()
199                 {
200                         CultureInfo ci = new CultureInfo ("nl");
201                         try {
202                                 DateTimeFormatInfo dfi = ci.DateTimeFormat;
203 #if NET_4_0
204                                 Assert.IsNotNull (dfi, "#1");
205 #else
206                                 Assert.Fail ("#1:" + (dfi != null));
207 #endif
208                         } catch (NotSupportedException ex) {
209                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
210                                 Assert.IsNull (ex.InnerException, "#3");
211                                 Assert.IsNotNull (ex.Message, "#4");
212                         }
213                 }
214
215                 [Test] // bug #72081
216                 public void GetAllCulturesInvariant ()
217                 {
218                         CultureInfo invariant = CultureInfo.InvariantCulture;
219                         CultureInfo [] infos = CultureInfo.GetCultures (CultureTypes.AllCultures);
220                         foreach (CultureInfo culture in infos) {
221                                 if (culture.Equals (invariant))
222                                         return;
223                         }
224
225                         Assert.Fail ("InvariantCulture not found in the array from GetCultures()");
226                 }
227
228                 [Test]
229                 public void GetAllCultures_Specific ()
230                 {
231                         CultureInfo [] infos = CultureInfo.GetCultures (CultureTypes.SpecificCultures);
232                         foreach (CultureInfo ci in infos) {
233                                 Assert.IsNotNull (ci.DateTimeFormat);
234                         }
235                 }
236
237                 [Test]
238 #if !NET_4_0
239                 [ExpectedException (typeof (NotSupportedException))]
240 #endif
241                 public void TrySetNeutralCultureNotInvariant ()
242                 {
243                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("ar");
244                 }
245
246                 [Test]
247                 // make sure that all CultureInfo holds non-null calendars.
248                 public void OptionalCalendars ()
249                 {
250 #if MOBILE
251                         // ensure the linker does not remove them so we can test them
252                         Assert.IsNotNull (typeof (UmAlQuraCalendar), "UmAlQuraCalendar");
253 #endif
254                         foreach (CultureInfo ci in CultureInfo.GetCultures (
255                                 CultureTypes.AllCultures))
256                                 Assert.IsNotNull (ci.OptionalCalendars, String.Format ("{0} {1}",
257                                         ci.LCID, ci.Name));
258                 }
259
260                 [Test] // bug #77347
261                 public void CloneNeutral ()
262                 {
263                         CultureInfo culture = new CultureInfo ("en");
264                         CultureInfo cultureClone = culture.Clone () as CultureInfo;
265                         Assert.IsTrue (culture.Equals (cultureClone));
266                 }
267
268                 [Test]
269                 public void IsNeutral ()
270                 {
271                         var ci = new CultureInfo (0x6C1A);
272                         Assert.IsTrue (ci.IsNeutralCulture, "#1");
273                         Assert.AreEqual ("srp", ci.ThreeLetterISOLanguageName, "#2");
274
275                         ci = new CultureInfo ("en-US");
276                         Assert.IsFalse (ci.IsNeutralCulture, "#1a");
277                         Assert.AreEqual ("eng", ci.ThreeLetterISOLanguageName, "#2a");
278                 }
279
280                 [Test] // bug #81930
281                 public void IsReadOnly ()
282                 {
283                         CultureInfo ci;
284
285                         ci = new CultureInfo ("en-US");
286                         Assert.IsFalse (ci.IsReadOnly, "#A1");
287                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A2");
288                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A3");
289                         ci.NumberFormat.NumberGroupSeparator = ",";
290                         ci.NumberFormat = new NumberFormatInfo ();
291                         ci.DateTimeFormat.DateSeparator = "/";
292                         ci.DateTimeFormat = new DateTimeFormatInfo ();
293                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A4");
294                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A5");
295
296                         ci = new CultureInfo (CultureInfo.InvariantCulture.LCID);
297                         Assert.IsFalse (ci.IsReadOnly, "#B1");
298                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B2");
299                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B3");
300                         ci.NumberFormat.NumberGroupSeparator = ",";
301                         ci.NumberFormat = new NumberFormatInfo ();
302                         ci.DateTimeFormat.DateSeparator = "/";
303                         ci.DateTimeFormat = new DateTimeFormatInfo ();
304                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B4");
305                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B5");
306
307                         ci = CultureInfo.CurrentCulture;
308                         Assert.IsTrue (ci.IsReadOnly, "#C1:" + ci.DisplayName);
309                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#C2");
310                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#C3");
311                         try {
312                                 ci.NumberFormat.NumberGroupSeparator = ",";
313                                 Assert.Fail ("#C4");
314                         } catch (InvalidOperationException ex) {
315                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C5");
316                                 Assert.IsNull (ex.InnerException, "#C6");
317                                 Assert.IsNotNull (ex.Message, "#C7");
318                         }
319
320                         ci = CultureInfo.CurrentUICulture;
321                         Assert.IsTrue (ci.IsReadOnly, "#D1");
322                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#D2");
323                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#D3");
324                         try {
325                                 ci.NumberFormat.NumberGroupSeparator = ",";
326                                 Assert.Fail ("#D4");
327                         } catch (InvalidOperationException ex) {
328                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D5");
329                                 Assert.IsNull (ex.InnerException, "#D6");
330                                 Assert.IsNotNull (ex.Message, "#D7");
331                         }
332
333                         ci = CultureInfo.InvariantCulture;
334                         Assert.IsTrue (ci.IsReadOnly, "#F1");
335                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#F2");
336                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#F3");
337                         try {
338                                 ci.NumberFormat.NumberGroupSeparator = ",";
339                                 Assert.Fail ("#F4");
340                         } catch (InvalidOperationException ex) {
341                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F5");
342                                 Assert.IsNull (ex.InnerException, "#F6");
343                                 Assert.IsNotNull (ex.Message, "#F7");
344                         }
345
346                         ci = new CultureInfo (string.Empty);
347                         Assert.IsFalse (ci.IsReadOnly, "#G1");
348                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G2");
349                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G3");
350                         ci.NumberFormat.NumberGroupSeparator = ",";
351                         ci.NumberFormat = new NumberFormatInfo ();
352                         ci.DateTimeFormat.DateSeparator = "/";
353                         ci.DateTimeFormat = new DateTimeFormatInfo ();
354                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G4");
355                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G5");
356                 }
357
358                 [Test]
359                 public void IsReadOnly_GetCultures ()
360                 {
361                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
362                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
363                                 Assert.IsFalse (ci.IsReadOnly, "#1:" + cultureMsg);
364                                 if (ci.IsNeutralCulture)
365                                         continue;
366                                 Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
367                                 Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
368                         }
369                 }
370
371                 [Test]
372                 [Category ("NotWorking")]
373                 public void IsReadOnly_InstalledUICulture ()
374                 {
375                         CultureInfo ci = CultureInfo.InstalledUICulture;
376                         Assert.IsTrue (ci.IsReadOnly, "#1");
377                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#2");
378                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#3");
379                         try {
380                                 ci.NumberFormat.NumberGroupSeparator = ",";
381                                 Assert.Fail ("#4");
382                         } catch (InvalidOperationException ex) {
383                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
384                                 Assert.IsNull (ex.InnerException, "#6");
385                                 Assert.IsNotNull (ex.Message, "#7");
386                         }
387                 }
388
389                 [Test] // bug #69652
390                 public void Norwegian ()
391                 {
392                         new CultureInfo ("no");
393                         new CultureInfo ("nb-NO");
394                         new CultureInfo ("nn-NO");
395                 }
396
397                 [Test]
398                 public void NumberFormat_Neutral_Culture ()
399                 {
400                         CultureInfo ci = new CultureInfo ("nl");
401                         try {
402                                 NumberFormatInfo nfi = ci.NumberFormat;
403 #if NET_4_0
404                                 Assert.IsNotNull (nfi, "#1");
405 #else
406                                 Assert.Fail ("#1:" + (nfi != null));
407 #endif
408                         } catch (NotSupportedException ex) {
409                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
410                                 Assert.IsNull (ex.InnerException, "#3");
411                                 Assert.IsNotNull (ex.Message, "#4");
412                         }
413                 }
414
415                 [Test]
416                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
417                 public void GetCultureInfo_Identifier ()
418                 {
419                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
420                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
421                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.LCID);
422                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
423                                 if (culture.IsNeutralCulture)
424                                         continue;
425                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
426                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
427                         }
428                 }
429
430                 [Test]
431                 public void GetCultureInfo_Identifier_Negative ()
432                 {
433                         try {
434                                 CultureInfo.GetCultureInfo (-1);
435                                 Assert.Fail ("#1");
436                         } catch (ArgumentOutOfRangeException ex) {
437                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
438                                 Assert.IsNull (ex.InnerException, "#3");
439                                 Assert.IsNotNull (ex.Message, "#4");
440                                 Assert.IsNotNull (ex.ParamName, "#5");
441                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
442                         }
443                 }
444
445                 [Test]
446                 public void GetCultureInfo_Identifier_NotSupported ()
447                 {
448                         try {
449                                 CultureInfo.GetCultureInfo (666);
450                                 Assert.Fail ("#1");
451                         } catch (ArgumentException ex) {
452 #if NET_4_0
453                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
454 #else
455                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
456 #endif
457                                 Assert.IsNull (ex.InnerException, "#3");
458                                 Assert.IsNotNull (ex.Message, "#4");
459                                 Assert.IsNotNull (ex.ParamName, "#5");
460                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
461                         }
462                 }
463
464                 [Test]
465                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
466                 public void GetCultureInfo_Name ()
467                 {
468                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
469                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
470                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.Name);
471                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
472                                 if (culture.IsNeutralCulture)
473                                         continue;
474                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
475                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
476                         }
477                 }
478
479                 [Test]
480                 public void GetCultureInfo_Name_NotSupported ()
481                 {
482                         try {
483                                 CultureInfo.GetCultureInfo ("666");
484                                 Assert.Fail ("#1");
485                         } catch (ArgumentException ex) {
486 #if NET_4_0
487                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
488 #else
489                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
490 #endif
491                                 Assert.IsNull (ex.InnerException, "#3");
492                                 Assert.IsNotNull (ex.Message, "#4");
493                                 Assert.IsNotNull (ex.ParamName, "#5");
494                                 Assert.AreEqual ("name", ex.ParamName, "#6");
495                         }
496                 }
497
498                 [Test]
499                 public void GetCultureInfo_Name_Null ()
500                 {
501                         try {
502                                 CultureInfo.GetCultureInfo ((string) null);
503                                 Assert.Fail ("#1");
504                         } catch (ArgumentNullException ex) {
505                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
506                                 Assert.IsNull (ex.InnerException, "#3");
507                                 Assert.IsNotNull (ex.Message, "#4");
508                                 Assert.IsNotNull (ex.ParamName, "#5");
509                                 Assert.AreEqual ("name", ex.ParamName, "#6");
510                         }
511                 }
512
513                 [Test]
514                 public void UseUserOverride_CurrentCulture ()
515                 {
516                         CultureInfo ci = CultureInfo.CurrentCulture;
517                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
518                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
519
520                         ci = (CultureInfo) ci.Clone ();
521                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
522                 }
523
524                 [Test]
525                 public void UseUserOverride_CurrentUICulture ()
526                 {
527                         CultureInfo ci = CultureInfo.CurrentCulture;
528                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
529                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
530
531                         ci = (CultureInfo) ci.Clone ();
532                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
533                 }
534
535                 [Test]
536                 public void UseUserOverride_GetCultureInfo ()
537                 {
538                         CultureInfo culture;
539
540                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
541                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
542                                 culture = CultureInfo.GetCultureInfo (ci.Name);
543                                 Assert.IsFalse (culture.UseUserOverride, "#1: " + cultureMsg);
544                                 culture = CultureInfo.GetCultureInfo (ci.LCID);
545                                 Assert.IsFalse (culture.UseUserOverride, "#2: " + cultureMsg);
546                         }
547                 }
548
549                 [Test]
550                 public void UseUserOverride_GetCultures ()
551                 {
552                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
553                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
554                                 if (ci.LCID == CultureInfo.InvariantCulture.LCID)
555                                         Assert.IsFalse (ci.UseUserOverride, cultureMsg);
556                                 else
557                                         Assert.IsTrue (ci.UseUserOverride, cultureMsg);
558                         }
559                 }
560
561                 [Test]
562                 public void UseUserOverride_InvariantCulture ()
563                 {
564                         CultureInfo ci = CultureInfo.InvariantCulture;
565                         Assert.IsFalse (ci.UseUserOverride, "#21");
566
567                         ci = (CultureInfo) ci.Clone ();
568                         Assert.IsFalse (ci.UseUserOverride, "#2");
569                 }
570
571                 [Test]
572                 public void Bug402128 ()
573                 {
574                         var culture = new CultureInfo ("en-US");
575                         var ms = new MemoryStream ();
576                         var formatter = new BinaryFormatter ();
577                         formatter.Serialize (ms, culture);
578                         ms.Seek (0, SeekOrigin.Begin);
579                         var deserializedCulture = (CultureInfo) formatter.Deserialize (ms);
580                 }
581
582                 [Test]
583                 public void ZhHant ()
584                 {
585                         Assert.AreEqual (31748, new CultureInfo ("zh-Hant").LCID);
586                         Assert.AreEqual (31748, CultureInfo.GetCultureInfo ("zh-Hant").LCID);
587                         Assert.AreEqual (31748, new CultureInfo ("zh-CHT").LCID);
588                         Assert.AreEqual (31748, new CultureInfo ("zh-CHT").Parent.LCID);
589                 }
590
591                 [Test]
592                 public void ZhHans ()
593                 {
594                         Assert.AreEqual (4, new CultureInfo ("zh-Hans").LCID);
595                         Assert.AreEqual (4, CultureInfo.GetCultureInfo ("zh-Hans").LCID);
596                         Assert.AreEqual (4, new CultureInfo ("zh-CHS").LCID);
597                         Assert.AreEqual (4, new CultureInfo ("zh-CHS").Parent.LCID);
598                 }
599
600                 [Test]
601                 [SetCulture ("zh-TW")]
602                 public void ParentOfZh ()
603                 {
604                         Assert.AreEqual (31748, CultureInfo.CurrentCulture.Parent.LCID);
605                         Assert.AreEqual (31748, CultureInfo.CurrentCulture.Parent.Parent.LCID);
606                 }
607                 
608                 [Test]
609                 public void CurrentCulture ()
610                 {
611                         Assert.IsNotNull (CultureInfo.CurrentCulture, "CurrentCulture");
612                 }
613                 
614                 [Test]
615 #if NET_4_0
616                 [ExpectedException (typeof (CultureNotFoundException))]
617 #else
618                 [ExpectedException (typeof (ArgumentException))]
619 #endif
620                 public void CultureNotFound ()
621                 {
622                         // that's how the 'locale' gets defined for a device with an English UI
623                         // and it's international settings set for Hong Kong
624                         // https://bugzilla.xamarin.com/show_bug.cgi?id=3471
625                         new CultureInfo ("en-HK");
626                 }
627
628                 [Test]
629                 public void ChineseSimplifiedDontEqual ()
630                 {
631                         CultureInfo zh1 = new CultureInfo ("zh-Hans");
632                         CultureInfo zh2 = new CultureInfo ("zh-CHS");
633
634                         Assert.IsFalse (zh1.Equals (zh2), "#1");
635                         Assert.IsFalse (zh2.Equals (zh1), "#2");
636                 }
637
638 #if NET_4_5
639                 CountdownEvent barrier = new CountdownEvent (3);
640                 AutoResetEvent[] evt = new AutoResetEvent [] { new AutoResetEvent (false), new AutoResetEvent (false), new AutoResetEvent (false)};
641
642                 CultureInfo[] initial_culture = new CultureInfo[4];
643                 CultureInfo[] changed_culture = new CultureInfo[4];
644                 CultureInfo[] changed_culture2 = new CultureInfo[4];
645                 CultureInfo alternative_culture = new CultureInfo("pt-BR");
646
647                 void StepAllPhases (int index)
648                 {
649                         initial_culture [index] = CultureInfo.CurrentCulture;
650                         /*Phase 1 - we witness the original value */
651                         barrier.Signal ();
652
653                         /*Phase 2 - main thread changes culture */
654                         evt [index].WaitOne ();
655
656                         /*Phase 3 - we witness the new value */
657                         changed_culture [index] = CultureInfo.CurrentCulture;
658                         barrier.Signal ();
659
660                         /* Phase 4 - main thread changes culture back */
661                         evt [index].WaitOne ();
662
663                         /*Phase 5 - we witness the new value */
664                         changed_culture2 [index] = CultureInfo.CurrentCulture;
665                         barrier.Signal ();
666                 }
667
668                 void ThreadWithoutChange () {
669                         StepAllPhases (1);
670                 }
671
672                 void ThreadWithChange () {
673                         Thread.CurrentThread.CurrentCulture = alternative_culture;
674                         StepAllPhases (2);
675                 }
676
677                 void ThreadPoolWithoutChange () {
678                         StepAllPhases (3);
679                 }
680
681                 [Test]
682                 public void DefaultThreadCurrentCulture () {
683                         var orig_culture = CultureInfo.CurrentCulture;
684                         var new_culture = new CultureInfo("fr-FR");
685
686                         // The test doesn't work if the current culture is already set
687                         if (orig_culture != CultureInfo.InvariantCulture)
688                                 Assert.Ignore ("The test doesn't work if the current culture is already set.");
689
690                         /* Phase 0 - warm up */
691                         new Thread (ThreadWithoutChange).Start ();
692                         new Thread (ThreadWithChange).Start ();
693                         Action x = ThreadPoolWithoutChange;
694                         x.BeginInvoke (null, null);
695
696                         /* Phase 1 - let everyone witness initial values */
697                         initial_culture [0] = CultureInfo.CurrentCulture;
698                         barrier.Wait ();
699                         barrier.Reset ();
700
701                         /* Phase 2 - change the default culture*/
702                         CultureInfo.DefaultThreadCurrentCulture = new_culture;
703                         evt [0].Set ();
704                         evt [1].Set ();
705                         evt [2].Set ();
706                         /* Phase 3 - let everyone witness the new value */
707                         changed_culture [0] = CultureInfo.CurrentCulture;
708                         barrier.Wait ();
709                         barrier.Reset ();
710
711                         /* Phase 4 - revert the default culture back to null */
712                         CultureInfo.DefaultThreadCurrentCulture = null;
713                         evt [0].Set ();
714                         evt [1].Set ();
715                         evt [2].Set ();
716
717                         /* Phase 5 - let everyone witness the new value */
718                         changed_culture2 [0] = CultureInfo.CurrentCulture;
719                         barrier.Wait ();
720                         barrier.Reset ();
721
722                         CultureInfo.DefaultThreadCurrentCulture = null;
723
724                         Assert.AreEqual (orig_culture, initial_culture [0], "#1");
725                         Assert.AreEqual (orig_culture, initial_culture [1], "#2");
726                         Assert.AreEqual (alternative_culture, initial_culture [2], "#3");
727                         Assert.AreEqual (orig_culture, initial_culture [3], "#4");
728
729                         Assert.AreEqual (new_culture, changed_culture [0], "#5");
730                         Assert.AreEqual (new_culture, changed_culture [1], "#6");
731                         Assert.AreEqual (alternative_culture, changed_culture [2], "#7");
732                         Assert.AreEqual (new_culture, changed_culture [3], "#8");
733
734                         Assert.AreEqual (orig_culture, changed_culture [0], "#9");
735                         Assert.AreEqual (orig_culture, changed_culture2 [1], "#10");
736                         Assert.AreEqual (alternative_culture, changed_culture2 [2], "#11");
737                         Assert.AreEqual (orig_culture, changed_culture2 [3], "#12");
738                 }
739
740                 [Test]
741                 public void DefaultThreadCurrentCultureAndNumberFormaters () {
742                         string us_str = null;
743                         string br_str = null;
744                         var thread = new Thread (() => {
745                                 CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
746                                 us_str = 100000.ToString ("C");
747                                 CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-BR");
748                                 br_str = 100000.ToString ("C");
749                         });
750                         thread.Start ();
751                         thread.Join ();
752                         CultureInfo.DefaultThreadCurrentCulture = null;
753                         Assert.AreEqual ("$100,000.00", us_str, "#1");
754                         Assert.AreEqual ("R$ 100.000,00", br_str, "#2");
755                 }
756 #endif
757         }
758 }