Fix array binary comparer values ordering. Fixes #5409
authorMarek Safar <marek.safar@gmail.com>
Wed, 30 May 2012 13:30:48 +0000 (14:30 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 30 May 2012 13:30:48 +0000 (14:30 +0100)
mcs/class/corlib/System/Array.cs
mcs/class/corlib/Test/System/ArrayTest.cs

index 5f3696e7af373ea8e458d867ef67539aeeae58fe..18865297cfecf64c45f072da869b6b81388fd6a2 100644 (file)
@@ -2723,11 +2723,12 @@ namespace System
                                while (iMin <= iMax) {
                                        // Be careful with overflows
                                        int iMid = iMin + ((iMax - iMin) / 2);
-                                       iCmp = comparer.Compare (value, array [iMid]);
+                                       iCmp = comparer.Compare (array [iMid], value);
 
                                        if (iCmp == 0)
                                                return iMid;
-                                       else if (iCmp < 0)
+
+                                       if (iCmp > 0)
                                                iMax = iMid - 1;
                                        else
                                                iMin = iMid + 1; // compensate for the rounding down
index f53eb6dd25d062f8fed18b780db13b1e027bd2dd..b8148a1260d805aca4c608cd4a99a85f49672012 100644 (file)
@@ -225,7 +225,23 @@ public class ArrayTest
                Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
        }
 
-       // TODO - testBinarySearch with explicit IComparer args
+       class TestComparer7 : IComparer<int>
+       {
+               public int Compare (int x, int y)
+               {
+                       if (y != 7)
+                               throw new ApplicationException ();
+
+                       return x.CompareTo (y);
+               }
+       }
+
+       [Test]
+       public void BinarySearch_WithComparer ()
+       {
+               var a = new int[] { 2, 6, 9 };
+               Assert.AreEqual (-3, Array.BinarySearch (a, 7, new TestComparer7 ()));
+       }
 
        [Test]
        public void TestClear() {
@@ -657,7 +673,7 @@ public class ArrayTest
                        }
                        Assert.IsTrue (errorThrown, "#F03a");
                }
-#if NET_1_1
+
                {
                        bool errorThrown = false;
                        try {
@@ -667,7 +683,6 @@ public class ArrayTest
                        }
                        Assert.IsTrue (errorThrown, "#F03b");
                }
-#endif
 #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
                {
                        bool errorThrown = false;