changeset 8279:a8b9c58fbd24

CopyOnWriteArrayList.subList().removeIf was not copying enough elements Contributed-by: Arne Siegel <v.a.ammodytes@googlemail.com>
author akhil
date Sat, 20 Apr 2013 20:42:26 -0700
parents a699ac548600
children 31ee59886c95
files src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java test/java/util/Collection/ListDefaults.java
diffstat 2 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Sat Apr 20 18:56:27 2013 -0400
+++ b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java	Sat Apr 20 20:42:26 2013 -0700
@@ -1524,15 +1524,16 @@
                     newElements[i] = elements[i];
                 }
                 // elements [from, to) are subject to removal
-                int j = from;
+                int j = 0;
                 for (int i = 0; (i < range) && (j < newRange); i++) {
                     i = removeSet.nextClearBit(i);
                     if (i >= range) {
                         break;
                     }
-                    newElements[j++] = elements[from + i];
+                    newElements[from + (j++)] = elements[from + i];
                 }
                 // copy any remaining elements beyond [from, to)
+                j += from;
                 for (int i = to; (i < elements.length) && (j < newSize); i++) {
                     newElements[j++] = elements[i];
                 }
--- a/test/java/util/Collection/ListDefaults.java	Sat Apr 20 18:56:27 2013 -0400
+++ b/test/java/util/Collection/ListDefaults.java	Sat Apr 20 20:42:26 2013 -0700
@@ -22,6 +22,7 @@
  */
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Comparators;
@@ -78,8 +79,8 @@
             (x, y) -> x.intValue() - y.intValue();
 
     private static final int SIZE = 100;
-    private static final int SUBLIST_FROM = 2;
-    private static final int SUBLIST_TO = SIZE - SUBLIST_FROM;
+    private static final int SUBLIST_FROM = 20;
+    private static final int SUBLIST_TO = SIZE - 5;
     private static final int SUBLIST_SIZE = SUBLIST_TO - SUBLIST_FROM;
 
     private static interface Callback {
@@ -481,4 +482,28 @@
         }
     }
 
+    private static final List<Integer> SLICED_EXPECTED = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 8, 9);
+    private static final List<Integer> SLICED_EXPECTED2 = Arrays.asList(0, 1, 2, 5, 6, 7, 8, 9);
+
+    @DataProvider(name="shortIntListProvider", parallel=true)
+    public static Object[][] intListCases() {
+        final Integer[] DATA = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+        final List<Object[]> cases = new LinkedList<>();
+        cases.add(new Object[] { new ArrayList<>(Arrays.asList(DATA)) });
+        cases.add(new Object[] { new LinkedList<>(Arrays.asList(DATA)) });
+        cases.add(new Object[] { new Vector<>(Arrays.asList(DATA)) });
+        cases.add(new Object[] { new CopyOnWriteArrayList<>(Arrays.asList(DATA)) });
+        return cases.toArray(new Object[0][cases.size()]);
+    }
+
+    @Test(dataProvider = "shortIntListProvider")
+    public void testRemoveIfFromSlice(final List<Integer> list) throws Exception {
+        final List<Integer> sublist = list.subList(3, 6);
+        assertTrue(sublist.removeIf(x -> x == 4));
+        CollectionAsserts.assertContents(list, SLICED_EXPECTED);
+
+        final List<Integer> sublist2 = list.subList(2, 5);
+        assertTrue(sublist2.removeIf(x -> x == 3));
+        CollectionAsserts.assertContents(list, SLICED_EXPECTED2);
+    }
 }