2014-01-27 39 views
-3

我是一名新的Java程序員,並且我一直遇到與JUnit有關的問題。
我在我的機器上運行了一個預先測試過的代碼和一個測試,但是他們沒能通過另一個測試。代碼和測試是100%的工作,我猜測我的電腦有問題(運行Windows 7和DrJava)。Junit測試保持失敗,但代碼是100%正確的

有什麼我必須安裝或其他我必須做的?

此代碼保持測試失敗,我永遠不知道爲什麼。如果你能告訴我我真的很感激它的錯誤。

public class FeatureVector 
{ 
    private String name ; 
    private double [] features ; 
    private int size ; 
    public static boolean verbose = true ; 



    public static boolean setVerbose (boolean x ) {verbose = x; 
    return verbose ;} // static method to set the detailed message for printing 



    public FeatureVector (String name , int size) 
    { this.name = name;        // the attributes -instanse variables- 
    this.size = size ; 
    this.features = new double [size] ; 

    for (int x = 0 ; x < size ; x ++) {this.features[x] = 0.0 ;} // first constructor 
    } 

    public FeatureVector (String name , double [] elems) // second constructor 
    { 
    this.name = name ; 
    this.features = elems ; 
    elems = null ; // set elems to null so we wont occupy un-needed memory 
    this.size = this.features.length ; 

    } 


    public String getName() { return this.name ;} // a name getter 

    public int getSize() {return this.size ;}  // a size getter 

    public double featureAt (int index) { return this.features[index] ; } // an index getter 


    public void featureSet (int index , double value) { this.features[index] = value;} // an index setter 


    public FeatureVector copy() 

    { FeatureVector copied = new FeatureVector ("a copy" , 0) ; 

    copied.name = this.name ; 

    for (int i = 0 ; i < this.size ; i ++) { copied.features[i] = this.features[i] ;} 

    return copied ;} 



    public double getDistance (FeatureVector x) 
    { double sum = 0 ; 
    double values ; 
    for (int m = 0 ; m < this.features.length ; m ++) 
    { 
     values = (this.features[m] - x.features[m]) ; 
     Math.pow(values,2) ; 
     sum = sum + values ; } 
    Math.sqrt(sum) ; 
    return sum ;} 

    public void plus (FeatureVector x) 
    { 
    for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i] + x.features[i] ;} } 
    public void div (FeatureVector x) 
    { for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i]/x.features[i] ;} 
    } 
    public void div (double x) 
    { for (int i = 0 ; i < this.features.length ; i ++) {this.features[i] = this.features[i]/x ;} 
    } 


    public String toString() 
    { String message = "" ; 

    if (details == true) 
    { System.out.print (name + ": {") ; 
     for (int i = 0 ; i < this.features.length ; i ++) 
     {System.out.print (this.features[i]) ; 
     System.out.print (", ");} 
     System.out.print ("}"); 
    } 
    else { message = this.name ; } 

    return message ; } 

    public boolean equals (FeatureVector x) 
    { 
    boolean result ; 
    if (this.features.length == x.features.length) {result = true ;} 
    else {result = false ; } 
    return result ; } 



} 

和測試

import junit.framework.Assert; import junit.framework.TestCase; import 
junit.framework.TestSuite; 

public class FeatureVectorTest extends TestCase { 

    public void testGetSize() { FeatureVector v, w; 

    v = new FeatureVector("V", 10); Assert.assertEquals(10, 
v.getSize()); 

    w = new FeatureVector("W", 0); Assert.assertEquals(0, w.getSize()); 
    } 

    public void testFeatureAt() { FeatureVector v = new FeatureVector("V", 10); 

    for (int i = 0; i < 10; i++) { 
     v.featureSet(i, (double) i); 
     Assert.assertEquals((double) i, v.featureAt(i)); } 
    } 

    public void testFeatureVector() { double[] vs = { -Math.PI, 1.6180339887, 2.7182818284, 1.4142135623 }; FeatureVector v = new FeatureVector("V", vs); 

    for (int i = 0; i < vs.length; i++) { 
     Assert.assertEquals(vs[i], v.featureAt(i)); } 

    // What is the purpose of this test? for (int i = 0; i < vs.length; 
i++) { 
     double tmp = vs[i]; 
     vs[i] = 0.0; 
     Assert.assertEquals(tmp, v.featureAt(i)); } 
    } 

    public void testGetDistance() { FeatureVector v = new FeatureVector("V", new ouble[] { 0.0, 2.0, 1.0, 5.0 }); 
FeatureVector w = new FeatureVector("W", new double[] { 2.0, 0.0, 
-1.0, 7.0 }); 

    Assert.assertEquals(0.0, v.getDistance(v), 0.00001); 
Assert.assertEquals(0.0, w.getDistance(w), 0.00001); 

    Assert.assertEquals(4.0, v.getDistance(w), 0.00001); 
Assert.assertEquals(4.0, w.getDistance(v), 0.00001); 

    v = new FeatureVector("V", 0); w = new FeatureVector("W", 0); 

    Assert.assertEquals(0.0, v.getDistance(w), 0.00001); 
    } 

    public void testEquals() { FeatureVector v = new FeatureVector("V", new double[] { 0.0, 2.0, 1.0, 5.0 }); 
FeatureVector w = new FeatureVector("W", new double[] { 2.0, 0.0, -1.0 
}); FeatureVector x = new FeatureVector("X", new double[] { 2.0, 0.0, 
-1.0 }); FeatureVector y = new FeatureVector("Y", new double[] { 0.0, 2.0, 1.0 }); 

    Assert.assertTrue(v.equals(v)); Assert.assertTrue(w.equals(w)); 
Assert.assertTrue(w.equals(x)); 

    Assert.assertFalse(v.equals(w)); Assert.assertFalse(x.equals(y)); 
Assert.assertFalse(x.equals(null)); 
    } 

    public void testPlus() { double[] vs = { -1.0, 0.0, 1.0, 2.0 }; double[] ws = { 1.0, 1.0, 1.0, 1.0 }; FeatureVector v = new 
FeatureVector("V", vs); FeatureVector w = new FeatureVector("V", ws); 

    v.plus(w); 

    for (int i = 0; i < vs.length; i++) { 
     Assert.assertEquals((double) i, v.featureAt(i)); } 

    } 

    public void testDiv() { double[] vs = { 0.0, 2.0, 4.0, 6.0 }; FeatureVector v = new FeatureVector("V", vs); 

    v.div(2.0); 

    for (int i = 0; i < vs.length; i++) { 
     Assert.assertEquals((double) i, v.featureAt(i)); } 

    } 

    public static void main(String[] args) { TestSuite suite = new TestSuite(); suite.addTestSuite(FeatureVectorTest.class); 
junit.textui.TestRunner.run(suite); 
    } 

} 
+3

向我們展示測試和它測試的代碼將是一個好的開始。 – Keppil

+0

我該如何在這裏展示它?評論線程不允許多個字符? –

+0

@ user3238603你可以編輯你的文章並添加相關信息。 – ajb

回答

0

我已經使用Eclipse,當幾年前之前的Java莫名其妙地失敗。我正在運行一個簡單的解析算法,並且我的代碼拒絕承認1等於1.我使用了另一個IDE,它運行得非常完美。也許你遇到了一個特殊的錯誤,另一個開發環境會運行你的代碼。

1

toString()方法有錯誤。您引用了一個名爲details的變量,該變量似乎不存在。您可能想要將其更改爲verbose