diff --git a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java index 22b67b9d5..bb4c69019 100644 --- a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java +++ b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java @@ -153,7 +153,7 @@ public void testFloatUnusedBits() throws Exception assertArrayEquals(new byte[] { 0x08, 0x00, 0x00, 0x00, 0x00 }, Arrays.copyOfRange(encoded, 1, encoded.length)); -} + } // [dataformats-binary#300] @Test @@ -202,4 +202,46 @@ public void testNumbersAsString() throws Exception gen.close(); assertEquals(10, out.toByteArray().length); } + + // [dataformats-binary#608] + @Test + public void testFloat32FromSpecEncoding() throws Exception + { + final float f32 = 29.9510f; + assertEquals(0x41ef9ba6, Float.floatToIntBits(f32)); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (SmileGenerator gen = smileGenerator(out, false)) { + gen.writeNumber(f32); + } + byte[] encoded = out.toByteArray(); + assertEquals(6, encoded.length); + assertEquals(0x28, encoded[0]); // type byte, float + + // From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits) + assertArrayEquals(new byte[] { + 0x04, 0x0f, 0x3e, 0x37, 0x26 + }, Arrays.copyOfRange(encoded, 1, encoded.length)); + } + + // [dataformats-binary#608] + @Test + public void testDouble64FromSpecEncoding() throws Exception + { + final double d64 = -29.9510; + assertEquals(0xc03df374bc6a7efaL, Double.doubleToLongBits(d64)); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (SmileGenerator gen = smileGenerator(out, false)) { + gen.writeNumber(d64); + } + byte[] encoded = out.toByteArray(); + assertEquals(11, encoded.length); + assertEquals(0x29, encoded[0]); // type byte, float + + // From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits) + assertArrayEquals(new byte[] { + 0x01, 0x40, 0x1e, 0x7c, 0x6e, 0x4b, 0x63, 0x29, 0x7d, 0x7a + }, Arrays.copyOfRange(encoded, 1, encoded.length)); + } } diff --git a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SmileNumberParsingTest.java b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SmileNumberParsingTest.java index 5f8264f13..f99093449 100644 --- a/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SmileNumberParsingTest.java +++ b/smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SmileNumberParsingTest.java @@ -214,7 +214,7 @@ public void testArrayWithInts() throws IOException } @Test - public void testFloats() throws IOException + public void testFloats() throws Exception { ByteArrayOutputStream bo = new ByteArrayOutputStream(); SmileGenerator g = smileGenerator(bo, false); @@ -240,7 +240,7 @@ public void testFloats() throws IOException } @Test - public void testDoubles() throws IOException + public void testDoubles() throws Exception { ByteArrayOutputStream bo = new ByteArrayOutputStream(); SmileGenerator g = smileGenerator(bo, false); @@ -265,6 +265,38 @@ public void testDoubles() throws IOException p.close(); } + // [dataformats-binary#608] + @Test + public void testFloat32FromSpecEncoding() throws Exception { + final float f32 = 29.9510f; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (SmileGenerator gen = smileGenerator(out, false)) { + gen.writeNumber(f32); + } + try (SmileParser p = _smileParser(out.toByteArray())) { + assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(JsonParser.NumberType.FLOAT, p.getNumberType()); + assertEquals(f32, p.getFloatValue()); + assertNull(p.nextToken()); + } + } + + // [dataformats-binary#608] + @Test + public void testDouble64FromSpecEncoding() throws Exception { + final double d64 = -29.9510; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + try (SmileGenerator gen = smileGenerator(out, false)) { + gen.writeNumber(d64); + } + try (SmileParser p = _smileParser(out.toByteArray())) { + assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(JsonParser.NumberType.DOUBLE, p.getNumberType()); + assertEquals(d64, p.getDoubleValue()); + assertNull(p.nextToken()); + } + } + @Test public void testArrayWithDoubles() throws IOException {