OSDN Git Service

add calculator, and test case
authortama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Thu, 4 Dec 2008 08:53:47 +0000 (08:53 +0000)
committertama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Thu, 4 Dec 2008 08:53:47 +0000 (08:53 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/plugins/trunk@337 acee48c3-7b26-0410-bdac-b3d0e5314bbc

wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WeightCalculator.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkElementTest.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternTest.java [deleted file]

diff --git a/wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WeightCalculator.java b/wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WeightCalculator.java
new file mode 100644 (file)
index 0000000..6af3eb1
--- /dev/null
@@ -0,0 +1,44 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+public class WeightCalculator{
+    
+    public int calculateWeight(int[][] wcs){
+        int weight = 0;
+        boolean[][] availableFlag = new boolean[wcs.length][wcs[0].length];
+        for(int i = 0; i < wcs.length; i++){
+            for(int j = 0; j < wcs[i].length; j++){
+                availableFlag[i][j] = true;
+            }
+        }
+
+        int length = wcs.length;
+        if(length < wcs[0].length){
+            length = wcs[0].length;
+        }
+        for(int k = 0; k < length; k++){
+            int max = Integer.MIN_VALUE;
+            int column = -1;
+            int row = -1;
+            for(int i = 0; i < wcs.length; i++){
+                for(int j = 0; j < wcs[i].length; j++){
+                    if(max < wcs[i][j] && availableFlag[i][j]){
+                        max = wcs[i][j];
+                        row = i;
+                        column = j;
+                    }
+                }
+            }
+            if(column >= 0 && row >= 0){
+                for(int i = 0; i < wcs.length; i++){
+                    availableFlag[i][column] = false;
+                }
+                for(int j = 0; j < wcs[0].length; j++){
+                    availableFlag[row][j] = false;
+                }
+                weight += wcs[column][row];
+            }
+        }
+
+        return weight;
+    }
+}
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkElementTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkElementTest.java
new file mode 100644 (file)
index 0000000..32b814b
--- /dev/null
@@ -0,0 +1,82 @@
+package jp.sourceforge.stigmata.birthmarks.wsp;
+
+/*
+ * $Id$
+ */
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * 
+ * @author Haruaki Tamada
+ * @version $Revision$
+ */
+public class StackPatternBasedBirthmarkElementTest{
+    private StackPatternBasedBirthmarkElement element;
+
+    @Before
+    public void setup(){
+        CurrentDepth[] depthList = new CurrentDepth[7];
+
+        depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        depthList[1] = new CurrentDepth(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        depthList[2] = new CurrentDepth(3, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
+        depthList[3] = new CurrentDepth(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
+        depthList[4] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
+        depthList[5] = new CurrentDepth(1, new Opcode(104, "imul",         0, -1, Opcode.Category.NORMAL, 6));
+        depthList[6] = new CurrentDepth(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
+
+        element = new StackPatternBasedBirthmarkElement(depthList);
+    }
+
+    @Test
+    public void testBasic() throws Exception{
+        Assert.assertEquals(7, element.getLength());
+        Assert.assertEquals(28, element.getWeight());
+
+        Assert.assertEquals(1, element.getDepth(0).getDepth());
+        Assert.assertEquals(2, element.getDepth(1).getDepth());
+        Assert.assertEquals(3, element.getDepth(2).getDepth());
+        Assert.assertEquals(2, element.getDepth(3).getDepth());
+        Assert.assertEquals(2, element.getDepth(4).getDepth());
+        Assert.assertEquals(1, element.getDepth(5).getDepth());
+        Assert.assertEquals(0, element.getDepth(6).getDepth());
+    }
+
+    @Test
+    public void testBuildFromString() throws Exception{
+        StackPatternBasedBirthmarkElement element2 = new StackPatternBasedBirthmarkElement(element.toString());
+
+        Assert.assertEquals(7, element2.getLength());
+        Assert.assertEquals(28, element2.getWeight());
+
+        Assert.assertEquals(1, element2.getDepth(0).getDepth());
+        Assert.assertEquals(2, element2.getDepth(1).getDepth());
+        Assert.assertEquals(3, element2.getDepth(2).getDepth());
+        Assert.assertEquals(2, element2.getDepth(3).getDepth());
+        Assert.assertEquals(2, element2.getDepth(4).getDepth());
+        Assert.assertEquals(1, element2.getDepth(5).getDepth());
+        Assert.assertEquals(0, element2.getDepth(6).getDepth());
+    }
+
+    @Test
+    public void testCalculateWeightedCommonSubsequence(){
+        CurrentDepth[] depthList = new CurrentDepth[10];
+        depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        depthList[1] = new CurrentDepth(2, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
+        depthList[2] = new CurrentDepth(1, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
+        depthList[3] = new CurrentDepth(1, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
+        depthList[4] = new CurrentDepth(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
+        depthList[5] = new CurrentDepth(3, new Opcode(  5, "iconst_2",     0,  1, Opcode.Category.NORMAL, 1));
+        depthList[6] = new CurrentDepth(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
+        depthList[7] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
+        depthList[8] = new CurrentDepth(1, new Opcode( 96, "iadd",         0, -1, Opcode.Category.NORMAL, 3));
+        depthList[9] = new CurrentDepth(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
+        StackPatternBasedBirthmarkElement pattern2 = new StackPatternBasedBirthmarkElement(depthList);
+
+        Assert.assertEquals(21, element.getWeight(pattern2));
+        Assert.assertEquals(21, pattern2.getWeight(element));
+    }
+}
diff --git a/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternTest.java b/wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternTest.java
deleted file mode 100644 (file)
index 3eb6146..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-package jp.sourceforge.stigmata.birthmarks.wsp;
-
-/*
- * $Id$
- */
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- * 
- * @author Haruaki Tamada
- * @version $Revision$
- */
-public class StackPatternTest{
-    private StackPattern pattern;
-
-    @Before
-    public void setup(){
-        pattern = new StackPattern();
-
-        pattern.update(new CurrentDepth(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7)));
-        pattern.update(new CurrentDepth(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7)));
-        pattern.update(new CurrentDepth(3, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1)));
-        pattern.update(new CurrentDepth(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4)));
-        pattern.update(new CurrentDepth(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1)));
-        pattern.update(new CurrentDepth(1, new Opcode(104, "imul",         0, -1, Opcode.Category.NORMAL, 6)));
-        pattern.update(new CurrentDepth(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2)));
-    }
-
-    @Test
-    public void testBasic() throws Exception{
-        Assert.assertEquals(7, pattern.getLength());
-        Assert.assertEquals(28, pattern.getWeight());
-
-        Assert.assertEquals(1, pattern.getDepth(0).getDepth());
-        Assert.assertEquals(2, pattern.getDepth(1).getDepth());
-        Assert.assertEquals(3, pattern.getDepth(2).getDepth());
-        Assert.assertEquals(2, pattern.getDepth(3).getDepth());
-        Assert.assertEquals(2, pattern.getDepth(4).getDepth());
-        Assert.assertEquals(1, pattern.getDepth(5).getDepth());
-        Assert.assertEquals(0, pattern.getDepth(6).getDepth());
-    }
-
-    @Test
-    public void testCalculateWeightedCommonSubsequence(){
-        StackPattern pattern2 = new StackPattern();
-        
-        pattern2.update(1, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
-        pattern2.update(2, new Opcode(  4, "iconst_1",     0,  1, Opcode.Category.NORMAL, 1));
-        pattern2.update(1, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
-        pattern2.update(1, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
-        pattern2.update(2, new Opcode( 26, "iload_0",      0,  1, Opcode.Category.NORMAL, 7));
-        pattern2.update(3, new Opcode(  5, "iconst_2",     0,  1, Opcode.Category.NORMAL, 1));
-        pattern2.update(2, new Opcode(100, "isub",         0, -1, Opcode.Category.NORMAL, 4));
-        pattern2.update(2, new Opcode(184, "invokestatic", 2,  0, Opcode.Category.INVOKE, 1));
-        pattern2.update(1, new Opcode( 96, "iadd",         0, -1, Opcode.Category.NORMAL, 3));
-        pattern2.update(0, new Opcode(172, "ireturn",      0, -1, Opcode.Category.NORMAL, 2));
-
-        Assert.assertEquals(21, pattern.calculateWeight(pattern2));
-        Assert.assertEquals(21, pattern2.calculateWeight(pattern));
-    }
-}