ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/LinkedHashSet/Basic.java
Revision: 1.3
Committed: Sun Sep 5 21:32:19 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.2: +4 -4 lines
Log Message:
Update legal notices to Oracle wording

File Contents

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