Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/LinkedHashSet/Basic.java
38811 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.util.*;30import java.io.*;3132public class Basic {33static Random rnd = new Random(666);3435public static void main(String[] args) throws Exception {36int numItr = 500;37int setSize = 500;3839for (int i=0; i<numItr; i++) {40Set s1 = new LinkedHashSet();41AddRandoms(s1, setSize);4243Set s2 = new LinkedHashSet();44AddRandoms(s2, setSize);4546Set intersection = clone(s1);47intersection.retainAll(s2);48Set diff1 = clone(s1); diff1.removeAll(s2);49Set diff2 = clone(s2); diff2.removeAll(s1);50Set union = clone(s1); union.addAll(s2);5152if (diff1.removeAll(diff2))53throw new Exception("Set algebra identity 2 failed");54if (diff1.removeAll(intersection))55throw new Exception("Set algebra identity 3 failed");56if (diff2.removeAll(diff1))57throw new Exception("Set algebra identity 4 failed");58if (diff2.removeAll(intersection))59throw new Exception("Set algebra identity 5 failed");60if (intersection.removeAll(diff1))61throw new Exception("Set algebra identity 6 failed");62if (intersection.removeAll(diff1))63throw new Exception("Set algebra identity 7 failed");6465intersection.addAll(diff1); intersection.addAll(diff2);66if (!intersection.equals(union))67throw new Exception("Set algebra identity 1 failed");6869if (new LinkedHashSet(union).hashCode() != union.hashCode())70throw new Exception("Incorrect hashCode computation.");7172Iterator e = union.iterator();73while (e.hasNext())74if (!intersection.remove(e.next()))75throw new Exception("Couldn't remove element from copy.");76if (!intersection.isEmpty())77throw new Exception("Copy nonempty after deleting all elements.");7879e = union.iterator();80while (e.hasNext()) {81Object o = e.next();82if (!union.contains(o))83throw new Exception("Set doesn't contain one of its elements.");84e.remove();85if (union.contains(o))86throw new Exception("Set contains element after deletion.");87}88if (!union.isEmpty())89throw new Exception("Set nonempty after deleting all elements.");9091s1.clear();92if (!s1.isEmpty())93throw new Exception("Set nonempty after clear.");94}95System.err.println("Success.");96}9798static Set clone(Set s) throws Exception {99Set clone;100int method = rnd.nextInt(3);101clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() :102(method==1 ? new LinkedHashSet(Arrays.asList(s.toArray())) :103serClone(s)));104if (!s.equals(clone))105throw new Exception("Set not equal to copy: "+method);106if (!s.containsAll(clone))107throw new Exception("Set does not contain copy.");108if (!clone.containsAll(s))109throw new Exception("Copy does not contain set.");110return clone;111}112113private static Set serClone(Set m) {114Set result = null;115try {116// Serialize117ByteArrayOutputStream bos = new ByteArrayOutputStream();118ObjectOutputStream out = new ObjectOutputStream(bos);119out.writeObject(m);120out.flush();121122// Deserialize123ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());124out.close();125ObjectInputStream in = new ObjectInputStream(bis);126result = (Set)in.readObject();127in.close();128} catch(Exception e) {129e.printStackTrace();130}131return result;132}133134static void AddRandoms(Set s, int n) throws Exception {135for (int i=0; i<n; i++) {136int r = rnd.nextInt() % n;137Integer e = new Integer(r < 0 ? -r : r);138139int preSize = s.size();140boolean prePresent = s.contains(e);141boolean added = s.add(e);142if (!s.contains(e))143throw new Exception("Element not present after addition.");144if (added == prePresent)145throw new Exception("added == alreadyPresent");146int postSize = s.size();147if (added && preSize == postSize)148throw new Exception("Add returned true, but size didn't change.");149if (!added && preSize != postSize)150throw new Exception("Add returned false, but size changed.");151}152}153}154155156