ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedListTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:55 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

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    
12     public class LinkedListTest extends TestCase {
13    
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(LinkedListTest.class);
25     }
26    
27     /**
28     * Create a queue of given size containing consecutive
29     * Integers 0 ... n.
30     */
31     private LinkedList fullQueue(int n) {
32     LinkedList q = new LinkedList();
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 LinkedList().size());
43     }
44    
45     public void testConstructor3() {
46     try {
47     LinkedList q = new LinkedList((Collection)null);
48     fail("Cannot make from null collection");
49     }
50     catch (NullPointerException success) {}
51     }
52    
53     public void testConstructor6(){
54     try {
55     Integer[] ints = new Integer[N];
56     for (int i = 0; i < N; ++i)
57     ints[i] = new Integer(i);
58     LinkedList q = new LinkedList(Arrays.asList(ints));
59     for (int i = 0; i < N; ++i)
60     assertEquals(ints[i], q.poll());
61     }
62     finally {}
63     }
64    
65     public void testEmpty() {
66     LinkedList q = new LinkedList();
67     assertTrue(q.isEmpty());
68     q.add(new Integer(1));
69     assertFalse(q.isEmpty());
70     q.add(new Integer(2));
71     q.remove();
72     q.remove();
73     assertTrue(q.isEmpty());
74     }
75    
76     public void testSize() {
77     LinkedList q = fullQueue(N);
78     for (int i = 0; i < N; ++i) {
79     assertEquals(N-i, q.size());
80     q.remove();
81     }
82     for (int i = 0; i < N; ++i) {
83     assertEquals(i, q.size());
84     q.add(new Integer(i));
85     }
86     }
87    
88     public void testOfferNull(){
89     try {
90     LinkedList q = new LinkedList();
91     q.offer(null);
92     } catch (NullPointerException ie) {
93     fail("should not throw NPE");
94     }
95     }
96    
97     public void testOffer() {
98     LinkedList q = new LinkedList();
99     assertTrue(q.offer(new Integer(0)));
100     assertTrue(q.offer(new Integer(1)));
101     }
102    
103     public void testAdd(){
104     LinkedList q = new LinkedList();
105     for (int i = 0; i < N; ++i) {
106     assertEquals(i, q.size());
107     assertTrue(q.add(new Integer(i)));
108     }
109     }
110    
111     public void testAddAll1(){
112     try {
113     LinkedList q = new LinkedList();
114     q.addAll(null);
115     fail("Cannot add null collection");
116     }
117     catch (NullPointerException success) {}
118     }
119    
120     public void testAddAll5(){
121     try {
122     Integer[] empty = new Integer[0];
123     Integer[] ints = new Integer[N];
124     for (int i = 0; i < N; ++i)
125     ints[i] = new Integer(i);
126     LinkedList q = new LinkedList();
127     assertFalse(q.addAll(Arrays.asList(empty)));
128     assertTrue(q.addAll(Arrays.asList(ints)));
129     for (int i = 0; i < N; ++i)
130     assertEquals(ints[i], q.poll());
131     }
132     finally {}
133     }
134    
135     public void testPoll(){
136     LinkedList q = fullQueue(N);
137     for (int i = 0; i < N; ++i) {
138     assertEquals(i, ((Integer)q.poll()).intValue());
139     }
140     assertNull(q.poll());
141     }
142    
143     public void testPeek(){
144     LinkedList q = fullQueue(N);
145     for (int i = 0; i < N; ++i) {
146     assertEquals(i, ((Integer)q.peek()).intValue());
147     q.poll();
148     assertTrue(q.peek() == null ||
149     i != ((Integer)q.peek()).intValue());
150     }
151     assertNull(q.peek());
152     }
153    
154     public void testElement(){
155     LinkedList q = fullQueue(N);
156     for (int i = 0; i < N; ++i) {
157     assertEquals(i, ((Integer)q.element()).intValue());
158     q.poll();
159     }
160     try {
161     q.element();
162     fail("no such element");
163     }
164     catch (NoSuchElementException success) {}
165     }
166    
167     public void testRemove(){
168     LinkedList q = fullQueue(N);
169     for (int i = 0; i < N; ++i) {
170     assertEquals(i, ((Integer)q.remove()).intValue());
171     }
172     try {
173     q.remove();
174     fail("remove should throw");
175     } catch (NoSuchElementException success){
176     }
177     }
178    
179     public void testRemoveElement(){
180     LinkedList q = fullQueue(N);
181     for (int i = 1; i < N; i+=2) {
182     assertTrue(q.remove(new Integer(i)));
183     }
184     for (int i = 0; i < N; i+=2) {
185     assertTrue(q.remove(new Integer(i)));
186     assertFalse(q.remove(new Integer(i+1)));
187     }
188     assert(q.isEmpty());
189     }
190    
191     public void testContains(){
192     LinkedList q = fullQueue(N);
193     for (int i = 0; i < N; ++i) {
194     assertTrue(q.contains(new Integer(i)));
195     q.poll();
196     assertFalse(q.contains(new Integer(i)));
197     }
198     }
199    
200     public void testClear(){
201     LinkedList q = fullQueue(N);
202     q.clear();
203     assertTrue(q.isEmpty());
204     assertEquals(0, q.size());
205     q.add(new Integer(1));
206     assertFalse(q.isEmpty());
207     q.clear();
208     assertTrue(q.isEmpty());
209     }
210    
211     public void testContainsAll(){
212     LinkedList q = fullQueue(N);
213     LinkedList p = new LinkedList();
214     for (int i = 0; i < N; ++i) {
215     assertTrue(q.containsAll(p));
216     assertFalse(p.containsAll(q));
217     p.add(new Integer(i));
218     }
219     assertTrue(p.containsAll(q));
220     }
221    
222     public void testRetainAll(){
223     LinkedList q = fullQueue(N);
224     LinkedList p = fullQueue(N);
225     for (int i = 0; i < N; ++i) {
226     boolean changed = q.retainAll(p);
227     if (i == 0)
228     assertFalse(changed);
229     else
230     assertTrue(changed);
231    
232     assertTrue(q.containsAll(p));
233     assertEquals(N-i, q.size());
234     p.remove();
235     }
236     }
237    
238     public void testRemoveAll(){
239     for (int i = 1; i < N; ++i) {
240     LinkedList q = fullQueue(N);
241     LinkedList p = fullQueue(i);
242     assertTrue(q.removeAll(p));
243     assertEquals(N-i, q.size());
244     for (int j = 0; j < i; ++j) {
245     Integer I = (Integer)(p.remove());
246     assertFalse(q.contains(I));
247     }
248     }
249     }
250    
251     public void testToArray(){
252     LinkedList q = fullQueue(N);
253     Object[] o = q.toArray();
254     Arrays.sort(o);
255     for(int i = 0; i < o.length; i++)
256     assertEquals(o[i], q.poll());
257     }
258    
259     public void testToArray2(){
260     LinkedList q = fullQueue(N);
261     Integer[] ints = new Integer[N];
262     ints = (Integer[])q.toArray(ints);
263     Arrays.sort(ints);
264     for(int i = 0; i < ints.length; i++)
265     assertEquals(ints[i], q.poll());
266     }
267    
268     public void testIterator(){
269     LinkedList q = fullQueue(N);
270     int i = 0;
271     Iterator it = q.iterator();
272     while(it.hasNext()) {
273     assertTrue(q.contains(it.next()));
274     ++i;
275     }
276     assertEquals(i, N);
277     }
278    
279     public void testIteratorOrdering() {
280    
281     final LinkedList q = new LinkedList();
282    
283     q.add(new Integer(1));
284     q.add(new Integer(2));
285     q.add(new Integer(3));
286    
287     int k = 0;
288     for (Iterator it = q.iterator(); it.hasNext();) {
289     int i = ((Integer)(it.next())).intValue();
290     assertEquals("items should come out in order", ++k, i);
291     }
292    
293     assertEquals("should go through 3 elements", 3, k);
294     }
295    
296     public void testIteratorRemove () {
297     final LinkedList q = new LinkedList();
298    
299     q.add(new Integer(1));
300     q.add(new Integer(2));
301     q.add(new Integer(3));
302    
303     Iterator it = q.iterator();
304     it.next();
305     it.remove();
306    
307     it = q.iterator();
308     assertEquals(it.next(), new Integer(2));
309     assertEquals(it.next(), new Integer(3));
310     assertFalse(it.hasNext());
311     }
312    
313    
314     public void testToString(){
315     LinkedList q = fullQueue(N);
316     String s = q.toString();
317     for (int i = 0; i < N; ++i) {
318     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
319     }
320     }
321    
322     public void testAddFirst(){
323     LinkedList q = fullQueue(3);
324     q.addFirst(new Integer(4));
325     assertEquals(new Integer(4),q.get(0));
326     }
327    
328     public void testAddLast(){
329     LinkedList q = fullQueue(3);
330     q.addLast(new Integer(3));
331     assertEquals(new Integer(3),q.get(3));
332     }
333    
334     public void testGetFirst() {
335     LinkedList q = fullQueue(3);
336     assertEquals(new Integer(0),q.getFirst());
337     }
338    
339     public void testGetLast() {
340     LinkedList q = fullQueue(3);
341     assertEquals(new Integer(2),q.getLast());
342     }
343    
344     public void testIndexOf(){
345     LinkedList q = fullQueue(3);
346     assertEquals(0,q.indexOf(new Integer(0)));
347     assertEquals(1,q.indexOf(new Integer(1)));
348     assertEquals(2,q.indexOf(new Integer(2)));
349     assertEquals(-1, q.indexOf("not there"));
350     }
351    
352     public void testLastIndexOf(){
353     LinkedList q = fullQueue(3);
354     q.add(new Integer(2));
355     assertEquals(3,q.lastIndexOf(new Integer(2)));
356     assertEquals(-1, q.lastIndexOf("not there"));
357     }
358    
359     public void testSet(){
360     LinkedList q = fullQueue(3);
361     q.set(0,(new Integer(1)));
362     assertFalse(q.contains(new Integer(0)));
363     assertEquals(new Integer(1), q.get(0));
364     }
365    
366    
367     public void testGetFirst_NoSuchElementException(){
368     try {
369     LinkedList l = new LinkedList();
370     l.getFirst();
371     fail("First Element");
372     }
373     catch(NoSuchElementException success) {}
374     }
375    
376     public void testRemoveFirst() {
377     try {
378     LinkedList l = new LinkedList();
379     l.removeFirst();
380     fail("R: First Element");
381     }
382     catch(NoSuchElementException success) {}
383     }
384    
385     public void testRemoveLast() {
386     try {
387     LinkedList l = new LinkedList();
388     l.removeLast();
389     fail("R: Last Element");
390     }
391     catch(NoSuchElementException success) {}
392     }
393    
394     public void testGetLast_NoSuchElementException(){
395     try {
396     LinkedList l = new LinkedList();
397     l.getLast();
398     fail("Last Element");
399     }
400     catch(NoSuchElementException success) {}
401     }
402    
403    
404     public void testAddAll_NullPointerException(){
405     try {
406     LinkedList l = new LinkedList();
407     l.addAll((Collection)null);
408     fail("Add All Failed");
409     }
410     catch(NullPointerException success){}
411     }
412    
413    
414     public void testAddAll1_OutOfBounds() {
415     try {
416     LinkedList l = new LinkedList();
417     l.addAll(4,new LinkedList());
418     fail("boolean addAll(int, Collection) should throw IndexOutOfBoundsException");
419     }
420     catch(IndexOutOfBoundsException success) {}
421     }
422    
423    
424     public void testAddAll2_IndexOutOfBoundsException() {
425     try {
426     LinkedList l = new LinkedList();
427     l.add(new Object());
428     LinkedList m = new LinkedList();
429     m.add(new Object());
430     l.addAll(4,m);
431     fail("Add All Failed " + l.size());
432     }catch(IndexOutOfBoundsException success) {}
433     }
434    
435     public void testAddAll4_BadIndex() {
436     try {
437     LinkedList l = new LinkedList();
438     l.add(new Object());
439     LinkedList m = new LinkedList();
440     m.add(new Object());
441     l.addAll(-1,m);
442     fail("Add All Failed " + l.size());
443     }catch(IndexOutOfBoundsException success){}
444     }
445    
446     public void testget1() {
447     try {
448     LinkedList l = new LinkedList();
449     l.add(new Object());
450     l.get(-1);
451     fail("get Failed - l.get(-1)");
452     }catch(IndexOutOfBoundsException success) {}
453     }
454    
455     public void testget2() {
456     try {
457     LinkedList l = new LinkedList();
458     l.add(new Object());
459     l.get(5);
460     fail("get Failed - l.get(5) l.size(): " + l.size());
461     }catch(IndexOutOfBoundsException success){}
462     }
463    
464     public void testset1() {
465     try {
466     LinkedList l = new LinkedList();
467     l.add(new Object());
468     l.set(-1,new Object());
469     fail("set failed - l.set(-1,...)" + l.size());
470     }catch(IndexOutOfBoundsException success){}
471     }
472    
473     public void testset2() {
474     try {
475     LinkedList l = new LinkedList();
476     l.add(new Object());
477     l.set(5,new Object());
478     fail("set failed = l.set(5,..) l.size():" + l.size());
479     }catch(IndexOutOfBoundsException success){}
480     }
481    
482     public void testadd1() {
483     try {
484     LinkedList l = new LinkedList();
485     l.add(new Object());
486     l.add(-1,new Object());
487     fail("Add Failed - l.add(-1) l.size(): " + l.size());
488     }catch(IndexOutOfBoundsException success){}
489     }
490    
491     public void add2(){
492     try {
493     LinkedList l = new LinkedList();
494     l.add(new Object());
495     l.add(5,new Object());
496     fail("Add Failed l.add(f,...)");
497     }catch(IndexOutOfBoundsException success) {}
498     }
499    
500     public void testremove(){
501     try {
502     LinkedList l = new LinkedList();
503     l.add(new Object());
504     l.remove(-1);
505     fail("Remove Failed l.remove(-1); l.size():" + l.size());
506     }catch(IndexOutOfBoundsException success){}
507     }
508    
509     public void testremove1(){
510     try {
511     LinkedList l = new LinkedList();
512     l.add(new Object());
513     l.remove(5);
514     fail("Remove Failed l.remove(5); l.size():" + l.size());
515     }catch(IndexOutOfBoundsException success){}
516     }
517    
518    
519     public void testremove2(){
520     try{
521     LinkedList l = new LinkedList();
522     l.remove();
523     fail("LinkedList - Object remove() should throw a NoSuchElementException");
524     }catch(NoSuchElementException e){}
525     }
526    
527     public void testlistIt1() {
528     try {
529     LinkedList l = new LinkedList();
530     l.add(new Object());
531     l.listIterator(5);
532     fail("l.listIterator(5) l.size():" + l.size());
533     }catch(IndexOutOfBoundsException success){}
534     }
535    
536     public void testlistIt2() {
537     try {
538     LinkedList l = new LinkedList();
539     l.add(new Object());
540     l.listIterator(-1);
541     fail("l.listIterator(-1) l.size():" + l.size());
542     }catch(IndexOutOfBoundsException success){}
543     }
544    
545     public void testlistIt3() {
546     try {
547     LinkedList l = new LinkedList();
548     l.add(new Object());
549     ListIterator a = l.listIterator(0);
550     l.removeFirst();
551     a.next();
552     fail("l.listIterator(-1) l.size():" + l.size());
553     }catch(ConcurrentModificationException success){}
554     }
555    
556     public void testToArray_BadArg() {
557     try {
558     LinkedList l = new LinkedList();
559     l.add(new Object());
560     Object o[] = l.toArray(null);
561     fail("l.toArray(null) did not throw an exception");
562     }catch(NullPointerException success){}
563     }
564    
565     public void testToArray1_BadArg() {
566     try {
567     LinkedList l = new LinkedList();
568     l.add(new Integer(5));
569     Object o[] = l.toArray(new String[10] );
570     fail("l.toArray(String[] f) did not throw an exception, an Integer was added");
571     }catch(ArrayStoreException success){}
572     }
573    
574     }