ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/LinkedHashSet/Basic.java
Revision: 1.7
Committed: Mon Jan 8 03:12:03 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +9 -2 lines
Log Message:
organize imports

File Contents

# Content
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
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.util.Arrays;
35 import java.util.Iterator;
36 import java.util.LinkedHashSet;
37 import java.util.Random;
38 import java.util.Set;
39
40 public class Basic {
41 static Random rnd = new Random(666);
42
43 public static void main(String[] args) throws Exception {
44 int numItr = 500;
45 int setSize = 500;
46
47 for (int i=0; i<numItr; i++) {
48 Set s1 = new LinkedHashSet();
49 AddRandoms(s1, setSize);
50
51 Set s2 = new LinkedHashSet();
52 AddRandoms(s2, setSize);
53
54 Set intersection = clone(s1);
55 intersection.retainAll(s2);
56 Set diff1 = clone(s1); diff1.removeAll(s2);
57 Set diff2 = clone(s2); diff2.removeAll(s1);
58 Set union = clone(s1); union.addAll(s2);
59
60 if (diff1.removeAll(diff2))
61 throw new Exception("Set algebra identity 2 failed");
62 if (diff1.removeAll(intersection))
63 throw new Exception("Set algebra identity 3 failed");
64 if (diff2.removeAll(diff1))
65 throw new Exception("Set algebra identity 4 failed");
66 if (diff2.removeAll(intersection))
67 throw new Exception("Set algebra identity 5 failed");
68 if (intersection.removeAll(diff1))
69 throw new Exception("Set algebra identity 6 failed");
70 if (intersection.removeAll(diff1))
71 throw new Exception("Set algebra identity 7 failed");
72
73 intersection.addAll(diff1); intersection.addAll(diff2);
74 if (!intersection.equals(union))
75 throw new Exception("Set algebra identity 1 failed");
76
77 if (new LinkedHashSet(union).hashCode() != union.hashCode())
78 throw new Exception("Incorrect hashCode computation.");
79
80 Iterator e = union.iterator();
81 while (e.hasNext())
82 if (!intersection.remove(e.next()))
83 throw new Exception("Couldn't remove element from copy.");
84 if (!intersection.isEmpty())
85 throw new Exception("Copy nonempty after deleting all elements.");
86
87 e = union.iterator();
88 while (e.hasNext()) {
89 Object o = e.next();
90 if (!union.contains(o))
91 throw new Exception("Set doesn't contain one of its elements.");
92 e.remove();
93 if (union.contains(o))
94 throw new Exception("Set contains element after deletion.");
95 }
96 if (!union.isEmpty())
97 throw new Exception("Set nonempty after deleting all elements.");
98
99 s1.clear();
100 if (!s1.isEmpty())
101 throw new Exception("Set nonempty after clear.");
102 }
103 System.err.println("Success.");
104 }
105
106 static Set clone(Set s) throws Exception {
107 Set clone;
108 int method = rnd.nextInt(3);
109 clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() :
110 (method==1 ? new LinkedHashSet(Arrays.asList(s.toArray())) :
111 serClone(s)));
112 if (!s.equals(clone))
113 throw new Exception("Set not equal to copy: "+method);
114 if (!s.containsAll(clone))
115 throw new Exception("Set does not contain copy.");
116 if (!clone.containsAll(s))
117 throw new Exception("Copy does not contain set.");
118 return clone;
119 }
120
121 private static Set serClone(Set m) {
122 Set result = null;
123 try {
124 // Serialize
125 ByteArrayOutputStream bos = new ByteArrayOutputStream();
126 ObjectOutputStream out = new ObjectOutputStream(bos);
127 out.writeObject(m);
128 out.flush();
129
130 // Deserialize
131 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
132 out.close();
133 ObjectInputStream in = new ObjectInputStream(bis);
134 result = (Set)in.readObject();
135 in.close();
136 } catch (Exception e) {
137 e.printStackTrace();
138 }
139 return result;
140 }
141
142 static void AddRandoms(Set s, int n) throws Exception {
143 for (int i = 0; i < n; i++) {
144 Integer e = rnd.nextInt(n);
145
146 int preSize = s.size();
147 boolean prePresent = s.contains(e);
148 boolean added = s.add(e);
149 if (!s.contains(e))
150 throw new Exception("Element not present after addition.");
151 if (added == prePresent)
152 throw new Exception("added == alreadyPresent");
153 int postSize = s.size();
154 if (added && preSize == postSize)
155 throw new Exception("Add returned true, but size didn't change.");
156 if (!added && preSize != postSize)
157 throw new Exception("Add returned false, but size changed.");
158 }
159 }
160 }