harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r933872 - in /harmony/enhanced/java/branches/java6/classlib/modules/luni/src: main/java/java/util/Arrays.java test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
Date Wed, 14 Apr 2010 08:05:57 GMT
Author: zhoukevin
Date: Wed Apr 14 08:05:57 2010
New Revision: 933872

URL: http://svn.apache.org/viewvc?rev=933872&view=rev
Log:
Apply patch for HARMONY-6139: java.util.Arrays.binarySearch(short[] array, int startIndex,
int endIndex, short value) should return (-(insertion point) - 1) rather than -1 if value
is not found in the given range

Modified:
    harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/util/Arrays.java
    harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java

Modified: harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/util/Arrays.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/util/Arrays.java?rev=933872&r1=933871&r2=933872&view=diff
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/util/Arrays.java
(original)
+++ harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/util/Arrays.java
Wed Apr 14 08:05:57 2010
@@ -355,7 +355,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (value < array[mid] ? 1 : 2);
     }
@@ -396,7 +402,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (value < array[mid] ? 1 : 2);
     }
@@ -438,7 +450,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (lessThan(value, array[mid]) ? 1 : 2);
     }
@@ -480,7 +498,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (lessThan(value, array[mid]) ? 1 : 2);
     }
@@ -521,7 +545,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (value < array[mid] ? 1 : 2);
     }
@@ -562,7 +592,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (value < array[mid] ? 1 : 2);
     }
@@ -599,7 +635,7 @@ public class Arrays {
             return -1;
         }
 
-        int low = startIndex, mid = 0, high = endIndex - 1, result = 0;
+        int low = startIndex, mid = -1, high = endIndex - 1, result = 0;
         while (low <= high) {
             mid = (low + high) >>> 1;
             if ((result = ((Comparable<Object>)array[mid]).compareTo(object)) <
0){
@@ -610,6 +646,15 @@ public class Arrays {
                 high = mid - 1;
             }
         }
+        if (mid < 0) {
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (((Comparable<Object>) object).compareTo(array[index]) < 0) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
+        }
         return -mid - (result >= 0 ? 1 : 2);
     }
 
@@ -648,7 +693,7 @@ public class Arrays {
             return binarySearch(array, startIndex, endIndex, object);
         }
 
-        int low = startIndex, mid = 0, high = endIndex - 1, result = 0;
+        int low = startIndex, mid = -1, high = endIndex - 1, result = 0;
         while (low <= high) {
             mid = (low + high) >>> 1;
             if ((result = comparator.compare(array[mid], object)) < 0) {
@@ -659,6 +704,15 @@ public class Arrays {
                 high = mid - 1;
             }
         }
+        if (mid < 0) {
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (comparator.compare(object, array[index]) < 0) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
+        }
         return -mid - (result >= 0 ? 1 : 2);
     }
 
@@ -698,7 +752,13 @@ public class Arrays {
             }
         }
         if (mid < 0) {
-            return -1;
+            int insertPoint = endIndex;
+            for (int index = startIndex; index < endIndex; index++) {
+                if (value < array[index]) {
+                    insertPoint = index;
+                }
+            }
+            return -insertPoint - 1;
         }
         return -mid - (value < array[mid] ? 1 : 2);
     }

Modified: harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java?rev=933872&r1=933871&r2=933872&view=diff
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
(original)
+++ harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
Wed Apr 14 08:05:57 2010
@@ -330,6 +330,90 @@ public class ArraysTest extends junit.fr
 					Arrays.binarySearch(shortArray, (short) (counter - 50)) == counter);
 	}
 
