ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +67 -79 lines
Log Message:
New base class JSR166TestCase

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     public void testConstructor1(){
38     assertEquals(0, new ConcurrentLinkedQueue().size());
39     }
40    
41     public void testConstructor3() {
42     try {
43     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
44     fail("Cannot make from null collection");
45     }
46     catch (NullPointerException success) {}
47     }
48    
49     public void testConstructor4(){
50     try {
51 dl 1.3 Integer[] ints = new Integer[SIZE];
52 dl 1.1 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
53     fail("Cannot make with null elements");
54     }
55     catch (NullPointerException success) {}
56     }
57    
58     public void testConstructor5(){
59     try {
60 dl 1.3 Integer[] ints = new Integer[SIZE];
61     for (int i = 0; i < SIZE-1; ++i)
62 dl 1.1 ints[i] = new Integer(i);
63     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
64     fail("Cannot make with null elements");
65     }
66     catch (NullPointerException success) {}
67     }
68    
69     public void testConstructor6(){
70     try {
71 dl 1.3 Integer[] ints = new Integer[SIZE];
72     for (int i = 0; i < SIZE; ++i)
73 dl 1.1 ints[i] = new Integer(i);
74     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
75 dl 1.3 for (int i = 0; i < SIZE; ++i)
76 dl 1.1 assertEquals(ints[i], q.poll());
77     }
78     finally {}
79     }
80    
81     public void testEmpty() {
82     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
83     assertTrue(q.isEmpty());
84 dl 1.3 q.add(one);
85 dl 1.1 assertFalse(q.isEmpty());
86 dl 1.3 q.add(two);
87 dl 1.1 q.remove();
88     q.remove();
89     assertTrue(q.isEmpty());
90     }
91    
92     public void testSize() {
93 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
94     for (int i = 0; i < SIZE; ++i) {
95     assertEquals(SIZE-i, q.size());
96 dl 1.1 q.remove();
97     }
98 dl 1.3 for (int i = 0; i < SIZE; ++i) {
99 dl 1.1 assertEquals(i, q.size());
100     q.add(new Integer(i));
101     }
102     }
103    
104     public void testOfferNull(){
105     try {
106     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
107     q.offer(null);
108     fail("should throw NPE");
109     } catch (NullPointerException success) { }
110     }
111    
112     public void testOffer() {
113     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
114 dl 1.3 assertTrue(q.offer(zero));
115     assertTrue(q.offer(one));
116 dl 1.1 }
117    
118     public void testAdd(){
119     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
120 dl 1.3 for (int i = 0; i < SIZE; ++i) {
121 dl 1.1 assertEquals(i, q.size());
122     assertTrue(q.add(new Integer(i)));
123     }
124     }
125    
126     public void testAddAll1(){
127     try {
128     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
129     q.addAll(null);
130     fail("Cannot add null collection");
131     }
132     catch (NullPointerException success) {}
133     }
134     public void testAddAll2(){
135     try {
136     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
137 dl 1.3 Integer[] ints = new Integer[SIZE];
138 dl 1.1 q.addAll(Arrays.asList(ints));
139     fail("Cannot add null elements");
140     }
141     catch (NullPointerException success) {}
142     }
143     public void testAddAll3(){
144     try {
145     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
146 dl 1.3 Integer[] ints = new Integer[SIZE];
147     for (int i = 0; i < SIZE-1; ++i)
148 dl 1.1 ints[i] = new Integer(i);
149     q.addAll(Arrays.asList(ints));
150     fail("Cannot add null elements");
151     }
152     catch (NullPointerException success) {}
153     }
154    
155     public void testAddAll5(){
156     try {
157     Integer[] empty = new Integer[0];
158 dl 1.3 Integer[] ints = new Integer[SIZE];
159     for (int i = 0; i < SIZE; ++i)
160 dl 1.1 ints[i] = new Integer(i);
161     ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
162     assertFalse(q.addAll(Arrays.asList(empty)));
163     assertTrue(q.addAll(Arrays.asList(ints)));
164 dl 1.3 for (int i = 0; i < SIZE; ++i)
165 dl 1.1 assertEquals(ints[i], q.poll());
166     }
167     finally {}
168     }
169    
170     public void testPoll(){
171 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
172     for (int i = 0; i < SIZE; ++i) {
173 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
174     }
175     assertNull(q.poll());
176     }
177    
178     public void testPeek(){
179 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
180     for (int i = 0; i < SIZE; ++i) {
181 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
182     q.poll();
183     assertTrue(q.peek() == null ||
184     i != ((Integer)q.peek()).intValue());
185     }
186     assertNull(q.peek());
187     }
188    
189     public void testElement(){
190 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
191     for (int i = 0; i < SIZE; ++i) {
192 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
193     q.poll();
194     }
195     try {
196     q.element();
197     fail("no such element");
198     }
199     catch (NoSuchElementException success) {}
200     }
201    
202     public void testRemove(){
203 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
204     for (int i = 0; i < SIZE; ++i) {
205 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
206     }
207     try {
208     q.remove();
209     fail("remove should throw");
210     } catch (NoSuchElementException success){
211     }
212     }
213    
214     public void testRemoveElement(){
215 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
216     for (int i = 1; i < SIZE; i+=2) {
217 dl 1.1 assertTrue(q.remove(new Integer(i)));
218     }
219 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
220 dl 1.1 assertTrue(q.remove(new Integer(i)));
221     assertFalse(q.remove(new Integer(i+1)));
222     }
223 dl 1.2 assertTrue(q.isEmpty());
224 dl 1.1 }
225    
226     public void testContains(){
227 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
228     for (int i = 0; i < SIZE; ++i) {
229 dl 1.1 assertTrue(q.contains(new Integer(i)));
230     q.poll();
231     assertFalse(q.contains(new Integer(i)));
232     }
233     }
234    
235     public void testClear(){
236 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
237 dl 1.1 q.clear();
238     assertTrue(q.isEmpty());
239     assertEquals(0, q.size());
240 dl 1.3 q.add(one);
241 dl 1.1 assertFalse(q.isEmpty());
242     q.clear();
243     assertTrue(q.isEmpty());
244     }
245    
246     public void testContainsAll(){
247 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
248 dl 1.1 ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
249 dl 1.3 for (int i = 0; i < SIZE; ++i) {
250 dl 1.1 assertTrue(q.containsAll(p));
251     assertFalse(p.containsAll(q));
252     p.add(new Integer(i));
253     }
254     assertTrue(p.containsAll(q));
255     }
256    
257     public void testRetainAll(){
258 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
259     ConcurrentLinkedQueue p = populatedQueue(SIZE);
260     for (int i = 0; i < SIZE; ++i) {
261 dl 1.1 boolean changed = q.retainAll(p);
262     if (i == 0)
263     assertFalse(changed);
264     else
265     assertTrue(changed);
266    
267     assertTrue(q.containsAll(p));
268 dl 1.3 assertEquals(SIZE-i, q.size());
269 dl 1.1 p.remove();
270     }
271     }
272    
273     public void testRemoveAll(){
274 dl 1.3 for (int i = 1; i < SIZE; ++i) {
275     ConcurrentLinkedQueue q = populatedQueue(SIZE);
276     ConcurrentLinkedQueue p = populatedQueue(i);
277 dl 1.1 assertTrue(q.removeAll(p));
278 dl 1.3 assertEquals(SIZE-i, q.size());
279 dl 1.1 for (int j = 0; j < i; ++j) {
280     Integer I = (Integer)(p.remove());
281     assertFalse(q.contains(I));
282     }
283     }
284     }
285    
286     public void testToArray(){
287 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
288 dl 1.1 Object[] o = q.toArray();
289     Arrays.sort(o);
290     for(int i = 0; i < o.length; i++)
291     assertEquals(o[i], q.poll());
292     }
293    
294     public void testToArray2(){
295 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
296     Integer[] ints = new Integer[SIZE];
297 dl 1.1 ints = (Integer[])q.toArray(ints);
298     Arrays.sort(ints);
299     for(int i = 0; i < ints.length; i++)
300     assertEquals(ints[i], q.poll());
301     }
302    
303     public void testIterator(){
304 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
305 dl 1.1 int i = 0;
306     Iterator it = q.iterator();
307     while(it.hasNext()) {
308     assertTrue(q.contains(it.next()));
309     ++i;
310     }
311 dl 1.3 assertEquals(i, SIZE);
312 dl 1.1 }
313    
314     public void testIteratorOrdering() {
315     final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
316 dl 1.3 q.add(one);
317     q.add(two);
318     q.add(three);
319 dl 1.1
320     int k = 0;
321     for (Iterator it = q.iterator(); it.hasNext();) {
322     int i = ((Integer)(it.next())).intValue();
323     assertEquals("items should come out in order", ++k, i);
324     }
325    
326     assertEquals("should go through 3 elements", 3, k);
327     }
328    
329     public void testWeaklyConsistentIteration () {
330     final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
331 dl 1.3 q.add(one);
332     q.add(two);
333     q.add(three);
334 dl 1.1
335     try {
336     for (Iterator it = q.iterator(); it.hasNext();) {
337     q.remove();
338     it.next();
339     }
340     }
341     catch (ConcurrentModificationException e) {
342     fail("weakly consistent iterator; should not get CME");
343     }
344    
345     assertEquals("queue should be empty again", 0, q.size());
346     }
347    
348     public void testIteratorRemove () {
349     final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
350 dl 1.3 q.add(one);
351     q.add(two);
352     q.add(three);
353 dl 1.1 Iterator it = q.iterator();
354     it.next();
355     it.remove();
356     it = q.iterator();
357 dl 1.3 assertEquals(it.next(), two);
358     assertEquals(it.next(), three);
359 dl 1.1 assertFalse(it.hasNext());
360     }
361    
362    
363     public void testToString(){
364 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
365 dl 1.1 String s = q.toString();
366 dl 1.3 for (int i = 0; i < SIZE; ++i) {
367 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
368     }
369     }
370 dl 1.2
371     public void testSerialization() {
372 dl 1.3 ConcurrentLinkedQueue q = populatedQueue(SIZE);
373 dl 1.2 try {
374     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
375     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
376     out.writeObject(q);
377     out.close();
378    
379     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
380     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
381     ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
382     assertEquals(q.size(), r.size());
383     while (!q.isEmpty())
384     assertEquals(q.remove(), r.remove());
385     } catch(Exception e){
386     e.printStackTrace();
387     fail("unexpected exception");
388     }
389     }
390 dl 1.1
391     }