Testy jednostkowe (liczby zespolone)

0

Mam klasę Complex:

``````public final class Complex {
private final double re;
private final double im;
public static final Complex ZERO = new Complex(0,0);
public static final Complex ONE = new Complex(1,0);
public static final Complex I = new Complex(0,1);

public Complex(double re, double im){
this.re = re;
this.im = im;
}

public double realPart(){
return re;
}
public double imagPart(){
return im;
}
return new Complex(re+c.re,im+c.im);
}
public Complex subtract(Complex c){
return new Complex(re-c.re,im-c.im);
}
public Complex multiply(Complex c){
return new Complex(re*c.re-im*c.im,
re*c.im+im*c.re);
}
public Complex divide(Complex c){
double tmp = c.re*c.re + c.im*c.im;
return new Complex((re*c.re+im*c.im)/tmp,
(im*c.re-re*c.im)/tmp);
}
@Override
public boolean equals(Object o){
if(o==this) return true;
if(!(o instanceof Complex)) return false;
Complex c = (Complex) o;
return Double.compare(re,c.re) == 0 && Double.compare(im, c.im)==0;
}

public int hashCode(){
int result = 17 + hashDouble(re);
result = 31* result+ hashDouble(im);
return result;
}

private static int hashDouble(double val){
long longBits = Double.doubleToLongBits(val);
return (int) (longBits^(longBits >>> 32));
}
@Override
public String toString(){
return "("+re+"+"+im+"i)";
}
}``````

Jakie i jak mogę zrobić testy?

Czy mogą być takie:

``````public class ComplexTest {
private double eps;
@Before
public void setUp(){
eps = 10*Double.MIN_VALUE;

}
@Test
public void testCons3(){
Complex a = new Complex(2,3);
assertEquals(2.0, a.realPart(), eps);
assertEquals(3.0, a.imagPart(), eps);
}
@Test
Complex a = new Complex(2,3);
Complex b = new Complex(4,7);
assertEquals(2.0, a.realPart(), eps);
assertEquals(3.0, a.imagPart(), eps);
}
@Test
public void testsubtract(){
Complex a = new Complex(2,3);
Complex b = new Complex(4,7);
a.subtract(b);
assertEquals(2.0, a.realPart(), eps);
assertEquals(3.0, a.imagPart(), eps);
}
public void testmultiply(){
Complex a = new Complex(2,3);
Complex b = new Complex(4,7);
a.multiply(b);
assertEquals(2.0, a.realPart(), eps);
assertEquals(3.0, a.imagPart(), eps);
}
public void testdivide(){
Complex a = new Complex(2,3);
Complex b = new Complex(4,7);
a.divide(b);
assertEquals(2.0, a.realPart(), eps);
assertEquals(3.0, a.imagPart(), eps);
}

public static void main(String[] args){
JUnitCore.main("complex.ComplexTest");
}
}``````
0

Ja bym wpierw podrasował trochę definicje klasy:

• jakoś trzeba obsłużyć dzielenie przez zero,
• metoda toString() gdy im<0 wyświetli coś takiego (3+-4i).
0

Ja Ci w testach nie pomogę...ale dzięki za wklejenie kodu - zawsze chciałem się dowiedzieć jak zrobić własny typ danych:)