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