Example #1
0
  public void testNegativeZero(int scale, IonDecimal actual) {
    assertEquals(-0f, actual.floatValue());
    assertEquals(-0d, actual.doubleValue());

    BigDecimal bd = actual.bigDecimalValue();
    Decimal dec = actual.decimalValue();

    assertEquals(0, BigDecimal.ZERO.compareTo(bd));

    checkDecimal(0, scale, bd);
    checkDecimal(0, scale, dec);
    assertEquals(0, Decimal.NEGATIVE_ZERO.compareTo(dec));
    assertTrue(dec.isNegativeZero());
  }
Example #2
0
  /** Ensure that {@link Decimal#equals} and {@link Decimal#hashCode} are in sync. */
  @Test
  public void testDecimalHash() {
    BigDecimal d1 = new BigDecimal("0.");
    BigDecimal d2 = Decimal.valueOf("0.");
    assertEquals(d1, d2);
    assertEquals("hash code", d1.hashCode(), d2.hashCode());

    d1 = new BigDecimal("0.00");
    d2 = Decimal.valueOf("0.00");
    assertEquals(d1, d2);
    assertEquals("hash code", d1.hashCode(), d2.hashCode());

    // Decimal doesn't override equals() so it negative and positive zeros
    // are equal.  We can't change that since we can't make it symmetric.
    // We must also keep the hash codes the same.
    d1 = new BigDecimal("0.00");
    d2 = Decimal.valueOf("-0.00");
    assertEquals(d1, d2);
    assertEquals("hash code", d1.hashCode(), d2.hashCode());
  }