jdk/test/java/util/Collection/SetFactories.java
author redestad
Thu, 12 Jan 2017 13:38:27 +0100
changeset 43068 df01087b2c43
parent 34527 e3caf3a43d09
permissions -rw-r--r--
8166365: Small immutable collections should provide optimized implementations when possible Reviewed-by: smarks, psandoz, attila
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     1
/*
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     4
 *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     8
 *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    13
 * accompanied this code).
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    14
 *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    18
 *
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    21
 * questions.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    22
 */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    23
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    26
import java.io.IOException;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    27
import java.io.ObjectInputStream;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    28
import java.io.ObjectOutputStream;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    29
import java.util.ArrayList;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    30
import java.util.Arrays;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    31
import java.util.Collections;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    32
import java.util.Iterator;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    33
import java.util.HashSet;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    34
import java.util.List;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    35
import java.util.Set;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    36
import java.util.stream.Collectors;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    37
import static org.testng.Assert.assertEquals;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    38
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    39
import org.testng.annotations.DataProvider;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    40
import org.testng.annotations.Test;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    41
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    42
import static org.testng.Assert.assertFalse;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    43
import static org.testng.Assert.assertTrue;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    44
import static org.testng.Assert.fail;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    45
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    46
/*
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    47
 * @test
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    48
 * @bug 8048330
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    49
 * @summary Test convenience static factory methods on Set.
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    50
 * @run testng SetFactories
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    51
 */
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    52
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    53
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    54
public class SetFactories {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    55
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    56
    static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    57
    static final String[] stringArray;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    58
    static {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    59
        String[] sa = new String[NUM_STRINGS];
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    60
        for (int i = 0; i < NUM_STRINGS; i++) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    61
            sa[i] = String.valueOf((char)('a' + i));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    62
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    63
        stringArray = sa;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    64
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    65
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    66
    static Object[] a(Set<String> act, Set<String> exp) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    67
        return new Object[] { act, exp };
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    68
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    69
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    70
    static Set<String> hashSetOf(String... args) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    71
        return new HashSet<>(Arrays.asList(args));
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    72
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    73
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    74
    @DataProvider(name="empty")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    75
    public Iterator<Object[]> empty() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    76
        return Collections.singletonList(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    77
            // actual, expected
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    78
            a(Set.of(), Collections.emptySet())
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    79
        ).iterator();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    80
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    81
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    82
    @DataProvider(name="nonempty")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    83
    public Iterator<Object[]> nonempty() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    84
        return Arrays.asList(
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    85
            // actual, expected
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    86
            a(   Set.of("a"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    87
              hashSetOf("a")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    88
            a(   Set.of("a", "b"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    89
              hashSetOf("a", "b")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    90
            a(   Set.of("a", "b", "c"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    91
              hashSetOf("a", "b", "c")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    92
            a(   Set.of("a", "b", "c", "d"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    93
              hashSetOf("a", "b", "c", "d")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    94
            a(   Set.of("a", "b", "c", "d", "e"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    95
              hashSetOf("a", "b", "c", "d", "e")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    96
            a(   Set.of("a", "b", "c", "d", "e", "f"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    97
              hashSetOf("a", "b", "c", "d", "e", "f")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    98
            a(   Set.of("a", "b", "c", "d", "e", "f", "g"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
    99
              hashSetOf("a", "b", "c", "d", "e", "f", "g")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   100
            a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   101
              hashSetOf("a", "b", "c", "d", "e", "f", "g", "h")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   102
            a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   103
              hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i")),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   104
            a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   105
              hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   106
            a(   Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   107
                 Set.of("j", "i", "h", "g", "f", "e", "d", "c", "b", "a")),
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   108
            a(   Set.of(stringArray),
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   109
              hashSetOf(stringArray))
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   110
        ).iterator();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   111
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   112
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   113
    @DataProvider(name="all")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   114
    public Iterator<Object[]> all() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   115
        List<Object[]> all = new ArrayList<>();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   116
        empty().forEachRemaining(all::add);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   117
        nonempty().forEachRemaining(all::add);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   118
        return all.iterator();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   119
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   120
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   121
    @Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   122
    public void cannotAdd(Set<String> act, Set<String> exp) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   123
        act.add("x");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   124
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   125
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   126
    @Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   127
    public void cannotRemove(Set<String> act, Set<String> exp) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   128
        act.remove(act.iterator().next());
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   129
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   130
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   131
    @Test(dataProvider="all")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   132
    public void contentsMatch(Set<String> act, Set<String> exp) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   133
        assertEquals(act, exp);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   134
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   135
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   136
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   137
    public void dupsDisallowed2() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   138
        Set<String> set = Set.of("a", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   139
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   140
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   141
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   142
    public void dupsDisallowed3() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   143
        Set<String> set = Set.of("a", "b", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   144
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   145
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   146
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   147
    public void dupsDisallowed4() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   148
        Set<String> set = Set.of("a", "b", "c", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   149
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   150
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   151
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   152
    public void dupsDisallowed5() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   153
        Set<String> set = Set.of("a", "b", "c", "d", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   154
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   155
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   156
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   157
    public void dupsDisallowed6() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   158
        Set<String> set = Set.of("a", "b", "c", "d", "e", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   159
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   160
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   161
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   162
    public void dupsDisallowed7() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   163
        Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   164
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   165
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   166
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   167
    public void dupsDisallowed8() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   168
        Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   169
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   170
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   171
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   172
    public void dupsDisallowed9() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   173
        Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   174
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   175
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   176
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   177
    public void dupsDisallowed10() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   178
        Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   179
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   180
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   181
    @Test(expectedExceptions=IllegalArgumentException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   182
    public void dupsDisallowedN() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   183
        String[] array = stringArray.clone();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   184
        array[0] = array[1];
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   185
        Set<String> set = Set.of(array);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   186
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   187
43068
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   188
    @Test(dataProvider="all")
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   189
    public void hashCodeEqual(Set<String> act, Set<String> exp) {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   190
        assertEquals(act.hashCode(), exp.hashCode());
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   191
    }
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   192
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   193
    @Test(dataProvider="all")
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   194
    public void containsAll(Set<String> act, Set<String> exp) {
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   195
        assertTrue(act.containsAll(exp));
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   196
        assertTrue(exp.containsAll(act));
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   197
    }
df01087b2c43 8166365: Small immutable collections should provide optimized implementations when possible
redestad
parents: 34527
diff changeset
   198
34527
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   199
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   200
    public void nullDisallowed1() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   201
        Set.of((String)null); // force one-arg overload
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   202
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   203
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   204
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   205
    public void nullDisallowed2a() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   206
        Set.of("a", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   207
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   208
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   209
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   210
    public void nullDisallowed2b() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   211
        Set.of(null, "b");
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   212
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   213
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   214
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   215
    public void nullDisallowed3() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   216
        Set.of("a", "b", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   217
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   218
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   219
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   220
    public void nullDisallowed4() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   221
        Set.of("a", "b", "c", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   222
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   223
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   224
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   225
    public void nullDisallowed5() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   226
        Set.of("a", "b", "c", "d", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   227
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   228
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   229
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   230
    public void nullDisallowed6() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   231
        Set.of("a", "b", "c", "d", "e", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   232
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   233
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   234
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   235
    public void nullDisallowed7() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   236
        Set.of("a", "b", "c", "d", "e", "f", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   237
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   238
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   239
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   240
    public void nullDisallowed8() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   241
        Set.of("a", "b", "c", "d", "e", "f", "g", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   242
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   243
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   244
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   245
    public void nullDisallowed9() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   246
        Set.of("a", "b", "c", "d", "e", "f", "g", "h", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   247
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   248
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   249
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   250
    public void nullDisallowed10() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   251
        Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   252
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   253
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   254
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   255
    public void nullDisallowedN() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   256
        String[] array = stringArray.clone();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   257
        array[0] = null;
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   258
        Set.of(array);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   259
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   260
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   261
    @Test(expectedExceptions=NullPointerException.class)
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   262
    public void nullArrayDisallowed() {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   263
        Set.of((Object[])null);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   264
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   265
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   266
    @Test(dataProvider="all")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   267
    public void serialEquality(Set<String> act, Set<String> exp) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   268
        // assume that act.equals(exp) tested elsewhere
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   269
        Set<String> copy = serialClone(act);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   270
        assertEquals(act, copy);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   271
        assertEquals(copy, exp);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   272
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   273
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   274
    @SuppressWarnings("unchecked")
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   275
    static <T> T serialClone(T obj) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   276
        try {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   277
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   278
            ObjectOutputStream oos = new ObjectOutputStream(baos);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   279
            oos.writeObject(obj);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   280
            oos.close();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   281
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   282
            ObjectInputStream ois = new ObjectInputStream(bais);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   283
            return (T) ois.readObject();
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   284
        } catch (IOException | ClassNotFoundException e) {
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   285
            throw new AssertionError(e);
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   286
        }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   287
    }
e3caf3a43d09 8139232: JEP-269 initial API and skeleton implementations
smarks
parents:
diff changeset
   288
}