harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r811363 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Collections.java test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java
Date Fri, 04 Sep 2009 12:30:31 GMT
Author: tellison
Date: Fri Sep  4 12:30:31 2009
New Revision: 811363

URL: http://svn.apache.org/viewvc?rev=811363&view=rev
Log:
Additional fix for HARMONY-6323 ([classlib] [luni] Simplify and speed-up collection shuffle,
add a non-probabilistic test)
and add back test cases.

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

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java?rev=811363&r1=811362&r2=811363&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
Fri Sep  4 12:30:31 2009
@@ -1841,16 +1841,16 @@
      */
     public static void shuffle(List<?> list, Random random) {
         @SuppressWarnings("unchecked") // we won't put foreign objects in
-        List<Object> objectList = (List<Object>) list;
+        final List<Object> objectList = (List<Object>) list;
 
         if (list instanceof RandomAccess) {
-            for (int i = objectList.size() - 1; i > 1; i--) {
+            for (int i = objectList.size() - 1; i > 0; i--) {
                 int index = random.nextInt(i + 1);
                 objectList.set(index, objectList.set(i, objectList.get(index)));
             }
         } else {
-            Object[] array = list.toArray();
-            for (int i = array.length - 1; i > 1; i--) {
+            Object[] array = objectList.toArray();
+            for (int i = array.length - 1; i > 0; i--) {
                 int index = random.nextInt(i + 1);
                 Object temp = array[i];
                 array[i] = array[index];

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java?rev=811363&r1=811362&r2=811363&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java
Fri Sep  4 12:30:31 2009
@@ -37,6 +37,7 @@
 import java.util.SortedSet;
 import java.util.TreeMap;
 import java.util.TreeSet;
+import java.util.Arrays;
 
 import org.apache.harmony.luni.internal.nls.Messages;
 
@@ -586,6 +587,19 @@
 		testShuffle(ll2, "Random Access", false);
 	}
 
+    public void testShuffleRandomAccessWithSeededRandom() {
+        List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F", "G");
+        Collections.shuffle(list, new Random(0));
+        assertEquals(Arrays.asList("B", "A", "D", "C", "G", "E", "F"), list);
+    }
+
+    public void testShuffleWithSeededRandom() {
+        List<String> list = new LinkedList<String>(Arrays.asList(
+                "A", "B", "C", "D", "E", "F", "G"));
+        Collections.shuffle(list, new Random(0));
+        assertEquals(Arrays.asList("B", "A", "D", "C", "G", "E", "F"), list);
+    }
+
 	private void testShuffle(List list, String type, boolean random) {
 		boolean sorted = true;
 		boolean allMatch = true;



Mime
View raw message