10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
13 |
+ |
import java.util.Deque; |
14 |
+ |
import java.util.HashSet; |
15 |
+ |
import java.util.Iterator; |
16 |
+ |
import java.util.List; |
17 |
+ |
import java.util.NoSuchElementException; |
18 |
+ |
import java.util.Queue; |
19 |
+ |
import java.util.Spliterator; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.Executors; |
22 |
|
import java.util.concurrent.ExecutorService; |
23 |
|
import java.util.concurrent.Future; |
24 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
25 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.atomic.AtomicLong; |
27 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
28 |
|
import java.util.function.Consumer; |
29 |
+ |
import java.util.function.Predicate; |
30 |
|
|
31 |
|
import junit.framework.Test; |
32 |
|
|
49 |
|
impl); |
50 |
|
} |
51 |
|
|
52 |
+ |
/** Checks properties of empty collections. */ |
53 |
+ |
public void testEmptyMeansEmpty() { |
54 |
+ |
Collection c = impl.emptyCollection(); |
55 |
+ |
assertTrue(c.isEmpty()); |
56 |
+ |
assertEquals(0, c.size()); |
57 |
+ |
assertEquals("[]", c.toString()); |
58 |
+ |
{ |
59 |
+ |
Object[] a = c.toArray(); |
60 |
+ |
assertEquals(0, a.length); |
61 |
+ |
assertSame(Object[].class, a.getClass()); |
62 |
+ |
} |
63 |
+ |
{ |
64 |
+ |
Object[] a = new Object[0]; |
65 |
+ |
assertSame(a, c.toArray(a)); |
66 |
+ |
} |
67 |
+ |
{ |
68 |
+ |
Integer[] a = new Integer[0]; |
69 |
+ |
assertSame(a, c.toArray(a)); |
70 |
+ |
} |
71 |
+ |
{ |
72 |
+ |
Integer[] a = { 1, 2, 3}; |
73 |
+ |
assertSame(a, c.toArray(a)); |
74 |
+ |
assertNull(a[0]); |
75 |
+ |
assertSame(2, a[1]); |
76 |
+ |
assertSame(3, a[2]); |
77 |
+ |
} |
78 |
+ |
assertIteratorExhausted(c.iterator()); |
79 |
+ |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
80 |
+ |
c.forEach(alwaysThrows); |
81 |
+ |
c.iterator().forEachRemaining(alwaysThrows); |
82 |
+ |
c.spliterator().forEachRemaining(alwaysThrows); |
83 |
+ |
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
84 |
+ |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
85 |
+ |
Queue q = (Queue) c; |
86 |
+ |
assertNull(q.peek()); |
87 |
+ |
assertNull(q.poll()); |
88 |
+ |
} |
89 |
+ |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
90 |
+ |
Deque d = (Deque) c; |
91 |
+ |
assertNull(d.peekFirst()); |
92 |
+ |
assertNull(d.peekLast()); |
93 |
+ |
assertNull(d.pollFirst()); |
94 |
+ |
assertNull(d.pollLast()); |
95 |
+ |
assertIteratorExhausted(d.descendingIterator()); |
96 |
+ |
} |
97 |
+ |
} |
98 |
+ |
|
99 |
+ |
public void testNullPointerExceptions() { |
100 |
+ |
Collection c = impl.emptyCollection(); |
101 |
+ |
assertThrows( |
102 |
+ |
NullPointerException.class, |
103 |
+ |
() -> c.addAll(null), |
104 |
+ |
() -> c.containsAll(null), |
105 |
+ |
() -> c.retainAll(null), |
106 |
+ |
() -> c.removeAll(null), |
107 |
+ |
() -> c.removeIf(null), |
108 |
+ |
() -> c.forEach(null), |
109 |
+ |
() -> c.iterator().forEachRemaining(null), |
110 |
+ |
() -> c.spliterator().forEachRemaining(null), |
111 |
+ |
() -> c.spliterator().tryAdvance(null), |
112 |
+ |
() -> c.toArray(null)); |
113 |
+ |
|
114 |
+ |
if (!impl.permitsNulls()) { |
115 |
+ |
assertThrows( |
116 |
+ |
NullPointerException.class, |
117 |
+ |
() -> c.add(null)); |
118 |
+ |
} |
119 |
+ |
if (!impl.permitsNulls() |
120 |
+ |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
121 |
+ |
Queue q = (Queue) c; |
122 |
+ |
assertThrows( |
123 |
+ |
NullPointerException.class, |
124 |
+ |
() -> q.offer(null)); |
125 |
+ |
} |
126 |
+ |
if (!impl.permitsNulls() |
127 |
+ |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
128 |
+ |
Deque d = (Deque) c; |
129 |
+ |
assertThrows( |
130 |
+ |
NullPointerException.class, |
131 |
+ |
() -> d.addFirst(null), |
132 |
+ |
() -> d.addLast(null), |
133 |
+ |
() -> d.offerFirst(null), |
134 |
+ |
() -> d.offerLast(null), |
135 |
+ |
() -> d.push(null), |
136 |
+ |
() -> d.descendingIterator().forEachRemaining(null)); |
137 |
+ |
} |
138 |
+ |
} |
139 |
+ |
|
140 |
+ |
public void testNoSuchElementExceptions() { |
141 |
+ |
Collection c = impl.emptyCollection(); |
142 |
+ |
assertThrows( |
143 |
+ |
NoSuchElementException.class, |
144 |
+ |
() -> c.iterator().next()); |
145 |
+ |
|
146 |
+ |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
147 |
+ |
Queue q = (Queue) c; |
148 |
+ |
assertThrows( |
149 |
+ |
NoSuchElementException.class, |
150 |
+ |
() -> q.element(), |
151 |
+ |
() -> q.remove()); |
152 |
+ |
} |
153 |
+ |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
154 |
+ |
Deque d = (Deque) c; |
155 |
+ |
assertThrows( |
156 |
+ |
NoSuchElementException.class, |
157 |
+ |
() -> d.getFirst(), |
158 |
+ |
() -> d.getLast(), |
159 |
+ |
() -> d.removeFirst(), |
160 |
+ |
() -> d.removeLast(), |
161 |
+ |
() -> d.pop(), |
162 |
+ |
() -> d.descendingIterator().next()); |
163 |
+ |
} |
164 |
+ |
} |
165 |
+ |
|
166 |
+ |
public void testRemoveIf() { |
167 |
+ |
Collection c = impl.emptyCollection(); |
168 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
169 |
+ |
int n = rnd.nextInt(6); |
170 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
171 |
+ |
AtomicReference threwAt = new AtomicReference(null); |
172 |
+ |
ArrayList survivors = new ArrayList(c); |
173 |
+ |
ArrayList accepts = new ArrayList(); |
174 |
+ |
ArrayList rejects = new ArrayList(); |
175 |
+ |
Predicate randomPredicate = (e) -> { |
176 |
+ |
assertNull(threwAt.get()); |
177 |
+ |
switch (rnd.nextInt(3)) { |
178 |
+ |
case 0: accepts.add(e); return true; |
179 |
+ |
case 1: rejects.add(e); return false; |
180 |
+ |
case 2: threwAt.set(e); throw new ArithmeticException(); |
181 |
+ |
default: throw new AssertionError(); |
182 |
+ |
} |
183 |
+ |
}; |
184 |
+ |
try { |
185 |
+ |
try { |
186 |
+ |
boolean modified = c.removeIf(randomPredicate); |
187 |
+ |
if (!modified) { |
188 |
+ |
assertNull(threwAt.get()); |
189 |
+ |
assertEquals(n, rejects.size()); |
190 |
+ |
assertEquals(0, accepts.size()); |
191 |
+ |
} |
192 |
+ |
} catch (ArithmeticException ok) {} |
193 |
+ |
survivors.removeAll(accepts); |
194 |
+ |
assertEquals(n - accepts.size(), c.size()); |
195 |
+ |
assertTrue(c.containsAll(survivors)); |
196 |
+ |
assertTrue(survivors.containsAll(rejects)); |
197 |
+ |
for (Object x : accepts) assertFalse(c.contains(x)); |
198 |
+ |
if (threwAt.get() == null) |
199 |
+ |
assertEquals(accepts.size() + rejects.size(), n); |
200 |
+ |
} catch (Throwable ex) { |
201 |
+ |
System.err.println(impl.klazz()); |
202 |
+ |
System.err.printf("c=%s%n", c); |
203 |
+ |
System.err.printf("n=%d%n", n); |
204 |
+ |
System.err.printf("accepts=%s%n", accepts); |
205 |
+ |
System.err.printf("rejects=%s%n", rejects); |
206 |
+ |
System.err.println(survivors); |
207 |
+ |
System.err.printf("threw=%s%n", threwAt.get()); |
208 |
+ |
throw ex; |
209 |
+ |
} |
210 |
+ |
} |
211 |
+ |
|
212 |
+ |
/** |
213 |
+ |
* Various ways of traversing a collection yield same elements |
214 |
+ |
*/ |
215 |
+ |
public void testIteratorEquivalence() { |
216 |
+ |
Collection c = impl.emptyCollection(); |
217 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
218 |
+ |
int n = rnd.nextInt(6); |
219 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
220 |
+ |
ArrayList iterated = new ArrayList(); |
221 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
222 |
+ |
ArrayList spliterated = new ArrayList(); |
223 |
+ |
ArrayList foreached = new ArrayList(); |
224 |
+ |
for (Object x : c) iterated.add(x); |
225 |
+ |
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
226 |
+ |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
227 |
+ |
c.forEach(e -> foreached.add(e)); |
228 |
+ |
boolean ordered = |
229 |
+ |
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
230 |
+ |
if (c instanceof List || c instanceof Deque) |
231 |
+ |
assertTrue(ordered); |
232 |
+ |
if (ordered) { |
233 |
+ |
assertEquals(iterated, iteratedForEachRemaining); |
234 |
+ |
assertEquals(iterated, spliterated); |
235 |
+ |
assertEquals(iterated, foreached); |
236 |
+ |
} else { |
237 |
+ |
HashSet cset = new HashSet(c); |
238 |
+ |
assertEquals(cset, new HashSet(iterated)); |
239 |
+ |
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
240 |
+ |
assertEquals(cset, new HashSet(spliterated)); |
241 |
+ |
assertEquals(cset, new HashSet(foreached)); |
242 |
+ |
} |
243 |
+ |
if (c instanceof Deque) { |
244 |
+ |
Deque d = (Deque) c; |
245 |
+ |
ArrayList descending = new ArrayList(); |
246 |
+ |
ArrayList descendingForEachRemaining = new ArrayList(); |
247 |
+ |
for (Iterator it = d.descendingIterator(); it.hasNext(); ) |
248 |
+ |
descending.add(it.next()); |
249 |
+ |
d.descendingIterator().forEachRemaining( |
250 |
+ |
e -> descendingForEachRemaining.add(e)); |
251 |
+ |
Collections.reverse(descending); |
252 |
+ |
Collections.reverse(descendingForEachRemaining); |
253 |
+ |
assertEquals(iterated, descending); |
254 |
+ |
assertEquals(iterated, descendingForEachRemaining); |
255 |
+ |
} |
256 |
+ |
} |
257 |
+ |
|
258 |
+ |
/** |
259 |
+ |
* Calling Iterator#remove() after Iterator#forEachRemaining |
260 |
+ |
* should remove last element |
261 |
+ |
*/ |
262 |
+ |
public void testRemoveAfterForEachRemaining() { |
263 |
+ |
Collection c = impl.emptyCollection(); |
264 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
265 |
+ |
{ |
266 |
+ |
int n = 3 + rnd.nextInt(2); |
267 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
268 |
+ |
Iterator it = c.iterator(); |
269 |
+ |
assertTrue(it.hasNext()); |
270 |
+ |
assertEquals(impl.makeElement(0), it.next()); |
271 |
+ |
assertTrue(it.hasNext()); |
272 |
+ |
assertEquals(impl.makeElement(1), it.next()); |
273 |
+ |
it.forEachRemaining((e) -> {}); |
274 |
+ |
it.remove(); |
275 |
+ |
assertEquals(n - 1, c.size()); |
276 |
+ |
for (int i = 0; i < n - 1; i++) |
277 |
+ |
assertTrue(c.contains(impl.makeElement(i))); |
278 |
+ |
assertFalse(c.contains(impl.makeElement(n - 1))); |
279 |
+ |
} |
280 |
+ |
if (c instanceof Deque) { |
281 |
+ |
Deque d = (Deque) impl.emptyCollection(); |
282 |
+ |
int n = 3 + rnd.nextInt(2); |
283 |
+ |
for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); |
284 |
+ |
Iterator it = d.descendingIterator(); |
285 |
+ |
assertTrue(it.hasNext()); |
286 |
+ |
assertEquals(impl.makeElement(n - 1), it.next()); |
287 |
+ |
assertTrue(it.hasNext()); |
288 |
+ |
assertEquals(impl.makeElement(n - 2), it.next()); |
289 |
+ |
it.forEachRemaining((e) -> {}); |
290 |
+ |
it.remove(); |
291 |
+ |
assertEquals(n - 1, d.size()); |
292 |
+ |
for (int i = 1; i < n; i++) |
293 |
+ |
assertTrue(d.contains(impl.makeElement(i))); |
294 |
+ |
assertFalse(d.contains(impl.makeElement(0))); |
295 |
+ |
} |
296 |
+ |
} |
297 |
+ |
|
298 |
|
/** |
299 |
|
* stream().forEach returns elements in the collection |
300 |
|
*/ |