OSDN Git Service

cflib は plugins プロジェクトから,Stigmata直下のプロジェクトに移行したため,このリポジトリからは削除した.
[stigmata/stigmata-plugins.git] / osb / src / test / java / jp / sourceforge / stigmata / birthmarks / osb / OperandStackBehaviorsBirthmarkElementTest.java
1 package jp.sourceforge.stigmata.birthmarks.osb;\r
2 \r
3 import java.util.Iterator;\r
4 \r
5 import jp.sourceforge.stigmata.cflib.Opcode;\r
6 import jp.sourceforge.stigmata.cflib.OpcodeManager;\r
7 \r
8 import org.junit.Assert;\r
9 import org.junit.Before;\r
10 import org.junit.Test;\r
11 \r
12 public class OperandStackBehaviorsBirthmarkElementTest{\r
13     private OperandStackBehaviorsBirthmarkElement element;\r
14 \r
15     @Before\r
16     public void setUp(){\r
17         OpcodeManager manager = OpcodeManager.getInstance();\r
18         Opcode[] opcodes = new Opcode[4];\r
19         opcodes[0] = manager.getOpcode(27); // iload_1\r
20         opcodes[1] = manager.getOpcode(28); // iload_2\r
21         opcodes[2] = manager.getOpcode(96); // iadd\r
22         opcodes[3] = manager.getOpcode(62); // istore_3\r
23 \r
24         element = new OperandStackBehaviorsBirthmarkElement(opcodes);\r
25     }\r
26 \r
27     @Test\r
28     public void testSimilarity(){\r
29         OpcodeManager manager = OpcodeManager.getInstance();\r
30         OperandStackBehaviorsBirthmarkElement element2 = new OperandStackBehaviorsBirthmarkElement(\r
31             new Opcode[] {\r
32                 manager.getOpcode(27),  // iload_1\r
33                 manager.getOpcode(28),  // iload_2\r
34                 manager.getOpcode(104), // imul\r
35                 manager.getOpcode(29),  // iload_3\r
36                 manager.getOpcode(100), // isub\r
37                 manager.getOpcode(54),  // istore\r
38             }\r
39         );\r
40 \r
41         Assert.assertEquals(3d/7d, element.getSimilarity(element2), 1E-5);\r
42         Assert.assertEquals(3d/7d, element2.getSimilarity(element), 1E-5);\r
43     }\r
44 \r
45     @Test\r
46     public void testBasic(){\r
47         Assert.assertEquals(4, element.getLength());\r
48         Assert.assertEquals("27, 28, 96, 62", element.toString());\r
49     }\r
50 \r
51     @Test\r
52     public void testCategoryCheck(){\r
53         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(0));\r
54         Assert.assertEquals(Opcode.Category.LOAD,  element.getCategory(1));\r
55         Assert.assertEquals(Opcode.Category.ADD,   element.getCategory(2));\r
56         Assert.assertEquals(Opcode.Category.STORE, element.getCategory(3));\r
57     }\r
58 \r
59     @Test\r
60     public void testOpcodeCheck(){\r
61         OpcodeManager manager = OpcodeManager.getInstance();\r
62         Assert.assertEquals(manager.getOpcode(27), element.getOpcode(0));\r
63         Assert.assertEquals(manager.getOpcode(28), element.getOpcode(1));\r
64         Assert.assertEquals(manager.getOpcode(96), element.getOpcode(2));\r
65         Assert.assertEquals(manager.getOpcode(62), element.getOpcode(3));\r
66     }\r
67 \r
68     @Test\r
69     public void testOpcodeCheck2(){\r
70         OpcodeManager manager = OpcodeManager.getInstance();\r
71         Iterator<Opcode> iterator = element.iterator();\r
72         Assert.assertTrue(iterator.hasNext());\r
73         Assert.assertEquals(manager.getOpcode(27), iterator.next());\r
74         Assert.assertTrue(iterator.hasNext());\r
75         Assert.assertEquals(manager.getOpcode(28), iterator.next());\r
76         Assert.assertTrue(iterator.hasNext());\r
77         Assert.assertEquals(manager.getOpcode(96), iterator.next());\r
78         Assert.assertTrue(iterator.hasNext());\r
79         Assert.assertEquals(manager.getOpcode(62), iterator.next());\r
80         Assert.assertFalse(iterator.hasNext());\r
81     }\r
82 }\r