2014-01-27 1 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

있습니다. 나는 간단한 파싱 알고리즘을 실행하고 있었고 코드는 1이 1과 동일하다는 것을 인정하지 않았습니다. 다른 IDE를 사용하고 완벽하게 작동했습니다. 아마 당신은 특이한 버그에 부딪쳤을 것이고 또 다른 개발 환경은 당신의 코드를 실행할 것입니다.

1

toString() 메서드에 오류가 있습니다. 존재하지 않는 details이라는 변수를 참조하고 있습니다. 아마도 verbose으로 변경하고 싶을 것입니다.