Path: blob/master/test/jdk/java/util/LinkedHashSet/Basic.java
41149 views
/*1* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 424580926* @summary Basic test for LinkedHashSet. (Based on SetBash)27*/2829import java.io.ByteArrayInputStream;30import java.io.ByteArrayOutputStream;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.util.Arrays;34import java.util.Iterator;35import java.util.LinkedHashSet;36import java.util.Random;37import java.util.Set;3839public class Basic {40static Random rnd = new Random(666);4142public static void main(String[] args) throws Exception {43int numItr = 500;44int setSize = 500;4546for (int i=0; i<numItr; i++) {47Set s1 = new LinkedHashSet();48AddRandoms(s1, setSize);4950Set s2 = new LinkedHashSet();51AddRandoms(s2, setSize);5253Set intersection = clone(s1);54intersection.retainAll(s2);55Set diff1 = clone(s1); diff1.removeAll(s2);56Set diff2 = clone(s2); diff2.removeAll(s1);57Set union = clone(s1); union.addAll(s2);5859if (diff1.removeAll(diff2))60throw new Exception("Set algebra identity 2 failed");61if (diff1.removeAll(intersection))62throw new Exception("Set algebra identity 3 failed");63if (diff2.removeAll(diff1))64throw new Exception("Set algebra identity 4 failed");65if (diff2.removeAll(intersection))66throw new Exception("Set algebra identity 5 failed");67if (intersection.removeAll(diff1))68throw new Exception("Set algebra identity 6 failed");69if (intersection.removeAll(diff1))70throw new Exception("Set algebra identity 7 failed");7172intersection.addAll(diff1); intersection.addAll(diff2);73if (!intersection.equals(union))74throw new Exception("Set algebra identity 1 failed");7576if (new LinkedHashSet(union).hashCode() != union.hashCode())77throw new Exception("Incorrect hashCode computation.");7879Iterator e = union.iterator();80while (e.hasNext())81if (!intersection.remove(e.next()))82throw new Exception("Couldn't remove element from copy.");83if (!intersection.isEmpty())84throw new Exception("Copy nonempty after deleting all elements.");8586e = union.iterator();87while (e.hasNext()) {88Object o = e.next();89if (!union.contains(o))90throw new Exception("Set doesn't contain one of its elements.");91e.remove();92if (union.contains(o))93throw new Exception("Set contains element after deletion.");94}95if (!union.isEmpty())96throw new Exception("Set nonempty after deleting all elements.");9798s1.clear();99if (!s1.isEmpty())100throw new Exception("Set nonempty after clear.");101}102System.err.println("Success.");103}104105static Set clone(Set s) throws Exception {106Set clone;107int method = rnd.nextInt(3);108clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() :109(method==1 ? new LinkedHashSet(Arrays.asList(s.toArray())) :110serClone(s)));111if (!s.equals(clone))112throw new Exception("Set not equal to copy: "+method);113if (!s.containsAll(clone))114throw new Exception("Set does not contain copy.");115if (!clone.containsAll(s))116throw new Exception("Copy does not contain set.");117return clone;118}119120private static Set serClone(Set m) {121Set result = null;122try {123// Serialize124ByteArrayOutputStream bos = new ByteArrayOutputStream();125ObjectOutputStream out = new ObjectOutputStream(bos);126out.writeObject(m);127out.flush();128129// Deserialize130ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());131out.close();132ObjectInputStream in = new ObjectInputStream(bis);133result = (Set)in.readObject();134in.close();135} catch (Exception e) {136e.printStackTrace();137}138return result;139}140141static void AddRandoms(Set s, int n) throws Exception {142for (int i = 0; i < n; i++) {143Integer e = rnd.nextInt(n);144145int preSize = s.size();146boolean prePresent = s.contains(e);147boolean added = s.add(e);148if (!s.contains(e))149throw new Exception("Element not present after addition.");150if (added == prePresent)151throw new Exception("added == alreadyPresent");152int postSize = s.size();153if (added && preSize == postSize)154throw new Exception("Add returned true, but size didn't change.");155if (!added && preSize != postSize)156throw new Exception("Add returned false, but size changed.");157}158}159}160161162