[corlib] Hashtable constructor with equality comparer needs to use it during initiali...
authorMarek Safar <marek.safar@gmail.com>
Wed, 14 Jan 2015 22:24:06 +0000 (23:24 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 14 Jan 2015 22:25:32 +0000 (23:25 +0100)
mcs/class/corlib/System.Collections/Hashtable.cs
mcs/class/corlib/Test/System.Collections/HashtableTest.cs

index a1774d0580c695b7e2bafb478b05d7ee4c97f15d..e5b19fbfc812bb32d3d1ffe4698f7a57fc61275d 100644 (file)
@@ -207,9 +207,15 @@ namespace System.Collections {
                {
                }
 
-               public Hashtable (IDictionary d, float loadFactor, IEqualityComparer equalityComparer) : this (d, loadFactor)
+               public Hashtable (IDictionary d, float loadFactor, IEqualityComparer equalityComparer) : this (d == null ? 0 : d.Count, loadFactor, equalityComparer)
                {
-                       this.equalityComparer = equalityComparer;
+                       if (d  ==  null)
+                               throw new ArgumentNullException ("d");
+
+                       IDictionaryEnumerator it = d.GetEnumerator ();
+                       while (it.MoveNext ()) {
+                               Add (it.Key, it.Value);
+                       }
                }
 
                public Hashtable (IEqualityComparer equalityComparer) : this (1, 1.0f, equalityComparer)
index 544ade9f826aa843aaf821646ddfedeb83e6227d..8afc14bffc375fa5fe31e20f17aaa8733ffeaec8 100644 (file)
@@ -812,6 +812,19 @@ public class HashtableTest {
                        dd.Remove (v);\r
                }\r
        }\r
+\r
+       [Test]\r
+       public void HashtableCopyWithCustomComparer ()\r
+       {\r
+               var ht = new Hashtable ();\r
+               ht.Add ("a", "b");\r
+               try {\r
+                       new Hashtable (ht, new IEqualityComparer_ApplicationException ());\r
+                       Assert.Fail ("custom comparer not used");\r
+               } catch (ApplicationException) {\r
+\r
+               }\r
+       }\r
 }\r
 \r
 class IDHashtable : Hashtable {\r
@@ -857,5 +870,17 @@ public class Bug :ISerializable {
        }\r
 };\r
 \r
+       class IEqualityComparer_ApplicationException : IEqualityComparer\r
+       {\r
+               public new bool Equals (object x, object y)\r
+               {\r
+                       return false;\r
+               }\r
+\r
+               public int GetHashCode (object obj)\r
+               {\r
+                       throw new ApplicationException ();\r
+               }\r
+       }\r
 \r
 }\r