1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 */ 22 23 /* 24 * This file is available under and governed by the GNU General Public 25 * License version 2 only, as published by the Free Software Foundation. 26 * However, the following notice accompanied the original version of this 27 * file: 28 * 29 * Written by Doug Lea and Martin Buchholz with assistance from 30 * members of JCP JSR-166 Expert Group and released to the public 31 * domain, as explained at 32 * http://creativecommons.org/publicdomain/zero/1.0/ 33 */ 34 35 import static java.util.concurrent.TimeUnit.HOURS; 36 import static java.util.concurrent.TimeUnit.MILLISECONDS; 37 38 import java.util.ArrayDeque; 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.Collection; 42 import java.util.Collections; 43 import java.util.ConcurrentModificationException; 44 import java.util.Deque; 45 import java.util.HashSet; 46 import java.util.Iterator; 47 import java.util.List; 48 import java.util.NoSuchElementException; 49 import java.util.Queue; 50 import java.util.Set; 51 import java.util.Spliterator; 52 import java.util.concurrent.BlockingDeque; 53 import java.util.concurrent.BlockingQueue; 54 import java.util.concurrent.ConcurrentLinkedQueue; 55 import java.util.concurrent.CountDownLatch; 56 import java.util.concurrent.Executors; 57 import java.util.concurrent.ExecutorService; 58 import java.util.concurrent.Future; 59 import java.util.concurrent.Phaser; 60 import java.util.concurrent.ThreadLocalRandom; 61 import java.util.concurrent.atomic.AtomicBoolean; 62 import java.util.concurrent.atomic.AtomicLong; 63 import java.util.concurrent.atomic.AtomicReference; 64 import java.util.function.Consumer; 65 import java.util.function.Predicate; 66 import java.util.stream.Collectors; 67 68 import junit.framework.Test; 69 70 /** 71 * Contains tests applicable to all jdk8+ Collection implementations. 72 * An extension of CollectionTest. 73 */ 74 public class Collection8Test extends JSR166TestCase { 75 final CollectionImplementation impl; 76 77 /** Tests are parameterized by a Collection implementation. */ 78 Collection8Test(CollectionImplementation impl, String methodName) { 79 super(methodName); 80 this.impl = impl; 81 } 82 83 public static Test testSuite(CollectionImplementation impl) { 84 return parameterizedTestSuite(Collection8Test.class, 85 CollectionImplementation.class, 86 impl); 87 } 88 89 Object bomb() { 90 return new Object() { 91 @Override public boolean equals(Object x) { throw new AssertionError(); } 92 @Override public int hashCode() { throw new AssertionError(); } 93 @Override public String toString() { throw new AssertionError(); } 94 }; 95 } 96 97 /** Checks properties of empty collections. */ 98 public void testEmptyMeansEmpty() throws Throwable { 99 Collection c = impl.emptyCollection(); 100 emptyMeansEmpty(c); 101 102 if (c instanceof java.io.Serializable) { 103 try { 104 emptyMeansEmpty(serialClonePossiblyFailing(c)); 105 } catch (java.io.NotSerializableException ex) { 106 // excusable when we have a serializable wrapper around 107 // a non-serializable collection, as can happen with: 108 // Vector.subList() => wrapped AbstractList$RandomAccessSubList 109 if (testImplementationDetails 110 && (! c.getClass().getName().matches( 111 "java.util.Collections.*"))) 112 throw ex; 113 } 114 } 115 116 Collection clone = cloneableClone(c); 117 if (clone != null) 118 emptyMeansEmpty(clone); 119 } 120 121 void emptyMeansEmpty(Collection c) throws InterruptedException { 122 assertTrue(c.isEmpty()); 123 assertEquals(0, c.size()); 124 assertEquals("[]", c.toString()); 125 if (c instanceof List<?>) { 126 List x = (List) c; 127 assertEquals(1, x.hashCode()); 128 assertEquals(x, Collections.emptyList()); 129 assertEquals(Collections.emptyList(), x); 130 assertEquals(-1, x.indexOf(impl.makeElement(86))); 131 assertEquals(-1, x.lastIndexOf(impl.makeElement(99))); 132 assertThrows( 133 IndexOutOfBoundsException.class, 134 () -> x.get(0), 135 () -> x.set(0, impl.makeElement(42))); 136 } 137 else if (c instanceof Set<?>) { 138 assertEquals(0, c.hashCode()); 139 assertEquals(c, Collections.emptySet()); 140 assertEquals(Collections.emptySet(), c); 141 } 142 { 143 Object[] a = c.toArray(); 144 assertEquals(0, a.length); 145 assertSame(Object[].class, a.getClass()); 146 } 147 { 148 Object[] a = new Object[0]; 149 assertSame(a, c.toArray(a)); 150 } 151 { 152 Integer[] a = new Integer[0]; 153 assertSame(a, c.toArray(a)); 154 } 155 { 156 Integer[] a = { 1, 2, 3}; 157 assertSame(a, c.toArray(a)); 158 assertNull(a[0]); 159 assertSame(2, a[1]); 160 assertSame(3, a[2]); 161 } 162 assertIteratorExhausted(c.iterator()); 163 Consumer alwaysThrows = e -> { throw new AssertionError(); }; 164 c.forEach(alwaysThrows); 165 c.iterator().forEachRemaining(alwaysThrows); 166 c.spliterator().forEachRemaining(alwaysThrows); 167 assertFalse(c.spliterator().tryAdvance(alwaysThrows)); 168 if (c.spliterator().hasCharacteristics(Spliterator.SIZED)) 169 assertEquals(0, c.spliterator().estimateSize()); 170 assertFalse(c.contains(bomb())); 171 assertFalse(c.remove(bomb())); 172 if (c instanceof Queue) { 173 Queue q = (Queue) c; 174 assertNull(q.peek()); 175 assertNull(q.poll()); 176 } 177 if (c instanceof Deque) { 178 Deque d = (Deque) c; 179 assertNull(d.peekFirst()); 180 assertNull(d.peekLast()); 181 assertNull(d.pollFirst()); 182 assertNull(d.pollLast()); 183 assertIteratorExhausted(d.descendingIterator()); 184 d.descendingIterator().forEachRemaining(alwaysThrows); 185 assertFalse(d.removeFirstOccurrence(bomb())); 186 assertFalse(d.removeLastOccurrence(bomb())); 187 } 188 if (c instanceof BlockingQueue) { 189 BlockingQueue q = (BlockingQueue) c; 190 assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit())); 191 } 192 if (c instanceof BlockingDeque) { 193 BlockingDeque q = (BlockingDeque) c; 194 assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit())); 195 assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit())); 196 } 197 } 198 199 public void testNullPointerExceptions() throws InterruptedException { 200 Collection c = impl.emptyCollection(); 201 assertThrows( 202 NullPointerException.class, 203 () -> c.addAll(null), 204 () -> c.containsAll(null), 205 () -> c.retainAll(null), 206 () -> c.removeAll(null), 207 () -> c.removeIf(null), 208 () -> c.forEach(null), 209 () -> c.iterator().forEachRemaining(null), 210 () -> c.spliterator().forEachRemaining(null), 211 () -> c.spliterator().tryAdvance(null), 212 () -> c.toArray((Object[])null)); 213 214 if (!impl.permitsNulls()) { 215 assertThrows( 216 NullPointerException.class, 217 () -> c.add(null)); 218 } 219 if (!impl.permitsNulls() && c instanceof Queue) { 220 Queue q = (Queue) c; 221 assertThrows( 222 NullPointerException.class, 223 () -> q.offer(null)); 224 } 225 if (!impl.permitsNulls() && c instanceof Deque) { 226 Deque d = (Deque) c; 227 assertThrows( 228 NullPointerException.class, 229 () -> d.addFirst(null), 230 () -> d.addLast(null), 231 () -> d.offerFirst(null), 232 () -> d.offerLast(null), 233 () -> d.push(null), 234 () -> d.descendingIterator().forEachRemaining(null)); 235 } 236 if (c instanceof BlockingQueue) { 237 BlockingQueue q = (BlockingQueue) c; 238 assertThrows( 239 NullPointerException.class, 240 () -> q.offer(null, 1L, HOURS), 241 () -> q.put(null)); 242 } 243 if (c instanceof BlockingDeque) { 244 BlockingDeque q = (BlockingDeque) c; 245 assertThrows( 246 NullPointerException.class, 247 () -> q.offerFirst(null, 1L, HOURS), 248 () -> q.offerLast(null, 1L, HOURS), 249 () -> q.putFirst(null), 250 () -> q.putLast(null)); 251 } 252 } 253 254 public void testNoSuchElementExceptions() { 255 Collection c = impl.emptyCollection(); 256 assertThrows( 257 NoSuchElementException.class, 258 () -> c.iterator().next()); 259 260 if (c instanceof Queue) { 261 Queue q = (Queue) c; 262 assertThrows( 263 NoSuchElementException.class, 264 () -> q.element(), 265 () -> q.remove()); 266 } 267 if (c instanceof Deque) { 268 Deque d = (Deque) c; 269 assertThrows( 270 NoSuchElementException.class, 271 () -> d.getFirst(), 272 () -> d.getLast(), 273 () -> d.removeFirst(), 274 () -> d.removeLast(), 275 () -> d.pop(), 276 () -> d.descendingIterator().next()); 277 } 278 if (c instanceof List) { 279 List x = (List) c; 280 assertThrows( 281 NoSuchElementException.class, 282 () -> x.iterator().next(), 283 () -> x.listIterator().next(), 284 () -> x.listIterator(0).next(), 285 () -> x.listIterator().previous(), 286 () -> x.listIterator(0).previous()); 287 } 288 } 289 290 public void testRemoveIf() { 291 Collection c = impl.emptyCollection(); 292 boolean ordered = 293 c.spliterator().hasCharacteristics(Spliterator.ORDERED); 294 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 295 int n = rnd.nextInt(6); 296 for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); 297 AtomicReference threwAt = new AtomicReference(null); 298 List orig = rnd.nextBoolean() 299 ? new ArrayList(c) 300 : Arrays.asList(c.toArray()); 301 302 // Merely creating an iterator can change ArrayBlockingQueue behavior 303 Iterator it = rnd.nextBoolean() ? c.iterator() : null; 304 305 ArrayList survivors = new ArrayList(); 306 ArrayList accepts = new ArrayList(); 307 ArrayList rejects = new ArrayList(); 308 309 Predicate randomPredicate = e -> { 310 assertNull(threwAt.get()); 311 switch (rnd.nextInt(3)) { 312 case 0: accepts.add(e); return true; 313 case 1: rejects.add(e); return false; 314 case 2: threwAt.set(e); throw new ArithmeticException(); 315 default: throw new AssertionError(); 316 } 317 }; 318 try { 319 try { 320 boolean modified = c.removeIf(randomPredicate); 321 assertNull(threwAt.get()); 322 assertEquals(modified, accepts.size() > 0); 323 assertEquals(modified, rejects.size() != n); 324 assertEquals(accepts.size() + rejects.size(), n); 325 if (ordered) { 326 assertEquals(rejects, 327 Arrays.asList(c.toArray())); 328 } else { 329 assertEquals(new HashSet(rejects), 330 new HashSet(Arrays.asList(c.toArray()))); 331 } 332 } catch (ArithmeticException ok) { 333 assertNotNull(threwAt.get()); 334 assertTrue(c.contains(threwAt.get())); 335 } 336 if (it != null && impl.isConcurrent()) 337 // check for weakly consistent iterator 338 while (it.hasNext()) assertTrue(orig.contains(it.next())); 339 switch (rnd.nextInt(4)) { 340 case 0: survivors.addAll(c); break; 341 case 1: survivors.addAll(Arrays.asList(c.toArray())); break; 342 case 2: c.forEach(survivors::add); break; 343 case 3: for (Object e : c) survivors.add(e); break; 344 } 345 assertTrue(orig.containsAll(accepts)); 346 assertTrue(orig.containsAll(rejects)); 347 assertTrue(orig.containsAll(survivors)); 348 assertTrue(orig.containsAll(c)); 349 assertTrue(c.containsAll(rejects)); 350 assertTrue(c.containsAll(survivors)); 351 assertTrue(survivors.containsAll(rejects)); 352 if (threwAt.get() == null) { 353 assertEquals(n - accepts.size(), c.size()); 354 for (Object x : accepts) assertFalse(c.contains(x)); 355 } else { 356 // Two acceptable behaviors: entire removeIf call is one 357 // transaction, or each element processed is one transaction. 358 assertTrue(n == c.size() || n == c.size() + accepts.size()); 359 int k = 0; 360 for (Object x : accepts) if (c.contains(x)) k++; 361 assertTrue(k == accepts.size() || k == 0); 362 } 363 } catch (Throwable ex) { 364 System.err.println(impl.klazz()); 365 // c is at risk of corruption if we got here, so be lenient 366 try { System.err.printf("c=%s%n", c); } 367 catch (Throwable t) { t.printStackTrace(); } 368 System.err.printf("n=%d%n", n); 369 System.err.printf("orig=%s%n", orig); 370 System.err.printf("accepts=%s%n", accepts); 371 System.err.printf("rejects=%s%n", rejects); 372 System.err.printf("survivors=%s%n", survivors); 373 System.err.printf("threwAt=%s%n", threwAt.get()); 374 throw ex; 375 } 376 } 377 378 /** 379 * All elements removed in the middle of CONCURRENT traversal. 380 */ 381 public void testElementRemovalDuringTraversal() { 382 Collection c = impl.emptyCollection(); 383 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 384 int n = rnd.nextInt(6); 385 ArrayList copy = new ArrayList(); 386 for (int i = 0; i < n; i++) { 387 Object x = impl.makeElement(i); 388 copy.add(x); 389 c.add(x); 390 } 391 ArrayList iterated = new ArrayList(); 392 ArrayList spliterated = new ArrayList(); 393 Spliterator s = c.spliterator(); 394 Iterator it = c.iterator(); 395 for (int i = rnd.nextInt(n + 1); --i >= 0; ) { 396 assertTrue(s.tryAdvance(spliterated::add)); 397 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 398 iterated.add(it.next()); 399 } 400 Consumer alwaysThrows = e -> { throw new AssertionError(); }; 401 if (s.hasCharacteristics(Spliterator.CONCURRENT)) { 402 c.clear(); // TODO: many more removal methods 403 if (testImplementationDetails 404 && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) { 405 if (rnd.nextBoolean()) 406 assertFalse(s.tryAdvance(alwaysThrows)); 407 else 408 s.forEachRemaining(alwaysThrows); 409 } 410 if (it.hasNext()) iterated.add(it.next()); 411 if (rnd.nextBoolean()) assertIteratorExhausted(it); 412 } 413 assertTrue(copy.containsAll(iterated)); 414 assertTrue(copy.containsAll(spliterated)); 415 } 416 417 /** 418 * Some elements randomly disappear in the middle of traversal. 419 */ 420 public void testRandomElementRemovalDuringTraversal() { 421 Collection c = impl.emptyCollection(); 422 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 423 int n = rnd.nextInt(6); 424 ArrayList copy = new ArrayList(); 425 for (int i = 0; i < n; i++) { 426 Object x = impl.makeElement(i); 427 copy.add(x); 428 c.add(x); 429 } 430 ArrayList iterated = new ArrayList(); 431 ArrayList spliterated = new ArrayList(); 432 ArrayList removed = new ArrayList(); 433 Spliterator s = c.spliterator(); 434 Iterator it = c.iterator(); 435 if (! (s.hasCharacteristics(Spliterator.CONCURRENT) || 436 s.hasCharacteristics(Spliterator.IMMUTABLE))) 437 return; 438 for (int i = rnd.nextInt(n + 1); --i >= 0; ) { 439 assertTrue(s.tryAdvance(e -> {})); 440 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 441 it.next(); 442 } 443 // TODO: many more removal methods 444 if (rnd.nextBoolean()) { 445 for (Iterator z = c.iterator(); z.hasNext(); ) { 446 Object e = z.next(); 447 if (rnd.nextBoolean()) { 448 try { 449 z.remove(); 450 } catch (UnsupportedOperationException ok) { return; } 451 removed.add(e); 452 } 453 } 454 } else { 455 Predicate randomlyRemove = e -> { 456 if (rnd.nextBoolean()) { removed.add(e); return true; } 457 else return false; 458 }; 459 c.removeIf(randomlyRemove); 460 } 461 s.forEachRemaining(spliterated::add); 462 while (it.hasNext()) 463 iterated.add(it.next()); 464 assertTrue(copy.containsAll(iterated)); 465 assertTrue(copy.containsAll(spliterated)); 466 assertTrue(copy.containsAll(removed)); 467 if (s.hasCharacteristics(Spliterator.CONCURRENT)) { 468 ArrayList iteratedAndRemoved = new ArrayList(iterated); 469 ArrayList spliteratedAndRemoved = new ArrayList(spliterated); 470 iteratedAndRemoved.retainAll(removed); 471 spliteratedAndRemoved.retainAll(removed); 472 assertTrue(iteratedAndRemoved.size() <= 1); 473 assertTrue(spliteratedAndRemoved.size() <= 1); 474 if (testImplementationDetails 475 && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) 476 assertTrue(spliteratedAndRemoved.isEmpty()); 477 } 478 } 479 480 /** 481 * Various ways of traversing a collection yield same elements 482 */ 483 public void testTraversalEquivalence() { 484 Collection c = impl.emptyCollection(); 485 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 486 int n = rnd.nextInt(6); 487 for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); 488 ArrayList iterated = new ArrayList(); 489 ArrayList iteratedForEachRemaining = new ArrayList(); 490 ArrayList tryAdvanced = new ArrayList(); 491 ArrayList spliterated = new ArrayList(); 492 ArrayList splitonced = new ArrayList(); 493 ArrayList forEached = new ArrayList(); 494 ArrayList streamForEached = new ArrayList(); 495 ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue(); 496 ArrayList removeIfed = new ArrayList(); 497 for (Object x : c) iterated.add(x); 498 c.iterator().forEachRemaining(iteratedForEachRemaining::add); 499 for (Spliterator s = c.spliterator(); 500 s.tryAdvance(tryAdvanced::add); ) {} 501 c.spliterator().forEachRemaining(spliterated::add); 502 { // trySplit returns "strict prefix" 503 Spliterator s1 = c.spliterator(), s2 = s1.trySplit(); 504 if (s2 != null) s2.forEachRemaining(splitonced::add); 505 s1.forEachRemaining(splitonced::add); 506 } 507 c.forEach(forEached::add); 508 c.stream().forEach(streamForEached::add); 509 c.parallelStream().forEach(parallelStreamForEached::add); 510 c.removeIf(e -> { removeIfed.add(e); return false; }); 511 boolean ordered = 512 c.spliterator().hasCharacteristics(Spliterator.ORDERED); 513 if (c instanceof List || c instanceof Deque) 514 assertTrue(ordered); 515 HashSet cset = new HashSet(c); 516 assertEquals(cset, new HashSet(parallelStreamForEached)); 517 if (ordered) { 518 assertEquals(iterated, iteratedForEachRemaining); 519 assertEquals(iterated, tryAdvanced); 520 assertEquals(iterated, spliterated); 521 assertEquals(iterated, splitonced); 522 assertEquals(iterated, forEached); 523 assertEquals(iterated, streamForEached); 524 assertEquals(iterated, removeIfed); 525 } else { 526 assertEquals(cset, new HashSet(iterated)); 527 assertEquals(cset, new HashSet(iteratedForEachRemaining)); 528 assertEquals(cset, new HashSet(tryAdvanced)); 529 assertEquals(cset, new HashSet(spliterated)); 530 assertEquals(cset, new HashSet(splitonced)); 531 assertEquals(cset, new HashSet(forEached)); 532 assertEquals(cset, new HashSet(streamForEached)); 533 assertEquals(cset, new HashSet(removeIfed)); 534 } 535 if (c instanceof Deque) { 536 Deque d = (Deque) c; 537 ArrayList descending = new ArrayList(); 538 ArrayList descendingForEachRemaining = new ArrayList(); 539 for (Iterator it = d.descendingIterator(); it.hasNext(); ) 540 descending.add(it.next()); 541 d.descendingIterator().forEachRemaining( 542 e -> descendingForEachRemaining.add(e)); 543 Collections.reverse(descending); 544 Collections.reverse(descendingForEachRemaining); 545 assertEquals(iterated, descending); 546 assertEquals(iterated, descendingForEachRemaining); 547 } 548 } 549 550 /** 551 * Iterator.forEachRemaining has same behavior as Iterator's 552 * default implementation. 553 */ 554 public void testForEachRemainingConsistentWithDefaultImplementation() { 555 Collection c = impl.emptyCollection(); 556 if (!testImplementationDetails 557 || c.getClass() == java.util.LinkedList.class) 558 return; 559 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 560 int n = 1 + rnd.nextInt(3); 561 for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); 562 ArrayList iterated = new ArrayList(); 563 ArrayList iteratedForEachRemaining = new ArrayList(); 564 Iterator it1 = c.iterator(); 565 Iterator it2 = c.iterator(); 566 assertTrue(it1.hasNext()); 567 assertTrue(it2.hasNext()); 568 c.clear(); 569 Object r1, r2; 570 try { 571 while (it1.hasNext()) iterated.add(it1.next()); 572 r1 = iterated; 573 } catch (ConcurrentModificationException ex) { 574 r1 = ConcurrentModificationException.class; 575 assertFalse(impl.isConcurrent()); 576 } 577 try { 578 it2.forEachRemaining(iteratedForEachRemaining::add); 579 r2 = iteratedForEachRemaining; 580 } catch (ConcurrentModificationException ex) { 581 r2 = ConcurrentModificationException.class; 582 assertFalse(impl.isConcurrent()); 583 } 584 assertEquals(r1, r2); 585 } 586 587 /** 588 * Calling Iterator#remove() after Iterator#forEachRemaining 589 * should (maybe) remove last element 590 */ 591 public void testRemoveAfterForEachRemaining() { 592 Collection c = impl.emptyCollection(); 593 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 594 ArrayList copy = new ArrayList(); 595 boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED); 596 testCollection: { 597 int n = 3 + rnd.nextInt(2); 598 for (int i = 0; i < n; i++) { 599 Object x = impl.makeElement(i); 600 c.add(x); 601 copy.add(x); 602 } 603 Iterator it = c.iterator(); 604 if (ordered) { 605 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 606 assertEquals(impl.makeElement(0), it.next()); 607 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 608 assertEquals(impl.makeElement(1), it.next()); 609 } else { 610 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 611 assertTrue(copy.contains(it.next())); 612 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 613 assertTrue(copy.contains(it.next())); 614 } 615 if (rnd.nextBoolean()) assertTrue(it.hasNext()); 616 it.forEachRemaining( 617 e -> { 618 assertTrue(c.contains(e)); 619 assertTrue(copy.contains(e));}); 620 if (testImplementationDetails) { 621 if (c instanceof java.util.concurrent.ArrayBlockingQueue) { 622 assertIteratorExhausted(it); 623 } else { 624 try { it.remove(); } 625 catch (UnsupportedOperationException ok) { 626 break testCollection; 627 } 628 assertEquals(n - 1, c.size()); 629 if (ordered) { 630 for (int i = 0; i < n - 1; i++) 631 assertTrue(c.contains(impl.makeElement(i))); 632 assertFalse(c.contains(impl.makeElement(n - 1))); 633 } 634 } 635 } 636 } 637 if (c instanceof Deque) { 638 Deque d = (Deque) impl.emptyCollection(); 639 assertTrue(ordered); 640 int n = 3 + rnd.nextInt(2); 641 for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); 642 Iterator it = d.descendingIterator(); 643 assertTrue(it.hasNext()); 644 assertEquals(impl.makeElement(n - 1), it.next()); 645 assertTrue(it.hasNext()); 646 assertEquals(impl.makeElement(n - 2), it.next()); 647 it.forEachRemaining(e -> assertTrue(c.contains(e))); 648 if (testImplementationDetails) { 649 it.remove(); 650 assertEquals(n - 1, d.size()); 651 for (int i = 1; i < n; i++) 652 assertTrue(d.contains(impl.makeElement(i))); 653 assertFalse(d.contains(impl.makeElement(0))); 654 } 655 } 656 } 657 658 /** 659 * stream().forEach returns elements in the collection 660 */ 661 public void testStreamForEach() throws Throwable { 662 final Collection c = impl.emptyCollection(); 663 final Object x = impl.makeElement(1); 664 final Object y = impl.makeElement(2); 665 final ArrayList found = new ArrayList(); 666 Consumer<Object> spy = o -> found.add(o); 667 c.stream().forEach(spy); 668 assertTrue(found.isEmpty()); 669 670 assertTrue(c.add(x)); 671 c.stream().forEach(spy); 672 assertEquals(Collections.singletonList(x), found); 673 found.clear(); 674 675 assertTrue(c.add(y)); 676 c.stream().forEach(spy); 677 assertEquals(2, found.size()); 678 assertTrue(found.contains(x)); 679 assertTrue(found.contains(y)); 680 found.clear(); 681 682 c.clear(); 683 c.stream().forEach(spy); 684 assertTrue(found.isEmpty()); 685 } 686 687 public void testStreamForEachConcurrentStressTest() throws Throwable { 688 if (!impl.isConcurrent()) return; 689 final Collection c = impl.emptyCollection(); 690 final long testDurationMillis = timeoutMillis(); 691 final AtomicBoolean done = new AtomicBoolean(false); 692 final Object elt = impl.makeElement(1); 693 final Future<?> f1, f2; 694 final ExecutorService pool = Executors.newCachedThreadPool(); 695 try (PoolCleaner cleaner = cleaner(pool, done)) { 696 final CountDownLatch threadsStarted = new CountDownLatch(2); 697 Runnable checkElt = () -> { 698 threadsStarted.countDown(); 699 while (!done.get()) 700 c.stream().forEach(x -> assertSame(x, elt)); }; 701 Runnable addRemove = () -> { 702 threadsStarted.countDown(); 703 while (!done.get()) { 704 assertTrue(c.add(elt)); 705 assertTrue(c.remove(elt)); 706 }}; 707 f1 = pool.submit(checkElt); 708 f2 = pool.submit(addRemove); 709 Thread.sleep(testDurationMillis); 710 } 711 assertNull(f1.get(0L, MILLISECONDS)); 712 assertNull(f2.get(0L, MILLISECONDS)); 713 } 714 715 /** 716 * collection.forEach returns elements in the collection 717 */ 718 public void testForEach() throws Throwable { 719 final Collection c = impl.emptyCollection(); 720 final Object x = impl.makeElement(1); 721 final Object y = impl.makeElement(2); 722 final ArrayList found = new ArrayList(); 723 Consumer<Object> spy = o -> found.add(o); 724 c.forEach(spy); 725 assertTrue(found.isEmpty()); 726 727 assertTrue(c.add(x)); 728 c.forEach(spy); 729 assertEquals(Collections.singletonList(x), found); 730 found.clear(); 731 732 assertTrue(c.add(y)); 733 c.forEach(spy); 734 assertEquals(2, found.size()); 735 assertTrue(found.contains(x)); 736 assertTrue(found.contains(y)); 737 found.clear(); 738 739 c.clear(); 740 c.forEach(spy); 741 assertTrue(found.isEmpty()); 742 } 743 744 /** TODO: promote to a common utility */ 745 static <T> T chooseOne(T ... ts) { 746 return ts[ThreadLocalRandom.current().nextInt(ts.length)]; 747 } 748 749 /** TODO: more random adders and removers */ 750 static <E> Runnable adderRemover(Collection<E> c, E e) { 751 return chooseOne( 752 () -> { 753 assertTrue(c.add(e)); 754 assertTrue(c.contains(e)); 755 assertTrue(c.remove(e)); 756 assertFalse(c.contains(e)); 757 }, 758 () -> { 759 assertTrue(c.add(e)); 760 assertTrue(c.contains(e)); 761 assertTrue(c.removeIf(x -> x == e)); 762 assertFalse(c.contains(e)); 763 }, 764 () -> { 765 assertTrue(c.add(e)); 766 assertTrue(c.contains(e)); 767 for (Iterator it = c.iterator();; ) 768 if (it.next() == e) { 769 try { it.remove(); } 770 catch (UnsupportedOperationException ok) { 771 c.remove(e); 772 } 773 break; 774 } 775 assertFalse(c.contains(e)); 776 }); 777 } 778 779 /** 780 * Concurrent Spliterators, once exhausted, stay exhausted. 781 */ 782 public void testStickySpliteratorExhaustion() throws Throwable { 783 if (!impl.isConcurrent()) return; 784 if (!testImplementationDetails) return; 785 final ThreadLocalRandom rnd = ThreadLocalRandom.current(); 786 final Consumer alwaysThrows = e -> { throw new AssertionError(); }; 787 final Collection c = impl.emptyCollection(); 788 final Spliterator s = c.spliterator(); 789 if (rnd.nextBoolean()) { 790 assertFalse(s.tryAdvance(alwaysThrows)); 791 } else { 792 s.forEachRemaining(alwaysThrows); 793 } 794 final Object one = impl.makeElement(1); 795 // Spliterator should not notice added element 796 c.add(one); 797 if (rnd.nextBoolean()) { 798 assertFalse(s.tryAdvance(alwaysThrows)); 799 } else { 800 s.forEachRemaining(alwaysThrows); 801 } 802 } 803 804 /** 805 * Motley crew of threads concurrently randomly hammer the collection. 806 */ 807 public void testDetectRaces() throws Throwable { 808 if (!impl.isConcurrent()) return; 809 final ThreadLocalRandom rnd = ThreadLocalRandom.current(); 810 final Collection c = impl.emptyCollection(); 811 final long testDurationMillis 812 = expensiveTests ? LONG_DELAY_MS : timeoutMillis(); 813 final AtomicBoolean done = new AtomicBoolean(false); 814 final Object one = impl.makeElement(1); 815 final Object two = impl.makeElement(2); 816 final Consumer checkSanity = x -> assertTrue(x == one || x == two); 817 final Consumer<Object[]> checkArraySanity = array -> { 818 // assertTrue(array.length <= 2); // duplicates are permitted 819 for (Object x : array) assertTrue(x == one || x == two); 820 }; 821 final Object[] emptyArray = 822 (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); 823 final List<Future<?>> futures; 824 final Phaser threadsStarted = new Phaser(1); // register this thread 825 final Runnable[] frobbers = { 826 () -> c.forEach(checkSanity), 827 () -> c.stream().forEach(checkSanity), 828 () -> c.parallelStream().forEach(checkSanity), 829 () -> c.spliterator().trySplit(), 830 () -> { 831 Spliterator s = c.spliterator(); 832 s.tryAdvance(checkSanity); 833 s.trySplit(); 834 }, 835 () -> { 836 Spliterator s = c.spliterator(); 837 do {} while (s.tryAdvance(checkSanity)); 838 }, 839 () -> { for (Object x : c) checkSanity.accept(x); }, 840 () -> checkArraySanity.accept(c.toArray()), 841 () -> checkArraySanity.accept(c.toArray(emptyArray)), 842 () -> { 843 Object[] a = new Object[5]; 844 Object three = impl.makeElement(3); 845 Arrays.fill(a, 0, a.length, three); 846 Object[] x = c.toArray(a); 847 if (x == a) 848 for (int i = 0; i < a.length && a[i] != null; i++) 849 checkSanity.accept(a[i]); 850 // A careful reading of the spec does not support: 851 // for (i++; i < a.length; i++) assertSame(three, a[i]); 852 else 853 checkArraySanity.accept(x); 854 }, 855 adderRemover(c, one), 856 adderRemover(c, two), 857 }; 858 final List<Runnable> tasks = 859 Arrays.stream(frobbers) 860 .filter(task -> rnd.nextBoolean()) // random subset 861 .map(task -> (Runnable) () -> { 862 threadsStarted.arriveAndAwaitAdvance(); 863 while (!done.get()) 864 task.run(); 865 }) 866 .collect(Collectors.toList()); 867 final ExecutorService pool = Executors.newCachedThreadPool(); 868 try (PoolCleaner cleaner = cleaner(pool, done)) { 869 threadsStarted.bulkRegister(tasks.size()); 870 futures = tasks.stream() 871 .map(pool::submit) 872 .collect(Collectors.toList()); 873 threadsStarted.arriveAndDeregister(); 874 Thread.sleep(testDurationMillis); 875 } 876 for (Future future : futures) 877 assertNull(future.get(0L, MILLISECONDS)); 878 } 879 880 /** 881 * Spliterators are either IMMUTABLE or truly late-binding or, if 882 * concurrent, use the same "late-binding style" of returning 883 * elements added between creation and first use. 884 */ 885 public void testLateBindingStyle() { 886 if (!testImplementationDetails) return; 887 if (impl.klazz() == ArrayList.class) return; // for jdk8 888 // Immutable (snapshot) spliterators are exempt 889 if (impl.emptyCollection().spliterator() 890 .hasCharacteristics(Spliterator.IMMUTABLE)) 891 return; 892 final Object one = impl.makeElement(1); 893 { 894 final Collection c = impl.emptyCollection(); 895 final Spliterator split = c.spliterator(); 896 c.add(one); 897 assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); 898 assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); 899 assertTrue(c.contains(one)); 900 } 901 { 902 final AtomicLong count = new AtomicLong(0); 903 final Collection c = impl.emptyCollection(); 904 final Spliterator split = c.spliterator(); 905 c.add(one); 906 split.forEachRemaining( 907 e -> { assertSame(e, one); count.getAndIncrement(); }); 908 assertEquals(1L, count.get()); 909 assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); 910 assertTrue(c.contains(one)); 911 } 912 } 913 914 /** 915 * Spliterator.getComparator throws IllegalStateException iff the 916 * spliterator does not report SORTED. 917 */ 918 public void testGetComparator_IllegalStateException() { 919 Collection c = impl.emptyCollection(); 920 Spliterator s = c.spliterator(); 921 boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED); 922 try { 923 s.getComparator(); 924 assertTrue(reportsSorted); 925 } catch (IllegalStateException ex) { 926 assertFalse(reportsSorted); 927 } 928 } 929 930 public void testCollectionCopies() throws Exception { 931 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 932 Collection c = impl.emptyCollection(); 933 for (int n = rnd.nextInt(4); n--> 0; ) 934 c.add(impl.makeElement(rnd.nextInt())); 935 assertEquals(c, c); 936 if (c instanceof List) 937 assertCollectionsEquals(c, new ArrayList(c)); 938 else if (c instanceof Set) 939 assertCollectionsEquals(c, new HashSet(c)); 940 else if (c instanceof Deque) 941 assertCollectionsEquivalent(c, new ArrayDeque(c)); 942 943 Collection clone = cloneableClone(c); 944 if (clone != null) { 945 assertSame(c.getClass(), clone.getClass()); 946 assertCollectionsEquivalent(c, clone); 947 } 948 try { 949 Collection serialClone = serialClonePossiblyFailing(c); 950 assertSame(c.getClass(), serialClone.getClass()); 951 assertCollectionsEquivalent(c, serialClone); 952 } catch (java.io.NotSerializableException acceptable) {} 953 } 954 955 /** 956 * TODO: move out of limbo 957 * 8203662: remove increment of modCount from ArrayList and Vector replaceAll() 958 */ 959 public void DISABLED_testReplaceAllIsNotStructuralModification() { 960 Collection c = impl.emptyCollection(); 961 if (!(c instanceof List)) 962 return; 963 List list = (List) c; 964 ThreadLocalRandom rnd = ThreadLocalRandom.current(); 965 for (int n = rnd.nextInt(2, 10); n--> 0; ) 966 list.add(impl.makeElement(rnd.nextInt())); 967 ArrayList copy = new ArrayList(list); 968 int size = list.size(), half = size / 2; 969 Iterator it = list.iterator(); 970 for (int i = 0; i < half; i++) 971 assertEquals(it.next(), copy.get(i)); 972 list.replaceAll(n -> n); 973 // ConcurrentModificationException must not be thrown here. 974 for (int i = half; i < size; i++) 975 assertEquals(it.next(), copy.get(i)); 976 } 977 978 // public void testCollection8DebugFail() { 979 // fail(impl.klazz().getSimpleName()); 980 // } 981 }