Skip to content

Commit 1cb6506

Browse files
authored
Add BigDecimal equals vs compareTo quirk test (#105)
1 parent b44fe65 commit 1cb6506

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎FirstSteps/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@
4848

4949
- [Integer Cache](src/test/java/pl/mperor/lab/java/quirk/IntegerCacheTest.java)
5050
- [Finally Overriding Return](src/test/java/pl/mperor/lab/java/quirk/FinallyOverridingReturnTest.java)
51+
- [BigDecimal equals vs compareTo](src/test/java/pl/mperor/lab/java/quirk/BigDecimalEqualsVsCompareToTest.java)
5152

5253
[Check it out 🔍](src/test/java/pl/mperor/lab/java)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package pl.mperor.lab.java.quirk;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.math.BigDecimal;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
import java.util.TreeSet;
10+
11+
public class BigDecimalEqualsVsCompareToTest {
12+
13+
@Test
14+
public void testEqualsComparesScaleButCompareToIgnoresIt() {
15+
BigDecimal two = new BigDecimal("2.0");
16+
BigDecimal twoWithMoreZeros = new BigDecimal("2.00");
17+
18+
Assertions.assertFalse(two.equals(twoWithMoreZeros), "equals also compares scale, and 2.0 has scale 1 while 2.00 has scale 2");
19+
Assertions.assertEquals(0, two.compareTo(twoWithMoreZeros), "compareTo only compares numeric value, so it treats them as equal");
20+
}
21+
22+
@Test
23+
public void testHashSetKeepsBothButTreeSetKeepsOne() {
24+
BigDecimal two = new BigDecimal("2.0");
25+
BigDecimal twoWithMoreZeros = new BigDecimal("2.00");
26+
27+
Set<BigDecimal> hashSet = new HashSet<>();
28+
hashSet.add(two);
29+
hashSet.add(twoWithMoreZeros);
30+
Assertions.assertEquals(2, hashSet.size(), "HashSet uses equals/hashCode, which treat differently-scaled values as distinct");
31+
32+
Set<BigDecimal> treeSet = new TreeSet<>();
33+
treeSet.add(two);
34+
treeSet.add(twoWithMoreZeros);
35+
Assertions.assertEquals(1, treeSet.size(), "TreeSet uses compareTo for uniqueness, so the second insert is considered a duplicate");
36+
}
37+
38+
@Test
39+
public void testDoubleConstructorVsValueOf() {
40+
BigDecimal fromDoubleConstructor = new BigDecimal(0.1);
41+
BigDecimal fromValueOf = BigDecimal.valueOf(0.1);
42+
43+
Assertions.assertNotEquals(fromDoubleConstructor, fromValueOf, "new BigDecimal(double) exposes the exact binary representation of 0.1, which is not 0.1");
44+
Assertions.assertEquals("0.1", fromValueOf.toString(), "BigDecimal.valueOf(double) goes through Double.toString, giving the shortest decimal that round-trips");
45+
Assertions.assertTrue(fromDoubleConstructor.toPlainString().length() > fromValueOf.toPlainString().length(),
46+
"the double constructor's result carries dozens of extra digits from the binary approximation");
47+
}
48+
49+
@Test
50+
public void testStripTrailingZerosNormalizesScaleForEquals() {
51+
BigDecimal two = new BigDecimal("2.0");
52+
BigDecimal twoWithMoreZeros = new BigDecimal("2.00");
53+
54+
Assertions.assertEquals(two.stripTrailingZeros(), twoWithMoreZeros.stripTrailingZeros(),
55+
"stripTrailingZeros removes the scale difference, so equals now agrees with compareTo");
56+
57+
BigDecimal strippedZero = new BigDecimal("0.00").stripTrailingZeros();
58+
Assertions.assertEquals(0, strippedZero.compareTo(BigDecimal.ZERO), "an exact zero still compares equal to ZERO after stripping zeros");
59+
}
60+
}

0 commit comments

Comments
 (0)