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
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() {
}
Assert.IsTrue (errorThrown, "#F03a");
}
-#if NET_1_1
+
{
bool errorThrown = false;
try {
}
Assert.IsTrue (errorThrown, "#F03b");
}
-#endif
#if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
{
bool errorThrown = false;