OSDN Git Service

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