test/java/util/Collection/ListDefaults.java

Print this page
rev 7994 : 8024291: Rename Collection.removeIf(Predicate) back to removeAll(Predicate)
Reviewed-by: duke


 110 
 111         cases.add(new Object[] { new ArrayList(){{add(42);}} });
 112         cases.add(new Object[] { new LinkedList(){{add(42);}} });
 113         cases.add(new Object[] { new Vector(){{add(42);}} });
 114         cases.add(new Object[] { new Stack(){{add(42);}} });
 115         cases.add(new Object[] { new CopyOnWriteArrayList(){{add(42);}} });
 116         return cases.toArray(new Object[0][cases.size()]);
 117     }
 118 
 119     @Test(dataProvider = "listProvider")
 120     public void testProvidedWithNull(final List<Integer> list) throws Exception {
 121         try {
 122             list.forEach(null);
 123             fail("expected NPE not thrown");
 124         } catch (NullPointerException npe) {}
 125         try {
 126             list.replaceAll(null);
 127             fail("expected NPE not thrown");
 128         } catch (NullPointerException npe) {}
 129         try {
 130             list.removeIf(null);
 131             fail("expected NPE not thrown");
 132         } catch (NullPointerException npe) {}
 133         try {
 134             list.sort(null);
 135         } catch (Throwable t) {
 136             fail("Exception not expected: " + t);
 137         }
 138     }
 139 
 140     @Test
 141     public void testForEach() throws Exception {
 142         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 143         for (final CollectionSupplier.TestCase test : supplier.get()) {
 144             final List<Integer> original = ((List<Integer>) test.original);
 145             final List<Integer> list = ((List<Integer>) test.collection);
 146         }
 147         for (final CollectionSupplier.TestCase test : supplier.get()) {
 148             final List<Integer> original = ((List<Integer>) test.original);
 149             final List<Integer> list = ((List<Integer>) test.collection);
 150 


 164                 final List<Integer> actualSubList = new LinkedList<>();
 165                 subList.forEach(actualSubList::add);
 166                 assertEquals(actualSubList.size(), SUBLIST_SIZE);
 167                 for (int i = 0; i < SUBLIST_SIZE; i++) {
 168                     assertEquals(actualSubList.get(i), original.get(i + SUBLIST_FROM));
 169                 }
 170             }
 171 
 172             trimmedSubList(list, new Callback() {
 173                 @Override
 174                 public void call(final List<Integer> list) {
 175                     final List<Integer> actual = new LinkedList<>();
 176                     list.forEach(actual::add);
 177                     CollectionAsserts.assertContents(actual, list);
 178                 }
 179             });
 180         }
 181     }
 182 
 183     @Test
 184     public void testRemoveIf() throws Exception {
 185         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 186 
 187         for (final CollectionSupplier.TestCase test : supplier.get()) {
 188             final List<Integer> original = ((List<Integer>) test.original);
 189             final List<Integer> list = ((List<Integer>) test.collection);
 190 
 191             try {
 192                 list.removeIf(null);
 193                 fail("expected NPE not thrown");
 194             } catch (NullPointerException npe) {}
 195             CollectionAsserts.assertContents(list, original);
 196 
 197             final AtomicInteger offset = new AtomicInteger(1);
 198             while (list.size() > 0) {
 199                 removeFirst(original, list, offset);
 200             }
 201         }
 202 
 203         for (final CollectionSupplier.TestCase test : supplier.get()) {
 204             final List<Integer> original = ((List<Integer>) test.original);
 205             final List<Integer> list = ((List<Integer>) test.collection);
 206             list.removeIf(pOdd);
 207             for (int i : list) {
 208                 assertTrue((i % 2) == 0);
 209             }
 210             for (int i : original) {
 211                 if (i % 2 == 0) {
 212                     assertTrue(list.contains(i));
 213                 }
 214             }
 215             list.removeIf(pEven);
 216             assertTrue(list.isEmpty());
 217         }
 218 
 219         for (final CollectionSupplier.TestCase test : supplier.get()) {
 220             final List<Integer> original = ((List<Integer>) test.original);
 221             final List<Integer> list = ((List<Integer>) test.collection);
 222             final List<Integer> listCopy = new ArrayList<>(list);
 223             if (original.size() > SUBLIST_SIZE) {
 224                 final List<Integer> subList = list.subList(SUBLIST_FROM, SUBLIST_TO);
 225                 final List<Integer> subListCopy = new ArrayList<>(subList);
 226                 listCopy.removeAll(subList);
 227                 subList.removeIf(pOdd);
 228                 for (int i : subList) {
 229                     assertTrue((i % 2) == 0);
 230                 }
 231                 for (int i : subListCopy) {
 232                     if (i % 2 == 0) {
 233                         assertTrue(subList.contains(i));
 234                     } else {
 235                         assertFalse(subList.contains(i));
 236                     }
 237                 }
 238                 subList.removeIf(pEven);
 239                 assertTrue(subList.isEmpty());
 240                 // elements outside the view should remain
 241                 CollectionAsserts.assertContents(list, listCopy);
 242             }
 243         }
 244 
 245         for (final CollectionSupplier.TestCase test : supplier.get()) {
 246             final List<Integer> list = ((List<Integer>) test.collection);
 247             trimmedSubList(list, new Callback() {
 248                 @Override
 249                 public void call(final List<Integer> list) {
 250                     final List<Integer> copy = new ArrayList<>(list);
 251                     list.removeIf(pOdd);
 252                     for (int i : list) {
 253                         assertTrue((i % 2) == 0);
 254                     }
 255                     for (int i : copy) {
 256                         if (i % 2 == 0) {
 257                             assertTrue(list.contains(i));
 258                         } else {
 259                             assertFalse(list.contains(i));
 260                         }
 261                     }
 262                 }
 263             });
 264         }
 265     }
 266 
 267     // remove the first element
 268     private void removeFirst(final List<Integer> original, final List<Integer> list, final AtomicInteger offset) {
 269         final AtomicBoolean first = new AtomicBoolean(true);
 270         list.removeIf(x -> first.getAndSet(false));
 271         CollectionAsserts.assertContents(original.subList(offset.getAndIncrement(), original.size()), list);
 272     }
 273 
 274     @Test
 275     public void testReplaceAll() throws Exception {
 276         final int scale = 3;
 277         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 278         for (final CollectionSupplier.TestCase test : supplier.get()) {
 279             final List<Integer> original = ((List<Integer>) test.original);
 280             final List<Integer> list = ((List<Integer>) test.collection);
 281 
 282             try {
 283                 list.replaceAll(null);
 284                 fail("expected NPE not thrown");
 285             } catch (NullPointerException npe) {}
 286             CollectionAsserts.assertContents(list, original);
 287 
 288             list.replaceAll(x -> scale * x);
 289             for (int i=0; i < original.size(); i++) {
 290                 assertTrue(list.get(i) == (scale * original.get(i)), "mismatch at index " + i);


 430         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 431         for (final CollectionSupplier.TestCase test : supplier.get()) {
 432             final List<Integer> list = ((List<Integer>) test.collection);
 433             if (list.size() <= 1) {
 434                 continue;
 435             }
 436             boolean gotException = false;
 437             try {
 438                 // bad predicate that modifies its list, should throw CME
 439                 list.forEach((x) -> {list.add(x);});
 440             } catch (ConcurrentModificationException cme) {
 441                 gotException = true;
 442             }
 443             if (!gotException) {
 444                 fail("expected CME was not thrown from " + test);
 445             }
 446         }
 447     }
 448 
 449     @Test
 450     public void testRemoveIfThrowsCME() throws Exception {
 451         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 452         for (final CollectionSupplier.TestCase test : supplier.get()) {
 453             final List<Integer> list = ((List<Integer>) test.collection);
 454             if (list.size() <= 1) {
 455                 continue;
 456             }
 457             boolean gotException = false;
 458             try {
 459                 // bad predicate that modifies its list, should throw CME
 460                 list.removeIf((x) -> {return list.add(x);});
 461             } catch (ConcurrentModificationException cme) {
 462                 gotException = true;
 463             }
 464             if (!gotException) {
 465                 fail("expected CME was not thrown from " + test);
 466             }
 467         }
 468     }
 469 
 470     @Test
 471     public void testReplaceAllThrowsCME() throws Exception {
 472         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 473         for (final CollectionSupplier.TestCase test : supplier.get()) {
 474             final List<Integer> list = ((List<Integer>) test.collection);
 475             if (list.size() <= 1) {
 476                 continue;
 477             }
 478             boolean gotException = false;
 479             try {
 480                 // bad predicate that modifies its list, should throw CME


 507                 fail("expected CME was not thrown from " + test);
 508             }
 509         }
 510     }
 511 
 512     private static final List<Integer> SLICED_EXPECTED = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 8, 9);
 513     private static final List<Integer> SLICED_EXPECTED2 = Arrays.asList(0, 1, 2, 5, 6, 7, 8, 9);
 514 
 515     @DataProvider(name="shortIntListProvider", parallel=true)
 516     public static Object[][] intListCases() {
 517         final Integer[] DATA = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 518         final List<Object[]> cases = new LinkedList<>();
 519         cases.add(new Object[] { new ArrayList<>(Arrays.asList(DATA)) });
 520         cases.add(new Object[] { new LinkedList<>(Arrays.asList(DATA)) });
 521         cases.add(new Object[] { new Vector<>(Arrays.asList(DATA)) });
 522         cases.add(new Object[] { new CopyOnWriteArrayList<>(Arrays.asList(DATA)) });
 523         return cases.toArray(new Object[0][cases.size()]);
 524     }
 525 
 526     @Test(dataProvider = "shortIntListProvider")
 527     public void testRemoveIfFromSlice(final List<Integer> list) throws Exception {
 528         final List<Integer> sublist = list.subList(3, 6);
 529         assertTrue(sublist.removeIf(x -> x == 4));
 530         CollectionAsserts.assertContents(list, SLICED_EXPECTED);
 531 
 532         final List<Integer> sublist2 = list.subList(2, 5);
 533         assertTrue(sublist2.removeIf(x -> x == 3));
 534         CollectionAsserts.assertContents(list, SLICED_EXPECTED2);
 535     }
 536 }


 110 
 111         cases.add(new Object[] { new ArrayList(){{add(42);}} });
 112         cases.add(new Object[] { new LinkedList(){{add(42);}} });
 113         cases.add(new Object[] { new Vector(){{add(42);}} });
 114         cases.add(new Object[] { new Stack(){{add(42);}} });
 115         cases.add(new Object[] { new CopyOnWriteArrayList(){{add(42);}} });
 116         return cases.toArray(new Object[0][cases.size()]);
 117     }
 118 
 119     @Test(dataProvider = "listProvider")
 120     public void testProvidedWithNull(final List<Integer> list) throws Exception {
 121         try {
 122             list.forEach(null);
 123             fail("expected NPE not thrown");
 124         } catch (NullPointerException npe) {}
 125         try {
 126             list.replaceAll(null);
 127             fail("expected NPE not thrown");
 128         } catch (NullPointerException npe) {}
 129         try {
 130             list.removeAll((Predicate<Integer>)null);
 131             fail("expected NPE not thrown");
 132         } catch (NullPointerException npe) {}
 133         try {
 134             list.sort(null);
 135         } catch (Throwable t) {
 136             fail("Exception not expected: " + t);
 137         }
 138     }
 139 
 140     @Test
 141     public void testForEach() throws Exception {
 142         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 143         for (final CollectionSupplier.TestCase test : supplier.get()) {
 144             final List<Integer> original = ((List<Integer>) test.original);
 145             final List<Integer> list = ((List<Integer>) test.collection);
 146         }
 147         for (final CollectionSupplier.TestCase test : supplier.get()) {
 148             final List<Integer> original = ((List<Integer>) test.original);
 149             final List<Integer> list = ((List<Integer>) test.collection);
 150 


 164                 final List<Integer> actualSubList = new LinkedList<>();
 165                 subList.forEach(actualSubList::add);
 166                 assertEquals(actualSubList.size(), SUBLIST_SIZE);
 167                 for (int i = 0; i < SUBLIST_SIZE; i++) {
 168                     assertEquals(actualSubList.get(i), original.get(i + SUBLIST_FROM));
 169                 }
 170             }
 171 
 172             trimmedSubList(list, new Callback() {
 173                 @Override
 174                 public void call(final List<Integer> list) {
 175                     final List<Integer> actual = new LinkedList<>();
 176                     list.forEach(actual::add);
 177                     CollectionAsserts.assertContents(actual, list);
 178                 }
 179             });
 180         }
 181     }
 182 
 183     @Test
 184     public void testRemoveAll() throws Exception {
 185         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 186 
 187         for (final CollectionSupplier.TestCase test : supplier.get()) {
 188             final List<Integer> original = ((List<Integer>) test.original);
 189             final List<Integer> list = ((List<Integer>) test.collection);
 190 
 191             try {
 192                 list.removeAll((Predicate<Integer>)null);
 193                 fail("expected NPE not thrown");
 194             } catch (NullPointerException npe) {}
 195             CollectionAsserts.assertContents(list, original);
 196 
 197             final AtomicInteger offset = new AtomicInteger(1);
 198             while (list.size() > 0) {
 199                 removeFirst(original, list, offset);
 200             }
 201         }
 202 
 203         for (final CollectionSupplier.TestCase test : supplier.get()) {
 204             final List<Integer> original = ((List<Integer>) test.original);
 205             final List<Integer> list = ((List<Integer>) test.collection);
 206             list.removeAll(pOdd);
 207             for (int i : list) {
 208                 assertTrue((i % 2) == 0);
 209             }
 210             for (int i : original) {
 211                 if (i % 2 == 0) {
 212                     assertTrue(list.contains(i));
 213                 }
 214             }
 215             list.removeAll(pEven);
 216             assertTrue(list.isEmpty());
 217         }
 218 
 219         for (final CollectionSupplier.TestCase test : supplier.get()) {
 220             final List<Integer> original = ((List<Integer>) test.original);
 221             final List<Integer> list = ((List<Integer>) test.collection);
 222             final List<Integer> listCopy = new ArrayList<>(list);
 223             if (original.size() > SUBLIST_SIZE) {
 224                 final List<Integer> subList = list.subList(SUBLIST_FROM, SUBLIST_TO);
 225                 final List<Integer> subListCopy = new ArrayList<>(subList);
 226                 listCopy.removeAll(subList);
 227                 subList.removeAll(pOdd);
 228                 for (int i : subList) {
 229                     assertTrue((i % 2) == 0);
 230                 }
 231                 for (int i : subListCopy) {
 232                     if (i % 2 == 0) {
 233                         assertTrue(subList.contains(i));
 234                     } else {
 235                         assertFalse(subList.contains(i));
 236                     }
 237                 }
 238                 subList.removeAll(pEven);
 239                 assertTrue(subList.isEmpty());
 240                 // elements outside the view should remain
 241                 CollectionAsserts.assertContents(list, listCopy);
 242             }
 243         }
 244 
 245         for (final CollectionSupplier.TestCase test : supplier.get()) {
 246             final List<Integer> list = ((List<Integer>) test.collection);
 247             trimmedSubList(list, new Callback() {
 248                 @Override
 249                 public void call(final List<Integer> list) {
 250                     final List<Integer> copy = new ArrayList<>(list);
 251                     list.removeAll(pOdd);
 252                     for (int i : list) {
 253                         assertTrue((i % 2) == 0);
 254                     }
 255                     for (int i : copy) {
 256                         if (i % 2 == 0) {
 257                             assertTrue(list.contains(i));
 258                         } else {
 259                             assertFalse(list.contains(i));
 260                         }
 261                     }
 262                 }
 263             });
 264         }
 265     }
 266 
 267     // remove the first element
 268     private void removeFirst(final List<Integer> original, final List<Integer> list, final AtomicInteger offset) {
 269         final AtomicBoolean first = new AtomicBoolean(true);
 270         list.removeAll(x -> first.getAndSet(false));
 271         CollectionAsserts.assertContents(original.subList(offset.getAndIncrement(), original.size()), list);
 272     }
 273 
 274     @Test
 275     public void testReplaceAll() throws Exception {
 276         final int scale = 3;
 277         final CollectionSupplier supplier = new CollectionSupplier(LIST_CLASSES, SIZE);
 278         for (final CollectionSupplier.TestCase test : supplier.get()) {
 279             final List<Integer> original = ((List<Integer>) test.original);
 280             final List<Integer> list = ((List<Integer>) test.collection);
 281 
 282             try {
 283                 list.replaceAll(null);
 284                 fail("expected NPE not thrown");
 285             } catch (NullPointerException npe) {}
 286             CollectionAsserts.assertContents(list, original);
 287 
 288             list.replaceAll(x -> scale * x);
 289             for (int i=0; i < original.size(); i++) {
 290                 assertTrue(list.get(i) == (scale * original.get(i)), "mismatch at index " + i);


 430         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 431         for (final CollectionSupplier.TestCase test : supplier.get()) {
 432             final List<Integer> list = ((List<Integer>) test.collection);
 433             if (list.size() <= 1) {
 434                 continue;
 435             }
 436             boolean gotException = false;
 437             try {
 438                 // bad predicate that modifies its list, should throw CME
 439                 list.forEach((x) -> {list.add(x);});
 440             } catch (ConcurrentModificationException cme) {
 441                 gotException = true;
 442             }
 443             if (!gotException) {
 444                 fail("expected CME was not thrown from " + test);
 445             }
 446         }
 447     }
 448 
 449     @Test
 450     public void testRemoveAllThrowsCME() throws Exception {
 451         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 452         for (final CollectionSupplier.TestCase test : supplier.get()) {
 453             final List<Integer> list = ((List<Integer>) test.collection);
 454             if (list.size() <= 1) {
 455                 continue;
 456             }
 457             boolean gotException = false;
 458             try {
 459                 // bad predicate that modifies its list, should throw CME
 460                 list.removeAll((x) -> {return list.add(x);});
 461             } catch (ConcurrentModificationException cme) {
 462                 gotException = true;
 463             }
 464             if (!gotException) {
 465                 fail("expected CME was not thrown from " + test);
 466             }
 467         }
 468     }
 469 
 470     @Test
 471     public void testReplaceAllThrowsCME() throws Exception {
 472         final CollectionSupplier supplier = new CollectionSupplier(LIST_CME_CLASSES, SIZE);
 473         for (final CollectionSupplier.TestCase test : supplier.get()) {
 474             final List<Integer> list = ((List<Integer>) test.collection);
 475             if (list.size() <= 1) {
 476                 continue;
 477             }
 478             boolean gotException = false;
 479             try {
 480                 // bad predicate that modifies its list, should throw CME


 507                 fail("expected CME was not thrown from " + test);
 508             }
 509         }
 510     }
 511 
 512     private static final List<Integer> SLICED_EXPECTED = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 8, 9);
 513     private static final List<Integer> SLICED_EXPECTED2 = Arrays.asList(0, 1, 2, 5, 6, 7, 8, 9);
 514 
 515     @DataProvider(name="shortIntListProvider", parallel=true)
 516     public static Object[][] intListCases() {
 517         final Integer[] DATA = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 518         final List<Object[]> cases = new LinkedList<>();
 519         cases.add(new Object[] { new ArrayList<>(Arrays.asList(DATA)) });
 520         cases.add(new Object[] { new LinkedList<>(Arrays.asList(DATA)) });
 521         cases.add(new Object[] { new Vector<>(Arrays.asList(DATA)) });
 522         cases.add(new Object[] { new CopyOnWriteArrayList<>(Arrays.asList(DATA)) });
 523         return cases.toArray(new Object[0][cases.size()]);
 524     }
 525 
 526     @Test(dataProvider = "shortIntListProvider")
 527     public void testRemoveAllFromSlice(final List<Integer> list) throws Exception {
 528         final List<Integer> sublist = list.subList(3, 6);
 529         assertTrue(sublist.removeAll(x -> x == 4));
 530         CollectionAsserts.assertContents(list, SLICED_EXPECTED);
 531 
 532         final List<Integer> sublist2 = list.subList(2, 5);
 533         assertTrue(sublist2.removeAll(x -> x == 3));
 534         CollectionAsserts.assertContents(list, SLICED_EXPECTED2);
 535     }
 536 }