2
|
1 |
/*
|
|
2 |
* Copyright 2003-2004 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4904067 5023830
|
|
27 |
* @summary Unit test for Collections.checkedMap
|
|
28 |
* @author Josh Bloch
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
public class CheckedMapBash {
|
|
34 |
static Random rnd = new Random();
|
|
35 |
static Object nil = new Integer(0);
|
|
36 |
|
|
37 |
public static void main(String[] args) {
|
|
38 |
int numItr = 100;
|
|
39 |
int mapSize = 100;
|
|
40 |
|
|
41 |
// Linked List test
|
|
42 |
for (int i=0; i<numItr; i++) {
|
|
43 |
Map m = newMap();
|
|
44 |
Object head = nil;
|
|
45 |
|
|
46 |
for (int j=0; j<mapSize; j++) {
|
|
47 |
Object newHead;
|
|
48 |
do {
|
|
49 |
newHead = new Integer(rnd.nextInt());
|
|
50 |
} while (m.containsKey(newHead));
|
|
51 |
m.put(newHead, head);
|
|
52 |
head = newHead;
|
|
53 |
}
|
|
54 |
if (m.size() != mapSize)
|
|
55 |
fail("Size not as expected.");
|
|
56 |
|
|
57 |
{
|
|
58 |
HashMap hm = new HashMap(m);
|
|
59 |
if (! (hm.hashCode() == m.hashCode() &&
|
|
60 |
hm.entrySet().hashCode() == m.entrySet().hashCode() &&
|
|
61 |
hm.keySet().hashCode() == m.keySet().hashCode()))
|
|
62 |
fail("Incorrect hashCode computation.");
|
|
63 |
|
|
64 |
if (! (hm.equals(m) &&
|
|
65 |
hm.entrySet().equals(m.entrySet()) &&
|
|
66 |
hm.keySet().equals(m.keySet()) &&
|
|
67 |
m.equals(hm) &&
|
|
68 |
m.entrySet().equals(hm.entrySet()) &&
|
|
69 |
m.keySet().equals(hm.keySet())))
|
|
70 |
fail("Incorrect equals computation.");
|
|
71 |
}
|
|
72 |
|
|
73 |
Map m2 = newMap(); m2.putAll(m);
|
|
74 |
m2.values().removeAll(m.keySet());
|
|
75 |
if (m2.size()!= 1 || !m2.containsValue(nil))
|
|
76 |
fail("Collection views test failed.");
|
|
77 |
|
|
78 |
int j=0;
|
|
79 |
while (head != nil) {
|
|
80 |
if (!m.containsKey(head))
|
|
81 |
fail("Linked list doesn't contain a link.");
|
|
82 |
Object newHead = m.get(head);
|
|
83 |
if (newHead == null)
|
|
84 |
fail("Could not retrieve a link.");
|
|
85 |
m.remove(head);
|
|
86 |
head = newHead;
|
|
87 |
j++;
|
|
88 |
}
|
|
89 |
if (!m.isEmpty())
|
|
90 |
fail("Map nonempty after removing all links.");
|
|
91 |
if (j != mapSize)
|
|
92 |
fail("Linked list size not as expected.");
|
|
93 |
}
|
|
94 |
|
|
95 |
Map m = newMap();
|
|
96 |
for (int i=0; i<mapSize; i++)
|
|
97 |
if (m.put(new Integer(i), new Integer(2*i)) != null)
|
|
98 |
fail("put returns a non-null value erroenously.");
|
|
99 |
for (int i=0; i<2*mapSize; i++)
|
|
100 |
if (m.containsValue(new Integer(i)) != (i%2==0))
|
|
101 |
fail("contains value "+i);
|
|
102 |
if (m.put(nil, nil) == null)
|
|
103 |
fail("put returns a null value erroenously.");
|
|
104 |
Map m2 = newMap(); m2.putAll(m);
|
|
105 |
if (!m.equals(m2))
|
|
106 |
fail("Clone not equal to original. (1)");
|
|
107 |
if (!m2.equals(m))
|
|
108 |
fail("Clone not equal to original. (2)");
|
|
109 |
Set s = m.entrySet(), s2 = m2.entrySet();
|
|
110 |
if (!s.equals(s2))
|
|
111 |
fail("Clone not equal to original. (3)");
|
|
112 |
if (!s2.equals(s))
|
|
113 |
fail("Clone not equal to original. (4)");
|
|
114 |
if (!s.containsAll(s2))
|
|
115 |
fail("Original doesn't contain clone!");
|
|
116 |
if (!s2.containsAll(s))
|
|
117 |
fail("Clone doesn't contain original!");
|
|
118 |
|
|
119 |
s2.removeAll(s);
|
|
120 |
if (!m2.isEmpty())
|
|
121 |
fail("entrySet().removeAll failed.");
|
|
122 |
|
|
123 |
m2.putAll(m);
|
|
124 |
m2.clear();
|
|
125 |
if (!m2.isEmpty())
|
|
126 |
fail("clear failed.");
|
|
127 |
|
|
128 |
Iterator i = m.entrySet().iterator();
|
|
129 |
while(i.hasNext()) {
|
|
130 |
i.next();
|
|
131 |
i.remove();
|
|
132 |
}
|
|
133 |
if (!m.isEmpty())
|
|
134 |
fail("Iterator.remove() failed");
|
|
135 |
}
|
|
136 |
|
|
137 |
static Map newMap() {
|
|
138 |
Map m = Collections.checkedMap(new HashMap(),
|
|
139 |
Integer.class, Integer.class);
|
|
140 |
|
|
141 |
if (!m.isEmpty())
|
|
142 |
fail("New instance non empty.");
|
|
143 |
return m;
|
|
144 |
}
|
|
145 |
|
|
146 |
static void fail(String s) {
|
|
147 |
throw new RuntimeException(s);
|
|
148 |
}
|
|
149 |
}
|