Skip to content

Commit

Permalink
Fix compilation warnings & add test (#2614)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 authored Feb 10, 2024
1 parent db61bb0 commit d148301
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions gson/src/test/java/com/google/gson/JsonPrimitiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ public void testBigDecimalEquals() {
assertThat(small.equals(large)).isFalse();

BigDecimal doubleMax = BigDecimal.valueOf(Double.MAX_VALUE);
JsonPrimitive smallBD = new JsonPrimitive(doubleMax.add(new BigDecimal("100.0")));
JsonPrimitive largeBD = new JsonPrimitive(doubleMax.add(new BigDecimal("200.0")));
assertThat(smallBD.equals(largeBD)).isFalse();
JsonPrimitive smallDecimal = new JsonPrimitive(doubleMax.add(new BigDecimal("100.0")));
JsonPrimitive largeDecimal = new JsonPrimitive(doubleMax.add(new BigDecimal("200.0")));
assertThat(smallDecimal.equals(largeDecimal)).isFalse();
}

@Test
Expand All @@ -347,6 +347,22 @@ public void testBigDecimalEqualsZero() {
.isTrue();
}

/**
* Verifies that {@link JsonPrimitive#equals(Object)} is <i>transitive</i> for {@link BigDecimal},
* as required by the {@link Object#equals(Object)} documentation.
*/
@Test
public void testBigDecimalEqualsTransitive() {
JsonPrimitive x = new JsonPrimitive(new BigDecimal("0"));
JsonPrimitive y = new JsonPrimitive(0.0d);
JsonPrimitive z = new JsonPrimitive(new BigDecimal("0.00"));

assertThat(x.equals(y)).isTrue();
assertThat(y.equals(z)).isTrue();
// ... implies
assertThat(x.equals(z)).isTrue();
}

@Test
public void testEqualsDoubleNaNAndBigDecimal() {
assertThat(new JsonPrimitive(Double.NaN).equals(new JsonPrimitive(new BigDecimal("1.0"))))
Expand Down

0 comments on commit d148301

Please sign in to comment.