Beispiel #1
0
  public void checkEquality(boolean expected, String v1, String v2) {
    IonDecimal d1 = decimal(v1);
    IonDecimal d2 = decimal(v2);

    assertEquals(expected, d1.equals(d2));
    assertEquals(expected, d2.equals(d1));
  }
Beispiel #2
0
  @Test
  public void testScale() {
    final BigDecimal one_00 = new BigDecimal("1.00");

    assertEquals(1, one_00.intValue());
    assertEquals(100, one_00.unscaledValue().intValue());
    assertEquals(2, one_00.scale());

    IonDecimal value = (IonDecimal) oneValue("1.00");
    assertEquals(one_00, value.bigDecimalValue());
  }
Beispiel #3
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());
  }
Beispiel #4
0
  @Test
  public void testBinaryDecimals() throws Exception {
    IonDatagram dg = loadTestFile("good/decimalOneDotZero.10n");
    assertEquals(1, dg.size());

    IonDecimal value = (IonDecimal) dg.get(0);
    BigDecimal dec = value.bigDecimalValue();
    checkDecimal(10, 1, dec);
    assertEquals(1, dec.intValue());

    dg = loadTestFile("good/decimalNegativeOneDotZero.10n");
    assertEquals(1, dg.size());

    value = (IonDecimal) dg.get(0);
    dec = value.bigDecimalValue();
    checkDecimal(-10, 1, dec);
    assertEquals(-1, dec.intValue());
  }
Beispiel #5
0
  public static void checkNullDecimal(IonDecimal value) {
    assertSame(IonType.DECIMAL, value.getType());
    assertTrue("isNullValue is false", value.isNullValue());

    try {
      value.floatValue();
      fail("Expected NullValueException");
    } catch (NullValueException e) {
    }

    try {
      value.doubleValue();
      fail("Expected NullValueException");
    } catch (NullValueException e) {
    }

    assertNull("bigDecimalValue() isn't null", value.bigDecimalValue());
  }
Beispiel #6
0
  @Test
  public void testNegativeZero() {
    IonDecimal value = decimal("-0.");
    testNegativeZero(0, value);

    IonDecimal value2 = decimal("-0d2");
    assertFalse(value2.equals(value));
    testNegativeZero(-2, value2);

    value2 = decimal("-0d1");
    assertFalse(value2.equals(value));
    testNegativeZero(-1, value2);

    value2 = decimal("1.");
    value2.setValue(-0f);
    testNegativeZero(1, value2);

    value2 = decimal("1.");
    value2.setValue(-0d);
    testNegativeZero(1, value2);
  }
Beispiel #7
0
  public void modifyDecimal(IonDecimal value) {
    float fVal = 123.45F;

    value.setValue(fVal);
    assertEquals(fVal, value.floatValue());
    assertEquals((double) fVal, value.doubleValue());
    assertEquals(fVal, value.bigDecimalValue().floatValue());

    value.setValue(A_DOUBLE);
    assertEquals(A_DOUBLE, value.doubleValue());
    assertEquals(A_DOUBLE, value.bigDecimalValue().doubleValue());

    value.setValue(null);
    checkNullDecimal(value);
  }
Beispiel #8
0
  @Test
  public void testDecimals() {
    IonDecimal value = (IonDecimal) oneValue("1.0");
    assertSame(IonType.DECIMAL, value.getType());
    assertFalse(value.isNullValue());
    assertArrayEquals(new String[0], value.getTypeAnnotations());
    assertEquals(1.0F, value.floatValue());
    assertEquals(1.0D, value.doubleValue());

    assertEquals(new BigDecimal(1).setScale(1), value.bigDecimalValue());
    // TODO more...

    value = (IonDecimal) oneValue("a::1.0");
    assertFalse(value.isNullValue());
    checkAnnotation("a", value);

    // Ensure that annotation makes it through value mods
    modifyDecimal(value);
    checkAnnotation("a", value);
  }
Beispiel #9
0
 @Test
 public void testSetValue() {
   IonDecimal value = decimal("1.23");
   value.setValue(123);
   checkDecimal(123, 0, value.bigDecimalValue());
 }
Beispiel #10
0
 public void testPrinting(String input, String output) {
   IonDecimal value = (IonDecimal) oneValue(input);
   assertEquals(output, value.toString());
 }
Beispiel #11
0
  @Test
  public void testDFormat() {
    IonDecimal value = (IonDecimal) oneValue("0d0");
    assertEquals(0D, value.doubleValue());

    value = (IonDecimal) oneValue("0D0");
    assertEquals(0D, value.doubleValue());

    value = (IonDecimal) oneValue("123d0");
    assertEquals(123D, value.doubleValue());

    value = (IonDecimal) oneValue("123D0");
    assertEquals(123D, value.doubleValue());

    value = (IonDecimal) oneValue("123.45d0");
    assertEquals(123.45D, value.doubleValue());

    value = (IonDecimal) oneValue("123.45D0");
    assertEquals(123.45D, value.doubleValue());

    value = (IonDecimal) oneValue("123d1");
    assertEquals(1230D, value.doubleValue());

    value = (IonDecimal) oneValue("-123d1");
    assertEquals(-1230D, value.doubleValue());

    value = (IonDecimal) oneValue("123d+1");
    assertEquals(1230D, value.doubleValue());

    value = (IonDecimal) oneValue("-123d+1");
    assertEquals(-1230D, value.doubleValue());

    value = (IonDecimal) oneValue("123d-1");
    assertEquals(12.3D, value.doubleValue());

    value = (IonDecimal) oneValue("-123d-1");
    assertEquals(-12.3D, value.doubleValue());
  }