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

Passage test assertJ

parent 8bd52473
Branches
No related tags found
No related merge requests found
Pipeline #1364 failed
...@@ -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")
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,99 +30,103 @@ public class ComplexTest { ...@@ -33,99 +30,103 @@ 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));
assertThat(twoI.getReal()).isCloseTo(0.,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");
} }
} }
\ 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