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