ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.3: +133 -38 lines
Log Message:
Documentation scaffolding

File Contents

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