ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
Revision: 1.1
Committed: Sun Jun 14 20:58:14 2015 UTC (8 years, 10 months ago) by jsr166
Branch: MAIN
Log Message:
add support for parameterized test execution; add support for automatically executing jdk8+ tests; add tests for JDK-8085978

File Contents

# User Rev Content
1 jsr166 1.1 /*
2     * Written by Doug Lea and Martin Buchholz with assistance from
3     * members of JCP JSR-166 Expert Group and released to the public
4     * domain, as explained at
5     * http://creativecommons.org/publicdomain/zero/1.0/
6     */
7    
8     import static java.util.concurrent.TimeUnit.MILLISECONDS;
9    
10     import java.util.ArrayList;
11     import java.util.Collection;
12     import java.util.Collections;
13     import java.util.concurrent.Executors;
14     import java.util.concurrent.ExecutorService;
15     import java.util.concurrent.Future;
16     import java.util.concurrent.atomic.AtomicBoolean;
17     import java.util.concurrent.atomic.AtomicLong;
18     import java.util.function.Consumer;
19    
20     import junit.framework.Test;
21    
22     /**
23     * Contains tests applicable to all jdk8+ Collection implementations.
24     * An extension of CollectionTest.
25     */
26     public class Collection8Test extends JSR166TestCase {
27     final CollectionImplementation impl;
28    
29     /** Tests are parameterized by a Collection implementation. */
30     Collection8Test(CollectionImplementation impl, String methodName) {
31     super(methodName);
32     this.impl = impl;
33     }
34    
35     public static Test testSuite(CollectionImplementation impl) {
36     return parameterizedTestSuite(Collection8Test.class,
37     CollectionImplementation.class,
38     impl);
39     }
40    
41     /**
42     * stream().forEach returns elements in the collection
43     */
44     public void testForEach() throws Throwable {
45     final Collection c = impl.emptyCollection();
46     final AtomicLong count = new AtomicLong(0L);
47     final Object x = impl.makeElement(1);
48     final Object y = impl.makeElement(2);
49     final ArrayList found = new ArrayList();
50     Consumer<Object> spy = (o) -> { found.add(o); };
51     c.stream().forEach(spy);
52     assertTrue(found.isEmpty());
53    
54     assertTrue(c.add(x));
55     c.stream().forEach(spy);
56     assertEquals(Collections.singletonList(x), found);
57     found.clear();
58    
59     assertTrue(c.add(y));
60     c.stream().forEach(spy);
61     assertEquals(2, found.size());
62     assertTrue(found.contains(x));
63     assertTrue(found.contains(y));
64     found.clear();
65    
66     c.clear();
67     c.stream().forEach(spy);
68     assertTrue(found.isEmpty());
69     }
70    
71     public void testForEachConcurrentStressTest() throws Throwable {
72     if (!impl.isConcurrent()) return;
73     final Collection c = impl.emptyCollection();
74     final long testDurationMillis = SHORT_DELAY_MS;
75     final AtomicBoolean done = new AtomicBoolean(false);
76     final Object elt = impl.makeElement(1);
77     ExecutorService pool = Executors.newCachedThreadPool();
78     Runnable checkElt = () -> {
79     while (!done.get())
80     c.stream().forEach((x) -> { assertSame(x, elt); }); };
81     Runnable addRemove = () -> {
82     while (!done.get()) {
83     assertTrue(c.add(elt));
84     assertTrue(c.remove(elt));
85     }};
86     Future<?> f1 = pool.submit(checkElt);
87     Future<?> f2 = pool.submit(addRemove);
88     Thread.sleep(testDurationMillis);
89     done.set(true);
90     pool.shutdown();
91     assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
92     assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS));
93     assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS));
94     }
95    
96     // public void testCollection8DebugFail() { fail(); }
97     }