Skip to content

Commit bae203c

Browse files
authored
Fixes #608: add FP-encoding tests for Smile codec (#609)
1 parent f7f99aa commit bae203c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/gen/SmileGeneratorNumbersTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void testFloatUnusedBits() throws Exception
153153
assertArrayEquals(new byte[] {
154154
0x08, 0x00, 0x00, 0x00, 0x00
155155
}, Arrays.copyOfRange(encoded, 1, encoded.length));
156-
}
156+
}
157157

158158
// [dataformats-binary#300]
159159
@Test
@@ -202,4 +202,46 @@ public void testNumbersAsString() throws Exception
202202
gen.close();
203203
assertEquals(10, out.toByteArray().length);
204204
}
205+
206+
// [dataformats-binary#608]
207+
@Test
208+
public void testFloat32FromSpecEncoding() throws Exception
209+
{
210+
final float f32 = 29.9510f;
211+
assertEquals(0x41ef9ba6, Float.floatToIntBits(f32));
212+
213+
ByteArrayOutputStream out = new ByteArrayOutputStream();
214+
try (SmileGenerator gen = smileGenerator(out, false)) {
215+
gen.writeNumber(f32);
216+
}
217+
byte[] encoded = out.toByteArray();
218+
assertEquals(6, encoded.length);
219+
assertEquals(0x28, encoded[0]); // type byte, float
220+
221+
// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
222+
assertArrayEquals(new byte[] {
223+
0x04, 0x0f, 0x3e, 0x37, 0x26
224+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
225+
}
226+
227+
// [dataformats-binary#608]
228+
@Test
229+
public void testDouble64FromSpecEncoding() throws Exception
230+
{
231+
final double d64 = -29.9510;
232+
assertEquals(0xc03df374bc6a7efaL, Double.doubleToLongBits(d64));
233+
234+
ByteArrayOutputStream out = new ByteArrayOutputStream();
235+
try (SmileGenerator gen = smileGenerator(out, false)) {
236+
gen.writeNumber(d64);
237+
}
238+
byte[] encoded = out.toByteArray();
239+
assertEquals(11, encoded.length);
240+
assertEquals(0x29, encoded[0]); // type byte, float
241+
242+
// From 0x80 0x00 0x00 0x00 (spread over 5 x 7bits)
243+
assertArrayEquals(new byte[] {
244+
0x01, 0x40, 0x1e, 0x7c, 0x6e, 0x4b, 0x63, 0x29, 0x7d, 0x7a
245+
}, Arrays.copyOfRange(encoded, 1, encoded.length));
246+
}
205247
}

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/parse/SmileNumberParsingTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void testArrayWithInts() throws IOException
214214
}
215215

216216
@Test
217-
public void testFloats() throws IOException
217+
public void testFloats() throws Exception
218218
{
219219
ByteArrayOutputStream bo = new ByteArrayOutputStream();
220220
SmileGenerator g = smileGenerator(bo, false);
@@ -240,7 +240,7 @@ public void testFloats() throws IOException
240240
}
241241

242242
@Test
243-
public void testDoubles() throws IOException
243+
public void testDoubles() throws Exception
244244
{
245245
ByteArrayOutputStream bo = new ByteArrayOutputStream();
246246
SmileGenerator g = smileGenerator(bo, false);
@@ -265,6 +265,38 @@ public void testDoubles() throws IOException
265265
p.close();
266266
}
267267

268+
// [dataformats-binary#608]
269+
@Test
270+
public void testFloat32FromSpecEncoding() throws Exception {
271+
final float f32 = 29.9510f;
272+
ByteArrayOutputStream out = new ByteArrayOutputStream();
273+
try (SmileGenerator gen = smileGenerator(out, false)) {
274+
gen.writeNumber(f32);
275+
}
276+
try (SmileParser p = _smileParser(out.toByteArray())) {
277+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
278+
assertEquals(JsonParser.NumberType.FLOAT, p.getNumberType());
279+
assertEquals(f32, p.getFloatValue());
280+
assertNull(p.nextToken());
281+
}
282+
}
283+
284+
// [dataformats-binary#608]
285+
@Test
286+
public void testDouble64FromSpecEncoding() throws Exception {
287+
final double d64 = -29.9510;
288+
ByteArrayOutputStream out = new ByteArrayOutputStream();
289+
try (SmileGenerator gen = smileGenerator(out, false)) {
290+
gen.writeNumber(d64);
291+
}
292+
try (SmileParser p = _smileParser(out.toByteArray())) {
293+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
294+
assertEquals(JsonParser.NumberType.DOUBLE, p.getNumberType());
295+
assertEquals(d64, p.getDoubleValue());
296+
assertNull(p.nextToken());
297+
}
298+
}
299+
268300
@Test
269301
public void testArrayWithDoubles() throws IOException
270302
{

0 commit comments

Comments
 (0)