[System.Net] Add support for .pac proxy config scripts on mac
[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 #if !NET_4_0
230                 [ExpectedException (typeof (NotSupportedException))]
231 #endif
232                 public void TrySetNeutralCultureNotInvariant ()
233                 {
234                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("ar");
235                 }
236
237                 [Test]
238                 [Category ("TargetJvmNotWorking")] //OptionalCalendars not yet supported for TARGET_JVM.
239                 // make sure that all CultureInfo holds non-null calendars.
240                 public void OptionalCalendars ()
241                 {
242 #if MOBILE
243                         // ensure the linker does not remove them so we can test them
244                         Assert.IsNotNull (typeof (UmAlQuraCalendar), "UmAlQuraCalendar");
245 #endif
246                         foreach (CultureInfo ci in CultureInfo.GetCultures (
247                                 CultureTypes.AllCultures))
248                                 Assert.IsNotNull (ci.OptionalCalendars, String.Format ("{0} {1}",
249                                         ci.LCID, ci.Name));
250                 }
251
252                 [Test] // bug #77347
253                 public void CloneNeutral ()
254                 {
255                         CultureInfo culture = new CultureInfo ("en");
256                         CultureInfo cultureClone = culture.Clone () as CultureInfo;
257                         Assert.IsTrue (culture.Equals (cultureClone));
258                 }
259
260                 [Test]
261                 public void IsNeutral ()
262                 {
263                         var ci = new CultureInfo (0x6C1A);
264                         Assert.IsTrue (ci.IsNeutralCulture, "#1");
265                         Assert.AreEqual ("srp", ci.ThreeLetterISOLanguageName, "#2");
266
267                         ci = new CultureInfo ("en-US");
268                         Assert.IsFalse (ci.IsNeutralCulture, "#1a");
269                         Assert.AreEqual ("eng", ci.ThreeLetterISOLanguageName, "#2a");
270                 }
271
272                 [Test] // bug #81930
273                 public void IsReadOnly ()
274                 {
275                         CultureInfo ci;
276
277                         ci = new CultureInfo ("en-US");
278                         Assert.IsFalse (ci.IsReadOnly, "#A1");
279                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A2");
280                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A3");
281                         ci.NumberFormat.NumberGroupSeparator = ",";
282                         ci.NumberFormat = new NumberFormatInfo ();
283                         ci.DateTimeFormat.DateSeparator = "/";
284                         ci.DateTimeFormat = new DateTimeFormatInfo ();
285                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#A4");
286                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#A5");
287
288                         ci = new CultureInfo (CultureInfo.InvariantCulture.LCID);
289                         Assert.IsFalse (ci.IsReadOnly, "#B1");
290                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B2");
291                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B3");
292                         ci.NumberFormat.NumberGroupSeparator = ",";
293                         ci.NumberFormat = new NumberFormatInfo ();
294                         ci.DateTimeFormat.DateSeparator = "/";
295                         ci.DateTimeFormat = new DateTimeFormatInfo ();
296                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#B4");
297                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#B5");
298
299                         ci = CultureInfo.CurrentCulture;
300                         Assert.IsTrue (ci.IsReadOnly, "#C1:" + ci.DisplayName);
301                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#C2");
302                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#C3");
303                         try {
304                                 ci.NumberFormat.NumberGroupSeparator = ",";
305                                 Assert.Fail ("#C4");
306                         } catch (InvalidOperationException ex) {
307                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C5");
308                                 Assert.IsNull (ex.InnerException, "#C6");
309                                 Assert.IsNotNull (ex.Message, "#C7");
310                         }
311
312                         ci = CultureInfo.CurrentUICulture;
313                         Assert.IsTrue (ci.IsReadOnly, "#D1");
314                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#D2");
315                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#D3");
316                         try {
317                                 ci.NumberFormat.NumberGroupSeparator = ",";
318                                 Assert.Fail ("#D4");
319                         } catch (InvalidOperationException ex) {
320                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D5");
321                                 Assert.IsNull (ex.InnerException, "#D6");
322                                 Assert.IsNotNull (ex.Message, "#D7");
323                         }
324
325                         ci = CultureInfo.InvariantCulture;
326                         Assert.IsTrue (ci.IsReadOnly, "#F1");
327                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#F2");
328                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#F3");
329                         try {
330                                 ci.NumberFormat.NumberGroupSeparator = ",";
331                                 Assert.Fail ("#F4");
332                         } catch (InvalidOperationException ex) {
333                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F5");
334                                 Assert.IsNull (ex.InnerException, "#F6");
335                                 Assert.IsNotNull (ex.Message, "#F7");
336                         }
337
338                         ci = new CultureInfo (string.Empty);
339                         Assert.IsFalse (ci.IsReadOnly, "#G1");
340                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G2");
341                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G3");
342                         ci.NumberFormat.NumberGroupSeparator = ",";
343                         ci.NumberFormat = new NumberFormatInfo ();
344                         ci.DateTimeFormat.DateSeparator = "/";
345                         ci.DateTimeFormat = new DateTimeFormatInfo ();
346                         Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#G4");
347                         Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#G5");
348                 }
349
350                 [Test]
351                 public void IsReadOnly_GetCultures ()
352                 {
353                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
354                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
355                                 Assert.IsFalse (ci.IsReadOnly, "#1:" + cultureMsg);
356                                 if (ci.IsNeutralCulture)
357                                         continue;
358                                 Assert.IsFalse (ci.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
359                                 Assert.IsFalse (ci.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
360                         }
361                 }
362
363                 [Test]
364                 [Category ("NotWorking")]
365                 public void IsReadOnly_InstalledUICulture ()
366                 {
367                         CultureInfo ci = CultureInfo.InstalledUICulture;
368                         Assert.IsTrue (ci.IsReadOnly, "#1");
369                         Assert.IsTrue (ci.NumberFormat.IsReadOnly, "#2");
370                         Assert.IsTrue (ci.DateTimeFormat.IsReadOnly, "#3");
371                         try {
372                                 ci.NumberFormat.NumberGroupSeparator = ",";
373                                 Assert.Fail ("#4");
374                         } catch (InvalidOperationException ex) {
375                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#5");
376                                 Assert.IsNull (ex.InnerException, "#6");
377                                 Assert.IsNotNull (ex.Message, "#7");
378                         }
379                 }
380
381                 [Test] // bug #69652
382                 public void Norwegian ()
383                 {
384                         new CultureInfo ("no");
385                         new CultureInfo ("nb-NO");
386                         new CultureInfo ("nn-NO");
387                 }
388
389                 [Test]
390                 public void NumberFormat_Neutral_Culture ()
391                 {
392                         CultureInfo ci = new CultureInfo ("nl");
393                         try {
394                                 NumberFormatInfo nfi = ci.NumberFormat;
395 #if NET_4_0
396                                 Assert.IsNotNull (nfi, "#1");
397 #else
398                                 Assert.Fail ("#1:" + (nfi != null));
399 #endif
400                         } catch (NotSupportedException ex) {
401                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
402                                 Assert.IsNull (ex.InnerException, "#3");
403                                 Assert.IsNotNull (ex.Message, "#4");
404                         }
405                 }
406
407                 [Test]
408                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
409                 public void GetCultureInfo_Identifier ()
410                 {
411                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
412                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
413                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.LCID);
414                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
415                                 if (culture.IsNeutralCulture)
416                                         continue;
417                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
418                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
419                         }
420                 }
421
422                 [Test]
423                 public void GetCultureInfo_Identifier_Negative ()
424                 {
425                         try {
426                                 CultureInfo.GetCultureInfo (-1);
427                                 Assert.Fail ("#1");
428                         } catch (ArgumentOutOfRangeException ex) {
429                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
430                                 Assert.IsNull (ex.InnerException, "#3");
431                                 Assert.IsNotNull (ex.Message, "#4");
432                                 Assert.IsNotNull (ex.ParamName, "#5");
433                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
434                         }
435                 }
436
437                 [Test]
438                 public void GetCultureInfo_Identifier_NotSupported ()
439                 {
440                         try {
441                                 CultureInfo.GetCultureInfo (666);
442                                 Assert.Fail ("#1");
443                         } catch (ArgumentException ex) {
444 #if NET_4_0
445                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
446 #else
447                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
448 #endif
449                                 Assert.IsNull (ex.InnerException, "#3");
450                                 Assert.IsNotNull (ex.Message, "#4");
451                                 Assert.IsNotNull (ex.ParamName, "#5");
452                                 Assert.AreEqual ("culture", ex.ParamName, "#6");
453                         }
454                 }
455
456                 [Test]
457                 [Category ("NotDotNet")] // On MS, the NumberFormatInfo of the CultureInfo matching the current locale is not read-only
458                 public void GetCultureInfo_Name ()
459                 {
460                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
461                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
462                                 CultureInfo culture = CultureInfo.GetCultureInfo (ci.Name);
463                                 Assert.IsTrue (culture.IsReadOnly, "#1:" + cultureMsg);
464                                 if (culture.IsNeutralCulture)
465                                         continue;
466                                 Assert.IsTrue (culture.NumberFormat.IsReadOnly, "#2:" + cultureMsg);
467                                 Assert.IsTrue (culture.DateTimeFormat.IsReadOnly, "#3:" + cultureMsg);
468                         }
469                 }
470
471                 [Test]
472                 public void GetCultureInfo_Name_NotSupported ()
473                 {
474                         try {
475                                 CultureInfo.GetCultureInfo ("666");
476                                 Assert.Fail ("#1");
477                         } catch (ArgumentException ex) {
478 #if NET_4_0
479                                 Assert.AreEqual (typeof (CultureNotFoundException), ex.GetType (), "#2");
480 #else
481                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
482 #endif
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 GetCultureInfo_Name_Null ()
492                 {
493                         try {
494                                 CultureInfo.GetCultureInfo ((string) null);
495                                 Assert.Fail ("#1");
496                         } catch (ArgumentNullException ex) {
497                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
498                                 Assert.IsNull (ex.InnerException, "#3");
499                                 Assert.IsNotNull (ex.Message, "#4");
500                                 Assert.IsNotNull (ex.ParamName, "#5");
501                                 Assert.AreEqual ("name", ex.ParamName, "#6");
502                         }
503                 }
504
505                 [Test]
506                 public void UseUserOverride_CurrentCulture ()
507                 {
508                         CultureInfo ci = CultureInfo.CurrentCulture;
509                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
510                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
511
512                         ci = (CultureInfo) ci.Clone ();
513                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
514                 }
515
516                 [Test]
517                 public void UseUserOverride_CurrentUICulture ()
518                 {
519                         CultureInfo ci = CultureInfo.CurrentCulture;
520                         bool expected = (ci.LCID != CultureInfo.InvariantCulture.LCID);
521                         Assert.AreEqual (expected, ci.UseUserOverride, "#1");
522
523                         ci = (CultureInfo) ci.Clone ();
524                         Assert.AreEqual (expected, ci.UseUserOverride, "#2");
525                 }
526
527                 [Test]
528                 public void UseUserOverride_GetCultureInfo ()
529                 {
530                         CultureInfo culture;
531
532                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
533                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
534                                 culture = CultureInfo.GetCultureInfo (ci.Name);
535                                 Assert.IsFalse (culture.UseUserOverride, "#1: " + cultureMsg);
536                                 culture = CultureInfo.GetCultureInfo (ci.LCID);
537                                 Assert.IsFalse (culture.UseUserOverride, "#2: " + cultureMsg);
538                         }
539                 }
540
541                 [Test]
542                 public void UseUserOverride_GetCultures ()
543                 {
544                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
545                                 string cultureMsg = String.Format ("{0} {1}", ci.LCID, ci.Name);
546                                 if (ci.LCID == CultureInfo.InvariantCulture.LCID)
547                                         Assert.IsFalse (ci.UseUserOverride, cultureMsg);
548                                 else
549                                         Assert.IsTrue (ci.UseUserOverride, cultureMsg);
550                         }
551                 }
552
553                 [Test]
554                 public void UseUserOverride_InvariantCulture ()
555                 {
556                         CultureInfo ci = CultureInfo.InvariantCulture;
557                         Assert.IsFalse (ci.UseUserOverride, "#21");
558
559                         ci = (CultureInfo) ci.Clone ();
560                         Assert.IsFalse (ci.UseUserOverride, "#2");
561                 }
562
563                 [Test]
564                 public void Bug402128 ()
565                 {
566                         var culture = new CultureInfo ("en-US");
567                         var ms = new MemoryStream ();
568                         var formatter = new BinaryFormatter ();
569                         formatter.Serialize (ms, culture);
570                         ms.Seek (0, SeekOrigin.Begin);
571                         var deserializedCulture = (CultureInfo) formatter.Deserialize (ms);
572                 }
573
574                 [Test]
575                 public void ZhHant ()
576                 {
577                         Assert.AreEqual (31748, new CultureInfo ("zh-Hant").LCID);
578                         Assert.AreEqual (31748, CultureInfo.GetCultureInfo ("zh-Hant").LCID);
579                         Assert.AreEqual (31748, new CultureInfo ("zh-CHT").LCID);
580                         Assert.AreEqual (31748, new CultureInfo ("zh-CHT").Parent.LCID);
581                 }
582
583                 [Test]
584                 [SetCulture ("zh-TW")]
585                 public void ParentOfZh ()
586                 {
587                         Assert.AreEqual (31748, CultureInfo.CurrentCulture.Parent.LCID);
588                         Assert.AreEqual (31748, CultureInfo.CurrentCulture.Parent.Parent.LCID);
589                 }
590                 
591                 [Test]
592                 public void CurrentCulture ()
593                 {
594                         Assert.IsNotNull (CultureInfo.CurrentCulture, "CurrentCulture");
595                 }
596                 
597                 [Test]
598 #if NET_4_0
599                 [ExpectedException (typeof (CultureNotFoundException))]
600 #else
601                 [ExpectedException (typeof (ArgumentException))]
602 #endif
603                 public void CultureNotFound ()
604                 {
605                         // that's how the 'locale' gets defined for a device with an English UI
606                         // and it's international settings set for Hong Kong
607                         // https://bugzilla.xamarin.com/show_bug.cgi?id=3471
608                         new CultureInfo ("en-HK");
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 }