+    public void test_Arrays_binaraySearch_byte() {
+        assertEquals(-1, Arrays.binarySearch(new byte[] { '0' }, 0, 0,
+                (byte) '1'));
+        assertEquals(-2, Arrays.binarySearch(new byte[] { '0' }, 1, 1,
+                (byte) '1'));
+        assertEquals(-2, Arrays.binarySearch(new byte[] { '0', '1' }, 1, 1,
+                (byte) '2'));
+        assertEquals(-3, Arrays.binarySearch(new byte[] { '0', '1' }, 2, 2,
+                (byte) '2'));
+    }
+
+    public void test_Arrays_binaraySearch_char() {
+        assertEquals(-1, Arrays.binarySearch(new char[] { '0' }, 0, 0, '1'));
+        assertEquals(-2, Arrays.binarySearch(new char[] { '0' }, 1, 1, '1'));
+        assertEquals(-2, Arrays
+                .binarySearch(new char[] { '0', '1' }, 1, 1, '2'));
+        assertEquals(-3, Arrays
+                .binarySearch(new char[] { '0', '1' }, 2, 2, '2'));
+    }
+
+    public void test_Arrays_binaraySearch_float() {
+        assertEquals(-1, Arrays.binarySearch(new float[] { -1.0f }, 0, 0, 0.0f));
+        assertEquals(-2, Arrays.binarySearch(new float[] { -1.0f }, 1, 1, 0.0f));
+        assertEquals(-2, Arrays.binarySearch(new float[] { -1.0f, 0f }, 1, 1,
+                1f));
+        assertEquals(-3, Arrays.binarySearch(new float[] { -1.0f, 0f }, 2, 2,
+                1f));
+    }
+
+    public void test_Arrays_binaraySearch_double() {
+        assertEquals(-1, Arrays.binarySearch(new double[] { -1.0 }, 0, 0, 0.0));
+        assertEquals(-2, Arrays.binarySearch(new double[] { -1.0 }, 1, 1, 0.0));
+        assertEquals(-2, Arrays.binarySearch(new double[] { -1.0, 0 }, 1, 1, 1));
+        assertEquals(-3, Arrays.binarySearch(new double[] { -1.0, 0 }, 2, 2, 1));
+    }
+
+    public void test_Arrays_binaraySearch_int() {
+        assertEquals(-1, Arrays.binarySearch(new int[] { -1 }, 0, 0, 0));
+        assertEquals(-2, Arrays.binarySearch(new int[] { -1 }, 1, 1, 0));
+        assertEquals(-2, Arrays.binarySearch(new int[] { -1, 0 }, 1, 1, 1));
+        assertEquals(-3, Arrays.binarySearch(new int[] { -1, 0 }, 2, 2, 1));
+    }
+
+    public void test_Arrays_binaraySearch_long() {
+        assertEquals(-1, Arrays.binarySearch(new long[] { -1l }, 0, 0, 0l));
+        assertEquals(-2, Arrays.binarySearch(new long[] { -1l }, 1, 1, 0l));
+        assertEquals(-2, Arrays.binarySearch(new long[] { -1l, 0l }, 1, 1, 1l));
+        assertEquals(-3, Arrays.binarySearch(new long[] { -1l, 0l }, 2, 2, 1l));
+    }
+
+    public void test_Arrays_binaraySearch_short() {
+        assertEquals(-1, Arrays.binarySearch(new short[] { (short) -1 }, 0, 0,
+                (short) 0));
+        assertEquals(-2, Arrays.binarySearch(new short[] { (short) -1 }, 1, 1,
+                (short) 0));
+        assertEquals(-2, Arrays.binarySearch(new short[] { (short) -1,
+                (short) 0 }, 1, 1, (short) 1));
+        assertEquals(-3, Arrays.binarySearch(new short[] { (short) -1,
+                (short) 0 }, 2, 2, (short) 1));
+    }
+
+    public void test_Arrays_binaraySearch_Object() {
+        assertEquals(-1, Arrays.binarySearch(new Object[] { new Integer(-1) },
+                0, 0, new Integer(0)));
+        assertEquals(-2, Arrays.binarySearch(new Object[] { new Integer(-1) },
+                1, 1, new Integer(0)));
+        assertEquals(-2, Arrays.binarySearch(new Object[] { new Integer(-1),
+                new Integer(0) }, 1, 1, new Integer(1)));
+        assertEquals(-3, Arrays.binarySearch(new Object[] { new Integer(-1),
+                new Integer(0) }, 2, 2, new Integer(1)));
+    }
+
+    public void test_Arrays_binaraySearch_T() {
+        ReversedIntegerComparator reversedComparator = new ReversedIntegerComparator();
+        assertEquals(-1, Arrays.binarySearch(new Integer[] { new Integer(-1) },
+                0, 0, new Integer(0), reversedComparator));
+        assertEquals(-2, Arrays.binarySearch(new Integer[] { new Integer(-1) },
+                1, 1, new Integer(0), reversedComparator));
+        assertEquals(-2, Arrays.binarySearch(new Integer[] { new Integer(-1),
+                new Integer(0) }, 1, 1, new Integer(1), reversedComparator));
+        assertEquals(-3, Arrays.binarySearch(new Integer[] { new Integer(-1),
+                new Integer(0) }, 2, 2, new Integer(1), reversedComparator));
+    }
+
 	/**
 	 * @tests java.util.Arrays#fill(byte[], byte)
 	 */



Mime
View raw message