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