ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.3 by jsr166, Sun Oct 9 05:25:09 2016 UTC vs.
Revision 1.8 by jsr166, Thu Nov 3 15:31:25 2016 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines