Skip to content

Commit

Permalink
Add some more CBOR/BigDecimal testing for simplest case(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 7, 2023
1 parent fd33079 commit 7459723
Showing 1 changed file with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,52 @@ public void testBigDecimalLonger() throws Exception
}

private void _testBigDecimal(BigDecimal expValue) throws Exception
{
_testBigDecimalInArray(expValue);
_testBigDecimalInObject(expValue);
}

private void _testBigDecimalInArray(BigDecimal expValue) throws Exception
{
final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
try (final CBORGenerator sourceGen = cborGenerator(sourceBytes)) {
sourceGen.writeStartArray();
sourceGen.writeNumber(expValue);
sourceGen.writeEndArray();
}

byte[] b = sourceBytes.toByteArray();

// but verify that the original content can be parsed
try (CBORParser parser = cborParser(b)) {
assertToken(JsonToken.START_ARRAY, parser.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
assertEquals(expValue, parser.getDecimalValue());
assertToken(JsonToken.END_ARRAY, parser.nextToken());
}
}

private void _testBigDecimalInObject(BigDecimal expValue) throws Exception
{
final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
final CBORGenerator sourceGen = cborGenerator(sourceBytes);
sourceGen.writeStartObject();
sourceGen.writeFieldName("a");
sourceGen.writeNumber(expValue);
sourceGen.writeEndObject();
sourceGen.close();
try (final CBORGenerator sourceGen = cborGenerator(sourceBytes)) {
sourceGen.writeStartObject();
sourceGen.writeFieldName("a");
sourceGen.writeNumber(expValue);
sourceGen.writeEndObject();
}

byte[] b = sourceBytes.toByteArray();

// but verify that the original content can be parsed
CBORParser parser = cborParser(b);
assertToken(JsonToken.START_OBJECT, parser.nextToken());
assertToken(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("a", parser.getCurrentName());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
assertEquals(expValue, parser.getDecimalValue());
assertToken(JsonToken.END_OBJECT, parser.nextToken());
parser.close();
try (CBORParser parser = cborParser(b)) {
assertToken(JsonToken.START_OBJECT, parser.nextToken());
assertToken(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals("a", parser.getCurrentName());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
assertEquals(expValue, parser.getDecimalValue());
assertToken(JsonToken.END_OBJECT, parser.nextToken());
}
}

public void testBigInteger() throws Exception
Expand All @@ -77,26 +103,26 @@ public void testBigInteger() throws Exception
private void _testBigInteger(BigInteger expValue) throws Exception
{
final ByteArrayOutputStream sourceBytes = new ByteArrayOutputStream();
final CBORGenerator sourceGen = cborGenerator(sourceBytes);
sourceGen.writeNumber(expValue);
sourceGen.close();
try (CBORGenerator sourceGen = cborGenerator(sourceBytes)) {
sourceGen.writeNumber(expValue);
sourceGen.close();
}

// but verify that the original content can be parsed
CBORParser parser = cborParser(sourceBytes.toByteArray());
assertToken(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
assertEquals(expValue, parser.getBigIntegerValue());

// also, coercion to long at least
long expL = expValue.longValue();
assertEquals(expL, parser.getLongValue());

// and int, if feasible
if (expL >= Integer.MIN_VALUE && expL <= Integer.MAX_VALUE) {
assertEquals((int) expL, parser.getIntValue());
try (CBORParser parser = cborParser(sourceBytes.toByteArray())) {
assertToken(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
assertEquals(expValue, parser.getBigIntegerValue());

// also, coercion to long at least
long expL = expValue.longValue();
assertEquals(expL, parser.getLongValue());

// and int, if feasible
if (expL >= Integer.MIN_VALUE && expL <= Integer.MAX_VALUE) {
assertEquals((int) expL, parser.getIntValue());
}

assertNull(parser.nextToken());
}

assertNull(parser.nextToken());
parser.close();

}
}

0 comments on commit 7459723

Please sign in to comment.