|
| 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