OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / util / concurrent / ConcurrentLinkedQueueTest.java
1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/licenses/publicdomain
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8
9 package tests.api.java.util.concurrent; // android-added
10
11 import junit.framework.*;
12 import java.util.*;
13 import java.util.concurrent.*;
14 import java.io.*;
15
16 public class ConcurrentLinkedQueueTest extends JSR166TestCase {
17     public static Test suite() {
18         return new TestSuite(ConcurrentLinkedQueueTest.class);
19     }
20
21     /**
22      * Create a queue of given size containing consecutive
23      * Integers 0 ... n.
24      */
25     private ConcurrentLinkedQueue populatedQueue(int n) {
26         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
27         assertTrue(q.isEmpty());
28         for (int i = 0; i < n; ++i)
29             assertTrue(q.offer(new Integer(i)));
30         assertFalse(q.isEmpty());
31         assertEquals(n, q.size());
32         return q;
33     }
34
35     /**
36      * new queue is empty
37      */
38     public void testConstructor1() {
39         assertEquals(0, new ConcurrentLinkedQueue().size());
40     }
41
42     /**
43      *  Initializing from null Collection throws NPE
44      */
45     public void testConstructor3() {
46         try {
47             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
48             shouldThrow();
49         } catch (NullPointerException success) {}
50     }
51
52     /**
53      * Initializing from Collection of null elements throws NPE
54      */
55     public void testConstructor4() {
56         try {
57             Integer[] ints = new Integer[SIZE];
58             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
59             shouldThrow();
60         } catch (NullPointerException success) {}
61     }
62
63     /**
64      * Initializing from Collection with some null elements throws NPE
65      */
66     public void testConstructor5() {
67         try {
68             Integer[] ints = new Integer[SIZE];
69             for (int i = 0; i < SIZE-1; ++i)
70                 ints[i] = new Integer(i);
71             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
72             shouldThrow();
73         } catch (NullPointerException success) {}
74     }
75
76     /**
77      * Queue contains all elements of collection used to initialize
78      */
79     public void testConstructor6() {
80         Integer[] ints = new Integer[SIZE];
81         for (int i = 0; i < SIZE; ++i)
82             ints[i] = new Integer(i);
83         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
84         for (int i = 0; i < SIZE; ++i)
85             assertEquals(ints[i], q.poll());
86     }
87
88     /**
89      * isEmpty is true before add, false after
90      */
91     public void testEmpty() {
92         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
93         assertTrue(q.isEmpty());
94         q.add(one);
95         assertFalse(q.isEmpty());
96         q.add(two);
97         q.remove();
98         q.remove();
99         assertTrue(q.isEmpty());
100     }
101
102     /**
103      * size changes when elements added and removed
104      */
105     public void testSize() {
106         ConcurrentLinkedQueue q = populatedQueue(SIZE);
107         for (int i = 0; i < SIZE; ++i) {
108             assertEquals(SIZE-i, q.size());
109             q.remove();
110         }
111         for (int i = 0; i < SIZE; ++i) {
112             assertEquals(i, q.size());
113             q.add(new Integer(i));
114         }
115     }
116
117     /**
118      * offer(null) throws NPE
119      */
120     public void testOfferNull() {
121         try {
122             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
123             q.offer(null);
124             shouldThrow();
125         } catch (NullPointerException success) {}
126     }
127
128     /**
129      * add(null) throws NPE
130      */
131     public void testAddNull() {
132         try {
133             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
134             q.add(null);
135             shouldThrow();
136         } catch (NullPointerException success) {}
137     }
138
139
140     /**
141      * Offer returns true
142      */
143     public void testOffer() {
144         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
145         assertTrue(q.offer(zero));
146         assertTrue(q.offer(one));
147     }
148
149     /**
150      * add returns true
151      */
152     public void testAdd() {
153         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
154         for (int i = 0; i < SIZE; ++i) {
155             assertEquals(i, q.size());
156             assertTrue(q.add(new Integer(i)));
157         }
158     }
159
160     /**
161      * addAll(null) throws NPE
162      */
163     public void testAddAll1() {
164         try {
165             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
166             q.addAll(null);
167             shouldThrow();
168         } catch (NullPointerException success) {}
169     }
170
171     /**
172      * addAll(this) throws IAE
173      */
174     public void testAddAllSelf() {
175         try {
176             ConcurrentLinkedQueue q = populatedQueue(SIZE);
177             q.addAll(q);
178             shouldThrow();
179         } catch (IllegalArgumentException success) {}
180     }
181
182     /**
183      * addAll of a collection with null elements throws NPE
184      */
185     public void testAddAll2() {
186         try {
187             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
188             Integer[] ints = new Integer[SIZE];
189             q.addAll(Arrays.asList(ints));
190             shouldThrow();
191         } catch (NullPointerException success) {}
192     }
193     /**
194      *  addAll of a collection with any null elements throws NPE after
195      * possibly adding some elements
196      */
197     public void testAddAll3() {
198         try {
199             ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
200             Integer[] ints = new Integer[SIZE];
201             for (int i = 0; i < SIZE-1; ++i)
202                 ints[i] = new Integer(i);
203             q.addAll(Arrays.asList(ints));
204             shouldThrow();
205         } catch (NullPointerException success) {}
206     }
207
208     /**
209      * Queue contains all elements, in traversal order, of successful addAll
210      */
211     public void testAddAll5() {
212         Integer[] empty = new Integer[0];
213         Integer[] ints = new Integer[SIZE];
214         for (int i = 0; i < SIZE; ++i)
215             ints[i] = new Integer(i);
216         ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
217         assertFalse(q.addAll(Arrays.asList(empty)));
218         assertTrue(q.addAll(Arrays.asList(ints)));
219         for (int i = 0; i < SIZE; ++i)
220             assertEquals(ints[i], q.poll());
221     }
222
223     /**
224      * poll succeeds unless empty
225      */
226     public void testPoll() {
227         ConcurrentLinkedQueue q = populatedQueue(SIZE);
228         for (int i = 0; i < SIZE; ++i) {
229             assertEquals(i, q.poll());
230         }
231         assertNull(q.poll());
232     }
233
234     /**
235      * peek returns next element, or null if empty
236      */
237     public void testPeek() {
238         ConcurrentLinkedQueue q = populatedQueue(SIZE);
239         for (int i = 0; i < SIZE; ++i) {
240             assertEquals(i, q.peek());
241             assertEquals(i, q.poll());
242             assertTrue(q.peek() == null ||
243                        !q.peek().equals(i));
244         }
245         assertNull(q.peek());
246     }
247
248     /**
249      * element returns next element, or throws NSEE if empty
250      */
251     public void testElement() {
252         ConcurrentLinkedQueue q = populatedQueue(SIZE);
253         for (int i = 0; i < SIZE; ++i) {
254             assertEquals(i, q.element());
255             assertEquals(i, q.poll());
256         }
257         try {
258             q.element();
259             shouldThrow();
260         } catch (NoSuchElementException success) {}
261     }
262
263     /**
264      *  remove removes next element, or throws NSEE if empty
265      */
266     public void testRemove() {
267         ConcurrentLinkedQueue q = populatedQueue(SIZE);
268         for (int i = 0; i < SIZE; ++i) {
269             assertEquals(i, q.remove());
270         }
271         try {
272             q.remove();
273             shouldThrow();
274         } catch (NoSuchElementException success) {}
275     }
276
277     /**
278      * remove(x) removes x and returns true if present
279      */
280     public void testRemoveElement() {
281         ConcurrentLinkedQueue q = populatedQueue(SIZE);
282         for (int i = 1; i < SIZE; i+=2) {
283             assertTrue(q.remove(new Integer(i)));
284         }
285         for (int i = 0; i < SIZE; i+=2) {
286             assertTrue(q.remove(new Integer(i)));
287             assertFalse(q.remove(new Integer(i+1)));
288         }
289         assertTrue(q.isEmpty());
290     }
291
292     /**
293      * contains(x) reports true when elements added but not yet removed
294      */
295     public void testContains() {
296         ConcurrentLinkedQueue q = populatedQueue(SIZE);
297         for (int i = 0; i < SIZE; ++i) {
298             assertTrue(q.contains(new Integer(i)));
299             q.poll();
300             assertFalse(q.contains(new Integer(i)));
301         }
302     }
303
304     /**
305      * clear removes all elements
306      */
307     public void testClear() {
308         ConcurrentLinkedQueue q = populatedQueue(SIZE);
309         q.clear();
310         assertTrue(q.isEmpty());
311         assertEquals(0, q.size());
312         q.add(one);
313         assertFalse(q.isEmpty());
314         q.clear();
315         assertTrue(q.isEmpty());
316     }
317
318     /**
319      * containsAll(c) is true when c contains a subset of elements
320      */
321     public void testContainsAll() {
322         ConcurrentLinkedQueue q = populatedQueue(SIZE);
323         ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
324         for (int i = 0; i < SIZE; ++i) {
325             assertTrue(q.containsAll(p));
326             assertFalse(p.containsAll(q));
327             p.add(new Integer(i));
328         }
329         assertTrue(p.containsAll(q));
330     }
331
332     /**
333      * retainAll(c) retains only those elements of c and reports true if change
334      */
335     public void testRetainAll() {
336         ConcurrentLinkedQueue q = populatedQueue(SIZE);
337         ConcurrentLinkedQueue p = populatedQueue(SIZE);
338         for (int i = 0; i < SIZE; ++i) {
339             boolean changed = q.retainAll(p);
340             if (i == 0)
341                 assertFalse(changed);
342             else
343                 assertTrue(changed);
344
345             assertTrue(q.containsAll(p));
346             assertEquals(SIZE-i, q.size());
347             p.remove();
348         }
349     }
350
351     /**
352      * removeAll(c) removes only those elements of c and reports true if changed
353      */
354     public void testRemoveAll() {
355         for (int i = 1; i < SIZE; ++i) {
356             ConcurrentLinkedQueue q = populatedQueue(SIZE);
357             ConcurrentLinkedQueue p = populatedQueue(i);
358             assertTrue(q.removeAll(p));
359             assertEquals(SIZE-i, q.size());
360             for (int j = 0; j < i; ++j) {
361                 Integer I = (Integer)(p.remove());
362                 assertFalse(q.contains(I));
363             }
364         }
365     }
366
367     /**
368      * toArray contains all elements
369      */
370     public void testToArray() {
371         ConcurrentLinkedQueue q = populatedQueue(SIZE);
372         Object[] o = q.toArray();
373         Arrays.sort(o);
374         for (int i = 0; i < o.length; i++)
375             assertEquals(o[i], q.poll());
376     }
377
378     /**
379      *  toArray(a) contains all elements
380      */
381     public void testToArray2() {
382         ConcurrentLinkedQueue q = populatedQueue(SIZE);
383         Integer[] ints = new Integer[SIZE];
384         ints = (Integer[])q.toArray(ints);
385         Arrays.sort(ints);
386         for (int i = 0; i < ints.length; i++)
387             assertEquals(ints[i], q.poll());
388     }
389
390     /**
391      * toArray(null) throws NPE
392      */
393     public void testToArray_BadArg() {
394         ConcurrentLinkedQueue q = populatedQueue(SIZE);
395         try {
396             Object o[] = q.toArray(null);
397             shouldThrow();
398         } catch (NullPointerException success) {}
399     }
400
401     /**
402      * toArray with incompatible array type throws ArrayStoreException
403      */
404     public void testToArray1_BadArg() {
405         ConcurrentLinkedQueue q = populatedQueue(SIZE);
406         try {
407             Object o[] = q.toArray(new String[10]);
408             shouldThrow();
409         } catch (ArrayStoreException success) {}
410     }
411
412     /**
413      *  iterator iterates through all elements
414      */
415     public void testIterator() {
416         ConcurrentLinkedQueue q = populatedQueue(SIZE);
417         int i = 0;
418         Iterator it = q.iterator();
419         while (it.hasNext()) {
420             assertTrue(q.contains(it.next()));
421             ++i;
422         }
423         assertEquals(i, SIZE);
424     }
425
426     /**
427      * iterator ordering is FIFO
428      */
429     public void testIteratorOrdering() {
430         final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
431         q.add(one);
432         q.add(two);
433         q.add(three);
434
435         int k = 0;
436         for (Iterator it = q.iterator(); it.hasNext();) {
437             assertEquals(++k, it.next());
438         }
439
440         assertEquals(3, k);
441     }
442
443     /**
444      * Modifications do not cause iterators to fail
445      */
446     public void testWeaklyConsistentIteration () {
447         final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
448         q.add(one);
449         q.add(two);
450         q.add(three);
451
452         for (Iterator it = q.iterator(); it.hasNext();) {
453             q.remove();
454             it.next();
455         }
456
457         assertEquals("queue should be empty again", 0, q.size());
458     }
459
460     /**
461      * iterator.remove removes current element
462      */
463     public void testIteratorRemove () {
464         final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
465         q.add(one);
466         q.add(two);
467         q.add(three);
468         Iterator it = q.iterator();
469         it.next();
470         it.remove();
471         it = q.iterator();
472         assertSame(it.next(), two);
473         assertSame(it.next(), three);
474         assertFalse(it.hasNext());
475     }
476
477
478     /**
479      * toString contains toStrings of elements
480      */
481     public void testToString() {
482         ConcurrentLinkedQueue q = populatedQueue(SIZE);
483         String s = q.toString();
484         for (int i = 0; i < SIZE; ++i) {
485             assertTrue(s.indexOf(String.valueOf(i)) >= 0);
486         }
487     }
488
489     /**
490      * A deserialized serialized queue has same elements in same order
491      */
492     public void testSerialization() throws Exception {
493         ConcurrentLinkedQueue q = populatedQueue(SIZE);
494         ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
495         ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
496         out.writeObject(q);
497         out.close();
498
499         ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
500         ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
501         ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
502         assertEquals(q.size(), r.size());
503         while (!q.isEmpty())
504             assertEquals(q.remove(), r.remove());
505     }
506
507 }