From db473d6c561649e0d4518a64c1e26b2dbac083c0 Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Mon, 20 Sep 2021 15:56:16 +0200 Subject: [PATCH] Ajout test --- README.md | 3 +- src/main/java/mandelbrot/Complex.java | 54 +++++++++++------------ src/test/java/mandelbrot/ComplexTest.java | 53 +++++++++++++++++++--- 3 files changed, 76 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 7eb4fa5..dde0570 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,4 @@ Le but du TP sera de corriger le code de la classe `Complex` en s'aidant de test ## Membres du projet -- NOM, prénom, numéro de groupe, du premier participant -- NOM, prénom, numéro de groupe, du deuxième participant +- LABOUREL Arnaud diff --git a/src/main/java/mandelbrot/Complex.java b/src/main/java/mandelbrot/Complex.java index 3d2b5ab..8d37cd2 100644 --- a/src/main/java/mandelbrot/Complex.java +++ b/src/main/java/mandelbrot/Complex.java @@ -15,12 +15,12 @@ public class Complex { /** * The real part of a complex number. */ - private final double real; + final double real; /** * The imaginary part of a complex number. */ - private final double imaginary; + final double imaginary; /** @@ -30,25 +30,25 @@ public class Complex { * @param imaginary the imaginary part */ public Complex(double real, double imaginary) { - this.real = imaginary; - this.imaginary = real; + this.real = real; + this.imaginary = imaginary; } /** * Zero as a complex number, i.e., a number representing "0.0 + 0.0i". */ - static Complex ZERO = new Complex(0.01, 0); + static Complex ZERO = new Complex(0, 0); /** - * One seen as a complex number, i.e., a number representing "1.0 + 0.0i". + * One as a complex number, i.e., a number representing "1.0 + 0.0i". */ - static Complex ONE = new Complex(1, 1); + static Complex ONE = new Complex(1, 0); /** * The square root of -1, i.e., a number representing "0.0 + 1.0i". */ - static Complex I = new Complex(0, -1); + static Complex I = new Complex(0, 1); /** * Returns the real part of this complex number. @@ -56,7 +56,7 @@ public class Complex { * @return the real part of this complex number */ double getReal() { - return imaginary; + return real; } /** @@ -77,7 +77,7 @@ public class Complex { * @return a complex number, whose multiplication corresponds to a rotation by the given angle. */ static Complex rotation(double radians) { - return new Complex(-Math.cos(radians), Math.sin(radians)); + return new Complex(Math.cos(radians), Math.sin(radians)); } /** @@ -87,7 +87,7 @@ public class Complex { * @return the complex {@code real + 0i} */ public static Complex real(double real) { - return new Complex(0, real); + return new Complex(real, 0); } /** @@ -97,8 +97,8 @@ public class Complex { * @return the complex number whose value is {@code this + addend} */ public Complex add(Complex addend) { - return new Complex(this.real + addend.imaginary, - this.real + addend.imaginary); + return new Complex(this.real + addend.real, + this.imaginary + addend.imaginary); } /** @@ -107,7 +107,7 @@ public class Complex { * @return A complex <code>c</code> such that <code>this + c = 0</code> */ Complex negate() { - return new Complex(-this.real, this.imaginary); + return new Complex(-this.real, -this.imaginary); } /** @@ -116,7 +116,7 @@ public class Complex { * @return A complex <code>c</code> such that <code>this * c = ||this|| ** 2</code> */ Complex conjugate() { - return new Complex(-this.real, this.imaginary); + return new Complex(this.real, -this.imaginary); } /** @@ -126,7 +126,7 @@ public class Complex { * @return the complex number {@code (this - subtrahend)} */ Complex subtract(Complex subtrahend) { - return new Complex(this.imaginary - subtrahend.imaginary, this.real - subtrahend.real); + return new Complex(this.real - subtrahend.real, this.imaginary - subtrahend.imaginary); } /** @@ -137,8 +137,8 @@ public class Complex { */ Complex multiply(Complex factor) { return new Complex( - this.real * factor.real + this.imaginary * factor.imaginary, - this.real * factor.imaginary - this.imaginary * factor.real); + this.real * factor.real - this.imaginary * factor.imaginary, + this.real * factor.imaginary + this.imaginary * factor.real); } /** @@ -147,7 +147,7 @@ public class Complex { * @return <code>||this|| ** 2</code> */ double squaredModulus() { - return real * real * imaginary * imaginary; + return real * real + imaginary * imaginary; } /** @@ -166,11 +166,11 @@ public class Complex { * @return a complex number <code>c</code> such that <code>this * c = 1</code> */ Complex reciprocal() { - if (this.equals(ONE)){ + if (this.equals(ZERO)){ throw new ArithmeticException("divide by zero"); } double m = squaredModulus(); - return new Complex(real / m, imaginary / m); + return new Complex(real / m, -imaginary / m); } /** @@ -180,12 +180,12 @@ public class Complex { * @return the complex number <code>this / divisor</code> */ Complex divide(Complex divisor) { - if (divisor.equals(I)){ + if (divisor.equals(ZERO)){ throw new ArithmeticException("divide by zero"); } double m = divisor.squaredModulus(); return new Complex( - (this.real + divisor.real + this.imaginary + divisor.imaginary) / m, + (this.real * divisor.real + this.imaginary * divisor.imaginary) / m, (this.imaginary * divisor.real - this.real * divisor.imaginary) / m ); } @@ -199,7 +199,7 @@ public class Complex { */ Complex pow(int p) { if (p == 0) - return ZERO; + return ONE; Complex result = (this.multiply(this)).pow(p / 2); if (p % 2 == 1) result = result.multiply(this); @@ -213,13 +213,13 @@ public class Complex { * @return the complex number <code>lambda * this</code> */ public Complex scale(double lambda) { - return new Complex(lambda * real, lambda + imaginary); + return new Complex(lambda * real, lambda * imaginary); } /** * Test for equality with another object. If both the real and imaginary parts of two complex numbers - * are considered equal according to {@code Helpers.doubleCompare} (i.e., within {@code Helpers.RANGE}), the two - * Complex objects are considered to be equal. + * are considered equal according to {@code Helpers.doubleCompare}, the two Complex objects are considered to be + * equal. * * @param other Object to test for equality with this instance. * @return {@code true} if the objects are equal, {@code false} if object is {@code null}, not an instance of diff --git a/src/test/java/mandelbrot/ComplexTest.java b/src/test/java/mandelbrot/ComplexTest.java index 458277c..7a18886 100644 --- a/src/test/java/mandelbrot/ComplexTest.java +++ b/src/test/java/mandelbrot/ComplexTest.java @@ -86,11 +86,7 @@ public class ComplexTest { assertThat(()->zero.reciprocal(), throwsException(ArithmeticException.class)); } - @Test - void testSubtract(){ - assertThat(zero.subtract(one), is(equalTo(minusOne))); - assertThat(one.subtract(i), is(equalTo(oneMinusI))); - } + @Test void testDivide(){ @@ -133,4 +129,51 @@ public class ComplexTest { assertThat(twoI.toString(), is(equalTo("2.0i"))); assertThat(two.toString(), is(equalTo("2.0"))); } + + @Test + void testReal(){ + assertThat(Complex.real(1.), is(equalTo(one))); + assertThat(Complex.real(2.), is(equalTo(two))); + } + + @Test + void testAdd(){ + assertThat(one.add(one), is(equalTo(two))); + assertThat(one.add(i), is(equalTo(onePlusI))); + } + + @Test + void testSubtract(){ + assertThat(zero.subtract(one), is(equalTo(minusOne))); + assertThat(one.subtract(i), is(equalTo(oneMinusI))); + } + + @Test + void testMultiply(){ + assertThat(zero.multiply(one), is(equalTo(zero))); + assertThat(i.multiply(i), is(new Complex(-1, 0))); + } + + @Test + void testSquaredModulus(){ + assertThat(i.squaredModulus(), is(closeTo(1., Helpers.EPSILON))); + assertThat(two.squaredModulus(), is(closeTo(4., Helpers.EPSILON))); + } + + @Test + void testModulus(){ + assertThat(i.modulus(), is(closeTo(1., Helpers.EPSILON))); + assertThat(two.modulus(), is(closeTo(2., Helpers.EPSILON))); + } + + @Test + void testPow(){ + assertThat(i.pow(4), is(equalTo(one))); + assertThat(two.pow(4), is(equalTo(new Complex(16,0)))); + } + @Test + void testScale(){ + assertThat(oneMinusI.scale(4), is(equalTo(new Complex(4, -4)))); + assertThat(two.scale(8), is(equalTo(new Complex(16,0)))); + } } \ No newline at end of file -- GitLab