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