KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > tree > InsnListUnitTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.tree;
31
32 import java.util.ListIterator JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * InsnList unit tests.
38  *
39  * @author Eric Bruneton
40  */

41 public class InsnListUnitTest extends TestCase {
42
43     InsnList l1;
44
45     InsnList l2;
46
47     InsnNode in1;
48
49     InsnNode in2;
50
51     protected void setUp() throws Exception JavaDoc {
52         super.setUp();
53         InsnList.check = true;
54         l1 = new InsnList();
55         l2 = new InsnList();
56         in1 = new InsnNode(0);
57         in2 = new InsnNode(0);
58         l2.add(in1);
59         l2.add(in2);
60     }
61
62     protected void assertEquals(
63         final AbstractInsnNode[] expected,
64         final AbstractInsnNode[] value)
65     {
66         assertEquals(expected.length, value.length);
67         for (int i = 0; i < value.length; ++i) {
68             assertEquals(expected[i], value[i]);
69         }
70     }
71
72     public void testSize() {
73         assertEquals(0, l1.size());
74     }
75
76     public void testGetFirst() {
77         assertEquals(null, l1.getFirst());
78     }
79
80     public void testGetLast() {
81         assertEquals(null, l1.getLast());
82     }
83
84     public void testInvalidGet() {
85         try {
86             l1.get(0);
87             fail();
88         } catch (IndexOutOfBoundsException JavaDoc e) {
89         }
90     }
91
92     public void testContains() {
93         assertEquals(false, l1.contains(new InsnNode(0)));
94     }
95
96     public void testIterator() {
97         InsnNode insn = new InsnNode(0);
98         
99         // iteration
100
ListIterator JavaDoc it = l2.iterator();
101         assertTrue(it.hasNext());
102         assertEquals(in1, it.next());
103         assertTrue(it.hasNext());
104         assertEquals(in2, it.next());
105         assertFalse(it.hasNext());
106         assertTrue(it.hasPrevious());
107         assertEquals(in2, it.previous());
108         assertTrue(it.hasPrevious());
109         assertEquals(in1, it.previous());
110         assertFalse(it.hasPrevious());
111
112         l2.add(insn);
113
114         // remove()
115
it = l2.iterator();
116         assertTrue(it.hasNext());
117         assertEquals(in1, it.next());
118         assertTrue(it.hasNext());
119         assertEquals(in2, it.next());
120         assertTrue(it.hasNext());
121         it.remove(); // remove in2
122
assertTrue(it.hasNext());
123         assertEquals(insn, it.next());
124         assertFalse(it.hasNext());
125         assertTrue(it.hasPrevious());
126         
127         it = l2.iterator();
128         assertTrue(it.hasNext());
129         assertEquals(in1, it.next());
130         assertTrue(it.hasNext());
131         assertEquals(insn, it.next());
132         assertFalse(it.hasNext());
133         
134         l2.remove(insn);
135         l2.insert(in1, in2);
136         
137         // add() then next()
138
it = l2.iterator();
139         assertTrue(it.hasNext());
140         assertEquals(in1, it.next());
141         it.add(insn);
142         assertEquals(in2, it.next());
143         
144         l2.remove(insn);
145         
146         // add() then previous()
147
it = l2.iterator();
148         assertTrue(it.hasNext());
149         assertEquals(in1, it.next());
150         it.add(insn);
151         assertEquals(insn, it.previous());
152         assertEquals(insn, it.next());
153         assertTrue(it.hasNext());
154         assertEquals(in2, it.next());
155         assertFalse(it.hasNext());
156         
157         l2.remove(insn);
158         
159         // set() then previous()
160
it = l2.iterator();
161         assertTrue(it.hasNext());
162         assertEquals(in1, it.next());
163         it.set(insn);
164         assertEquals(insn, it.previous());
165         assertEquals(insn, it.next());
166         assertTrue(it.hasNext());
167         
168         l2.remove(insn);
169         l2.insertBefore(in2, in1);
170         
171         // add() then next()
172
it = l2.iterator();
173         assertTrue(it.hasNext());
174         assertEquals(in1, it.next());
175         it.set(insn);
176         assertEquals(in2, it.next());
177     }
178
179     public void testIterator2() {
180         ListIterator JavaDoc it = l2.iterator(l2.size());
181
182         assertFalse(it.hasNext());
183         assertTrue(it.hasPrevious());
184         assertEquals(1, it.previousIndex());
185         assertEquals(in2, it.previous());
186         assertTrue(it.hasPrevious());
187         assertEquals(0, it.previousIndex());
188         assertEquals(in1, it.previous());
189         assertFalse(it.hasPrevious());
190
191         assertEquals(-1, it.previousIndex());
192         
193         assertTrue(it.hasNext());
194         assertEquals(0, it.nextIndex());
195         assertEquals(in1, it.next());
196         assertTrue(it.hasNext());
197         assertEquals(1, it.nextIndex());
198         
199         InsnNode insn = new InsnNode(0);
200         it.add(insn);
201
202         assertEquals(2, it.nextIndex());
203         assertEquals(in2, it.next());
204         assertFalse(it.hasNext());
205         assertEquals(3, it.nextIndex());
206     }
207     
208     public void testInvalidIndexOf() {
209         try {
210             l1.indexOf(new InsnNode(0));
211             fail();
212         } catch (IllegalArgumentException JavaDoc e) {
213         }
214     }
215
216     public void testToArray() {
217         assertEquals(0, l1.toArray().length);
218     }
219
220     public void testInvalidSet() {
221         try {
222             l1.set(new InsnNode(0), new InsnNode(0));
223             fail();
224         } catch (IllegalArgumentException JavaDoc e) {
225         }
226     }
227
228     public void testSet() {
229         l1.add(new InsnNode(0));
230         AbstractInsnNode insn = new InsnNode(0);
231         l1.set(l1.getFirst(), insn);
232         assertEquals(1, l1.size());
233         assertEquals(insn, l1.getFirst());
234         
235         l1.remove(insn);
236         l1.add(new InsnNode(0));
237         
238         l1.set(l1.get(0), insn);
239         assertEquals(1, l1.size());
240         assertEquals(insn, l1.getFirst());
241     }
242
243     public void testInvalidAdd() {
244         try {
245             l1.add(in1);
246             fail();
247         } catch (IllegalArgumentException JavaDoc e) {
248         }
249     }
250
251     public void testAddEmpty() {
252         InsnNode insn = new InsnNode(0);
253         l1.add(insn);
254         assertEquals(1, l1.size());
255         assertEquals(insn, l1.getFirst());
256         assertEquals(insn, l1.getLast());
257         assertEquals(insn, l1.get(0));
258         assertEquals(true, l1.contains(insn));
259         assertEquals(0, l1.indexOf(insn));
260         assertEquals(new AbstractInsnNode[] { insn }, l1.toArray());
261         assertEquals(null, insn.getPrevious());
262         assertEquals(null, insn.getNext());
263     }
264
265     public void testAddNonEmpty() {
266         InsnNode insn = new InsnNode(0);
267         l1.add(new InsnNode(0));
268         l1.add(insn);
269         assertEquals(2, l1.size());
270         assertEquals(insn, l1.getLast());
271         assertEquals(insn, l1.get(1));
272         assertEquals(true, l1.contains(insn));
273         assertEquals(1, l1.indexOf(insn));
274     }
275
276     public void testAddEmptyList() {
277         l1.add(new InsnList());
278         assertEquals(0, l1.size());
279         assertEquals(null, l1.getFirst());
280         assertEquals(null, l1.getLast());
281         assertEquals(new AbstractInsnNode[0], l1.toArray());
282     }
283
284     public void testInvalidAddAll() {
285         try {
286             l1.add(l1);
287             fail();
288         } catch (IllegalArgumentException JavaDoc e) {
289         }
290     }
291
292     public void testAddAllEmpty() {
293         l1.add(l2);
294         assertEquals(2, l1.size());
295         assertEquals(in1, l1.getFirst());
296         assertEquals(in2, l1.getLast());
297         assertEquals(in1, l1.get(0));
298         assertEquals(true, l1.contains(in1));
299         assertEquals(true, l1.contains(in2));
300         assertEquals(0, l1.indexOf(in1));
301         assertEquals(1, l1.indexOf(in2));
302         assertEquals(new AbstractInsnNode[] { in1, in2 }, l1.toArray());
303     }
304
305     public void testAddAllNonEmpty() {
306         InsnNode insn = new InsnNode(0);
307         l1.add(insn);
308         l1.add(l2);
309         assertEquals(3, l1.size());
310         assertEquals(insn, l1.getFirst());
311         assertEquals(in2, l1.getLast());
312         assertEquals(insn, l1.get(0));
313         assertEquals(true, l1.contains(insn));
314         assertEquals(true, l1.contains(in1));
315         assertEquals(true, l1.contains(in2));
316         assertEquals(0, l1.indexOf(insn));
317         assertEquals(1, l1.indexOf(in1));
318         assertEquals(2, l1.indexOf(in2));
319         assertEquals(new AbstractInsnNode[] { insn, in1, in2 }, l1.toArray());
320     }
321
322     public void testInvalidInsert() {
323         try {
324             l1.insert(in1);
325             fail();
326         } catch (IllegalArgumentException JavaDoc e) {
327         }
328     }
329
330     public void testInsertEmpty() {
331         InsnNode insn = new InsnNode(0);
332         l1.insert(insn);
333         assertEquals(1, l1.size());
334         assertEquals(insn, l1.getFirst());
335         assertEquals(insn, l1.getLast());
336         assertEquals(insn, l1.get(0));
337         assertEquals(true, l1.contains(insn));
338         assertEquals(0, l1.indexOf(insn));
339         assertEquals(new AbstractInsnNode[] { insn }, l1.toArray());
340     }
341
342     public void testInsertNonEmpty() {
343         InsnNode insn = new InsnNode(0);
344         l1.add(new InsnNode(0));
345         l1.insert(insn);
346         assertEquals(2, l1.size());
347         assertEquals(insn, l1.getFirst());
348         assertEquals(insn, l1.get(0));
349         assertEquals(true, l1.contains(insn));
350         assertEquals(0, l1.indexOf(insn));
351     }
352
353     public void testInvalidInsertAll() {
354         try {
355             l1.insert(l1);
356             fail();
357         } catch (IllegalArgumentException JavaDoc e) {
358         }
359     }
360
361     public void testInsertAllEmptyList() {
362         l1.insert(new InsnList());
363         assertEquals(0, l1.size());
364         assertEquals(null, l1.getFirst());
365         assertEquals(null, l1.getLast());
366         assertEquals(new AbstractInsnNode[0], l1.toArray());
367     }
368
369     public void testInsertAllEmpty() {
370         l1.insert(l2);
371         assertEquals(2, l1.size(), 2);
372         assertEquals(in1, l1.getFirst());
373         assertEquals(in2, l1.getLast());
374         assertEquals(in1, l1.get(0));
375         assertEquals(true, l1.contains(in1));
376         assertEquals(true, l1.contains(in2));
377         assertEquals(0, l1.indexOf(in1));
378         assertEquals(1, l1.indexOf(in2));
379         assertEquals(new AbstractInsnNode[] { in1, in2 }, l1.toArray());
380     }
381
382     public void testInsertAllNonEmpty() {
383         InsnNode insn = new InsnNode(0);
384         l1.add(insn);
385         l1.insert(l2);
386         assertEquals(3, l1.size());
387         assertEquals(in1, l1.getFirst());
388         assertEquals(insn, l1.getLast());
389         assertEquals(in1, l1.get(0));
390         assertEquals(true, l1.contains(insn));
391         assertEquals(true, l1.contains(in1));
392         assertEquals(true, l1.contains(in2));
393         assertEquals(0, l1.indexOf(in1));
394         assertEquals(1, l1.indexOf(in2));
395         assertEquals(2, l1.indexOf(insn));
396         assertEquals(new AbstractInsnNode[] { in1, in2, insn }, l1.toArray());
397     }
398
399     public void testInvalidInsert2() {
400         try {
401             l1.insert(new InsnNode(0), new InsnNode(0));
402             fail();
403         } catch (IllegalArgumentException JavaDoc e) {
404         }
405     }
406
407     public void testInsert2NotLast() {
408         InsnNode insn = new InsnNode(0);
409         l2.insert(in1, insn);
410         assertEquals(3, l2.size());
411         assertEquals(in1, l2.getFirst());
412         assertEquals(in2, l2.getLast());
413         assertEquals(in1, l2.get(0));
414         assertEquals(true, l2.contains(insn));
415         assertEquals(1, l2.indexOf(insn));
416         assertEquals(new AbstractInsnNode[] { in1, insn, in2 }, l2.toArray());
417     }
418
419     public void testInsert2Last() {
420         InsnNode insn = new InsnNode(0);
421         l2.insert(in2, insn);
422         assertEquals(3, l2.size());
423         assertEquals(in1, l2.getFirst());
424         assertEquals(insn, l2.getLast());
425         assertEquals(in1, l2.get(0));
426         assertEquals(true, l2.contains(insn));
427         assertEquals(2, l2.indexOf(insn));
428         assertEquals(new AbstractInsnNode[] { in1, in2, insn }, l2.toArray());
429     }
430
431     public void testInsertBefore() {
432         InsnNode insn = new InsnNode(0);
433         l2.insertBefore(in2, insn);
434         assertEquals(3, l2.size());
435         assertEquals(in1, l2.getFirst());
436         assertEquals(in2, l2.getLast());
437         assertEquals(insn, l2.get(1));
438         assertEquals(true, l2.contains(insn));
439         assertEquals(1, l2.indexOf(insn));
440         assertEquals(new AbstractInsnNode[] { in1, insn, in2 }, l2.toArray());
441     }
442     
443     public void testInsertBeforeFirst() {
444         InsnNode insn = new InsnNode(0);
445         l2.insertBefore(in1, insn);
446         assertEquals(3, l2.size());
447         assertEquals(insn, l2.getFirst());
448         assertEquals(in2, l2.getLast());
449         assertEquals(insn, l2.get(0));
450         assertEquals(true, l2.contains(insn));
451         assertEquals(0, l2.indexOf(insn));
452         assertEquals(new AbstractInsnNode[] { insn, in1, in2 }, l2.toArray());
453     }
454     
455     public void testInvalidInsertBefore() {
456         try {
457             l1.insertBefore(new InsnNode(0), new InsnNode(0));
458             fail();
459         } catch (IllegalArgumentException JavaDoc e) {
460         }
461     }
462
463     public void testInvalidInsertAll2() {
464         try {
465             l1.insert(new InsnNode(0), new InsnList());
466             fail();
467         } catch (IllegalArgumentException JavaDoc e) {
468         }
469     }
470
471     public void testInsertAll2EmptyList() {
472         InsnNode insn = new InsnNode(0);
473         l1.add(insn);
474         l1.insert(insn, new InsnList());
475         assertEquals(1, l1.size());
476         assertEquals(insn, l1.getFirst());
477         assertEquals(insn, l1.getLast());
478         assertEquals(new AbstractInsnNode[] { insn }, l1.toArray());
479     }
480
481     public void testInsertAll2NotLast() {
482         InsnNode insn = new InsnNode(0);
483         l1.add(insn);
484         l1.add(new InsnNode(0));
485         l1.insert(insn, l2);
486         assertEquals(4, l1.size());
487         assertEquals(insn, l1.getFirst());
488         assertEquals(insn, l1.get(0));
489         assertEquals(true, l1.contains(insn));
490         assertEquals(true, l1.contains(in1));
491         assertEquals(true, l1.contains(in2));
492         assertEquals(0, l1.indexOf(insn));
493         assertEquals(1, l1.indexOf(in1));
494         assertEquals(2, l1.indexOf(in2));
495     }
496
497     public void testInsertAll2Last() {
498         InsnNode insn = new InsnNode(0);
499         l1.add(insn);
500         l1.insert(insn, l2);
501         assertEquals(3, l1.size());
502         assertEquals(insn, l1.getFirst());
503         assertEquals(in2, l1.getLast());
504         assertEquals(insn, l1.get(0));
505         assertEquals(true, l1.contains(insn));
506         assertEquals(true, l1.contains(in1));
507         assertEquals(true, l1.contains(in2));
508         assertEquals(0, l1.indexOf(insn));
509         assertEquals(1, l1.indexOf(in1));
510         assertEquals(2, l1.indexOf(in2));
511         assertEquals(new AbstractInsnNode[] { insn, in1, in2 }, l1.toArray());
512     }
513
514     public void testInvalidInsertBeforeAll() {
515         try {
516             l1.insertBefore(new InsnNode(0), new InsnList());
517             fail();
518         } catch (IllegalArgumentException JavaDoc e) {
519         }
520     }
521     
522     public void testInsertBeforeAll2EmptyList() {
523         InsnNode insn = new InsnNode(0);
524         l1.add(insn);
525         l1.insertBefore(insn, new InsnList());
526         assertEquals(1, l1.size());
527         assertEquals(insn, l1.getFirst());
528         assertEquals(insn, l1.getLast());
529         assertEquals(new AbstractInsnNode[] { insn }, l1.toArray());
530     }
531     
532     public void testInsertBeforeAll2NotLast() {
533         InsnNode insn = new InsnNode(0);
534         l1.add(new InsnNode(0));
535         l1.add(insn);
536         l1.insertBefore(insn, l2);
537         assertEquals(4, l1.size());
538         assertEquals(in1, l1.get(1));
539         assertEquals(in2, l1.get(2));
540         assertEquals(true, l1.contains(insn));
541         assertEquals(true, l1.contains(in1));
542         assertEquals(true, l1.contains(in2));
543         assertEquals(3, l1.indexOf(insn));
544         assertEquals(1, l1.indexOf(in1));
545         assertEquals(2, l1.indexOf(in2));
546     }
547     
548     public void testInsertBeforeAll2First() {
549         InsnNode insn = new InsnNode(0);
550         l1.insert(insn);
551         l1.insertBefore(insn, l2);
552         assertEquals(3, l1.size());
553         assertEquals(in1, l1.getFirst());
554         assertEquals(insn, l1.getLast());
555         assertEquals(in1, l1.get(0));
556         assertEquals(true, l1.contains(insn));
557         assertEquals(true, l1.contains(in1));
558         assertEquals(true, l1.contains(in2));
559         assertEquals(2, l1.indexOf(insn));
560         assertEquals(0, l1.indexOf(in1));
561         assertEquals(1, l1.indexOf(in2));
562         assertEquals(new AbstractInsnNode[] { in1, in2, insn }, l1.toArray());
563     }
564     
565     public void testInvalidRemove() {
566         try {
567             l1.remove(new InsnNode(0));
568         } catch (IllegalArgumentException JavaDoc e) {
569         }
570     }
571
572     public void testRemoveSingle() {
573         InsnNode insn = new InsnNode(0);
574         l1.add(insn);
575         l1.remove(insn);
576         assertEquals(0, l1.size());
577         assertEquals(null, l1.getFirst());
578         assertEquals(null, l1.getLast());
579         assertEquals(false, l1.contains(insn));
580         assertEquals(new AbstractInsnNode[0], l1.toArray());
581         assertEquals(null, insn.getPrevious());
582         assertEquals(null, insn.getNext());
583     }
584
585     public void testRemoveFirst() {
586         InsnNode insn = new InsnNode(0);
587         l1.add(insn);
588         l1.add(new InsnNode(0));
589         l1.remove(insn);
590         assertEquals(false, l1.contains(insn));
591         assertEquals(null, insn.getPrevious());
592         assertEquals(null, insn.getNext());
593     }
594
595     public void testRemoveMiddle() {
596         InsnNode insn = new InsnNode(0);
597         l1.add(new InsnNode(0));
598         l1.add(insn);
599         l1.add(new InsnNode(0));
600         l1.remove(insn);
601         assertEquals(false, l1.contains(insn));
602         assertEquals(null, insn.getPrevious());
603         assertEquals(null, insn.getNext());
604     }
605
606     public void testRemoveLast() {
607         InsnNode insn = new InsnNode(0);
608         l1.add(new InsnNode(0));
609         l1.add(insn);
610         l1.remove(insn);
611         assertEquals(false, l1.contains(insn));
612         assertEquals(null, insn.getPrevious());
613         assertEquals(null, insn.getNext());
614     }
615
616     public void testClear() {
617         InsnNode insn = new InsnNode(0);
618         l1.add(new InsnNode(0));
619         l1.add(insn);
620         l1.add(new InsnNode(0));
621         l1.clear();
622         assertEquals(0, l1.size());
623         assertEquals(null, l1.getFirst());
624         assertEquals(null, l1.getLast());
625         assertEquals(false, l1.contains(insn));
626         assertEquals(new AbstractInsnNode[0], l1.toArray());
627         assertEquals(null, insn.getPrevious());
628         assertEquals(null, insn.getNext());
629     }
630 }
631
Popular Tags