Я новый программист на 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);
}
}
Показывая нам тест и код, который он тестирует, будет хорошим началом. – Keppil
как я могу показать его здесь? поток комментариев не позволяет много символов? –
@ user3238603 вы можете отредактировать свой пост и добавить соответствующую информацию. – ajb