Author: gert
Date: 2007-07-05 13:39:44 -0400 (Thu, 05 Jul 2007)
New Revision: 81412

Modified:
   trunk/mcs/class/corlib/System.Collections/CaseInsensitiveHashCodeProvider.cs
   trunk/mcs/class/corlib/System.Collections/ChangeLog
   
trunk/mcs/class/corlib/Test/System.Collections/CaseInsensitiveHashCodeProviderTest.cs
   trunk/mcs/class/corlib/Test/System.Collections/ChangeLog
Log:
* CaseInsensitiveHashCodeProvider.cs: Lazy init Default instance and
reconstruct it if CurrentCulture changes.
* CaseInsensitiveHashCodeProviderTest.cs: Reworked test for Default
instance to pass on both Mono and MS. Added separate tests that
verify the specific Mono and MS behavior.


Modified: 
trunk/mcs/class/corlib/System.Collections/CaseInsensitiveHashCodeProvider.cs
===================================================================
--- 
trunk/mcs/class/corlib/System.Collections/CaseInsensitiveHashCodeProvider.cs    
    2007-07-05 17:39:41 UTC (rev 81411)
+++ 
trunk/mcs/class/corlib/System.Collections/CaseInsensitiveHashCodeProvider.cs    
    2007-07-05 17:39:44 UTC (rev 81412)
@@ -42,6 +42,8 @@
        {
                static readonly CaseInsensitiveHashCodeProvider 
singletonInvariant = new CaseInsensitiveHashCodeProvider (
                        CultureInfo.InvariantCulture);
+               static CaseInsensitiveHashCodeProvider singleton;
+               static readonly object sync = new object ();
 
                TextInfo m_text; // must match MS name for serialization
 
@@ -67,7 +69,20 @@
 
                public static CaseInsensitiveHashCodeProvider Default {
                        get {
-                               return new CaseInsensitiveHashCodeProvider ();
+                               // MS actually constructs a new instance on 
each call, for
+                               // performance reasons we're only constructing 
a new instance
+                               // if the CurrentCulture changes
+                               lock (sync) {
+                                       if (singleton == null) {
+                                               singleton = new 
CaseInsensitiveHashCodeProvider ();
+                                       } else if (singleton.m_text == null) {
+                                               if 
(CultureInfo.CurrentCulture.LCID != CultureInfo.InvariantCulture.LCID)
+                                                       singleton = new 
CaseInsensitiveHashCodeProvider ();
+                                       } else if (singleton.m_text.LCID != 
CultureInfo.CurrentCulture.LCID) {
+                                               singleton = new 
CaseInsensitiveHashCodeProvider ();
+                                       }
+                                       return singleton;
+                               }
                        }
                }
 

Modified: trunk/mcs/class/corlib/System.Collections/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Collections/ChangeLog 2007-07-05 17:39:41 UTC 
(rev 81411)
+++ trunk/mcs/class/corlib/System.Collections/ChangeLog 2007-07-05 17:39:44 UTC 
(rev 81412)
@@ -1,5 +1,10 @@
 2007-07-05  Gert Driesen  <[EMAIL PROTECTED]>
 
+       * CaseInsensitiveHashCodeProvider.cs: Lazy init Default instance and
+       reconstruct it if CurrentCulture changes.
+
+2007-07-05  Gert Driesen  <[EMAIL PROTECTED]>
+
        * CaseInsensitiveHashCodeProvider.cs: In default ctor, do not save
        TextInfo if current culture is invariant. Remoted private ctor.
        In Default, always construct new instance since the current culture

Modified: 
trunk/mcs/class/corlib/Test/System.Collections/CaseInsensitiveHashCodeProviderTest.cs
===================================================================
--- 
trunk/mcs/class/corlib/Test/System.Collections/CaseInsensitiveHashCodeProviderTest.cs
       2007-07-05 17:39:41 UTC (rev 81411)
+++ 
trunk/mcs/class/corlib/Test/System.Collections/CaseInsensitiveHashCodeProviderTest.cs
       2007-07-05 17:39:44 UTC (rev 81412)
@@ -15,11 +15,25 @@
 
 using NUnit.Framework;
 
-namespace MonoTests.System.Collections {
-
+namespace MonoTests.System.Collections
+{
        [TestFixture]
        public class CaseInsensitiveHashCodeProviderTest
        {
+               private CultureInfo old_culture;
+
+               [SetUp]
+               public void SetUp ()
+               {
+                       old_culture = Thread.CurrentThread.CurrentCulture;
+               }
+
+               [TearDown]
+               public void TearDown ()
+               {
+                       Thread.CurrentThread.CurrentCulture = old_culture;
+               }
+
                [Test]
                public void Default ()
                {
@@ -32,12 +46,33 @@
 
                        Assert.AreEqual (h1, h2, "#1");
 
-                       // Default always returns new instance
+                       Thread.CurrentThread.CurrentCulture = new CultureInfo 
("en-US");
                        CaseInsensitiveHashCodeProvider cih1 = 
CaseInsensitiveHashCodeProvider.Default;
+                       Thread.CurrentThread.CurrentCulture = new CultureInfo 
("nl-BE");
                        CaseInsensitiveHashCodeProvider cih2 = 
CaseInsensitiveHashCodeProvider.Default;
                        Assert.IsFalse (object.ReferenceEquals (cih1, cih2), 
"#2");
                }
 
+               [Test]
+               [Category ("NotDotNet")]
+               public void Default_Mono ()
+               {
+                       // we return same instance if current culture did not 
change
+                       CaseInsensitiveHashCodeProvider cih1 = 
CaseInsensitiveHashCodeProvider.Default;
+                       CaseInsensitiveHashCodeProvider cih2 = 
CaseInsensitiveHashCodeProvider.Default;
+                       Assert.IsTrue (object.ReferenceEquals (cih1, cih2));
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void Default_MS ()
+               {
+                       // MS always returns new instance
+                       CaseInsensitiveHashCodeProvider cih1 = 
CaseInsensitiveHashCodeProvider.Default;
+                       CaseInsensitiveHashCodeProvider cih2 = 
CaseInsensitiveHashCodeProvider.Default;
+                       Assert.IsFalse (object.ReferenceEquals (cih1, cih2));
+               }
+
 #if NET_2_0
                [Test]
                public void DefaultInvariant ()

Modified: trunk/mcs/class/corlib/Test/System.Collections/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.Collections/ChangeLog    2007-07-05 
17:39:41 UTC (rev 81411)
+++ trunk/mcs/class/corlib/Test/System.Collections/ChangeLog    2007-07-05 
17:39:44 UTC (rev 81412)
@@ -1,5 +1,11 @@
 2007-07-05  Gert Driesen  <[EMAIL PROTECTED]>
 
+       * CaseInsensitiveHashCodeProviderTest.cs: Reworked test for Default
+       instance to pass on both Mono and MS. Added separate tests that
+       verify the specific Mono and MS behavior.
+
+2007-07-05  Gert Driesen  <[EMAIL PROTECTED]>
+
        * CaseInsensitiveHashCodeProviderTest.cs: Improved test for
        Default property to show the a new intance is constructed. Added test
        for DefaultInvariant. Improved serialization tests. Added ctor test

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to