test/jdk/java/util/LinkedHashSet/Basic.java
author redestad
Fri, 31 May 2019 12:20:21 +0200
changeset 55124 f91999057a5a
parent 48541 946e34c2dec9
permissions -rw-r--r--
8224986: (str) optimize StringBuilder.append(CharSequence, int, int) for String arguments Reviewed-by: jlaskey, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
47730
c7b5b1ce8145 8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents: 47216
diff changeset
    24
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4245809
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic test for LinkedHashSet.  (Based on SetBash)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    30
import java.io.ByteArrayInputStream;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    31
import java.io.ByteArrayOutputStream;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    32
import java.io.ObjectInputStream;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    33
import java.io.ObjectOutputStream;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    34
import java.util.Arrays;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    35
import java.util.Iterator;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    36
import java.util.LinkedHashSet;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    37
import java.util.Random;
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47730
diff changeset
    38
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static Random rnd = new Random(666);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        int numItr =  500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int setSize = 500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        for (int i=0; i<numItr; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            Set s1 = new LinkedHashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            AddRandoms(s1, setSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Set s2 = new LinkedHashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            AddRandoms(s2, setSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            Set intersection = clone(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            intersection.retainAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            Set diff1 = clone(s1); diff1.removeAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            Set diff2 = clone(s2); diff2.removeAll(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            Set union = clone(s1); union.addAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (diff1.removeAll(diff2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                throw new Exception("Set algebra identity 2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (diff1.removeAll(intersection))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                throw new Exception("Set algebra identity 3 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (diff2.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new Exception("Set algebra identity 4 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (diff2.removeAll(intersection))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                throw new Exception("Set algebra identity 5 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (intersection.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                throw new Exception("Set algebra identity 6 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (intersection.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                throw new Exception("Set algebra identity 7 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            intersection.addAll(diff1); intersection.addAll(diff2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (!intersection.equals(union))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new Exception("Set algebra identity 1 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (new LinkedHashSet(union).hashCode() != union.hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                throw new Exception("Incorrect hashCode computation.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Iterator e = union.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            while (e.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if (!intersection.remove(e.next()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    throw new Exception("Couldn't remove element from copy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (!intersection.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                throw new Exception("Copy nonempty after deleting all elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            e = union.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                Object o = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (!union.contains(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    throw new Exception("Set doesn't contain one of its elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (union.contains(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    throw new Exception("Set contains element after deletion.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (!union.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw new Exception("Set nonempty after deleting all elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            s1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (!s1.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new Exception("Set nonempty after clear.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.err.println("Success.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static Set clone(Set s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Set clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int method = rnd.nextInt(3);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 5506
diff changeset
   109
        clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() :
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                 (method==1 ? new LinkedHashSet(Arrays.asList(s.toArray())) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                  serClone(s)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!s.equals(clone))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new Exception("Set not equal to copy: "+method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!s.containsAll(clone))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new Exception("Set does not contain copy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (!clone.containsAll(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new Exception("Copy does not contain set.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static Set serClone(Set m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Set result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            // Serialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ObjectOutputStream out = new ObjectOutputStream(bos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            out.writeObject(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // Deserialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            ObjectInputStream in = new ObjectInputStream(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            result = (Set)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            in.close();
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 5506
diff changeset
   136
        } catch (Exception e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static void AddRandoms(Set s, int n) throws Exception {
47730
c7b5b1ce8145 8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents: 47216
diff changeset
   143
        for (int i = 0; i < n; i++) {
c7b5b1ce8145 8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents: 47216
diff changeset
   144
            Integer e = rnd.nextInt(n);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int preSize = s.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            boolean prePresent = s.contains(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            boolean added = s.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (!s.contains(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                throw new Exception("Element not present after addition.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (added == prePresent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                throw new Exception("added == alreadyPresent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int postSize = s.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (added && preSize == postSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new Exception("Add returned true, but size didn't change.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (!added && preSize != postSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                throw new Exception("Add returned false, but size changed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}