Skip to content
Snippets Groups Projects
Commit 8fd3019c authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Test assertJ

parent db473d6c
No related branches found
No related tags found
No related merge requests found
Pipeline #1363 passed
...@@ -13,9 +13,9 @@ repositories { ...@@ -13,9 +13,9 @@ repositories {
} }
dependencies { dependencies {
testImplementation('org.junit.jupiter:junit-jupiter-api:5.7.2', testImplementation('org.junit.jupiter:junit-jupiter-api:5.8.0',
'org.hamcrest:hamcrest-library:2.2', 'net.obvj:junit-utils:1.3.1') "org.assertj:assertj-core:3.21.0", 'net.obvj:junit-utils:1.3.1')
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.0'
} }
test { test {
......
package mandelbrot; package mandelbrot;
import static net.obvj.junit.utils.matchers.AdvancedMatchers.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class ComplexTest { public class ComplexTest {
private Complex onePlusI; private Complex onePlusI;
...@@ -33,147 +30,149 @@ public class ComplexTest { ...@@ -33,147 +30,149 @@ public class ComplexTest {
@Test @Test
void testEquals(){ void testEquals(){
assertThat(onePlusI, is(equalTo(onePlusI))); assertThat(onePlusI).isEqualTo(onePlusI);
assertThat(onePlusI, is(equalTo(new Complex(1, 1)))); assertThat(onePlusI).isEqualTo(new Complex(1, 1));
assertThat(two, is(not(equalTo(twoI)))); assertThat(two).isNotEqualTo(twoI);
} }
@Test @Test
void testGetReal(){ void testGetReal(){
assertThat(two.getReal(), is(closeTo(2., Helpers.EPSILON))); assertThat(two.getReal()).isCloseTo(2., within(Helpers.EPSILON));
assertThat(onePlusI.getReal(), is(closeTo(1., Helpers.EPSILON))); assertThat(onePlusI.getReal()).isCloseTo(1., within(Helpers.EPSILON));
assertThat(oneMinusI.getReal(), is(closeTo(1., Helpers.EPSILON))); assertThat(oneMinusI.getReal()).isCloseTo(1., within(Helpers.EPSILON));
} }
@Test @Test
void testGetImaginary(){ void testGetImaginary(){
assertThat(two.getImaginary(), is(closeTo(0., Helpers.EPSILON))); assertThat(two.getImaginary()).isCloseTo(0., within(Helpers.EPSILON));
assertThat(onePlusI.getImaginary(), is(closeTo(1., Helpers.EPSILON))); assertThat(onePlusI.getImaginary()).isCloseTo(1., within(Helpers.EPSILON));
assertThat(oneMinusI.getImaginary(), is(closeTo(-1., Helpers.EPSILON))); assertThat(oneMinusI.getImaginary()).isCloseTo(-1., within(Helpers.EPSILON));
} }
@Test @Test
void testOne(){ void testOne(){
assertThat(Complex.ONE.getReal(), is(closeTo(1., Helpers.EPSILON))); assertThat(Complex.ONE.getReal()).isCloseTo(1., within(Helpers.EPSILON));
assertThat(Complex.ONE.getImaginary(), is(closeTo(0., Helpers.EPSILON))); assertThat(Complex.ONE.getImaginary()).isCloseTo(0., within(Helpers.EPSILON));
} }
@Test @Test
void testI(){ void testI(){
assertThat(Complex.I.getReal(), is(closeTo(0., Helpers.EPSILON))); assertThat(Complex.I.getReal()).isCloseTo(0., within(Helpers.EPSILON));
assertThat(Complex.I.getImaginary(), is(closeTo(1., Helpers.EPSILON))); assertThat(Complex.I.getImaginary()).isCloseTo(1., within(Helpers.EPSILON));
} }
@Test @Test
void testZero(){ void testZero(){
assertThat(Complex.ZERO.getReal(), is(closeTo(0., Helpers.EPSILON))); assertThat(Complex.ZERO.getReal()).isCloseTo(0., within(Helpers.EPSILON));
assertThat(Complex.ZERO.getImaginary(), is(closeTo(0., Helpers.EPSILON))); assertThat(Complex.ZERO.getImaginary()).isCloseTo(0., within(Helpers.EPSILON));
} }
@Test @Test
void testNegate(){ void testNegate(){
assertThat(two.negate(), is(equalTo(new Complex(-2,0)))); assertThat(two.negate()).isEqualTo(new Complex(-2,0));
assertThat(minusI.negate(), is(equalTo(i))); assertThat(minusI.negate()).isEqualTo(i);
assertThat(oneMinusI.negate(), is(equalTo(new Complex(-1, 1)))); assertThat(oneMinusI.negate()).isEqualTo(new Complex(-1, 1));
} }
@Test @Test
void testReciprocal(){ void testReciprocal(){
assertThat(one.reciprocal(), is(equalTo(one))); assertThat(one.reciprocal()).isEqualTo(one);
assertThat(minusI.reciprocal(), is(equalTo(i))); assertThat(minusI.reciprocal()).isEqualTo(i);
assertThat(two.reciprocal(), is(equalTo(new Complex(0.5,0)))); assertThat(two.reciprocal()).isEqualTo(new Complex(0.5,0));
assertThat(oneMinusI.reciprocal(), is(equalTo(new Complex(0.5,0.5)))); assertThat(oneMinusI.reciprocal()).isEqualTo(new Complex(0.5,0.5));
} }
@Test @Test
void testReciprocalOfZero(){ void testReciprocalOfZero(){
assertThat(()->zero.reciprocal(), throwsException(ArithmeticException.class)); assertThatExceptionOfType(ArithmeticException.class)
.isThrownBy(()->zero.reciprocal());
} }
@Test @Test
void testDivide(){ void testDivide(){
assertThat(onePlusI.divide(Complex.ONE), equalTo(onePlusI)); assertThat(onePlusI.divide(Complex.ONE)).isEqualTo(onePlusI);
assertThat(Complex.ONE.divide(two), equalTo(new Complex(0.5, 0))); assertThat(Complex.ONE.divide(two)).isEqualTo(new Complex(0.5, 0));
assertThat(oneMinusI.divide(onePlusI), equalTo(minusI)); assertThat(oneMinusI.divide(onePlusI)).isEqualTo(minusI);
} }
@Test @Test
void testDivideByZero(){ void testDivideByZero(){
assertThat(()->one.divide(zero), throwsException(ArithmeticException.class)); assertThatExceptionOfType(ArithmeticException.class)
.isThrownBy(()->one.divide(zero));
} }
@Test @Test
void testConjugate(){ void testConjugate(){
assertThat(two.conjugate(), equalTo(two)); assertThat(two.conjugate()).isEqualTo(two);
assertThat(oneMinusI.conjugate(), equalTo(onePlusI)); assertThat(oneMinusI.conjugate()).isEqualTo(onePlusI);
} }
@Test @Test
void testRotation(){ void testRotation(){
assertThat(Complex.rotation(Math.PI/2), equalTo(i)); assertThat(Complex.rotation(Math.PI/2)).isEqualTo(i);
assertThat(Complex.rotation(-Math.PI/2), equalTo(minusI)); assertThat(Complex.rotation(-Math.PI/2)).isEqualTo(minusI);
assertThat(Complex.rotation(0), equalTo(one)); assertThat(Complex.rotation(0)).isEqualTo(one);
assertThat(Complex.rotation(Math.PI/4), equalTo(new Complex(Math.sqrt(2)/2., Math.sqrt(2)/2.))); assertThat(Complex.rotation(Math.PI/4)).isEqualTo(new Complex(Math.sqrt(2)/2., Math.sqrt(2)/2.));
assertThat(Complex.rotation(Math.PI/3), equalTo(new Complex(1./2., Math.sqrt(3)/2.))); assertThat(Complex.rotation(Math.PI/3)).isEqualTo(new Complex(1./2., Math.sqrt(3)/2.));
} }
@Test @Test
void testBasicToString(){ void testBasicToString(){
assertThat(two.toString(), containsString("2.0")); assertThat(two.toString()).contains("2.0");
assertThat(i.toString(), containsString("i")); assertThat(i.toString()).contains("i");
} }
@Test @Test
void testToStringFormat(){ void testToStringFormat(){
assertThat(oneMinusI.toString(), is(equalTo("1.0 - 1.0i"))); assertThat(oneMinusI.toString()).isEqualTo("1.0 - 1.0i");
assertThat(onePlusI.toString(), is(equalTo("1.0 + 1.0i"))); assertThat(onePlusI.toString()).isEqualTo("1.0 + 1.0i");
assertThat(minusI.toString(), is(equalTo("-1.0i"))); assertThat(minusI.toString()).isEqualTo("-1.0i");
assertThat(twoI.toString(), is(equalTo("2.0i"))); assertThat(twoI.toString()).isEqualTo("2.0i");
assertThat(two.toString(), is(equalTo("2.0"))); assertThat(two.toString()).isEqualTo("2.0");
} }
@Test @Test
void testReal(){ void testReal(){
assertThat(Complex.real(1.), is(equalTo(one))); assertThat(Complex.real(1.)).isEqualTo(one);
assertThat(Complex.real(2.), is(equalTo(two))); assertThat(Complex.real(2.)).isEqualTo(two);
} }
@Test @Test
void testAdd(){ void testAdd(){
assertThat(one.add(one), is(equalTo(two))); assertThat(one.add(one)).isEqualTo(two);
assertThat(one.add(i), is(equalTo(onePlusI))); assertThat(one.add(i)).isEqualTo(onePlusI);
} }
@Test @Test
void testSubtract(){ void testSubtract(){
assertThat(zero.subtract(one), is(equalTo(minusOne))); assertThat(zero.subtract(one)).isEqualTo(minusOne);
assertThat(one.subtract(i), is(equalTo(oneMinusI))); assertThat(one.subtract(i)).isEqualTo(oneMinusI);
} }
@Test @Test
void testMultiply(){ void testMultiply(){
assertThat(zero.multiply(one), is(equalTo(zero))); assertThat(zero.multiply(one)).isEqualTo(zero);
assertThat(i.multiply(i), is(new Complex(-1, 0))); assertThat(i.multiply(i)).isEqualTo(new Complex(-1, 0));
} }
@Test @Test
void testSquaredModulus(){ void testSquaredModulus(){
assertThat(i.squaredModulus(), is(closeTo(1., Helpers.EPSILON))); assertThat(i.squaredModulus()).isCloseTo(1., within(Helpers.EPSILON));
assertThat(two.squaredModulus(), is(closeTo(4., Helpers.EPSILON))); assertThat(two.squaredModulus()).isCloseTo(4., within(Helpers.EPSILON));
} }
@Test @Test
void testModulus(){ void testModulus(){
assertThat(i.modulus(), is(closeTo(1., Helpers.EPSILON))); assertThat(i.modulus()).isCloseTo(1., within(Helpers.EPSILON));
assertThat(two.modulus(), is(closeTo(2., Helpers.EPSILON))); assertThat(two.modulus()).isCloseTo(2., within(Helpers.EPSILON));
} }
@Test @Test
void testPow(){ void testPow(){
assertThat(i.pow(4), is(equalTo(one))); assertThat(i.pow(4)).isEqualTo(one);
assertThat(two.pow(4), is(equalTo(new Complex(16,0)))); assertThat(two.pow(4)).isEqualTo(new Complex(16,0));
} }
@Test @Test
void testScale(){ void testScale(){
assertThat(oneMinusI.scale(4), is(equalTo(new Complex(4, -4)))); assertThat(oneMinusI.scale(4)).isEqualTo(new Complex(4, -4));
assertThat(two.scale(8), is(equalTo(new Complex(16,0)))); assertThat(two.scale(8)).isEqualTo(new Complex(16,0));
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment