1 |
|
/* |
2 |
< |
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2003, 2013, 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 |
23 |
|
|
24 |
|
/* |
25 |
|
* @test |
26 |
< |
* @bug 4904067 5023830 |
26 |
> |
* @bug 4904067 5023830 7129185 |
27 |
|
* @summary Unit test for Collections.checkedMap |
28 |
|
* @author Josh Bloch |
29 |
+ |
* @run testng CheckedMapBash |
30 |
+ |
* @key randomness |
31 |
|
*/ |
32 |
|
|
33 |
|
import java.util.*; |
34 |
+ |
import java.util.function.Supplier; |
35 |
+ |
import org.testng.annotations.Test; |
36 |
+ |
import org.testng.annotations.DataProvider; |
37 |
+ |
|
38 |
+ |
import static org.testng.Assert.fail; |
39 |
+ |
import static org.testng.Assert.assertTrue; |
40 |
|
|
41 |
|
public class CheckedMapBash { |
42 |
< |
static Random rnd = new Random(); |
43 |
< |
static Object nil = new Integer(0); |
42 |
> |
static final Random rnd = new Random(); |
43 |
> |
static final Object nil = new Integer(0); |
44 |
> |
static final int numItr = 100; |
45 |
> |
static final int mapSize = 100; |
46 |
> |
|
47 |
> |
@Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>") |
48 |
> |
public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) { |
49 |
> |
Map m = supplier.get(); |
50 |
> |
Object head = nil; |
51 |
> |
|
52 |
> |
for (int j=0; j<mapSize; j++) { |
53 |
> |
Object newHead; |
54 |
> |
do { |
55 |
> |
newHead = new Integer(rnd.nextInt()); |
56 |
> |
} while (m.containsKey(newHead)); |
57 |
> |
m.put(newHead, head); |
58 |
> |
head = newHead; |
59 |
> |
} |
60 |
> |
if (m.size() != mapSize) |
61 |
> |
fail("Size not as expected."); |
62 |
|
|
63 |
< |
public static void main(String[] args) { |
64 |
< |
int numItr = 100; |
65 |
< |
int mapSize = 100; |
66 |
< |
|
67 |
< |
// Linked List test |
68 |
< |
for (int i=0; i<numItr; i++) { |
69 |
< |
Map m = newMap(); |
70 |
< |
Object head = nil; |
71 |
< |
|
72 |
< |
for (int j=0; j<mapSize; j++) { |
73 |
< |
Object newHead; |
74 |
< |
do { |
75 |
< |
newHead = new Integer(rnd.nextInt()); |
76 |
< |
} 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."); |
63 |
> |
{ |
64 |
> |
HashMap hm = new HashMap(m); |
65 |
> |
if (! (hm.hashCode() == m.hashCode() && |
66 |
> |
hm.entrySet().hashCode() == m.entrySet().hashCode() && |
67 |
> |
hm.keySet().hashCode() == m.keySet().hashCode())) |
68 |
> |
fail("Incorrect hashCode computation."); |
69 |
> |
|
70 |
> |
if (! (hm.equals(m) && |
71 |
> |
hm.entrySet().equals(m.entrySet()) && |
72 |
> |
hm.keySet().equals(m.keySet()) && |
73 |
> |
m.equals(hm) && |
74 |
> |
m.entrySet().equals(hm.entrySet()) && |
75 |
> |
m.keySet().equals(hm.keySet()))) |
76 |
> |
fail("Incorrect equals computation."); |
77 |
|
} |
78 |
|
|
79 |
< |
Map m = newMap(); |
79 |
> |
Map m2 = supplier.get(); m2.putAll(m); |
80 |
> |
m2.values().removeAll(m.keySet()); |
81 |
> |
if (m2.size()!= 1 || !m2.containsValue(nil)) |
82 |
> |
fail("Collection views test failed."); |
83 |
> |
|
84 |
> |
int j=0; |
85 |
> |
while (head != nil) { |
86 |
> |
if (!m.containsKey(head)) |
87 |
> |
fail("Linked list doesn't contain a link."); |
88 |
> |
Object newHead = m.get(head); |
89 |
> |
if (newHead == null) |
90 |
> |
fail("Could not retrieve a link."); |
91 |
> |
m.remove(head); |
92 |
> |
head = newHead; |
93 |
> |
j++; |
94 |
> |
} |
95 |
> |
if (!m.isEmpty()) |
96 |
> |
fail("Map nonempty after removing all links."); |
97 |
> |
if (j != mapSize) |
98 |
> |
fail("Linked list size not as expected."); |
99 |
> |
} |
100 |
> |
|
101 |
> |
@Test(dataProvider = "Supplier<Map<Integer,Integer>>") |
102 |
> |
public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) { |
103 |
> |
Map m = supplier.get(); |
104 |
|
for (int i=0; i<mapSize; i++) |
105 |
|
if (m.put(new Integer(i), new Integer(2*i)) != null) |
106 |
< |
fail("put returns a non-null value erroneously."); |
106 |
> |
fail("put returns a non-null value erroenously."); |
107 |
|
for (int i=0; i<2*mapSize; i++) |
108 |
|
if (m.containsValue(new Integer(i)) != (i%2==0)) |
109 |
|
fail("contains value "+i); |
110 |
|
if (m.put(nil, nil) == null) |
111 |
< |
fail("put returns a null value erroneously."); |
112 |
< |
Map m2 = newMap(); m2.putAll(m); |
111 |
> |
fail("put returns a null value erroenously."); |
112 |
> |
Map m2 = supplier.get(); m2.putAll(m); |
113 |
|
if (!m.equals(m2)) |
114 |
|
fail("Clone not equal to original. (1)"); |
115 |
|
if (!m2.equals(m)) |
142 |
|
fail("Iterator.remove() failed"); |
143 |
|
} |
144 |
|
|
137 |
– |
static Map newMap() { |
138 |
– |
Map m = Collections.checkedMap(new HashMap(), |
139 |
– |
Integer.class, Integer.class); |
145 |
|
|
146 |
< |
if (!m.isEmpty()) |
147 |
< |
fail("New instance non empty."); |
148 |
< |
return m; |
146 |
> |
@DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true) |
147 |
> |
public static Iterator<Object[]> bashNavigableMapProvider() { |
148 |
> |
ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps()); |
149 |
> |
iters.ensureCapacity(numItr * iters.size()); |
150 |
> |
for (int each=1; each < numItr; each++) { |
151 |
> |
iters.addAll( makeCheckedMaps()); |
152 |
> |
} |
153 |
> |
return iters.iterator(); |
154 |
> |
} |
155 |
> |
|
156 |
> |
@DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true) |
157 |
> |
public static Iterator<Object[]> navigableMapProvider() { |
158 |
> |
return makeCheckedMaps().iterator(); |
159 |
|
} |
160 |
|
|
161 |
< |
static void fail(String s) { |
162 |
< |
throw new RuntimeException(s); |
161 |
> |
public static Collection<Object[]> makeCheckedMaps() { |
162 |
> |
return Arrays.asList( |
163 |
> |
new Object[]{"Collections.checkedMap(HashMap)", |
164 |
> |
(Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}}, |
165 |
> |
new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)", |
166 |
> |
(Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}}, |
167 |
> |
new Object[]{"Collections.checkedMap(TreeSet).descendingSet()", |
168 |
> |
(Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}, |
169 |
> |
new Object[]{"Collections.checkedNavigableMap(TreeSet)", |
170 |
> |
(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}}, |
171 |
> |
new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)", |
172 |
> |
(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}}, |
173 |
> |
new Object[]{"Collections.checkedNavigableMap().descendingSet()", |
174 |
> |
(Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}} |
175 |
> |
); |
176 |
|
} |
177 |
|
} |