ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
Revision: 1.2
Committed: Sun Sep 7 20:39:11 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.1: +22 -1 lines
Log Message:
Added serialization and lock tests

File Contents

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