OSDN Git Service

update
[stigmata/stigmata-plugins.git] / wsp / src / test / java / jp / sourceforge / stigmata / birthmarks / wsp / StackPatternBasedBirthmarkElementTest.java
1 package jp.sourceforge.stigmata.birthmarks.wsp;
2
3 /*
4  * $Id$
5  */
6
7 import jp.sourceforge.stigmata.birthmarks.Opcode;
8
9 import org.junit.Assert;
10 import org.junit.Before;
11 import org.junit.Test;
12
13 /**
14  *
15  * @author Haruaki Tamada
16  * @version $Revision$
17  */
18 public class StackPatternBasedBirthmarkElementTest{
19     private StackPatternBasedBirthmarkElement element;
20
21     @Before
22     public void setup(){
23         CurrentDepth[] depthList = new CurrentDepth[7];
24
25         depthList[0] = new CurrentDepth(1, new WSPOpcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
26         depthList[1] = new CurrentDepth(2, new WSPOpcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
27         depthList[2] = new CurrentDepth(3, new WSPOpcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
28         depthList[3] = new CurrentDepth(2, new WSPOpcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
29         depthList[4] = new CurrentDepth(2, new WSPOpcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
30         depthList[5] = new CurrentDepth(1, new WSPOpcode(104, "imul",         0, -1, Opcode.Category.NORMAL, 6));
31         depthList[6] = new CurrentDepth(0, new WSPOpcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
32
33         element = new StackPatternBasedBirthmarkElement(depthList);
34     }
35
36     @Test
37     public void testBasic() throws Exception{
38         Assert.assertEquals(7, element.getLength());
39         Assert.assertEquals(28, element.getWeight());
40
41         Assert.assertEquals(1, element.getDepth(0).getDepth());
42         Assert.assertEquals(2, element.getDepth(1).getDepth());
43         Assert.assertEquals(3, element.getDepth(2).getDepth());
44         Assert.assertEquals(2, element.getDepth(3).getDepth());
45         Assert.assertEquals(2, element.getDepth(4).getDepth());
46         Assert.assertEquals(1, element.getDepth(5).getDepth());
47         Assert.assertEquals(0, element.getDepth(6).getDepth());
48     }
49
50     @Test
51     public void testBuildFromString() throws Exception{
52         StackPatternBasedBirthmarkElement element2 = new StackPatternBasedBirthmarkElement(element.toString());
53
54         Assert.assertEquals(7, element2.getLength());
55         Assert.assertEquals(28, element2.getWeight());
56
57         Assert.assertEquals(1, element2.getDepth(0).getDepth());
58         Assert.assertEquals(2, element2.getDepth(1).getDepth());
59         Assert.assertEquals(3, element2.getDepth(2).getDepth());
60         Assert.assertEquals(2, element2.getDepth(3).getDepth());
61         Assert.assertEquals(2, element2.getDepth(4).getDepth());
62         Assert.assertEquals(1, element2.getDepth(5).getDepth());
63         Assert.assertEquals(0, element2.getDepth(6).getDepth());
64     }
65
66     @Test
67     public void testCalculateWeightedCommonSubsequence(){
68         CurrentDepth[] depthList = new CurrentDepth[10];
69         depthList[0] = new CurrentDepth(1, new WSPOpcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
70         depthList[1] = new CurrentDepth(2, new WSPOpcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
71         depthList[2] = new CurrentDepth(1, new WSPOpcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
72         depthList[3] = new CurrentDepth(1, new WSPOpcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
73         depthList[4] = new CurrentDepth(2, new WSPOpcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
74         depthList[5] = new CurrentDepth(3, new WSPOpcode(  5, "iconst_2",     0,  1, Opcode.Category.NORMAL, 1));
75         depthList[6] = new CurrentDepth(2, new WSPOpcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
76         depthList[7] = new CurrentDepth(2, new WSPOpcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
77         depthList[8] = new CurrentDepth(1, new WSPOpcode( 96, "iadd",         0, -1, Opcode.Category.NORMAL, 3));
78         depthList[9] = new CurrentDepth(0, new WSPOpcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
79         StackPatternBasedBirthmarkElement pattern2 = new StackPatternBasedBirthmarkElement(depthList);
80
81         Assert.assertEquals(21, element.getWeight(pattern2));
82         Assert.assertEquals(21, pattern2.getWeight(element));
83     }
84 }