test/jdk/java/util/Collection/CollectionDefaults.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     1
/*
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     4
 *
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     8
 *
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    13
 * accompanied this code).
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    14
 *
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    18
 *
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    21
 * questions.
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    22
 */
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    23
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    24
import java.util.Arrays;
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    25
import java.util.Collection;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    26
import java.util.Collections;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    27
import java.util.HashMap;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    28
import java.util.HashSet;
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    29
import java.util.Iterator;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    30
import java.util.LinkedHashMap;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    31
import java.util.LinkedHashSet;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    32
import java.util.LinkedList;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    33
import java.util.List;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    34
import java.util.Set;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    35
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    36
import java.util.SortedSet;
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    37
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    38
import org.testng.annotations.DataProvider;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    39
import org.testng.annotations.Test;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    40
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    41
import static org.testng.Assert.assertTrue;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    42
import static org.testng.Assert.fail;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    43
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    44
import java.util.TreeMap;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    45
import java.util.TreeSet;
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    46
import java.util.concurrent.ConcurrentHashMap;
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    47
import java.util.concurrent.ConcurrentSkipListMap;
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    48
import java.util.function.Function;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    49
import java.util.function.Predicate;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    50
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    51
/**
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    52
 * @test
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    53
 * @summary Unit tests for extension methods on Collection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    54
 * @library testlibrary
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    55
 * @build CollectionAsserts CollectionSupplier ExtendsAbstractSet ExtendsAbstractCollection
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    56
 * @run testng CollectionDefaults
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    57
 */
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    58
public class CollectionDefaults {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    59
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    60
    public static final Predicate<Integer> pEven = x -> 0 == x % 2;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    61
    public static final Predicate<Integer> pOdd = x -> 1 == x % 2;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    62
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    63
    private static final int SIZE = 100;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    64
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    65
    private static final List<Function<Collection<Integer>, Collection<Integer>>> TEST_SUPPLIERS = Arrays.asList(
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    66
            // Collection
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    67
            ExtendsAbstractCollection<Integer>::new,
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    68
            java.util.ArrayDeque<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    69
            java.util.concurrent.ConcurrentLinkedDeque<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    70
            java.util.concurrent.ConcurrentLinkedQueue<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    71
            java.util.concurrent.LinkedBlockingDeque<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    72
            java.util.concurrent.LinkedBlockingQueue<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    73
            java.util.concurrent.LinkedTransferQueue<Integer>::new,
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    74
            (coll) -> new java.util.concurrent.ArrayBlockingQueue<Integer>(
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 23354
diff changeset
    75
                3 * SIZE, false, coll),
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    76
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    77
            // Lists
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    78
            java.util.ArrayList<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    79
            java.util.LinkedList<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    80
            java.util.Vector<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    81
            java.util.concurrent.CopyOnWriteArrayList<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    82
            ExtendsAbstractList<Integer>::new,
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    83
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    84
            // Sets
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    85
            java.util.HashSet<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    86
            java.util.LinkedHashSet<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    87
            java.util.TreeSet<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    88
            java.util.concurrent.ConcurrentSkipListSet<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    89
            java.util.concurrent.CopyOnWriteArraySet<Integer>::new,
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    90
            ExtendsAbstractSet<Integer>::new
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
    91
       );
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    92
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    93
    @DataProvider(name="setProvider", parallel=true)
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    94
    public static Iterator<Object[]> setCases() {
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    95
        final List<Object[]> cases = new LinkedList<>();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    96
        cases.add(new Object[] { new HashSet<>() });
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    97
        cases.add(new Object[] { new LinkedHashSet<>() });
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
    98
        cases.add(new Object[] { new TreeSet<>() });
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
    99
        cases.add(new Object[] { new java.util.concurrent.ConcurrentSkipListSet<>() });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   100
        cases.add(new Object[] { new java.util.concurrent.CopyOnWriteArraySet<>() });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   101
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   102
        cases.add(new Object[] { new ExtendsAbstractSet<>() });
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   103
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   104
        cases.add(new Object[] { Collections.newSetFromMap(new HashMap<>()) });
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   105
        cases.add(new Object[] { Collections.newSetFromMap(new LinkedHashMap<>()) });
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   106
        cases.add(new Object[] { Collections.newSetFromMap(new TreeMap<>()) });
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   107
        cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentHashMap<>()) });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   108
        cases.add(new Object[] { Collections.newSetFromMap(new ConcurrentSkipListMap<>()) });
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   109
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   110
        cases.add(new Object[] { new HashSet<Integer>(){{add(42);}} });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   111
        cases.add(new Object[] { new ExtendsAbstractSet<Integer>(){{add(42);}} });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   112
        cases.add(new Object[] { new LinkedHashSet<Integer>(){{add(42);}} });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   113
        cases.add(new Object[] { new TreeSet<Integer>(){{add(42);}} });
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   114
        return cases.iterator();
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   115
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   116
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   117
    @Test(dataProvider = "setProvider")
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   118
    public void testProvidedWithNull(final Set<Integer> set) {
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   119
        try {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   120
            set.forEach(null);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   121
            fail("expected NPE not thrown");
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   122
        } catch (NullPointerException expected) { // expected
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   123
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   124
        try {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   125
            set.removeIf(null);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   126
            fail("expected NPE not thrown");
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   127
        } catch (NullPointerException expected) { // expected
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   128
        }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   129
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   130
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   131
    @Test
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   132
    public void testForEach() {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   133
        @SuppressWarnings("unchecked")
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   134
        final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   135
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   136
        for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   137
            final Collection<Integer> original = test.expected;
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   138
            final Collection<Integer> set = test.collection;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   139
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   140
            try {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   141
                set.forEach(null);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   142
                fail("expected NPE not thrown");
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   143
            } catch (NullPointerException expected) { // expected
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   144
            }
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   145
            if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   146
                CollectionAsserts.assertContentsUnordered(set, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   147
            } else {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   148
                CollectionAsserts.assertContents(set, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   149
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   150
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   151
            final List<Integer> actual = new LinkedList<>();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   152
            set.forEach(actual::add);
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   153
            if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   154
                CollectionAsserts.assertContentsUnordered(actual, set, test.toString());
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   155
                CollectionAsserts.assertContentsUnordered(actual, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   156
            } else {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   157
                CollectionAsserts.assertContents(actual, set, test.toString());
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   158
                CollectionAsserts.assertContents(actual, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   159
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   160
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   161
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   162
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   163
    @Test
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   164
    public void testRemoveIf() {
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   165
        @SuppressWarnings("unchecked")
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   166
        final CollectionSupplier<Collection<Integer>> supplier = new CollectionSupplier(TEST_SUPPLIERS, SIZE);
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   167
        for (final CollectionSupplier.TestCase<Collection<Integer>> test : supplier.get()) {
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   168
            final Collection<Integer> original = test.expected;
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   169
            final Collection<Integer> set = test.collection;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   170
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   171
            try {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   172
                set.removeIf(null);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   173
                fail("expected NPE not thrown");
23354
221bf0e3642d 8037106: Optimize Arrays.asList(...).forEach
psandoz
parents: 23010
diff changeset
   174
            } catch (NullPointerException expected) { // expected
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   175
            }
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   176
            if (set instanceof Set && !((set instanceof SortedSet) || (set instanceof LinkedHashSet))) {
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   177
                CollectionAsserts.assertContentsUnordered(set, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   178
            } else {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 17166
diff changeset
   179
                CollectionAsserts.assertContents(set, original, test.toString());
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   180
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   181
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   182
            set.removeIf(pEven);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   183
            for (int i : set) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   184
                assertTrue((i % 2) == 1);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   185
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   186
            for (int i : original) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   187
                if (i % 2 == 1) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   188
                    assertTrue(set.contains(i));
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   189
                }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   190
            }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   191
            set.removeIf(pOdd);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   192
            assertTrue(set.isEmpty());
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   193
        }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   194
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents:
diff changeset
   195
}