harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hinde...@apache.org
Subject svn commit: r616719 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Arrays.java test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
Date Wed, 30 Jan 2008 10:46:25 GMT
Author: hindessm
Date: Wed Jan 30 02:46:24 2008
New Revision: 616719

URL: http://svn.apache.org/viewvc?rev=616719&view=rev
Log:
Applied patch for "[#HARMONY-5443] [java][util]
Arrays.binarySearch(Object[],Object) will throw ClassCastException
while RI won't".

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

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java?rev=616719&r1=616718&r2=616719&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java Wed Jan
30 02:46:24 2008
@@ -362,11 +362,11 @@
         if (array.length == 0) {
             return -1;
         }
-        Comparable<Object> key = (Comparable<Object>) object;
+
         int low = 0, mid = 0, high = array.length - 1, result = 0;
         while (low <= high) {
             mid = (low + high) >>> 1;
-            if ((result = key.compareTo(array[mid])) > 0) {
+            if ((result = ((Comparable<Object>)array[mid]).compareTo(object)) <
0){
                 low = mid + 1;
             } else if (result == 0) {
                 return mid;
@@ -374,7 +374,7 @@
                 high = mid - 1;
             }
         }
-        return -mid - (result <= 0 ? 1 : 2);
+        return -mid - (result >= 0 ? 1 : 2);
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java?rev=616719&r1=616718&r2=616719&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/ArraysTest.java
Wed Jan 30 02:46:24 2008
@@ -36,6 +36,12 @@
 		}
 	}
 
+    static class MockComparable implements Comparable{
+        public int compareTo(Object o) {
+            return 0;
+        }
+    }
+    
 	final static int arraySize = 100;
 
 	static Object[] objArray;
@@ -266,6 +272,17 @@
 		assertTrue(
 				"Binary search succeeded for value not present in array 2",
 				Arrays.binarySearch(objectArray, new Integer(arraySize)) == -(arraySize + 1));
+        
+        Object object = new Object();
+        Object[] objects = new MockComparable[] { new MockComparable() };
+        assertEquals("Should always return 0", 0, Arrays.binarySearch(objects, object));
+
+        Object[] string_objects = new String[] { "one" };
+        try {
+            Arrays.binarySearch(string_objects, object);
+        } catch (ClassCastException e) {
+            // Expected
+        }
 	}
 
 	/**



Mime
View raw message