Merge pull request #10265 from maciejglowka/BAEL-4725
BAEL-4725: examples of double comparison in Java
This commit is contained in:
commit
de36111f42
|
@ -23,6 +23,18 @@
|
|||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>3.6.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.comparedouble;
|
||||
|
||||
import com.google.common.math.DoubleMath;
|
||||
import org.apache.commons.math3.util.Precision;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CompareDoubleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDoubleValuesThatShouldHaveSameValue_whenUsingSimpleComparison_thenFails() {
|
||||
double d1 = getFirstDouble(0);
|
||||
|
||||
double d2 = .1 * 8;
|
||||
|
||||
assertThat(d1 == d2).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValuesThatShouldHaveSameValue_whenUsingThresholdComparison_thenSuccess() {
|
||||
double d1 = getFirstDouble(0);
|
||||
|
||||
double d2 = .1 * 8;
|
||||
|
||||
double epsilon = 0.000001d;
|
||||
|
||||
assertThat(Math.abs(d1 - d2) < epsilon).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValuesThatShouldHaveSameValue_whenUsingGuavaFuzzyComparison_thenSuccess() {
|
||||
double d1 = getFirstDouble(0);
|
||||
double d2 = .1 * 8;
|
||||
|
||||
double epsilon = 0.000001d;
|
||||
|
||||
|
||||
assertThat(DoubleMath.fuzzyEquals(d1, d2, epsilon)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValuesThatShouldHaveSameValue_whenUsingCommonsMathComparison_thenSuccess() {
|
||||
double d1 = getFirstDouble(0);
|
||||
double d2 = .1 * 8;
|
||||
|
||||
double epsilon = 0.000001d;
|
||||
|
||||
|
||||
assertThat(Precision.equals(d1, d2, epsilon)).isTrue();
|
||||
assertThat(Precision.equals(d1, d2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDoubleValuesThatShouldHaveSameValue_whenUsingJunitComparison_thenSuccess() {
|
||||
double d1 = getFirstDouble(0);
|
||||
double d2 = .1 * 8;
|
||||
|
||||
double epsilon = 0.000001d;
|
||||
|
||||
assertEquals(d1, d2, epsilon);
|
||||
}
|
||||
|
||||
private double getFirstDouble(double d1) {
|
||||
for (int i = 1; i <= 8; i++) {
|
||||
d1 += .1;
|
||||
}
|
||||
return d1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue