• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import org.testng.Assert;
42 import org.testng.annotations.Test;
43 
44 public class Basic {
45     static Random rnd = new Random(666);
46 
47     @Test
testBasic()48     public void testBasic() {
49         int numItr =  500;
50         int setSize = 500;
51 
52         for (int i=0; i<numItr; i++) {
53             Set s1 = new LinkedHashSet();
54             AddRandoms(s1, setSize);
55 
56             Set s2 = new LinkedHashSet();
57             AddRandoms(s2, setSize);
58 
59             Set intersection = clone(s1);
60             intersection.retainAll(s2);
61             Set diff1 = clone(s1); diff1.removeAll(s2);
62             Set diff2 = clone(s2); diff2.removeAll(s1);
63             Set union = clone(s1); union.addAll(s2);
64 
65             Assert.assertFalse(diff1.removeAll(diff2));
66             Assert.assertFalse(diff1.removeAll(intersection));
67             Assert.assertFalse(diff2.removeAll(diff1));
68             Assert.assertFalse(diff2.removeAll(intersection));
69             Assert.assertFalse(intersection.removeAll(diff1));
70             Assert.assertFalse(intersection.removeAll(diff1));
71 
72             intersection.addAll(diff1); intersection.addAll(diff2);
73             Assert.assertTrue(intersection.equals(union));
74 
75             Assert.assertEquals(new LinkedHashSet(union).hashCode(), union.hashCode());
76 
77             Iterator e = union.iterator();
78             while (e.hasNext())
79                 Assert.assertTrue(intersection.remove(e.next()));
80             Assert.assertTrue(intersection.isEmpty());
81 
82             e = union.iterator();
83             while (e.hasNext()) {
84                 Object o = e.next();
85                 Assert.assertTrue(union.contains(o));
86                 e.remove();
87                 Assert.assertFalse(union.contains(o));
88             }
89             Assert.assertTrue(union.isEmpty());
90 
91             s1.clear();
92             Assert.assertTrue(s1.isEmpty());
93         }
94     }
95 
clone(Set s)96     static Set clone(Set s) {
97         Set clone;
98         int method = rnd.nextInt(3);
99         clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() :
100                 (method==1 ? new LinkedHashSet(Arrays.asList(s.toArray())) :
101                         serClone(s)));
102         Assert.assertTrue(s.equals(clone));
103         Assert.assertTrue(s.containsAll(clone));
104         Assert.assertTrue(clone.containsAll(s));
105         return clone;
106     }
107 
serClone(Set m)108     private static Set serClone(Set m) {
109         Set result = null;
110         try {
111             // Serialize
112             ByteArrayOutputStream bos = new ByteArrayOutputStream();
113             ObjectOutputStream out = new ObjectOutputStream(bos);
114             out.writeObject(m);
115             out.flush();
116 
117             // Deserialize
118             ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
119             out.close();
120             ObjectInputStream in = new ObjectInputStream(bis);
121             result = (Set)in.readObject();
122             in.close();
123         } catch (Exception e) {
124             Assert.fail();
125         }
126         return result;
127     }
128 
AddRandoms(Set s, int n)129     static void AddRandoms(Set s, int n) {
130         for (int i = 0; i < n; i++) {
131             Integer e = rnd.nextInt(n);
132 
133             int preSize = s.size();
134             boolean prePresent = s.contains(e);
135             boolean added = s.add(e);
136             Assert.assertTrue(s.contains(e));
137             Assert.assertFalse(added == prePresent);
138             int postSize = s.size();
139             Assert.assertFalse(added && preSize == postSize);
140             Assert.assertFalse(!added && preSize != postSize);
141         }
142     }
143 }