|
21 | 21 |
|
22 | 22 | import org.junit.Assert;
|
23 | 23 |
|
| 24 | +import com.amazon.ion.IonDecimal; |
24 | 25 | import com.amazon.ion.IonReader;
|
25 | 26 | import com.amazon.ion.IonSystem;
|
26 | 27 | import com.amazon.ion.IonValue;
|
27 | 28 | import com.amazon.ion.system.IonSystemBuilder;
|
28 | 29 |
|
29 | 30 | import java.io.IOException;
|
| 31 | +import java.math.BigDecimal; |
30 | 32 | import java.math.BigInteger;
|
31 | 33 | import org.junit.Test;
|
32 | 34 |
|
33 | 35 | @SuppressWarnings("resource")
|
34 | 36 | public class IonParserTest
|
35 | 37 | {
|
36 | 38 | @Test
|
37 |
| - public void testGetNumberType() throws Exception { |
| 39 | + public void testGetNumberTypeAndValue() throws Exception { |
38 | 40 | IonSystem ion = IonSystemBuilder.standard().build();
|
39 | 41 |
|
40 |
| - IonValue ionInt = ion.newInt(Integer.MAX_VALUE); |
| 42 | + Integer intValue = Integer.MAX_VALUE; |
| 43 | + IonValue ionInt = ion.newInt(intValue); |
41 | 44 | IonParser intParser = new IonFactory().createParser(ionInt);
|
42 | 45 | Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, intParser.nextToken());
|
43 | 46 | Assert.assertEquals(JsonParser.NumberType.INT, intParser.getNumberType());
|
| 47 | + Assert.assertEquals(intValue, intParser.getNumberValue()); |
44 | 48 |
|
45 |
| - IonValue ionLong = ion.newInt(Long.MAX_VALUE); |
| 49 | + Long longValue = Long.MAX_VALUE; |
| 50 | + IonValue ionLong = ion.newInt(longValue); |
46 | 51 | IonParser longParser = new IonFactory().createParser(ionLong);
|
47 | 52 | Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, longParser.nextToken());
|
48 | 53 | Assert.assertEquals(JsonParser.NumberType.LONG, longParser.getNumberType());
|
| 54 | + Assert.assertEquals(longValue, longParser.getNumberValue()); |
49 | 55 |
|
50 |
| - IonValue ionBigInt = ion.newInt(new BigInteger(Long.MAX_VALUE + "1")); |
| 56 | + BigInteger bigIntValue = new BigInteger(Long.MAX_VALUE + "1"); |
| 57 | + IonValue ionBigInt = ion.newInt(bigIntValue); |
51 | 58 | IonParser bigIntParser = new IonFactory().createParser(ionBigInt);
|
52 | 59 | Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, bigIntParser.nextToken());
|
53 | 60 | Assert.assertEquals(JsonParser.NumberType.BIG_INTEGER, bigIntParser.getNumberType());
|
| 61 | + Assert.assertEquals(bigIntValue, bigIntParser.getNumberValue()); |
54 | 62 |
|
55 |
| - // JoiParser is currently deficient with decimals -- all decimals are reported as Double. So this is all we can test. |
56 |
| - IonValue ionDecimal = ion.newDecimal(Double.MAX_VALUE); |
57 |
| - IonParser floatParser = new IonFactory().createParser(ionDecimal); |
| 63 | + Double decimalValue = Double.MAX_VALUE; |
| 64 | + IonValue ionDecimal = ion.newDecimal(decimalValue); |
| 65 | + IonParser decimalParser = new IonFactory().createParser(ionDecimal); |
| 66 | + Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, decimalParser.nextToken()); |
| 67 | + Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, decimalParser.getNumberType()); |
| 68 | + Assert.assertTrue(new BigDecimal("" + decimalValue).compareTo((BigDecimal)decimalParser.getNumberValue()) == 0); |
| 69 | + |
| 70 | + Double floatValue = Double.MAX_VALUE; |
| 71 | + IonValue ionFloat = ion.newFloat(floatValue); |
| 72 | + IonParser floatParser = new IonFactory().createParser(ionFloat); |
58 | 73 | Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, floatParser.nextToken());
|
59 | 74 | Assert.assertEquals(JsonParser.NumberType.DOUBLE, floatParser.getNumberType());
|
| 75 | + Assert.assertEquals(floatValue, floatParser.getNumberValue()); |
| 76 | + |
| 77 | + BigDecimal bigDecimalValue = new BigDecimal(Double.MAX_VALUE + "1"); |
| 78 | + IonValue ionBigDecimal = ion.newDecimal(bigDecimalValue); |
| 79 | + IonParser bigDecimalParser = new IonFactory().createParser(ionBigDecimal); |
| 80 | + Assert.assertEquals(JsonToken.VALUE_NUMBER_FLOAT, bigDecimalParser.nextToken()); |
| 81 | + Assert.assertEquals(JsonParser.NumberType.BIG_DECIMAL, bigDecimalParser.getNumberType()); |
| 82 | + Assert.assertTrue(bigDecimalValue.compareTo((BigDecimal)bigDecimalParser.getNumberValue()) == 0); |
60 | 83 | }
|
61 | 84 |
|
62 | 85 | @Test
|
63 |
| - public void testFloatType() throws IOException |
64 |
| - { |
| 86 | + public void testFloatType() throws IOException { |
65 | 87 | final byte[] data = "{ score:0.291e0 }".getBytes();
|
66 | 88 | IonSystem ion = IonSystemBuilder.standard().build();
|
67 | 89 | final IonValue ionFloat = ion.newFloat(Float.MAX_VALUE);
|
|
0 commit comments