KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TestIntList


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import junit.framework.*;
22
23 /**
24  * Class to test IntList
25  *
26  * @author Marc Johnson
27  */

28
29 public class TestIntList
30     extends TestCase
31 {
32
33     /**
34      * Constructor TestIntList
35      *
36      * @param name
37      */

38
39     public TestIntList(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     /**
45      * test the various IntListconstructors
46      */

47
48     public void testConstructors()
49     {
50         IntList list = new IntList();
51
52         assertTrue(list.isEmpty());
53         list.add(0);
54         list.add(1);
55         IntList list2 = new IntList(list);
56
57         assertEquals(list, list2);
58         IntList list3 = new IntList(2);
59
60         assertTrue(list3.isEmpty());
61     }
62
63     /**
64      * test the add method
65      */

66
67     public void testAdd()
68     {
69         IntList list = new IntList();
70         int[] testArray =
71         {
72             0, 1, 2, 3, 5
73         };
74
75         for (int j = 0; j < testArray.length; j++)
76         {
77             list.add(testArray[ j ]);
78         }
79         for (int j = 0; j < testArray.length; j++)
80         {
81             assertEquals(testArray[ j ], list.get(j));
82         }
83         assertEquals(testArray.length, list.size());
84
85         // add at the beginning
86
list.add(0, -1);
87         assertEquals(-1, list.get(0));
88         assertEquals(testArray.length + 1, list.size());
89         for (int j = 0; j < testArray.length; j++)
90         {
91             assertEquals(testArray[ j ], list.get(j + 1));
92         }
93
94         // add in the middle
95
list.add(5, 4);
96         assertEquals(4, list.get(5));
97         assertEquals(testArray.length + 2, list.size());
98         for (int j = 0; j < list.size(); j++)
99         {
100             assertEquals(j - 1, list.get(j));
101         }
102
103         // add at the end
104
list.add(list.size(), 6);
105         assertEquals(testArray.length + 3, list.size());
106         for (int j = 0; j < list.size(); j++)
107         {
108             assertEquals(j - 1, list.get(j));
109         }
110
111         // add past end
112
try
113         {
114             list.add(list.size() + 1, 8);
115             fail("should have thrown exception");
116         }
117         catch (IndexOutOfBoundsException JavaDoc e)
118         {
119
120             // as expected
121
}
122
123         // test growth
124
list = new IntList(0);
125         for (int j = 0; j < 1000; j++)
126         {
127             list.add(j);
128         }
129         assertEquals(1000, list.size());
130         for (int j = 0; j < 1000; j++)
131         {
132             assertEquals(j, list.get(j));
133         }
134         list = new IntList(0);
135         for (int j = 0; j < 1000; j++)
136         {
137             list.add(0, j);
138         }
139         assertEquals(1000, list.size());
140         for (int j = 0; j < 1000; j++)
141         {
142             assertEquals(j, list.get(999 - j));
143         }
144     }
145
146     /**
147      * test the addAll method
148      */

149
150     public void testAddAll()
151     {
152         IntList list = new IntList();
153
154         for (int j = 0; j < 5; j++)
155         {
156             list.add(j);
157         }
158         IntList list2 = new IntList(0);
159
160         list2.addAll(list);
161         list2.addAll(list);
162         assertEquals(2 * list.size(), list2.size());
163         for (int j = 0; j < 5; j++)
164         {
165             assertEquals(list2.get(j), j);
166             assertEquals(list2.get(j + list.size()), j);
167         }
168         IntList empty = new IntList();
169         int limit = list.size();
170
171         for (int j = 0; j < limit; j++)
172         {
173             assertTrue(list.addAll(j, empty));
174             assertEquals(limit, list.size());
175         }
176         try
177         {
178             list.addAll(limit + 1, empty);
179             fail("should have thrown an exception");
180         }
181         catch (IndexOutOfBoundsException JavaDoc e)
182         {
183
184             // as expected
185
}
186
187         // try add at beginning
188
empty.addAll(0, list);
189         assertEquals(empty, list);
190
191         // try in the middle
192
empty.addAll(1, list);
193         assertEquals(2 * list.size(), empty.size());
194         assertEquals(list.get(0), empty.get(0));
195         assertEquals(list.get(0), empty.get(1));
196         assertEquals(list.get(1), empty.get(2));
197         assertEquals(list.get(1), empty.get(6));
198         assertEquals(list.get(2), empty.get(3));
199         assertEquals(list.get(2), empty.get(7));
200         assertEquals(list.get(3), empty.get(4));
201         assertEquals(list.get(3), empty.get(8));
202         assertEquals(list.get(4), empty.get(5));
203         assertEquals(list.get(4), empty.get(9));
204
205         // try at the end
206
empty.addAll(empty.size(), list);
207         assertEquals(3 * list.size(), empty.size());
208         assertEquals(list.get(0), empty.get(0));
209         assertEquals(list.get(0), empty.get(1));
210         assertEquals(list.get(0), empty.get(10));
211         assertEquals(list.get(1), empty.get(2));
212         assertEquals(list.get(1), empty.get(6));
213         assertEquals(list.get(1), empty.get(11));
214         assertEquals(list.get(2), empty.get(3));
215         assertEquals(list.get(2), empty.get(7));
216         assertEquals(list.get(2), empty.get(12));
217         assertEquals(list.get(3), empty.get(4));
218         assertEquals(list.get(3), empty.get(8));
219         assertEquals(list.get(3), empty.get(13));
220         assertEquals(list.get(4), empty.get(5));
221         assertEquals(list.get(4), empty.get(9));
222         assertEquals(list.get(4), empty.get(14));
223     }
224
225     /**
226      * test the clear method
227      */

228
229     public void testClear()
230     {
231         IntList list = new IntList();
232
233         for (int j = 0; j < 500; j++)
234         {
235             list.add(j);
236         }
237         assertEquals(500, list.size());
238         list.clear();
239         assertEquals(0, list.size());
240         for (int j = 0; j < 500; j++)
241         {
242             list.add(j + 1);
243         }
244         assertEquals(500, list.size());
245         for (int j = 0; j < 500; j++)
246         {
247             assertEquals(j + 1, list.get(j));
248         }
249     }
250
251     /**
252      * test the contains method
253      */

254
255     public void testContains()
256     {
257         IntList list = new IntList();
258
259         for (int j = 0; j < 1000; j += 2)
260         {
261             list.add(j);
262         }
263         for (int j = 0; j < 1000; j++)
264         {
265             if (j % 2 == 0)
266             {
267                 assertTrue(list.contains(j));
268             }
269             else
270             {
271                 assertTrue(!list.contains(j));
272             }
273         }
274     }
275
276     /**
277      * test the containsAll method
278      */

279
280     public void testContainsAll()
281     {
282         IntList list = new IntList();
283
284         assertTrue(list.containsAll(list));
285         for (int j = 0; j < 10; j++)
286         {
287             list.add(j);
288         }
289         IntList list2 = new IntList(list);
290
291         assertTrue(list2.containsAll(list));
292         assertTrue(list.containsAll(list2));
293         list2.add(10);
294         assertTrue(list2.containsAll(list));
295         assertTrue(!list.containsAll(list2));
296         list.add(11);
297         assertTrue(!list2.containsAll(list));
298         assertTrue(!list.containsAll(list2));
299     }
300
301     /**
302      * test the equals method
303      */

304
305     public void testEquals()
306     {
307         IntList list = new IntList();
308
309         assertEquals(list, list);
310         assertTrue(!list.equals(null));
311         IntList list2 = new IntList(200);
312
313         assertEquals(list, list2);
314         assertEquals(list2, list);
315         assertEquals(list.hashCode(), list2.hashCode());
316         list.add(0);
317         list.add(1);
318         list2.add(1);
319         list2.add(0);
320         assertTrue(!list.equals(list2));
321         list2.removeValue(1);
322         list2.add(1);
323         assertEquals(list, list2);
324         assertEquals(list2, list);
325         list2.add(2);
326         assertTrue(!list.equals(list2));
327         assertTrue(!list2.equals(list));
328     }
329
330     /**
331      * test the get method
332      */

333
334     public void testGet()
335     {
336         IntList list = new IntList();
337
338         for (int j = 0; j < 1000; j++)
339         {
340             list.add(j);
341         }
342         for (int j = 0; j < 1001; j++)
343         {
344             try
345             {
346                 assertEquals(j, list.get(j));
347                 if (j == 1000)
348                 {
349                     fail("should have gotten exception");
350                 }
351             }
352             catch (IndexOutOfBoundsException JavaDoc e)
353             {
354                 if (j != 1000)
355                 {
356                     fail("unexpected IndexOutOfBoundsException");
357                 }
358             }
359         }
360     }
361
362     /**
363      * test the indexOf method
364      */

365
366     public void testIndexOf()
367     {
368         IntList list = new IntList();
369
370         for (int j = 0; j < 1000; j++)
371         {
372             list.add(j / 2);
373         }
374         for (int j = 0; j < 1000; j++)
375         {
376             if (j < 500)
377             {
378                 assertEquals(j * 2, list.indexOf(j));
379             }
380             else
381             {
382                 assertEquals(-1, list.indexOf(j));
383             }
384         }
385     }
386
387     /**
388      * test the isEmpty method
389      */

390
391     public void testIsEmpty()
392     {
393         IntList list1 = new IntList();
394         IntList list2 = new IntList(1000);
395         IntList list3 = new IntList(list1);
396
397         assertTrue(list1.isEmpty());
398         assertTrue(list2.isEmpty());
399         assertTrue(list3.isEmpty());
400         list1.add(1);
401         list2.add(2);
402         list3 = new IntList(list2);
403         assertTrue(!list1.isEmpty());
404         assertTrue(!list2.isEmpty());
405         assertTrue(!list3.isEmpty());
406         list1.clear();
407         list2.remove(0);
408         list3.removeValue(2);
409         assertTrue(list1.isEmpty());
410         assertTrue(list2.isEmpty());
411         assertTrue(list3.isEmpty());
412     }
413
414     /**
415      * test the lastIndexOf method
416      */

417
418     public void testLastIndexOf()
419     {
420         IntList list = new IntList();
421
422         for (int j = 0; j < 1000; j++)
423         {
424             list.add(j / 2);
425         }
426         for (int j = 0; j < 1000; j++)
427         {
428             if (j < 500)
429             {
430                 assertEquals(1 + j * 2, list.lastIndexOf(j));
431             }
432             else
433             {
434                 assertEquals(-1, list.indexOf(j));
435             }
436         }
437     }
438
439     /**
440      * test the remove method
441      */

442
443     public void testRemove()
444     {
445         IntList list = new IntList();
446
447         for (int j = 0; j < 1000; j++)
448         {
449             list.add(j);
450         }
451         for (int j = 0; j < 1000; j++)
452         {
453             assertEquals(j, list.remove(0));
454             assertEquals(999 - j, list.size());
455         }
456         for (int j = 0; j < 1000; j++)
457         {
458             list.add(j);
459         }
460         for (int j = 0; j < 1000; j++)
461         {
462             assertEquals(999 - j, list.remove(999 - j));
463             assertEquals(999 - j, list.size());
464         }
465         try
466         {
467             list.remove(0);
468             fail("should have caught IndexOutOfBoundsException");
469         }
470         catch (IndexOutOfBoundsException JavaDoc e)
471         {
472
473             // as expected
474
}
475     }
476
477     /**
478      * test the removeValue method
479      */

480
481     public void testRemoveValue()
482     {
483         IntList list = new IntList();
484
485         for (int j = 0; j < 1000; j++)
486         {
487             list.add(j / 2);
488         }
489         for (int j = 0; j < 1000; j++)
490         {
491             if (j < 500)
492             {
493                 assertTrue(list.removeValue(j));
494                 assertTrue(list.removeValue(j));
495             }
496             assertTrue(!list.removeValue(j));
497         }
498     }
499
500     /**
501      * test the removeAll method
502      */

503
504     public void testRemoveAll()
505     {
506         IntList list = new IntList();
507
508         for (int j = 0; j < 1000; j++)
509         {
510             list.add(j);
511         }
512         IntList listCopy = new IntList(list);
513         IntList listOdd = new IntList();
514         IntList listEven = new IntList();
515
516         for (int j = 0; j < 1000; j++)
517         {
518             if (j % 2 == 0)
519             {
520                 listEven.add(j);
521             }
522             else
523             {
524                 listOdd.add(j);
525             }
526         }
527         list.removeAll(listEven);
528         assertEquals(list, listOdd);
529         list.removeAll(listOdd);
530         assertTrue(list.isEmpty());
531         listCopy.removeAll(listOdd);
532         assertEquals(listCopy, listEven);
533         listCopy.removeAll(listEven);
534         assertTrue(listCopy.isEmpty());
535     }
536
537     /**
538      * test the retainAll method
539      */

540
541     public void testRetainAll()
542     {
543         IntList list = new IntList();
544
545         for (int j = 0; j < 1000; j++)
546         {
547             list.add(j);
548         }
549         IntList listCopy = new IntList(list);
550         IntList listOdd = new IntList();
551         IntList listEven = new IntList();
552
553         for (int j = 0; j < 1000; j++)
554         {
555             if (j % 2 == 0)
556             {
557                 listEven.add(j);
558             }
559             else
560             {
561                 listOdd.add(j);
562             }
563         }
564         list.retainAll(listOdd);
565         assertEquals(list, listOdd);
566         list.retainAll(listEven);
567         assertTrue(list.isEmpty());
568         listCopy.retainAll(listEven);
569         assertEquals(listCopy, listEven);
570         listCopy.retainAll(listOdd);
571         assertTrue(listCopy.isEmpty());
572     }
573
574     /**
575      * test the set method
576      */

577
578     public void testSet()
579     {
580         IntList list = new IntList();
581
582         for (int j = 0; j < 1000; j++)
583         {
584             list.add(j);
585         }
586         for (int j = 0; j < 1001; j++)
587         {
588             try
589             {
590                 list.set(j, j + 1);
591                 if (j == 1000)
592                 {
593                     fail("Should have gotten exception");
594                 }
595                 assertEquals(j + 1, list.get(j));
596             }
597             catch (IndexOutOfBoundsException JavaDoc e)
598             {
599                 if (j != 1000)
600                 {
601                     fail("premature exception");
602                 }
603             }
604         }
605     }
606
607     /**
608      * test the size method
609      */

610
611     public void testSize()
612     {
613         IntList list = new IntList();
614
615         for (int j = 0; j < 1000; j++)
616         {
617             assertEquals(j, list.size());
618             list.add(j);
619             assertEquals(j + 1, list.size());
620         }
621         for (int j = 0; j < 1000; j++)
622         {
623             assertEquals(1000 - j, list.size());
624             list.removeValue(j);
625             assertEquals(999 - j, list.size());
626         }
627     }
628
629     /**
630      * test the toArray method
631      */

632
633     public void testToArray()
634     {
635         IntList list = new IntList();
636
637         for (int j = 0; j < 1000; j++)
638         {
639             list.add(j);
640         }
641         int[] a1 = list.toArray();
642
643         assertEquals(a1.length, list.size());
644         for (int j = 0; j < 1000; j++)
645         {
646             assertEquals(a1[ j ], list.get(j));
647         }
648         int[] a2 = new int[ list.size() ];
649         int[] a3 = list.toArray(a2);
650
651         assertSame(a2, a3);
652         for (int j = 0; j < 1000; j++)
653         {
654             assertEquals(a2[ j ], list.get(j));
655         }
656         int[] aShort = new int[ list.size() - 1 ];
657         int[] aLong = new int[ list.size() + 1 ];
658         int[] a4 = list.toArray(aShort);
659         int[] a5 = list.toArray(aLong);
660
661         assertTrue(a4 != aShort);
662         assertTrue(a5 != aLong);
663         assertEquals(a4.length, list.size());
664         for (int j = 0; j < 1000; j++)
665         {
666             assertEquals(a3[ j ], list.get(j));
667         }
668         assertEquals(a5.length, list.size());
669         for (int j = 0; j < 1000; j++)
670         {
671             assertEquals(a5[ j ], list.get(j));
672         }
673     }
674
675     /**
676      * main method to run the unit tests
677      *
678      * @param unused_args
679      */

680
681     public static void main(String JavaDoc [] unused_args)
682     {
683         System.out.println("Testing util.IntList functionality");
684         junit.textui.TestRunner.run(TestIntList.class);
685     }
686 }
687
Popular Tags