KickJava   Java API By Example, From Geeks To Geeks.

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


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 ShortList
25  *
26  * @author Marc Johnson
27  */

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

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

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

66
67     public void testAdd()
68     {
69         ShortList list = new ShortList();
70         short[] 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, ( short ) -1);
87         assertEquals(( short ) -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, ( short ) 4);
96         assertEquals(( short ) 4, list.get(5));
97         assertEquals(testArray.length + 2, list.size());
98         for (int j = 0; j < list.size(); j++)
99         {
100             assertEquals(( short ) (j - 1), list.get(j));
101         }
102
103         // add at the end
104
list.add(list.size(), ( short ) 6);
105         assertEquals(testArray.length + 3, list.size());
106         for (int j = 0; j < list.size(); j++)
107         {
108             assertEquals(( short ) (j - 1), list.get(j));
109         }
110
111         // add past end
112
try
113         {
114             list.add(list.size() + 1, ( short ) 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 ShortList(0);
125         for (short j = 0; j < 1000; j++)
126         {
127             list.add(j);
128         }
129         assertEquals(1000, list.size());
130         for (short j = 0; j < 1000; j++)
131         {
132             assertEquals(j, list.get(j));
133         }
134         list = new ShortList(0);
135         for (short j = 0; j < 1000; j++)
136         {
137             list.add(0, j);
138         }
139         assertEquals(1000, list.size());
140         for (short 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         ShortList list = new ShortList();
153
154         for (short j = 0; j < 5; j++)
155         {
156             list.add(j);
157         }
158         ShortList list2 = new ShortList(0);
159
160         list2.addAll(list);
161         list2.addAll(list);
162         assertEquals(2 * list.size(), list2.size());
163         for (short j = 0; j < 5; j++)
164         {
165             assertEquals(list2.get(j), j);
166             assertEquals(list2.get(j + list.size()), j);
167         }
168         ShortList empty = new ShortList();
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         ShortList list = new ShortList();
232
233         for (short 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 (short j = 0; j < 500; j++)
241         {
242             list.add(( short ) (j + 1));
243         }
244         assertEquals(500, list.size());
245         for (short 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         ShortList list = new ShortList();
258
259         for (short j = 0; j < 1000; j += 2)
260         {
261             list.add(j);
262         }
263         for (short 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         ShortList list = new ShortList();
283
284         assertTrue(list.containsAll(list));
285         for (short j = 0; j < 10; j++)
286         {
287             list.add(j);
288         }
289         ShortList list2 = new ShortList(list);
290
291         assertTrue(list2.containsAll(list));
292         assertTrue(list.containsAll(list2));
293         list2.add(( short ) 10);
294         assertTrue(list2.containsAll(list));
295         assertTrue(!list.containsAll(list2));
296         list.add(( short ) 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         ShortList list = new ShortList();
308
309         assertEquals(list, list);
310         assertTrue(!list.equals(null));
311         ShortList list2 = new ShortList(200);
312
313         assertEquals(list, list2);
314         assertEquals(list2, list);
315         assertEquals(list.hashCode(), list2.hashCode());
316         list.add(( short ) 0);
317         list.add(( short ) 1);
318         list2.add(( short ) 1);
319         list2.add(( short ) 0);
320         assertTrue(!list.equals(list2));
321         list2.removeValue(( short ) 1);
322         list2.add(( short ) 1);
323         assertEquals(list, list2);
324         assertEquals(list2, list);
325         list2.add(( short ) 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         ShortList list = new ShortList();
337
338         for (short j = 0; j < 1000; j++)
339         {
340             list.add(j);
341         }
342         for (short 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         ShortList list = new ShortList();
369
370         for (short j = 0; j < 1000; j++)
371         {
372             list.add(( short ) (j / 2));
373         }
374         for (short 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         ShortList list1 = new ShortList();
394         ShortList list2 = new ShortList(1000);
395         ShortList list3 = new ShortList(list1);
396
397         assertTrue(list1.isEmpty());
398         assertTrue(list2.isEmpty());
399         assertTrue(list3.isEmpty());
400         list1.add(( short ) 1);
401         list2.add(( short ) 2);
402         list3 = new ShortList(list2);
403         assertTrue(!list1.isEmpty());
404         assertTrue(!list2.isEmpty());
405         assertTrue(!list3.isEmpty());
406         list1.clear();
407         list2.remove(0);
408         list3.removeValue(( short ) 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         ShortList list = new ShortList();
421
422         for (short j = 0; j < 1000; j++)
423         {
424             list.add(( short ) (j / 2));
425         }
426         for (short 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         ShortList list = new ShortList();
446
447         for (short j = 0; j < 1000; j++)
448         {
449             list.add(j);
450         }
451         for (short j = 0; j < 1000; j++)
452         {
453             assertEquals(j, list.remove(0));
454             assertEquals(( short ) (999 - j), list.size());
455         }
456         for (short j = 0; j < 1000; j++)
457         {
458             list.add(j);
459         }
460         for (short j = 0; j < 1000; j++)
461         {
462             assertEquals(( short ) (999 - j),
463                          list.remove(( short ) (999 - j)));
464             assertEquals(999 - j, list.size());
465         }
466         try
467         {
468             list.remove(0);
469             fail("should have caught IndexOutOfBoundsException");
470         }
471         catch (IndexOutOfBoundsException JavaDoc e)
472         {
473
474             // as expected
475
}
476     }
477
478     /**
479      * test the removeValue method
480      */

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

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

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

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

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

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

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