author | redestad |
Fri, 31 May 2019 12:20:21 +0200 | |
changeset 55124 | f91999057a5a |
parent 48541 | 946e34c2dec9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
47730
c7b5b1ce8145
8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents:
47216
diff
changeset
|
24 |
/* |
2 | 25 |
* @test |
26 |
* @bug 4245809 |
|
27 |
* @summary Basic test for LinkedHashSet. (Based on SetBash) |
|
28 |
*/ |
|
29 |
||
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
30 |
import java.io.ByteArrayInputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
31 |
import java.io.ByteArrayOutputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
32 |
import java.io.ObjectInputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
33 |
import java.io.ObjectOutputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
34 |
import java.util.Arrays; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
35 |
import java.util.Iterator; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
36 |
import java.util.LinkedHashSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
37 |
import java.util.Random; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47730
diff
changeset
|
38 |
import java.util.Set; |
2 | 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); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
5506
diff
changeset
|
109 |
clone = (method==0 ? (Set) ((LinkedHashSet)s).clone() : |
2 | 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(); |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
5506
diff
changeset
|
136 |
} catch (Exception e) { |
2 | 137 |
e.printStackTrace(); |
138 |
} |
|
139 |
return result; |
|
140 |
} |
|
141 |
||
142 |
static void AddRandoms(Set s, int n) throws Exception { |
|
47730
c7b5b1ce8145
8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents:
47216
diff
changeset
|
143 |
for (int i = 0; i < n; i++) { |
c7b5b1ce8145
8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents:
47216
diff
changeset
|
144 |
Integer e = rnd.nextInt(n); |
2 | 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 |
} |