blob: 6c86c947f6c297e6176fcfd26f59fc7aa8f70766 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
martineb76fa62008-05-10 11:49:25 -07002 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
27 * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
martin4d0abcd2008-05-10 12:14:53 -070028 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
duke6e45e102007-12-01 00:00:00 +000029 * @summary Run many tests on many Collection and Map implementations
30 * @author Martin Buchholz
31 */
32
33/* Mother Of All (Collection) Tests
34 *
35 * Testing of collection classes is often spotty, because many tests
36 * need to be performed on many implementations, but the onus on
37 * writing the tests falls on the engineer introducing the new
38 * implementation.
39 *
40 * The idea of this mega-test is that:
41 *
42 * An engineer adding a new collection implementation could simply add
43 * their new implementation to a list of implementations in this
44 * test's main method. Any general purpose Collection<Integer> or
45 * Map<Integer,Integer> class is appropriate.
46 *
47 * An engineer fixing a regression could add their regression test here and
48 * simultaneously test all other implementations.
49 */
50
51import java.io.*;
52import java.util.*;
53import java.util.concurrent.*;
54import static java.util.Collections.*;
55
56public class MOAT {
57 public static void realMain(String[] args) {
58
59 testCollection(new LinkedHashSet<Integer>());
60 testCollection(new HashSet<Integer>());
61 testCollection(new Vector<Integer>());
62 testCollection(new Vector<Integer>().subList(0,0));
63 testCollection(new ArrayDeque<Integer>());
64 testCollection(new ArrayList<Integer>());
65 testCollection(new ArrayList<Integer>().subList(0,0));
66 testCollection(new LinkedList<Integer>());
67 testCollection(new LinkedList<Integer>().subList(0,0));
68 testCollection(new TreeSet<Integer>());
69
70 testCollection(new CopyOnWriteArrayList<Integer>());
71 testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
72 testCollection(new CopyOnWriteArraySet<Integer>());
73 testCollection(new PriorityQueue<Integer>());
74 testCollection(new PriorityBlockingQueue<Integer>());
75 testCollection(new ArrayBlockingQueue<Integer>(20));
76 testCollection(new LinkedBlockingQueue<Integer>(20));
77 testCollection(new LinkedBlockingDeque<Integer>(20));
78 testCollection(new ConcurrentLinkedQueue<Integer>());
dl54d3afc2009-11-02 17:25:38 -080079 testCollection(new LinkedTransferQueue<Integer>());
duke6e45e102007-12-01 00:00:00 +000080 testCollection(new ConcurrentSkipListSet<Integer>());
81 testCollection(Arrays.asList(new Integer(42)));
82 testCollection(Arrays.asList(1,2,3));
83 testCollection(nCopies(25,1));
84 testImmutableList(nCopies(25,1));
85 testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
86
87 testMap(new HashMap<Integer,Integer>());
88 testMap(new LinkedHashMap<Integer,Integer>());
89 testMap(new WeakHashMap<Integer,Integer>());
90 testMap(new IdentityHashMap<Integer,Integer>());
91 testMap(new TreeMap<Integer,Integer>());
92 testMap(new Hashtable<Integer,Integer>());
93 testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
94 testMap(new ConcurrentSkipListMap<Integer,Integer>());
95
96 // Empty collections
97 final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
98 testCollection(emptyArray);
99 testEmptyList(emptyArray);
100 THROWS(IndexOutOfBoundsException.class,
101 new Fun(){void f(){ emptyArray.set(0,1); }});
102 THROWS(UnsupportedOperationException.class,
103 new Fun(){void f(){ emptyArray.add(0,1); }});
104
105 List<Integer> noOne = nCopies(0,1);
106 testCollection(noOne);
107 testEmptyList(noOne);
108 testImmutableList(noOne);
109
110 Set<Integer> emptySet = emptySet();
111 testCollection(emptySet);
112 testEmptySet(emptySet);
113 testEmptySet(EMPTY_SET);
114 testImmutableSet(emptySet);
115
116 List<Integer> emptyList = emptyList();
117 testCollection(emptyList);
118 testEmptyList(emptyList);
119 testEmptyList(EMPTY_LIST);
120 testImmutableList(emptyList);
121
122 Map<Integer,Integer> emptyMap = emptyMap();
123 testMap(emptyMap);
124 testEmptyMap(emptyMap);
125 testEmptyMap(EMPTY_MAP);
126 testImmutableMap(emptyMap);
127
128 // Singleton collections
129 Set<Integer> singletonSet = singleton(1);
130 equal(singletonSet.size(), 1);
131 testCollection(singletonSet);
132 testImmutableSet(singletonSet);
133
134 List<Integer> singletonList = singletonList(1);
135 equal(singletonList.size(), 1);
136 testCollection(singletonList);
137 testImmutableList(singletonList);
138 testImmutableList(singletonList.subList(0,1));
139 testImmutableList(singletonList.subList(0,1).subList(0,1));
140 testEmptyList(singletonList.subList(0,0));
141 testEmptyList(singletonList.subList(0,0).subList(0,0));
142
143 Map<Integer,Integer> singletonMap = singletonMap(1,2);
144 equal(singletonMap.size(), 1);
145 testMap(singletonMap);
146 testImmutableMap(singletonMap);
147 }
148
149 private static void checkContainsSelf(Collection<Integer> c) {
150 check(c.containsAll(c));
151 check(c.containsAll(Arrays.asList(c.toArray())));
152 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
153 }
154
155 private static void checkContainsEmpty(Collection<Integer> c) {
156 check(c.containsAll(new ArrayList<Integer>()));
157 }
158
martineb76fa62008-05-10 11:49:25 -0700159 private static <T> void testEmptyCollection(Collection<T> c) {
duke6e45e102007-12-01 00:00:00 +0000160 check(c.isEmpty());
161 equal(c.size(), 0);
162 equal(c.toString(),"[]");
163 equal(c.toArray().length, 0);
164 equal(c.toArray(new Object[0]).length, 0);
dlb1e19572009-08-25 19:19:42 -0700165 check(c.toArray(new Object[]{42})[0] == null);
duke6e45e102007-12-01 00:00:00 +0000166
167 Object[] a = new Object[1]; a[0] = Boolean.TRUE;
168 equal(c.toArray(a), a);
169 equal(a[0], null);
martineb76fa62008-05-10 11:49:25 -0700170 testEmptyIterator(c.iterator());
171 }
172
173 static <T> void testEmptyIterator(final Iterator<T> it) {
174 if (rnd.nextBoolean())
175 check(! it.hasNext());
176
177 THROWS(NoSuchElementException.class,
178 new Fun(){void f(){ it.next(); }});
179
180 try { it.remove(); }
181 catch (IllegalStateException _) { pass(); }
182 catch (UnsupportedOperationException _) { pass(); }
183 catch (Throwable t) { unexpected(t); }
184
185 if (rnd.nextBoolean())
186 check(! it.hasNext());
duke6e45e102007-12-01 00:00:00 +0000187 }
188
189 private static void testEmptyList(List<?> c) {
190 testEmptyCollection(c);
191 equal(c.hashCode(), 1);
192 equal2(c, Collections.<Integer>emptyList());
193 }
194
martineb76fa62008-05-10 11:49:25 -0700195 private static <T> void testEmptySet(Set<T> c) {
duke6e45e102007-12-01 00:00:00 +0000196 testEmptyCollection(c);
197 equal(c.hashCode(), 0);
198 equal2(c, Collections.<Integer>emptySet());
martineb76fa62008-05-10 11:49:25 -0700199 if (c instanceof NavigableSet<?>)
200 testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
duke6e45e102007-12-01 00:00:00 +0000201 }
202
203 private static void testImmutableCollection(final Collection<Integer> c) {
204 THROWS(UnsupportedOperationException.class,
205 new Fun(){void f(){ c.add(99); }},
206 new Fun(){void f(){ c.addAll(singleton(99)); }});
207 if (! c.isEmpty()) {
208 final Integer first = c.iterator().next();
209 THROWS(UnsupportedOperationException.class,
210 new Fun(){void f(){ c.clear(); }},
211 new Fun(){void f(){ c.remove(first); }},
212 new Fun(){void f(){ c.removeAll(singleton(first)); }},
213 new Fun(){void f(){ c.retainAll(emptyList()); }}
214 );
215 }
216 }
217
218 private static void testImmutableSet(final Set<Integer> c) {
219 testImmutableCollection(c);
220 }
221
222 private static void testImmutableList(final List<Integer> c) {
223 testList(c);
224 testImmutableCollection(c);
225 THROWS(UnsupportedOperationException.class,
226 new Fun(){void f(){ c.set(0,42); }},
227 new Fun(){void f(){ c.add(0,42); }},
228 new Fun(){void f(){ c.addAll(0,singleton(86)); }});
229 if (! c.isEmpty())
230 THROWS(UnsupportedOperationException.class,
231 new Fun(){void f(){
232 Iterator<Integer> it = c.iterator();
233 it.next(); it.remove();}},
234 new Fun(){void f(){
235 ListIterator<Integer> it = c.listIterator();
236 it.next(); it.remove();}});
237 }
238
239 private static void clear(Collection<Integer> c) {
240 try { c.clear(); }
241 catch (Throwable t) { unexpected(t); }
242 testEmptyCollection(c);
243 }
244
martineb76fa62008-05-10 11:49:25 -0700245 private static <K,V> void testEmptyMap(final Map<K,V> m) {
duke6e45e102007-12-01 00:00:00 +0000246 check(m.isEmpty());
247 equal(m.size(), 0);
248 equal(m.toString(),"{}");
249 testEmptySet(m.keySet());
250 testEmptySet(m.entrySet());
251 testEmptyCollection(m.values());
martin4d0abcd2008-05-10 12:14:53 -0700252
253 try { check(! m.containsValue(null)); }
254 catch (NullPointerException _) { /* OK */ }
255 try { check(! m.containsKey(null)); }
256 catch (NullPointerException _) { /* OK */ }
257 check(! m.containsValue(1));
258 check(! m.containsKey(1));
duke6e45e102007-12-01 00:00:00 +0000259 }
260
261 private static void testImmutableMap(final Map<Integer,Integer> m) {
262 THROWS(UnsupportedOperationException.class,
263 new Fun(){void f(){ m.put(1,1); }},
264 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
265 if (! m.isEmpty()) {
266 final Integer first = m.keySet().iterator().next();
267 THROWS(UnsupportedOperationException.class,
268 new Fun(){void f(){ m.remove(first); }},
269 new Fun(){void f(){ m.clear(); }});
270 final Map.Entry<Integer,Integer> me
271 = m.entrySet().iterator().next();
272 Integer key = me.getKey();
273 Integer val = me.getValue();
274 THROWS(UnsupportedOperationException.class,
275 new Fun(){void f(){ me.setValue(3); }});
276 equal(key, me.getKey());
277 equal(val, me.getValue());
278 }
279 testImmutableSet(m.keySet());
280 testImmutableCollection(m.values());
281 //testImmutableSet(m.entrySet());
282 }
283
284 private static void clear(Map<?,?> m) {
285 try { m.clear(); }
286 catch (Throwable t) { unexpected(t); }
287 testEmptyMap(m);
288 }
289
290 private static void oneElement(Collection<Integer> c) {
291 clear(c);
292 try {
293 check(c.add(-42));
294 equal(c.toString(), "[-42]");
295 if (c instanceof Set) check(! c.add(-42));
296 } catch (Throwable t) { unexpected(t); }
297 check(! c.isEmpty()); check(c.size() == 1);
298 }
299
300 private static boolean supportsAdd(Collection<Integer> c) {
301 try { check(c.add(778347983)); }
302 catch (UnsupportedOperationException t) { return false; }
303 catch (Throwable t) { unexpected(t); }
304
305 try {
306 check(c.contains(778347983));
307 check(c.remove(778347983));
308 } catch (Throwable t) { unexpected(t); }
309 return true;
310 }
311
312 private static boolean supportsRemove(Collection<Integer> c) {
313 try { check(! c.remove(19134032)); }
314 catch (UnsupportedOperationException t) { return false; }
315 catch (Throwable t) { unexpected(t); }
316 return true;
317 }
318
319 private static void checkFunctionalInvariants(Collection<Integer> c) {
320 try {
321 checkContainsSelf(c);
322 checkContainsEmpty(c);
323 check(c.size() != 0 ^ c.isEmpty());
324
325 {
326 int size = 0;
327 for (Integer i : c) size++;
328 check(c.size() == size);
329 }
330
331 check(c.toArray().length == c.size());
332 check(c.toArray().getClass() == Object[].class
333 ||
334 // !!!!
335 // 6260652: (coll) Arrays.asList(x).toArray().getClass()
336 // should be Object[].class
337 (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
338 );
339 for (int size : new int[]{0,1,c.size(), c.size()+1}) {
340 Integer[] a = c.toArray(new Integer[size]);
341 check((size > c.size()) || a.length == c.size());
342 int i = 0; for (Integer j : c) check(a[i++] == j);
343 check((size <= c.size()) || (a[c.size()] == null));
344 check(a.getClass() == Integer[].class);
345 }
346
347 check(c.equals(c));
348 if (c instanceof Serializable) {
349 //System.out.printf("Serializing %s%n", c.getClass().getName());
350 try {
351 Object clone = serialClone(c);
352 equal(c instanceof Serializable,
353 clone instanceof Serializable);
354 equal(c instanceof RandomAccess,
355 clone instanceof RandomAccess);
356 if ((c instanceof List) || (c instanceof Set))
357 equal(c, clone);
358 else
359 equal(new HashSet<Integer>(c),
360 new HashSet<Integer>(serialClone(c)));
361 } catch (Error xxx) {
362 if (! (xxx.getCause() instanceof NotSerializableException))
363 throw xxx;
364 }
365 }
366 }
367 catch (Throwable t) { unexpected(t); }
368 }
369
370 //----------------------------------------------------------------
371 // If add(null) succeeds, contains(null) & remove(null) should succeed
372 //----------------------------------------------------------------
373 private static void testNullElement(Collection<Integer> c) {
374 // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
375 if (c instanceof TreeSet) return;
376
377 try {
378 check(c.add(null));
379 if (c.size() == 1)
380 equal(c.toString(), "[null]");
381 try {
382 checkFunctionalInvariants(c);
383 check(c.contains(null));
384 check(c.remove(null));
385 }
386 catch (Throwable t) { unexpected(t); }
387 }
388 catch (NullPointerException e) { /* OK */ }
389 catch (Throwable t) { unexpected(t); }
390 }
391
392
393 //----------------------------------------------------------------
394 // If add("x") succeeds, contains("x") & remove("x") should succeed
395 //----------------------------------------------------------------
396 @SuppressWarnings("unchecked")
397 private static void testStringElement(Collection<Integer> c) {
398 Collection x = (Collection)c; // Make type-unsafe
399 try {
400 check(x.add("x"));
401 try {
402 check(x.contains("x"));
403 check(x.remove("x"));
404 } catch (Throwable t) { unexpected(t); }
405 }
406 catch (ClassCastException e) { /* OK */ }
407 catch (Throwable t) { unexpected(t); }
408 }
409
410 private static void testConcurrentCollection(Collection<Integer> c) {
411 try {
412 c.add(1);
413 Iterator<Integer> it = c.iterator();
414 check(it.hasNext());
415 clear(c);
416 check(it.next() instanceof Integer); // No CME
417 check(c.isEmpty());
418 }
419 catch (Throwable t) { unexpected(t); }
420 }
421
422 private static void testQueue(Queue<Integer> q) {
423 q.clear();
martinffd2d052009-11-05 16:12:45 -0800424 for (int i = 0; i < 5; i++) {
425 testQueueAddRemove(q, null);
426 testQueueAddRemove(q, 537);
duke6e45e102007-12-01 00:00:00 +0000427 q.add(i);
martinffd2d052009-11-05 16:12:45 -0800428 }
duke6e45e102007-12-01 00:00:00 +0000429 equal(q.size(), 5);
430 checkFunctionalInvariants(q);
431 q.poll();
432 equal(q.size(), 4);
433 checkFunctionalInvariants(q);
dl8e56eb52009-07-28 17:17:55 -0700434 if ((q instanceof LinkedBlockingQueue) ||
435 (q instanceof LinkedBlockingDeque) ||
436 (q instanceof ConcurrentLinkedQueue)) {
437 testQueueIteratorRemove(q);
438 }
439 }
440
martinffd2d052009-11-05 16:12:45 -0800441 private static void testQueueAddRemove(final Queue<Integer> q,
442 final Integer e) {
443 final List<Integer> originalContents = new ArrayList<Integer>(q);
444 final boolean isEmpty = q.isEmpty();
445 final boolean isList = (q instanceof List);
446 final List asList = isList ? (List) q : null;
447 check(!q.contains(e));
448 try {
449 q.add(e);
450 } catch (NullPointerException npe) {
451 check(e == null);
452 return; // Null elements not supported
453 }
454 check(q.contains(e));
455 check(q.remove(e));
456 check(!q.contains(e));
457 equal(new ArrayList<Integer>(q), originalContents);
458
459 if (q instanceof Deque<?>) {
460 final Deque<Integer> deq = (Deque<Integer>) q;
461 final List<Integer> singleton = Collections.singletonList(e);
462
463 // insert, query, remove element at head
464 if (isEmpty) {
465 THROWS(NoSuchElementException.class,
466 new Fun(){void f(){ deq.getFirst(); }},
467 new Fun(){void f(){ deq.element(); }},
468 new Fun(){void f(){ deq.iterator().next(); }});
469 check(deq.peekFirst() == null);
470 check(deq.peek() == null);
471 } else {
472 check(deq.getFirst() != e);
473 check(deq.element() != e);
474 check(deq.iterator().next() != e);
475 check(deq.peekFirst() != e);
476 check(deq.peek() != e);
477 }
478 check(!deq.contains(e));
479 check(!deq.removeFirstOccurrence(e));
480 check(!deq.removeLastOccurrence(e));
481 if (isList) {
482 check(asList.indexOf(e) == -1);
483 check(asList.lastIndexOf(e) == -1);
484 }
485 switch (rnd.nextInt(isList ? 4 : 3)) {
486 case 0: deq.addFirst(e); break;
487 case 1: check(deq.offerFirst(e)); break;
488 case 2: deq.push(e); break;
489 case 3: asList.add(0, e); break;
490 default: throw new AssertionError();
491 }
492 check(deq.peekFirst() == e);
493 check(deq.getFirst() == e);
494 check(deq.element() == e);
495 check(deq.peek() == e);
496 check(deq.iterator().next() == e);
497 check(deq.contains(e));
498 if (isList) {
499 check(asList.get(0) == e);
500 check(asList.indexOf(e) == 0);
501 check(asList.lastIndexOf(e) == 0);
502 check(asList.subList(0, 1).equals(singleton));
503 }
504 switch (rnd.nextInt(isList ? 11 : 9)) {
505 case 0: check(deq.pollFirst() == e); break;
506 case 1: check(deq.removeFirst() == e); break;
507 case 2: check(deq.remove() == e); break;
508 case 3: check(deq.pop() == e); break;
509 case 4: check(deq.removeFirstOccurrence(e)); break;
510 case 5: check(deq.removeLastOccurrence(e)); break;
511 case 6: check(deq.remove(e)); break;
512 case 7: check(deq.removeAll(singleton)); break;
513 case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break;
514 case 9: asList.remove(0); break;
515 case 10: asList.subList(0, 1).clear(); break;
516 default: throw new AssertionError();
517 }
518 if (isEmpty) {
519 THROWS(NoSuchElementException.class,
520 new Fun(){void f(){ deq.getFirst(); }},
521 new Fun(){void f(){ deq.element(); }},
522 new Fun(){void f(){ deq.iterator().next(); }});
523 check(deq.peekFirst() == null);
524 check(deq.peek() == null);
525 } else {
526 check(deq.getFirst() != e);
527 check(deq.element() != e);
528 check(deq.iterator().next() != e);
529 check(deq.peekFirst() != e);
530 check(deq.peek() != e);
531 }
532 check(!deq.contains(e));
533 check(!deq.removeFirstOccurrence(e));
534 check(!deq.removeLastOccurrence(e));
535 if (isList) {
536 check(isEmpty || asList.get(0) != e);
537 check(asList.indexOf(e) == -1);
538 check(asList.lastIndexOf(e) == -1);
539 }
540 equal(new ArrayList<Integer>(deq), originalContents);
541
542 // insert, query, remove element at tail
543 if (isEmpty) {
544 check(deq.peekLast() == null);
545 THROWS(NoSuchElementException.class,
546 new Fun(){void f(){ deq.getLast(); }});
547 } else {
548 check(deq.peekLast() != e);
549 check(deq.getLast() != e);
550 }
551 switch (rnd.nextInt(isList ? 6 : 4)) {
552 case 0: deq.addLast(e); break;
553 case 1: check(deq.offerLast(e)); break;
554 case 2: check(deq.add(e)); break;
555 case 3: deq.addAll(singleton); break;
556 case 4: asList.addAll(deq.size(), singleton); break;
557 case 5: asList.add(deq.size(), e); break;
558 default: throw new AssertionError();
559 }
560 check(deq.peekLast() == e);
561 check(deq.getLast() == e);
562 check(deq.contains(e));
563 if (isList) {
564 ListIterator it = asList.listIterator(asList.size());
565 check(it.previous() == e);
566 check(asList.get(asList.size() - 1) == e);
567 check(asList.indexOf(e) == asList.size() - 1);
568 check(asList.lastIndexOf(e) == asList.size() - 1);
569 int size = asList.size();
570 check(asList.subList(size - 1, size).equals(singleton));
571 }
572 switch (rnd.nextInt(isList ? 8 : 6)) {
573 case 0: check(deq.pollLast() == e); break;
574 case 1: check(deq.removeLast() == e); break;
575 case 2: check(deq.removeFirstOccurrence(e)); break;
576 case 3: check(deq.removeLastOccurrence(e)); break;
577 case 4: check(deq.remove(e)); break;
578 case 5: check(deq.removeAll(singleton)); break;
579 case 6: asList.remove(asList.size() - 1); break;
580 case 7:
581 ListIterator it = asList.listIterator(asList.size());
582 it.previous();
583 it.remove();
584 break;
585 default: throw new AssertionError();
586 }
587 if (isEmpty) {
588 check(deq.peekLast() == null);
589 THROWS(NoSuchElementException.class,
590 new Fun(){void f(){ deq.getLast(); }});
591 } else {
592 check(deq.peekLast() != e);
593 check(deq.getLast() != e);
594 }
595 check(!deq.contains(e));
596 equal(new ArrayList<Integer>(deq), originalContents);
597
598 // Test operations on empty deque
599 switch (rnd.nextInt(isList ? 4 : 2)) {
600 case 0: deq.clear(); break;
601 case 1:
602 Iterator it = deq.iterator();
603 while (it.hasNext()) {
604 it.next();
605 it.remove();
606 }
607 break;
608 case 2: asList.subList(0, asList.size()).clear(); break;
609 case 3:
610 ListIterator lit = asList.listIterator(asList.size());
611 while (lit.hasPrevious()) {
612 lit.previous();
613 lit.remove();
614 }
615 break;
616 default: throw new AssertionError();
617 }
618 testEmptyCollection(deq);
619 check(!deq.iterator().hasNext());
620 if (isList) {
621 check(!asList.listIterator().hasPrevious());
622 THROWS(NoSuchElementException.class,
623 new Fun(){void f(){ asList.listIterator().previous(); }});
624 }
625 THROWS(NoSuchElementException.class,
626 new Fun(){void f(){ deq.iterator().next(); }},
627 new Fun(){void f(){ deq.element(); }},
628 new Fun(){void f(){ deq.getFirst(); }},
629 new Fun(){void f(){ deq.getLast(); }},
630 new Fun(){void f(){ deq.pop(); }},
631 new Fun(){void f(){ deq.remove(); }},
632 new Fun(){void f(){ deq.removeFirst(); }},
633 new Fun(){void f(){ deq.removeLast(); }});
634
635 check(deq.poll() == null);
636 check(deq.pollFirst() == null);
637 check(deq.pollLast() == null);
638 check(deq.peek() == null);
639 check(deq.peekFirst() == null);
640 check(deq.peekLast() == null);
641 check(!deq.removeFirstOccurrence(e));
642 check(!deq.removeLastOccurrence(e));
643
644 check(deq.addAll(originalContents) == !isEmpty);
645 equal(new ArrayList<Integer>(deq), originalContents);
646 check(!deq.addAll(Collections.<Integer>emptyList()));
647 equal(new ArrayList<Integer>(deq), originalContents);
648 }
649 }
650
dl8e56eb52009-07-28 17:17:55 -0700651 private static void testQueueIteratorRemove(Queue<Integer> q) {
652 System.err.printf("testQueueIteratorRemove %s%n",
653 q.getClass().getSimpleName());
654 q.clear();
655 for (int i = 0; i < 5; i++)
656 q.add(i);
657 Iterator<Integer> it = q.iterator();
658 check(it.hasNext());
659 for (int i = 3; i >= 0; i--)
660 q.remove(i);
661 equal(it.next(), 0);
662 equal(it.next(), 4);
663
664 q.clear();
665 for (int i = 0; i < 5; i++)
666 q.add(i);
667 it = q.iterator();
668 equal(it.next(), 0);
669 check(it.hasNext());
670 for (int i = 1; i < 4; i++)
671 q.remove(i);
672 equal(it.next(), 1);
673 equal(it.next(), 4);
duke6e45e102007-12-01 00:00:00 +0000674 }
675
676 private static void testList(final List<Integer> l) {
677 //----------------------------------------------------------------
678 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
679 // doesn't throw IndexOutOfBoundsException
680 //----------------------------------------------------------------
681 try {
682 l.addAll(-1, Collections.<Integer>emptyList());
683 fail("Expected IndexOutOfBoundsException not thrown");
684 }
685 catch (UnsupportedOperationException _) {/* OK */}
686 catch (IndexOutOfBoundsException _) {/* OK */}
687 catch (Throwable t) { unexpected(t); }
688
689// equal(l instanceof Serializable,
690// l.subList(0,0) instanceof Serializable);
691 if (l.subList(0,0) instanceof Serializable)
692 check(l instanceof Serializable);
693
694 equal(l instanceof RandomAccess,
695 l.subList(0,0) instanceof RandomAccess);
696 }
697
698 private static void testCollection(Collection<Integer> c) {
dl8e56eb52009-07-28 17:17:55 -0700699 try { testCollection1(c); }
700 catch (Throwable t) { unexpected(t); }
701 }
702
703 private static void testCollection1(Collection<Integer> c) {
duke6e45e102007-12-01 00:00:00 +0000704
705 System.out.println("\n==> " + c.getClass().getName());
706
707 checkFunctionalInvariants(c);
708
709 if (! supportsAdd(c)) return;
710 //System.out.println("add() supported");
711
martineb76fa62008-05-10 11:49:25 -0700712 if (c instanceof NavigableSet) {
713 System.out.println("NavigableSet tests...");
714
715 NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
716 testNavigableSet(ns);
717 testNavigableSet(ns.headSet(6, false));
718 testNavigableSet(ns.headSet(5, true));
719 testNavigableSet(ns.tailSet(0, false));
720 testNavigableSet(ns.tailSet(1, true));
721 testNavigableSet(ns.subSet(0, false, 5, true));
722 testNavigableSet(ns.subSet(1, true, 6, false));
723 }
duke6e45e102007-12-01 00:00:00 +0000724
725 if (c instanceof Queue)
726 testQueue((Queue<Integer>)c);
727
728 if (c instanceof List)
729 testList((List<Integer>)c);
730
731 check(supportsRemove(c));
732
733 try {
734 oneElement(c);
735 checkFunctionalInvariants(c);
736 }
737 catch (Throwable t) { unexpected(t); }
738
739 clear(c); testNullElement(c);
740 oneElement(c); testNullElement(c);
741
742 clear(c); testStringElement(c);
743 oneElement(c); testStringElement(c);
744
745 if (c.getClass().getName().matches(".*concurrent.*"))
746 testConcurrentCollection(c);
747
748 //----------------------------------------------------------------
749 // The "all" operations should throw NPE when passed null
750 //----------------------------------------------------------------
751 {
752 oneElement(c);
753 try {
754 c.removeAll(null);
755 fail("Expected NullPointerException");
756 }
757 catch (NullPointerException e) { pass(); }
758 catch (Throwable t) { unexpected(t); }
759
760 oneElement(c);
761 try {
762 c.retainAll(null);
763 fail("Expected NullPointerException");
764 }
765 catch (NullPointerException e) { pass(); }
766 catch (Throwable t) { unexpected(t); }
767
768 oneElement(c);
769 try {
770 c.addAll(null);
771 fail("Expected NullPointerException");
772 }
773 catch (NullPointerException e) { pass(); }
774 catch (Throwable t) { unexpected(t); }
775
776 oneElement(c);
777 try {
778 c.containsAll(null);
779 fail("Expected NullPointerException");
780 }
781 catch (NullPointerException e) { pass(); }
782 catch (Throwable t) { unexpected(t); }
783 }
784 }
785
786 //----------------------------------------------------------------
787 // Map
788 //----------------------------------------------------------------
789 private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
790 check(m.keySet().size() == m.entrySet().size());
791 check(m.keySet().size() == m.size());
792 checkFunctionalInvariants(m.keySet());
793 checkFunctionalInvariants(m.values());
794 check(m.size() != 0 ^ m.isEmpty());
795 }
796
797 private static void testMap(Map<Integer,Integer> m) {
798 System.out.println("\n==> " + m.getClass().getName());
799
800 if (m instanceof ConcurrentMap)
801 testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
802
martineb76fa62008-05-10 11:49:25 -0700803 if (m instanceof NavigableMap) {
804 System.out.println("NavigableMap tests...");
805
806 NavigableMap<Integer,Integer> nm =
807 (NavigableMap<Integer,Integer>) m;
dl0ad61612009-03-24 19:42:23 -0700808 testNavigableMapRemovers(nm);
martineb76fa62008-05-10 11:49:25 -0700809 testNavigableMap(nm);
810 testNavigableMap(nm.headMap(6, false));
811 testNavigableMap(nm.headMap(5, true));
812 testNavigableMap(nm.tailMap(0, false));
813 testNavigableMap(nm.tailMap(1, true));
814 testNavigableMap(nm.subMap(1, true, 6, false));
815 testNavigableMap(nm.subMap(0, false, 5, true));
816 }
duke6e45e102007-12-01 00:00:00 +0000817
818 checkFunctionalInvariants(m);
819
820 if (supportsClear(m)) {
821 try { clear(m); }
822 catch (Throwable t) { unexpected(t); }
823 }
824
825 if (supportsPut(m)) {
826 try {
827 check(m.put(3333, 77777) == null);
828 check(m.put(9134, 74982) == null);
829 check(m.get(9134) == 74982);
830 check(m.put(9134, 1382) == 74982);
831 check(m.get(9134) == 1382);
832 check(m.size() == 2);
833 checkFunctionalInvariants(m);
834 checkNPEConsistency(m);
835 }
836 catch (Throwable t) { unexpected(t); }
837 }
838 }
839
840 private static boolean supportsPut(Map<Integer,Integer> m) {
841 // We're asking for .equals(...) semantics
842 if (m instanceof IdentityHashMap) return false;
843
844 try { check(m.put(778347983,12735) == null); }
845 catch (UnsupportedOperationException t) { return false; }
846 catch (Throwable t) { unexpected(t); }
847
848 try {
849 check(m.containsKey(778347983));
850 check(m.remove(778347983) != null);
851 } catch (Throwable t) { unexpected(t); }
852 return true;
853 }
854
855 private static boolean supportsClear(Map<?,?> m) {
856 try { m.clear(); }
857 catch (UnsupportedOperationException t) { return false; }
858 catch (Throwable t) { unexpected(t); }
859 return true;
860 }
861
862 //----------------------------------------------------------------
863 // ConcurrentMap
864 //----------------------------------------------------------------
865 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
866 System.out.println("ConcurrentMap tests...");
867
868 try {
869 clear(m);
870
871 check(m.putIfAbsent(18357,7346) == null);
872 check(m.containsKey(18357));
873 check(m.putIfAbsent(18357,8263) == 7346);
874 try { m.putIfAbsent(18357,null); fail("NPE"); }
875 catch (NullPointerException t) { }
876 check(m.containsKey(18357));
877
878 check(! m.replace(18357,8888,7777));
879 check(m.containsKey(18357));
880 try { m.replace(18357,null,7777); fail("NPE"); }
881 catch (NullPointerException t) { }
882 check(m.containsKey(18357));
883 check(m.get(18357) == 7346);
884 check(m.replace(18357,7346,5555));
885 check(m.replace(18357,5555,7346));
886 check(m.get(18357) == 7346);
887
888 check(m.replace(92347,7834) == null);
889 try { m.replace(18357,null); fail("NPE"); }
890 catch (NullPointerException t) { }
891 check(m.replace(18357,7346) == 7346);
892 check(m.replace(18357,5555) == 7346);
893 check(m.get(18357) == 5555);
894 check(m.replace(18357,7346) == 5555);
895 check(m.get(18357) == 7346);
896
897 check(! m.remove(18357,9999));
898 check(m.get(18357) == 7346);
899 check(m.containsKey(18357));
900 check(! m.remove(18357,null)); // 6272521
901 check(m.get(18357) == 7346);
902 check(m.remove(18357,7346));
903 check(m.get(18357) == null);
904 check(! m.containsKey(18357));
905 check(m.isEmpty());
906
907 m.putIfAbsent(1,2);
908 check(m.size() == 1);
909 check(! m.remove(1,null));
910 check(! m.remove(1,null));
911 check(! m.remove(1,1));
912 check(m.remove(1,2));
913 check(m.isEmpty());
914
915 testEmptyMap(m);
916 }
917 catch (Throwable t) { unexpected(t); }
918 }
919
920 private static void throwsConsistently(Class<? extends Throwable> k,
921 Iterable<Fun> fs) {
922 List<Class<? extends Throwable>> threw
923 = new ArrayList<Class<? extends Throwable>>();
924 for (Fun f : fs)
925 try { f.f(); threw.add(null); }
926 catch (Throwable t) {
927 check(k.isAssignableFrom(t.getClass()));
928 threw.add(t.getClass());
929 }
930 if (new HashSet<Object>(threw).size() != 1)
931 fail(threw.toString());
932 }
933
934 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
935 m.clear();
936 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
937 ? (ConcurrentMap<T,Integer>) m
938 : null;
939 List<Fun> fs = new ArrayList<Fun>();
940 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
941 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
942 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
943 if (cm != null) {
944 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
945 throwsConsistently(NullPointerException.class, fs);
946
947 fs.clear();
948 final Map<T,Integer> sm = singletonMap(null,1);
949 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
950 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
951 if (cm != null) {
952 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
953 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
954 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
955 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
956 }
957 throwsConsistently(NullPointerException.class, fs);
958 }
959
960 //----------------------------------------------------------------
961 // NavigableMap
962 //----------------------------------------------------------------
963 private static void
964 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
965 Integer i,
966 Integer lower,
967 Integer floor,
968 Integer ceiling,
969 Integer higher) {
970 equal(m.lowerKey(i), lower);
971 equal(m.floorKey(i), floor);
972 equal(m.ceilingKey(i), ceiling);
973 equal(m.higherKey(i), higher);
974 }
975
976 private static void
977 checkNavigableSetKeys(NavigableSet<Integer> m,
978 Integer i,
979 Integer lower,
980 Integer floor,
981 Integer ceiling,
982 Integer higher) {
983 equal(m.lower(i), lower);
984 equal(m.floor(i), floor);
985 equal(m.ceiling(i), ceiling);
986 equal(m.higher(i), higher);
987 }
988
989 static final Random rnd = new Random();
990 static void equalNext(final Iterator<?> it, Object expected) {
991 if (rnd.nextBoolean())
992 check(it.hasNext());
993 equal(it.next(), expected);
994 }
995
dl0ad61612009-03-24 19:42:23 -0700996 static void equalMaps(Map m1, Map m2) {
997 equal(m1, m2);
998 equal(m2, m1);
999 equal(m1.size(), m2.size());
1000 equal(m1.isEmpty(), m2.isEmpty());
1001 equal(m1.toString(), m2.toString());
1002 check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
1003 }
1004
1005 @SuppressWarnings({"unchecked", "rawtypes"})
1006 static void testNavigableMapRemovers(NavigableMap m)
1007 {
1008 final Map emptyMap = new HashMap();
1009
1010 final Map singletonMap = new HashMap();
1011 singletonMap.put(1, 2);
1012
1013 abstract class NavigableMapView {
1014 abstract NavigableMap view(NavigableMap m);
1015 }
1016
1017 NavigableMapView[] views = {
1018 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1019 return m; }},
1020 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1021 return m.headMap(99, true); }},
1022 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1023 return m.tailMap(-99, false); }},
1024 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1025 return m.subMap(-99, true, 99, false); }},
1026 };
1027
1028 abstract class Remover {
1029 abstract void remove(NavigableMap m, Object k, Object v);
1030 }
1031
1032 Remover[] removers = {
1033 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1034 equal(m.remove(k), v); }},
1035
1036 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1037 equal(m.descendingMap().remove(k), v); }},
1038 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1039 equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
1040 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1041 equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
1042
1043 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1044 equal(m.headMap(86, true).remove(k), v); }},
1045 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1046 equal(m.tailMap(-86, true).remove(k), v); }},
1047 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1048 equal(m.subMap(-86, false, 86, true).remove(k), v); }},
1049
1050 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1051 check(m.keySet().remove(k)); }},
1052 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1053 check(m.navigableKeySet().remove(k)); }},
1054
1055 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1056 check(m.navigableKeySet().headSet(86, true).remove(k)); }},
1057 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1058 check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
1059 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1060 check(m.navigableKeySet().subSet(-86, true, 86, false)
1061 .remove(k)); }},
1062
1063 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1064 check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
1065 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1066 check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
1067 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1068 check(m.descendingKeySet().subSet(86, true, -86, false)
1069 .remove(k)); }},
1070 };
1071
1072 for (NavigableMapView view : views) {
1073 for (Remover remover : removers) {
1074 try {
1075 m.clear();
1076 equalMaps(m, emptyMap);
1077 equal(m.put(1, 2), null);
1078 equalMaps(m, singletonMap);
1079 NavigableMap v = view.view(m);
1080 remover.remove(v, 1, 2);
1081 equalMaps(m, emptyMap);
1082 } catch (Throwable t) { unexpected(t); }
1083 }
1084 }
1085 }
1086
duke6e45e102007-12-01 00:00:00 +00001087 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
1088 {
duke6e45e102007-12-01 00:00:00 +00001089 clear(m);
1090 checkNavigableMapKeys(m, 1, null, null, null, null);
1091
1092 equal(m.put(1, 2), null);
1093 equal(m.put(3, 4), null);
1094 equal(m.put(5, 9), null);
1095
1096 equal(m.put(1, 2), 2);
1097 equal(m.put(3, 4), 4);
1098 equal(m.put(5, 6), 9);
1099
1100 checkNavigableMapKeys(m, 0, null, null, 1, 1);
1101 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
1102 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
1103 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
1104 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
1105 checkNavigableMapKeys(m, 6, 5, 5, null, null);
1106
martineb76fa62008-05-10 11:49:25 -07001107 for (final Iterator<Integer> it :
1108 (Iterator<Integer>[])
1109 new Iterator<?>[] {
1110 m.descendingKeySet().iterator(),
1111 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +00001112 equalNext(it, 5);
1113 equalNext(it, 3);
1114 equalNext(it, 1);
1115 check(! it.hasNext());
1116 THROWS(NoSuchElementException.class,
1117 new Fun(){void f(){it.next();}});
1118 }
1119
1120 {
1121 final Iterator<Map.Entry<Integer,Integer>> it
1122 = m.descendingMap().entrySet().iterator();
1123 check(it.hasNext()); equal(it.next().getKey(), 5);
1124 check(it.hasNext()); equal(it.next().getKey(), 3);
1125 check(it.hasNext()); equal(it.next().getKey(), 1);
1126 check(! it.hasNext());
1127 THROWS(NoSuchElementException.class,
1128 new Fun(){void f(){it.next();}});
1129 }
1130 }
1131
1132
1133 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +00001134 clear(s);
1135 checkNavigableSetKeys(s, 1, null, null, null, null);
1136
1137 check(s.add(1));
1138 check(s.add(3));
1139 check(s.add(5));
1140
1141 check(! s.add(1));
1142 check(! s.add(3));
1143 check(! s.add(5));
1144
1145 checkNavigableSetKeys(s, 0, null, null, 1, 1);
1146 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
1147 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
1148 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
1149 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
1150 checkNavigableSetKeys(s, 6, 5, 5, null, null);
1151
martineb76fa62008-05-10 11:49:25 -07001152 for (final Iterator<Integer> it :
1153 (Iterator<Integer>[])
1154 new Iterator<?>[] {
1155 s.descendingIterator(),
1156 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +00001157 equalNext(it, 5);
1158 equalNext(it, 3);
1159 equalNext(it, 1);
1160 check(! it.hasNext());
1161 THROWS(NoSuchElementException.class,
1162 new Fun(){void f(){it.next();}});
1163 }
1164 }
1165
1166 //--------------------- Infrastructure ---------------------------
1167 static volatile int passed = 0, failed = 0;
1168 static void pass() { passed++; }
1169 static void fail() { failed++; Thread.dumpStack(); }
1170 static void fail(String msg) { System.out.println(msg); fail(); }
1171 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
1172 static void check(boolean cond) { if (cond) pass(); else fail(); }
1173 static void equal(Object x, Object y) {
1174 if (x == null ? y == null : x.equals(y)) pass();
1175 else {System.out.println(x + " not equal to " + y); fail();}}
1176 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
1177 public static void main(String[] args) throws Throwable {
1178 try { realMain(args); } catch (Throwable t) { unexpected(t); }
1179
1180 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1181 if (failed > 0) throw new Exception("Some tests failed");
1182 }
1183 private static abstract class Fun {abstract void f() throws Throwable;}
1184 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
1185 for (Fun f : fs)
1186 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
1187 catch (Throwable t) {
1188 if (k.isAssignableFrom(t.getClass())) pass();
1189 else unexpected(t);}}
1190 static byte[] serializedForm(Object obj) {
1191 try {
1192 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1193 new ObjectOutputStream(baos).writeObject(obj);
1194 return baos.toByteArray();
1195 } catch (IOException e) { throw new Error(e); }}
1196 static Object readObject(byte[] bytes)
1197 throws IOException, ClassNotFoundException {
1198 InputStream is = new ByteArrayInputStream(bytes);
1199 return new ObjectInputStream(is).readObject();}
1200 @SuppressWarnings("unchecked")
1201 static <T> T serialClone(T obj) {
1202 try { return (T) readObject(serializedForm(obj)); }
1203 catch (Exception e) { throw new Error(e); }}
1204}