Use HashSet.Comparer for set methods with IEnumerable collection
authorEnrico Sada <enrico.sada@gmail.com>
Sat, 4 Aug 2012 17:06:19 +0000 (19:06 +0200)
committerEnrico Sada <enrico.sada@gmail.com>
Sat, 4 Aug 2012 17:06:19 +0000 (19:06 +0200)
fix bug 6373 ( https://bugzilla.xamarin.com/show_bug.cgi?id=6373 )

mcs/class/System.Core/System.Collections.Generic/HashSet.cs
mcs/class/System.Core/Test/System.Collections.Generic/HashSetTest.cs

index 39047dcd35804ffcc0443e90c7c3f49f7e7a9337..a417557d035886e547a32e820076df09524c0111 100644 (file)
@@ -438,7 +438,7 @@ namespace System.Collections.Generic {
                {
                        var set = enumerable as HashSet<T>;
                        if (set == null || !Comparer.Equals (set.Comparer))
-                               set = new HashSet<T> (enumerable);
+                               set = new HashSet<T> (enumerable, Comparer);
 
                        return set;
                }
index 1e515167531e29c9233189c9a540b4ca89694528..667db09a5215b3531344323b8bb3c070a7fcc174 100644 (file)
@@ -172,6 +172,20 @@ namespace MonoTests.System.Collections.Generic {
                        AssertContainsOnly (result, set);
                }
 
+               [Test]
+               public void TestIntersectWithComparer ()
+               {
+                       var data = new[] {"a", "b", "C", "d", "E"};
+                       var other = new[] {"a", "B", "e"};
+                       var result = new[] {"a", "b", "E"};
+
+                       var set = new HashSet<string> (data, StringComparer.OrdinalIgnoreCase);
+
+                       set.IntersectWith (other);
+
+                       AssertContainsOnly (result, set);
+               }
+
                [Test]
                public void TestExceptWith ()
                {
@@ -235,6 +249,18 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.IsFalse (set.IsSubsetOf (other3));
                }
 
+               [Test]
+               public void TestSubsetOfComparer ()
+               {
+                       var data = new[] { "abc", "DF", "gHIl" };
+
+                       var other1 = new[] { "pqR", "ABC", "ghil", "dF", "lmn" };
+
+                       var set = new HashSet<string> (data, StringComparer.OrdinalIgnoreCase);
+
+                       Assert.IsTrue (set.IsSubsetOf (other1));
+               }
+
                [Test]
                public void TestProperSubsetOf ()
                {
@@ -250,6 +276,18 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.IsFalse (set.IsProperSubsetOf (other3));
                }
 
+               [Test]
+               public void TestProperSubsetOfComparer ()
+               {
+                       var data = new[] { "abc", "DF", "gHIl" };
+
+                       var other1 = new[] { "pqR", "ABC", "ghil", "dF", "lmn" };
+
+                       var set = new HashSet<string> (data, StringComparer.OrdinalIgnoreCase);
+
+                       Assert.IsTrue (set.IsProperSubsetOf (other1));
+               }
+
                [Test]
                public void TestSupersetOf ()
                {
@@ -265,6 +303,20 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.IsFalse (set.IsSupersetOf (other3));
                }
 
+               [Test]
+               public void TestSupersetOfComparer ()
+               {
+                       var data = new[] {"a", "B", "c", "D"};
+
+                       var other1 = new[] {"A", "a", "C", "c"};
+                       var other2 = new[] {"A", "a", "B", "D", "C", "c"};
+
+                       var set = new HashSet<string> (data, StringComparer.OrdinalIgnoreCase);
+
+                       Assert.IsTrue (set.IsSupersetOf (other1));
+                       Assert.IsTrue (set.IsSupersetOf (other2));
+               }
+
                [Test]
                public void TestProperSupersetOf ()
                {
@@ -280,6 +332,20 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.IsFalse (set.IsProperSupersetOf (other3));
                }
 
+               [Test]
+               public void TestProperSupersetOfComparer ()
+               {
+                       var data = new[] { "a", "B", "c", "D" };
+
+                       var other1 = new[] { "A", "a", "d", "D" };
+                       var other2 = new[] { "A", "a", "B", "D", "C", "c" };
+
+                       var set = new HashSet<string> (data, StringComparer.OrdinalIgnoreCase);
+
+                       Assert.IsTrue (set.IsProperSupersetOf (other1));
+                       Assert.IsFalse (set.IsProperSupersetOf (other2));
+               }
+
                [Test]
                public void TestSetEquals ()
                {
@@ -300,6 +366,20 @@ namespace MonoTests.System.Collections.Generic {
                        Assert.IsFalse (set.SetEquals (other5));
                }
 
+               [Test]
+               public void TestSetEqualsComparer ()
+               {
+                       var data = new[] { "abc", "DF", "gHIl" };
+
+                       var other1 = new[] { "ABC", "DF", "GHIL" };
+                       var other2 = new[] { "ABC", "aBc", "DF", "GHIL", "ghil" };
+
+                       var set = new HashSet<string>(data, StringComparer.OrdinalIgnoreCase);
+
+                       Assert.IsTrue (set.SetEquals (other1));
+                       Assert.IsTrue (set.SetEquals (other2));
+               }
+
                [Test]
                public void TestCopyToFull ()
                {